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