xref: /webtrees/app/Date/JalaliDate.php (revision 83615acfc72bfb50678c6481f2a00bab04041a87)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Date;
20
21use Fisharebest\ExtCalendar\PersianCalendar;
22use Fisharebest\Webtrees\I18N;
23
24/**
25 * Definitions for Jalali dates.
26 */
27class JalaliDate extends AbstractCalendarDate
28{
29    // GEDCOM calendar escape
30    public const ESCAPE = '@#DJALALI@';
31
32    // Convert GEDCOM month names to month numbers
33    protected const MONTH_ABBREVIATIONS = [
34        ''      => 0,
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    /**
50     * Create a date from either:
51     * a Julian day number
52     * day/month/year strings from a GEDCOM date
53     * another CalendarDate object
54     *
55     * @param array|int|AbstractCalendarDate $date
56     */
57    public function __construct($date)
58    {
59        $this->calendar = new PersianCalendar();
60        parent::__construct($date);
61    }
62
63    /**
64     * Full month name in nominative case.
65     *
66     * @param int  $month
67     * @param bool $leap_year Some calendars use leap months
68     *
69     * @return string
70     */
71    protected function monthNameNominativeCase(int $month, bool $leap_year): string
72    {
73        static $translated_month_names;
74
75        if ($translated_month_names === null) {
76            $translated_month_names = [
77                0  => '',
78                /* I18N: 1st month in the Persian/Jalali calendar */
79                1  => I18N::translateContext('NOMINATIVE', 'Farvardin'),
80                /* I18N: 2nd month in the Persian/Jalali calendar */
81                2  => I18N::translateContext('NOMINATIVE', 'Ordibehesht'),
82                /* I18N: 3rd month in the Persian/Jalali calendar */
83                3  => I18N::translateContext('NOMINATIVE', 'Khordad'),
84                /* I18N: 4th month in the Persian/Jalali calendar */
85                4  => I18N::translateContext('NOMINATIVE', 'Tir'),
86                /* I18N: 5th month in the Persian/Jalali calendar */
87                5  => I18N::translateContext('NOMINATIVE', 'Mordad'),
88                /* I18N: 6th month in the Persian/Jalali calendar */
89                6  => I18N::translateContext('NOMINATIVE', 'Shahrivar'),
90                /* I18N: 7th month in the Persian/Jalali calendar */
91                7  => I18N::translateContext('NOMINATIVE', 'Mehr'),
92                /* I18N: 8th month in the Persian/Jalali calendar */
93                8  => I18N::translateContext('NOMINATIVE', 'Aban'),
94                /* I18N: 9th month in the Persian/Jalali calendar */
95                9  => I18N::translateContext('NOMINATIVE', 'Azar'),
96                /* I18N: 10th month in the Persian/Jalali calendar */
97                10 => I18N::translateContext('NOMINATIVE', 'Dey'),
98                /* I18N: 11th month in the Persian/Jalali calendar */
99                11 => I18N::translateContext('NOMINATIVE', 'Bahman'),
100                /* I18N: 12th month in the Persian/Jalali calendar */
101                12 => I18N::translateContext('NOMINATIVE', 'Esfand'),
102            ];
103        }
104
105        return $translated_month_names[$month];
106    }
107
108    /**
109     * Full month name in genitive case.
110     *
111     * @param int  $month
112     * @param bool $leap_year Some calendars use leap months
113     *
114     * @return string
115     */
116    protected function monthNameGenitiveCase(int $month, bool $leap_year): string
117    {
118        static $translated_month_names;
119
120        if ($translated_month_names === null) {
121            $translated_month_names = [
122                0  => '',
123                /* I18N: 1st month in the Persian/Jalali calendar */
124                1  => I18N::translateContext('GENITIVE', 'Farvardin'),
125                /* I18N: 2nd month in the Persian/Jalali calendar */
126                2  => I18N::translateContext('GENITIVE', 'Ordibehesht'),
127                /* I18N: 3rd month in the Persian/Jalali calendar */
128                3  => I18N::translateContext('GENITIVE', 'Khordad'),
129                /* I18N: 4th month in the Persian/Jalali calendar */
130                4  => I18N::translateContext('GENITIVE', 'Tir'),
131                /* I18N: 5th month in the Persian/Jalali calendar */
132                5  => I18N::translateContext('GENITIVE', 'Mordad'),
133                /* I18N: 6th month in the Persian/Jalali calendar */
134                6  => I18N::translateContext('GENITIVE', 'Shahrivar'),
135                /* I18N: 7th month in the Persian/Jalali calendar */
136                7  => I18N::translateContext('GENITIVE', 'Mehr'),
137                /* I18N: 8th month in the Persian/Jalali calendar */
138                8  => I18N::translateContext('GENITIVE', 'Aban'),
139                /* I18N: 9th month in the Persian/Jalali calendar */
140                9  => I18N::translateContext('GENITIVE', 'Azar'),
141                /* I18N: 10th month in the Persian/Jalali calendar */
142                10 => I18N::translateContext('GENITIVE', 'Dey'),
143                /* I18N: 11th month in the Persian/Jalali calendar */
144                11 => I18N::translateContext('GENITIVE', 'Bahman'),
145                /* I18N: 12th month in the Persian/Jalali calendar */
146                12 => I18N::translateContext('GENITIVE', 'Esfand'),
147            ];
148        }
149
150        return $translated_month_names[$month];
151    }
152
153    /**
154     * Full month name in locative case.
155     *
156     * @param int  $month
157     * @param bool $leap_year Some calendars use leap months
158     *
159     * @return string
160     */
161    protected function monthNameLocativeCase(int $month, bool $leap_year): string
162    {
163        static $translated_month_names;
164
165        if ($translated_month_names === null) {
166            $translated_month_names = [
167                0  => '',
168                /* I18N: 1st month in the Persian/Jalali calendar */
169                1  => I18N::translateContext('LOCATIVE', 'Farvardin'),
170                /* I18N: 2nd month in the Persian/Jalali calendar */
171                2  => I18N::translateContext('LOCATIVE', 'Ordibehesht'),
172                /* I18N: 3rd month in the Persian/Jalali calendar */
173                3  => I18N::translateContext('LOCATIVE', 'Khordad'),
174                /* I18N: 4th month in the Persian/Jalali calendar */
175                4  => I18N::translateContext('LOCATIVE', 'Tir'),
176                /* I18N: 5th month in the Persian/Jalali calendar */
177                5  => I18N::translateContext('LOCATIVE', 'Mordad'),
178                /* I18N: 6th month in the Persian/Jalali calendar */
179                6  => I18N::translateContext('LOCATIVE', 'Shahrivar'),
180                /* I18N: 7th month in the Persian/Jalali calendar */
181                7  => I18N::translateContext('LOCATIVE', 'Mehr'),
182                /* I18N: 8th month in the Persian/Jalali calendar */
183                8  => I18N::translateContext('LOCATIVE', 'Aban'),
184                /* I18N: 9th month in the Persian/Jalali calendar */
185                9  => I18N::translateContext('LOCATIVE', 'Azar'),
186                /* I18N: 10th month in the Persian/Jalali calendar */
187                10 => I18N::translateContext('LOCATIVE', 'Dey'),
188                /* I18N: 11th month in the Persian/Jalali calendar */
189                11 => I18N::translateContext('LOCATIVE', 'Bahman'),
190                /* I18N: 12th month in the Persian/Jalali calendar */
191                12 => I18N::translateContext('LOCATIVE', 'Esfand'),
192            ];
193        }
194
195        return $translated_month_names[$month];
196    }
197
198    /**
199     * Full month name in instrumental case.
200     *
201     * @param int  $month
202     * @param bool $leap_year Some calendars use leap months
203     *
204     * @return string
205     */
206    protected function monthNameInstrumentalCase(int $month, bool $leap_year): string
207    {
208        static $translated_month_names;
209
210        if ($translated_month_names === null) {
211            $translated_month_names = [
212                0  => '',
213                /* I18N: 1st month in the Persian/Jalali calendar */
214                1  => I18N::translateContext('INSTRUMENTAL', 'Farvardin'),
215                /* I18N: 2nd month in the Persian/Jalali calendar */
216                2  => I18N::translateContext('INSTRUMENTAL', 'Ordibehesht'),
217                /* I18N: 3rd month in the Persian/Jalali calendar */
218                3  => I18N::translateContext('INSTRUMENTAL', 'Khordad'),
219                /* I18N: 4th month in the Persian/Jalali calendar */
220                4  => I18N::translateContext('INSTRUMENTAL', 'Tir'),
221                /* I18N: 5th month in the Persian/Jalali calendar */
222                5  => I18N::translateContext('INSTRUMENTAL', 'Mordad'),
223                /* I18N: 6th month in the Persian/Jalali calendar */
224                6  => I18N::translateContext('INSTRUMENTAL', 'Shahrivar'),
225                /* I18N: 7th month in the Persian/Jalali calendar */
226                7  => I18N::translateContext('INSTRUMENTAL', 'Mehr'),
227                /* I18N: 8th month in the Persian/Jalali calendar */
228                8  => I18N::translateContext('INSTRUMENTAL', 'Aban'),
229                /* I18N: 9th month in the Persian/Jalali calendar */
230                9  => I18N::translateContext('INSTRUMENTAL', 'Azar'),
231                /* I18N: 10th month in the Persian/Jalali calendar */
232                10 => I18N::translateContext('INSTRUMENTAL', 'Dey'),
233                /* I18N: 11th month in the Persian/Jalali calendar */
234                11 => I18N::translateContext('INSTRUMENTAL', 'Bahman'),
235                /* I18N: 12th month in the Persian/Jalali calendar */
236                12 => I18N::translateContext('INSTRUMENTAL', 'Esfand'),
237            ];
238        }
239
240        return $translated_month_names[$month];
241    }
242
243    /**
244     * Abbreviated month name
245     *
246     * @param int  $month
247     * @param bool $leap_year Some calendars use leap months
248     *
249     * @return string
250     */
251    protected function monthNameAbbreviated(int $month, bool $leap_year): string
252    {
253        static $translated_month_names;
254
255        if ($translated_month_names === null) {
256            $translated_month_names = [
257                0  => '',
258                1  => I18N::translateContext('Abbreviation for Persian month: Farvardin', 'Far'),
259                2  => I18N::translateContext('Abbreviation for Persian month: Ordibehesht', 'Ord'),
260                3  => I18N::translateContext('Abbreviation for Persian month: Khordad', 'Khor'),
261                4  => I18N::translateContext('Abbreviation for Persian month: Tir', 'Tir'),
262                5  => I18N::translateContext('Abbreviation for Persian month: Mordad', 'Mor'),
263                6  => I18N::translateContext('Abbreviation for Persian month: Shahrivar', 'Shah'),
264                7  => I18N::translateContext('Abbreviation for Persian month: Mehr', 'Mehr'),
265                8  => I18N::translateContext('Abbreviation for Persian month: Aban', 'Aban'),
266                9  => I18N::translateContext('Abbreviation for Persian month: Azar', 'Azar'),
267                10 => I18N::translateContext('Abbreviation for Persian month: Dey', 'Dey'),
268                11 => I18N::translateContext('Abbreviation for Persian month: Bahman', 'Bah'),
269                12 => I18N::translateContext('Abbreviation for Persian month: Esfand', 'Esf'),
270            ];
271        }
272
273        return $translated_month_names[$month];
274    }
275}
276