xref: /webtrees/app/Date/HijriDate.php (revision 8d897fd11a9a8845d8456cb9eaad33b9c9d5a225)
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\ArabicCalendar;
22use Fisharebest\Webtrees\I18N;
23
24/**
25 * Definitions for Hijri dates.
26 *
27 * Note that these are "theoretical" dates.
28 * "True" dates are based on local lunar observations, and can be a +/- one day.
29 */
30class HijriDate extends AbstractCalendarDate
31{
32    // GEDCOM calendar escape
33    public const ESCAPE = '@#DHIJRI@';
34
35    // Convert GEDCOM month names to month numbers
36    protected const MONTH_ABBREVIATIONS = [
37        ''      => 0,
38        'MUHAR' => 1,
39        'SAFAR' => 2,
40        'RABIA' => 3,
41        'RABIT' => 4,
42        'JUMAA' => 5,
43        'JUMAT' => 6,
44        'RAJAB' => 7,
45        'SHAAB' => 8,
46        'RAMAD' => 9,
47        'SHAWW' => 10,
48        'DHUAQ' => 11,
49        'DHUAH' => 12,
50    ];
51
52    /**
53     * Create a date from either:
54     * a Julian day number
55     * day/month/year strings from a GEDCOM date
56     * another CalendarDate object
57     *
58     * @param array|int|AbstractCalendarDate $date
59     */
60    public function __construct($date)
61    {
62        $this->calendar = new ArabicCalendar();
63        parent::__construct($date);
64    }
65
66    /**
67     * Full month name in nominative case.
68     *
69     * @param int  $month
70     * @param bool $leap_year Some calendars use leap months
71     *
72     * @return string
73     */
74    protected function monthNameNominativeCase(int $month, bool $leap_year): string
75    {
76        static $translated_month_names;
77
78        if ($translated_month_names === null) {
79            $translated_month_names = [
80                0  => '',
81                /* I18N: http://en.wikipedia.org/wiki/Muharram */
82                1  => I18N::translateContext('NOMINATIVE', 'Muharram'),
83                /* I18N: http://en.wikipedia.org/wiki/Safar */
84                2  => I18N::translateContext('NOMINATIVE', 'Safar'),
85                /* I18N: http://en.wikipedia.org/wiki/Rabi%27_al-awwal */
86                3  => I18N::translateContext('NOMINATIVE', 'Rabi’ al-awwal'),
87                /* I18N: http://en.wikipedia.org/wiki/Rabi%27_al-thani */
88                4  => I18N::translateContext('NOMINATIVE', 'Rabi’ al-thani'),
89                /* I18N: http://en.wikipedia.org/wiki/Jumada_al-awwal */
90                5  => I18N::translateContext('NOMINATIVE', 'Jumada al-awwal'),
91                /* I18N: http://en.wikipedia.org/wiki/Jumada_al-thani */
92                6  => I18N::translateContext('NOMINATIVE', 'Jumada al-thani'),
93                /* I18N: http://en.wikipedia.org/wiki/Rajab */
94                7  => I18N::translateContext('NOMINATIVE', 'Rajab'),
95                /* I18N: http://en.wikipedia.org/wiki/Sha%27aban */
96                8  => I18N::translateContext('NOMINATIVE', 'Sha’aban'),
97                /* I18N: http://en.wikipedia.org/wiki/Ramadan_%28calendar_month%29 */
98                9  => I18N::translateContext('NOMINATIVE', 'Ramadan'),
99                /* I18N: http://en.wikipedia.org/wiki/Shawwal */
100                10 => I18N::translateContext('NOMINATIVE', 'Shawwal'),
101                /* I18N: http://en.wikipedia.org/wiki/Dhu_al-Qi%27dah */
102                11 => I18N::translateContext('NOMINATIVE', 'Dhu al-Qi’dah'),
103                /* I18N: http://en.wikipedia.org/wiki/Dhu_al-Hijjah */
104                12 => I18N::translateContext('NOMINATIVE', 'Dhu al-Hijjah'),
105            ];
106        }
107
108        return $translated_month_names[$month];
109    }
110
111    /**
112     * Full month name in genitive case.
113     *
114     * @param int  $month
115     * @param bool $leap_year Some calendars use leap months
116     *
117     * @return string
118     */
119    protected function monthNameGenitiveCase(int $month, bool $leap_year): string
120    {
121        static $translated_month_names;
122
123        if ($translated_month_names === null) {
124            $translated_month_names = [
125                0  => '',
126                /* I18N: http://en.wikipedia.org/wiki/Muharram */
127                1  => I18N::translateContext('GENITIVE', 'Muharram'),
128                /* I18N: http://en.wikipedia.org/wiki/Safar */
129                2  => I18N::translateContext('GENITIVE', 'Safar'),
130                /* I18N: http://en.wikipedia.org/wiki/Rabi%27_al-awwal */
131                3  => I18N::translateContext('GENITIVE', 'Rabi’ al-awwal'),
132                /* I18N: http://en.wikipedia.org/wiki/Rabi%27_al-thani */
133                4  => I18N::translateContext('GENITIVE', 'Rabi’ al-thani'),
134                /* I18N: http://en.wikipedia.org/wiki/Jumada_al-awwal */
135                5  => I18N::translateContext('GENITIVE', 'Jumada al-awwal'),
136                /* I18N: http://en.wikipedia.org/wiki/Jumada_al-thani */
137                6  => I18N::translateContext('GENITIVE', 'Jumada al-thani'),
138                /* I18N: http://en.wikipedia.org/wiki/Rajab */
139                7  => I18N::translateContext('GENITIVE', 'Rajab'),
140                /* I18N: http://en.wikipedia.org/wiki/Sha%27aban */
141                8  => I18N::translateContext('GENITIVE', 'Sha’aban'),
142                /* I18N: http://en.wikipedia.org/wiki/Ramadan_%28calendar_month%29 */
143                9  => I18N::translateContext('GENITIVE', 'Ramadan'),
144                /* I18N: http://en.wikipedia.org/wiki/Shawwal */
145                10 => I18N::translateContext('GENITIVE', 'Shawwal'),
146                /* I18N: http://en.wikipedia.org/wiki/Dhu_al-Qi%27dah */
147                11 => I18N::translateContext('GENITIVE', 'Dhu al-Qi’dah'),
148                /* I18N: http://en.wikipedia.org/wiki/Dhu_al-Hijjah */
149                12 => I18N::translateContext('GENITIVE', 'Dhu al-Hijjah'),
150            ];
151        }
152
153        return $translated_month_names[$month];
154    }
155
156    /**
157     * Full month name in locative case.
158     *
159     * @param int  $month
160     * @param bool $leap_year Some calendars use leap months
161     *
162     * @return string
163     */
164    protected function monthNameLocativeCase(int $month, bool $leap_year): string
165    {
166        static $translated_month_names;
167
168        if ($translated_month_names === null) {
169            $translated_month_names = [
170                0  => '',
171                /* I18N: http://en.wikipedia.org/wiki/Muharram */
172                1  => I18N::translateContext('LOCATIVE', 'Muharram'),
173                /* I18N: http://en.wikipedia.org/wiki/Safar */
174                2  => I18N::translateContext('LOCATIVE', 'Safar'),
175                /* I18N: http://en.wikipedia.org/wiki/Rabi%27_al-awwal */
176                3  => I18N::translateContext('LOCATIVE', 'Rabi’ al-awwal'),
177                /* I18N: http://en.wikipedia.org/wiki/Rabi%27_al-thani */
178                4  => I18N::translateContext('LOCATIVE', 'Rabi’ al-thani'),
179                /* I18N: http://en.wikipedia.org/wiki/Jumada_al-awwal */
180                5  => I18N::translateContext('LOCATIVE', 'Jumada al-awwal'),
181                /* I18N: http://en.wikipedia.org/wiki/Jumada_al-thani */
182                6  => I18N::translateContext('LOCATIVE', 'Jumada al-thani'),
183                /* I18N: http://en.wikipedia.org/wiki/Rajab */
184                7  => I18N::translateContext('LOCATIVE', 'Rajab'),
185                /* I18N: http://en.wikipedia.org/wiki/Sha%27aban */
186                8  => I18N::translateContext('LOCATIVE', 'Sha’aban'),
187                /* I18N: http://en.wikipedia.org/wiki/Ramadan_%28calendar_month%29 */
188                9  => I18N::translateContext('LOCATIVE', 'Ramadan'),
189                /* I18N: http://en.wikipedia.org/wiki/Shawwal */
190                10 => I18N::translateContext('LOCATIVE', 'Shawwal'),
191                /* I18N: http://en.wikipedia.org/wiki/Dhu_al-Qi%27dah */
192                11 => I18N::translateContext('LOCATIVE', 'Dhu al-Qi’dah'),
193                /* I18N: http://en.wikipedia.org/wiki/Dhu_al-Hijjah */
194                12 => I18N::translateContext('LOCATIVE', 'Dhu al-Hijjah'),
195            ];
196        }
197
198        return $translated_month_names[$month];
199    }
200
201    /**
202     * Full month name in instrumental case.
203     *
204     * @param int  $month
205     * @param bool $leap_year Some calendars use leap months
206     *
207     * @return string
208     */
209    protected function monthNameInstrumentalCase(int $month, bool $leap_year): string
210    {
211        static $translated_month_names;
212
213        if ($translated_month_names === null) {
214            $translated_month_names = [
215                0  => '',
216                /* I18N: http://en.wikipedia.org/wiki/Muharram */
217                1  => I18N::translateContext('INSTRUMENTAL', 'Muharram'),
218                /* I18N: http://en.wikipedia.org/wiki/Safar */
219                2  => I18N::translateContext('INSTRUMENTAL', 'Safar'),
220                /* I18N: http://en.wikipedia.org/wiki/Rabi%27_al-awwal */
221                3  => I18N::translateContext('INSTRUMENTAL', 'Rabi’ al-awwal'),
222                /* I18N: http://en.wikipedia.org/wiki/Rabi%27_al-thani */
223                4  => I18N::translateContext('INSTRUMENTAL', 'Rabi’ al-thani'),
224                /* I18N: http://en.wikipedia.org/wiki/Jumada_al-awwal */
225                5  => I18N::translateContext('INSTRUMENTAL', 'Jumada al-awwal'),
226                /* I18N: http://en.wikipedia.org/wiki/Jumada_al-thani */
227                6  => I18N::translateContext('INSTRUMENTAL', 'Jumada al-thani'),
228                /* I18N: http://en.wikipedia.org/wiki/Rajab */
229                7  => I18N::translateContext('INSTRUMENTAL', 'Rajab'),
230                /* I18N: http://en.wikipedia.org/wiki/Sha%27aban */
231                8  => I18N::translateContext('INSTRUMENTAL', 'Sha’aban'),
232                /* I18N: http://en.wikipedia.org/wiki/Ramadan_%28calendar_month%29 */
233                9  => I18N::translateContext('INSTRUMENTAL', 'Ramadan'),
234                /* I18N: http://en.wikipedia.org/wiki/Shawwal */
235                10 => I18N::translateContext('INSTRUMENTAL', 'Shawwal'),
236                /* I18N: http://en.wikipedia.org/wiki/Dhu_al-Qi%27dah */
237                11 => I18N::translateContext('INSTRUMENTAL', 'Dhu al-Qi’dah'),
238                /* I18N: http://en.wikipedia.org/wiki/Dhu_al-Hijjah */
239                12 => I18N::translateContext('INSTRUMENTAL', 'Dhu al-Hijjah'),
240            ];
241        }
242
243        return $translated_month_names[$month];
244    }
245
246    /**
247     * Abbreviated month name
248     *
249     * @param int  $month
250     * @param bool $leap_year Some calendars use leap months
251     *
252     * @return string
253     */
254    protected function monthNameAbbreviated(int $month, bool $leap_year): string
255    {
256        return $this->monthNameNominativeCase($month, $leap_year);
257    }
258}
259