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