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