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