xref: /webtrees/app/Date/JalaliDate.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Date;
21
22use Fisharebest\ExtCalendar\PersianCalendar;
23use Fisharebest\Webtrees\I18N;
24
25/**
26 * Definitions for Jalali dates.
27 */
28class JalaliDate extends AbstractCalendarDate
29{
30    // GEDCOM calendar escape
31    public const ESCAPE = '@#DJALALI@';
32
33    // Convert GEDCOM month names to month numbers
34    protected const MONTH_TO_NUMBER = [
35        'FARVA' => 1,
36        'ORDIB' => 2,
37        'KHORD' => 3,
38        'TIR'   => 4,
39        'MORDA' => 5,
40        'SHAHR' => 6,
41        'MEHR'  => 7,
42        'ABAN'  => 8,
43        'AZAR'  => 9,
44        'DEY'   => 10,
45        'BAHMA' => 11,
46        'ESFAN' => 12,
47    ];
48
49    protected const NUMBER_TO_MONTH = [
50        1  => 'FARVA',
51        2  => 'ORDIB',
52        3  => 'KHORD',
53        4  => 'TIR',
54        5  => 'MORDA',
55        6  => 'SHAHR',
56        7  => 'MEHR',
57        8  => 'ABAN',
58        9  => 'AZAR',
59        10 => 'DEY',
60        11 => 'BAHMA',
61        12 => 'ESFAN',
62    ];
63
64    /**
65     * Create a date from either:
66     * a Julian day number
67     * day/month/year strings from a GEDCOM date
68     * another CalendarDate object
69     *
70     * @param array<string>|int|AbstractCalendarDate $date
71     */
72    public function __construct($date)
73    {
74        $this->calendar = new PersianCalendar();
75        parent::__construct($date);
76    }
77
78    /**
79     * Full month name in nominative case.
80     *
81     * @param int  $month
82     * @param bool $leap_year Some calendars use leap months
83     *
84     * @return string
85     */
86    protected function monthNameNominativeCase(int $month, bool $leap_year): string
87    {
88        static $translated_month_names;
89
90        if ($translated_month_names === null) {
91            $translated_month_names = [
92                0  => '',
93                /* I18N: 1st month in the Persian/Jalali calendar */
94                1  => I18N::translateContext('NOMINATIVE', 'Farvardin'),
95                /* I18N: 2nd month in the Persian/Jalali calendar */
96                2  => I18N::translateContext('NOMINATIVE', 'Ordibehesht'),
97                /* I18N: 3rd month in the Persian/Jalali calendar */
98                3  => I18N::translateContext('NOMINATIVE', 'Khordad'),
99                /* I18N: 4th month in the Persian/Jalali calendar */
100                4  => I18N::translateContext('NOMINATIVE', 'Tir'),
101                /* I18N: 5th month in the Persian/Jalali calendar */
102                5  => I18N::translateContext('NOMINATIVE', 'Mordad'),
103                /* I18N: 6th month in the Persian/Jalali calendar */
104                6  => I18N::translateContext('NOMINATIVE', 'Shahrivar'),
105                /* I18N: 7th month in the Persian/Jalali calendar */
106                7  => I18N::translateContext('NOMINATIVE', 'Mehr'),
107                /* I18N: 8th month in the Persian/Jalali calendar */
108                8  => I18N::translateContext('NOMINATIVE', 'Aban'),
109                /* I18N: 9th month in the Persian/Jalali calendar */
110                9  => I18N::translateContext('NOMINATIVE', 'Azar'),
111                /* I18N: 10th month in the Persian/Jalali calendar */
112                10 => I18N::translateContext('NOMINATIVE', 'Dey'),
113                /* I18N: 11th month in the Persian/Jalali calendar */
114                11 => I18N::translateContext('NOMINATIVE', 'Bahman'),
115                /* I18N: 12th month in the Persian/Jalali calendar */
116                12 => I18N::translateContext('NOMINATIVE', 'Esfand'),
117            ];
118        }
119
120        return $translated_month_names[$month];
121    }
122
123    /**
124     * Full month name in genitive case.
125     *
126     * @param int  $month
127     * @param bool $leap_year Some calendars use leap months
128     *
129     * @return string
130     */
131    protected function monthNameGenitiveCase(int $month, bool $leap_year): string
132    {
133        static $translated_month_names;
134
135        if ($translated_month_names === null) {
136            $translated_month_names = [
137                0  => '',
138                /* I18N: 1st month in the Persian/Jalali calendar */
139                1  => I18N::translateContext('GENITIVE', 'Farvardin'),
140                /* I18N: 2nd month in the Persian/Jalali calendar */
141                2  => I18N::translateContext('GENITIVE', 'Ordibehesht'),
142                /* I18N: 3rd month in the Persian/Jalali calendar */
143                3  => I18N::translateContext('GENITIVE', 'Khordad'),
144                /* I18N: 4th month in the Persian/Jalali calendar */
145                4  => I18N::translateContext('GENITIVE', 'Tir'),
146                /* I18N: 5th month in the Persian/Jalali calendar */
147                5  => I18N::translateContext('GENITIVE', 'Mordad'),
148                /* I18N: 6th month in the Persian/Jalali calendar */
149                6  => I18N::translateContext('GENITIVE', 'Shahrivar'),
150                /* I18N: 7th month in the Persian/Jalali calendar */
151                7  => I18N::translateContext('GENITIVE', 'Mehr'),
152                /* I18N: 8th month in the Persian/Jalali calendar */
153                8  => I18N::translateContext('GENITIVE', 'Aban'),
154                /* I18N: 9th month in the Persian/Jalali calendar */
155                9  => I18N::translateContext('GENITIVE', 'Azar'),
156                /* I18N: 10th month in the Persian/Jalali calendar */
157                10 => I18N::translateContext('GENITIVE', 'Dey'),
158                /* I18N: 11th month in the Persian/Jalali calendar */
159                11 => I18N::translateContext('GENITIVE', 'Bahman'),
160                /* I18N: 12th month in the Persian/Jalali calendar */
161                12 => I18N::translateContext('GENITIVE', 'Esfand'),
162            ];
163        }
164
165        return $translated_month_names[$month];
166    }
167
168    /**
169     * Full month name in locative case.
170     *
171     * @param int  $month
172     * @param bool $leap_year Some calendars use leap months
173     *
174     * @return string
175     */
176    protected function monthNameLocativeCase(int $month, bool $leap_year): string
177    {
178        static $translated_month_names;
179
180        if ($translated_month_names === null) {
181            $translated_month_names = [
182                0  => '',
183                /* I18N: 1st month in the Persian/Jalali calendar */
184                1  => I18N::translateContext('LOCATIVE', 'Farvardin'),
185                /* I18N: 2nd month in the Persian/Jalali calendar */
186                2  => I18N::translateContext('LOCATIVE', 'Ordibehesht'),
187                /* I18N: 3rd month in the Persian/Jalali calendar */
188                3  => I18N::translateContext('LOCATIVE', 'Khordad'),
189                /* I18N: 4th month in the Persian/Jalali calendar */
190                4  => I18N::translateContext('LOCATIVE', 'Tir'),
191                /* I18N: 5th month in the Persian/Jalali calendar */
192                5  => I18N::translateContext('LOCATIVE', 'Mordad'),
193                /* I18N: 6th month in the Persian/Jalali calendar */
194                6  => I18N::translateContext('LOCATIVE', 'Shahrivar'),
195                /* I18N: 7th month in the Persian/Jalali calendar */
196                7  => I18N::translateContext('LOCATIVE', 'Mehr'),
197                /* I18N: 8th month in the Persian/Jalali calendar */
198                8  => I18N::translateContext('LOCATIVE', 'Aban'),
199                /* I18N: 9th month in the Persian/Jalali calendar */
200                9  => I18N::translateContext('LOCATIVE', 'Azar'),
201                /* I18N: 10th month in the Persian/Jalali calendar */
202                10 => I18N::translateContext('LOCATIVE', 'Dey'),
203                /* I18N: 11th month in the Persian/Jalali calendar */
204                11 => I18N::translateContext('LOCATIVE', 'Bahman'),
205                /* I18N: 12th month in the Persian/Jalali calendar */
206                12 => I18N::translateContext('LOCATIVE', 'Esfand'),
207            ];
208        }
209
210        return $translated_month_names[$month];
211    }
212
213    /**
214     * Full month name in instrumental case.
215     *
216     * @param int  $month
217     * @param bool $leap_year Some calendars use leap months
218     *
219     * @return string
220     */
221    protected function monthNameInstrumentalCase(int $month, bool $leap_year): string
222    {
223        static $translated_month_names;
224
225        if ($translated_month_names === null) {
226            $translated_month_names = [
227                0  => '',
228                /* I18N: 1st month in the Persian/Jalali calendar */
229                1  => I18N::translateContext('INSTRUMENTAL', 'Farvardin'),
230                /* I18N: 2nd month in the Persian/Jalali calendar */
231                2  => I18N::translateContext('INSTRUMENTAL', 'Ordibehesht'),
232                /* I18N: 3rd month in the Persian/Jalali calendar */
233                3  => I18N::translateContext('INSTRUMENTAL', 'Khordad'),
234                /* I18N: 4th month in the Persian/Jalali calendar */
235                4  => I18N::translateContext('INSTRUMENTAL', 'Tir'),
236                /* I18N: 5th month in the Persian/Jalali calendar */
237                5  => I18N::translateContext('INSTRUMENTAL', 'Mordad'),
238                /* I18N: 6th month in the Persian/Jalali calendar */
239                6  => I18N::translateContext('INSTRUMENTAL', 'Shahrivar'),
240                /* I18N: 7th month in the Persian/Jalali calendar */
241                7  => I18N::translateContext('INSTRUMENTAL', 'Mehr'),
242                /* I18N: 8th month in the Persian/Jalali calendar */
243                8  => I18N::translateContext('INSTRUMENTAL', 'Aban'),
244                /* I18N: 9th month in the Persian/Jalali calendar */
245                9  => I18N::translateContext('INSTRUMENTAL', 'Azar'),
246                /* I18N: 10th month in the Persian/Jalali calendar */
247                10 => I18N::translateContext('INSTRUMENTAL', 'Dey'),
248                /* I18N: 11th month in the Persian/Jalali calendar */
249                11 => I18N::translateContext('INSTRUMENTAL', 'Bahman'),
250                /* I18N: 12th month in the Persian/Jalali calendar */
251                12 => I18N::translateContext('INSTRUMENTAL', 'Esfand'),
252            ];
253        }
254
255        return $translated_month_names[$month];
256    }
257
258    /**
259     * Abbreviated month name
260     *
261     * @param int  $month
262     * @param bool $leap_year Some calendars use leap months
263     *
264     * @return string
265     */
266    protected function monthNameAbbreviated(int $month, bool $leap_year): string
267    {
268        static $translated_month_names;
269
270        if ($translated_month_names === null) {
271            $translated_month_names = [
272                0  => '',
273                1  => I18N::translateContext('Abbreviation for Persian month: Farvardin', 'Far'),
274                2  => I18N::translateContext('Abbreviation for Persian month: Ordibehesht', 'Ord'),
275                3  => I18N::translateContext('Abbreviation for Persian month: Khordad', 'Khor'),
276                4  => I18N::translateContext('Abbreviation for Persian month: Tir', 'Tir'),
277                5  => I18N::translateContext('Abbreviation for Persian month: Mordad', 'Mor'),
278                6  => I18N::translateContext('Abbreviation for Persian month: Shahrivar', 'Shah'),
279                7  => I18N::translateContext('Abbreviation for Persian month: Mehr', 'Mehr'),
280                8  => I18N::translateContext('Abbreviation for Persian month: Aban', 'Aban'),
281                9  => I18N::translateContext('Abbreviation for Persian month: Azar', 'Azar'),
282                10 => I18N::translateContext('Abbreviation for Persian month: Dey', 'Dey'),
283                11 => I18N::translateContext('Abbreviation for Persian month: Bahman', 'Bah'),
284                12 => I18N::translateContext('Abbreviation for Persian month: Esfand', 'Esf'),
285            ];
286        }
287
288        return $translated_month_names[$month];
289    }
290}
291