xref: /webtrees/app/Date/JewishDate.php (revision dd04c183d8beed05be2226b30b7dda485ea4538a)
1a25f0a04SGreg Roach<?php
2*dd04c183SGreg Roachnamespace Fisharebest\Webtrees;
3a25f0a04SGreg Roach
4a25f0a04SGreg Roach/**
5a25f0a04SGreg Roach * webtrees: online genealogy
6a25f0a04SGreg Roach * Copyright (C) 2015 webtrees development team
7a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
8a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
9a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
10a25f0a04SGreg Roach * (at your option) any later version.
11a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
12a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
13a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14a25f0a04SGreg Roach * GNU General Public License for more details.
15a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
16a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
17a25f0a04SGreg Roach */
18a25f0a04SGreg Roach
19a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\JewishCalendar;
20a25f0a04SGreg Roach
21a25f0a04SGreg Roach/**
22a25f0a04SGreg Roach * Class JewishDate - Definitions for the Jewish calendar
23a25f0a04SGreg Roach */
24a25f0a04SGreg Roachclass JewishDate extends CalendarDate {
25a25f0a04SGreg Roach	const CALENDAR_ESCAPE = '@#DHEBREW@';
26a25f0a04SGreg Roach	const MONTHS_IN_YEAR = 13;
27a25f0a04SGreg Roach	const CAL_START_JD = 347998; // 01 TSH 0001 = @#JULIAN@ 7 OCT 3761B.C.
28a25f0a04SGreg Roach	const GERSHAYIM = '״';
29a25f0a04SGreg Roach	const GERSH = '׳';
30a25f0a04SGreg Roach	const ALAFIM = 'אלפים';
31a25f0a04SGreg Roach
32a25f0a04SGreg Roach	/** {@inheritdoc} */
33a25f0a04SGreg Roach	public static $MONTH_ABBREV = array('' => 0, 'TSH' => 1, 'CSH' => 2, 'KSL' => 3, 'TVT' => 4, 'SHV' => 5, 'ADR' => 6, 'ADS' => 7, 'NSN' => 8, 'IYR' => 9, 'SVN' => 10, 'TMZ' => 11, 'AAV' => 12, 'ELL' => 13);
34a25f0a04SGreg Roach
35a25f0a04SGreg Roach	/** {@inheritdoc} */
36a25f0a04SGreg Roach	public function __construct($date) {
37a25f0a04SGreg Roach		$this->calendar = new JewishCalendar;
38a25f0a04SGreg Roach		parent::__construct($date);
39a25f0a04SGreg Roach	}
40a25f0a04SGreg Roach
41a25f0a04SGreg Roach	/** {@inheritdoc} */
42a25f0a04SGreg Roach	public static function calendarName() {
43a25f0a04SGreg Roach		return /* I18N: The Hebrew/Jewish calendar */
44a25f0a04SGreg Roach			I18N::translate('Jewish');
45a25f0a04SGreg Roach	}
46a25f0a04SGreg Roach
47a25f0a04SGreg Roach	/** {@inheritdoc} */
48a25f0a04SGreg Roach	function formatDayZeros() {
49a25f0a04SGreg Roach		if (WT_LOCALE == 'he') {
50a25f0a04SGreg Roach			return $this->numberToHebrewNumerals($this->d);
51a25f0a04SGreg Roach		} else {
52a25f0a04SGreg Roach			return $this->d;
53a25f0a04SGreg Roach		}
54a25f0a04SGreg Roach	}
55a25f0a04SGreg Roach
56a25f0a04SGreg Roach	/** {@inheritdoc} */
57a25f0a04SGreg Roach	function formatDay() {
58a25f0a04SGreg Roach		if (WT_LOCALE == 'he') {
59a25f0a04SGreg Roach			return $this->numberToHebrewNumerals($this->d);
60a25f0a04SGreg Roach		} else {
61a25f0a04SGreg Roach			return $this->d;
62a25f0a04SGreg Roach		}
63a25f0a04SGreg Roach	}
64a25f0a04SGreg Roach
65a25f0a04SGreg Roach	/** {@inheritdoc} */
66a25f0a04SGreg Roach	function formatShortYear() {
67a25f0a04SGreg Roach		if (WT_LOCALE == 'he') {
68a25f0a04SGreg Roach			return $this->numberToHebrewNumerals($this->y % 1000);
69a25f0a04SGreg Roach		} else {
70a25f0a04SGreg Roach			return $this->y;
71a25f0a04SGreg Roach		}
72a25f0a04SGreg Roach	}
73a25f0a04SGreg Roach
74a25f0a04SGreg Roach	/** {@inheritdoc} */
75a25f0a04SGreg Roach	function formatLongYear() {
76a25f0a04SGreg Roach		if (WT_LOCALE == 'he') {
77a25f0a04SGreg Roach			return $this->numberToHebrewNumerals($this->y);
78a25f0a04SGreg Roach		} else {
79a25f0a04SGreg Roach			return $this->y;
80a25f0a04SGreg Roach		}
81a25f0a04SGreg Roach	}
82a25f0a04SGreg Roach
83a25f0a04SGreg Roach	/** {@inheritdoc} */
84a25f0a04SGreg Roach	public static function monthNameNominativeCase($month_number, $leap_year) {
85a25f0a04SGreg Roach		static $translated_month_names;
86a25f0a04SGreg Roach
87a25f0a04SGreg Roach		if ($translated_month_names === null) {
88a25f0a04SGreg Roach			$translated_month_names = array(
89a25f0a04SGreg Roach				0  => '',
90a25f0a04SGreg Roach				1  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('NOMINATIVE', 'Tishrei'),
91a25f0a04SGreg Roach				2  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('NOMINATIVE', 'Heshvan'),
92a25f0a04SGreg Roach				3  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('NOMINATIVE', 'Kislev'),
93a25f0a04SGreg Roach				4  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('NOMINATIVE', 'Tevet'),
94a25f0a04SGreg Roach				5  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('NOMINATIVE', 'Shevat'),
95a25f0a04SGreg Roach				6  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('NOMINATIVE', 'Adar I'),
96a25f0a04SGreg Roach				7  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('NOMINATIVE', 'Adar'),
97a25f0a04SGreg Roach				-7 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('NOMINATIVE', 'Adar II'),
98a25f0a04SGreg Roach				8  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('NOMINATIVE', 'Nissan'),
99a25f0a04SGreg Roach				9  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('NOMINATIVE', 'Iyar'),
100a25f0a04SGreg Roach				10 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('NOMINATIVE', 'Sivan'),
101a25f0a04SGreg Roach				11 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('NOMINATIVE', 'Tamuz'),
102a25f0a04SGreg Roach				12 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('NOMINATIVE', 'Av'),
103a25f0a04SGreg Roach				13 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('NOMINATIVE', 'Elul'),
104a25f0a04SGreg Roach			);
105a25f0a04SGreg Roach		}
106a25f0a04SGreg Roach
107a25f0a04SGreg Roach		if ($month_number === 7 && $leap_year) {
108a25f0a04SGreg Roach			return $translated_month_names[-7];
109a25f0a04SGreg Roach		} else {
110a25f0a04SGreg Roach			return $translated_month_names[$month_number];
111a25f0a04SGreg Roach		}
112a25f0a04SGreg Roach	}
113a25f0a04SGreg Roach
114a25f0a04SGreg Roach	/** {@inheritdoc} */
115a25f0a04SGreg Roach	static function monthNameGenitiveCase($month_number, $leap_year) {
116a25f0a04SGreg Roach		static $translated_month_names;
117a25f0a04SGreg Roach
118a25f0a04SGreg Roach		if ($translated_month_names === null) {
119a25f0a04SGreg Roach			$translated_month_names = array(
120a25f0a04SGreg Roach				0  => '',
121a25f0a04SGreg Roach				1  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('GENITIVE', 'Tishrei'),
122a25f0a04SGreg Roach				2  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('GENITIVE', 'Heshvan'),
123a25f0a04SGreg Roach				3  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('GENITIVE', 'Kislev'),
124a25f0a04SGreg Roach				4  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('GENITIVE', 'Tevet'),
125a25f0a04SGreg Roach				5  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('GENITIVE', 'Shevat'),
126a25f0a04SGreg Roach				6  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('GENITIVE', 'Adar I'),
127a25f0a04SGreg Roach				7  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('GENITIVE', 'Adar'),
128a25f0a04SGreg Roach				-7 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('GENITIVE', 'Adar II'),
129a25f0a04SGreg Roach				8  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('GENITIVE', 'Nissan'),
130a25f0a04SGreg Roach				9  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('GENITIVE', 'Iyar'),
131a25f0a04SGreg Roach				10 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('GENITIVE', 'Sivan'),
132a25f0a04SGreg Roach				11 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('GENITIVE', 'Tamuz'),
133a25f0a04SGreg Roach				12 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('GENITIVE', 'Av'),
134a25f0a04SGreg Roach				13 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('GENITIVE', 'Elul'),
135a25f0a04SGreg Roach			);
136a25f0a04SGreg Roach		}
137a25f0a04SGreg Roach
138a25f0a04SGreg Roach		if ($month_number === 7 && $leap_year) {
139a25f0a04SGreg Roach			return $translated_month_names[-7];
140a25f0a04SGreg Roach		} else {
141a25f0a04SGreg Roach			return $translated_month_names[$month_number];
142a25f0a04SGreg Roach		}
143a25f0a04SGreg Roach	}
144a25f0a04SGreg Roach
145a25f0a04SGreg Roach	/** {@inheritdoc} */
146a25f0a04SGreg Roach	protected static function monthNameLocativeCase($month_number, $leap_year) {
147a25f0a04SGreg Roach		static $translated_month_names;
148a25f0a04SGreg Roach
149a25f0a04SGreg Roach		if ($translated_month_names === null) {
150a25f0a04SGreg Roach			$translated_month_names = array(
151a25f0a04SGreg Roach				0  => '',
152a25f0a04SGreg Roach				1  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('LOCATIVE', 'Tishrei'),
153a25f0a04SGreg Roach				2  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('LOCATIVE', 'Heshvan'),
154a25f0a04SGreg Roach				3  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('LOCATIVE', 'Kislev'),
155a25f0a04SGreg Roach				4  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('LOCATIVE', 'Tevet'),
156a25f0a04SGreg Roach				5  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('LOCATIVE', 'Shevat'),
157a25f0a04SGreg Roach				6  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('LOCATIVE', 'Adar I'),
158a25f0a04SGreg Roach				7  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('LOCATIVE', 'Adar'),
159a25f0a04SGreg Roach				-7 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('LOCATIVE', 'Adar II'),
160a25f0a04SGreg Roach				8  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('LOCATIVE', 'Nissan'),
161a25f0a04SGreg Roach				9  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('LOCATIVE', 'Iyar'),
162a25f0a04SGreg Roach				10 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('LOCATIVE', 'Sivan'),
163a25f0a04SGreg Roach				11 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('LOCATIVE', 'Tamuz'),
164a25f0a04SGreg Roach				12 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('LOCATIVE', 'Av'),
165a25f0a04SGreg Roach				13 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('LOCATIVE', 'Elul'),
166a25f0a04SGreg Roach			);
167a25f0a04SGreg Roach		}
168a25f0a04SGreg Roach
169a25f0a04SGreg Roach		if ($month_number === 7 && $leap_year) {
170a25f0a04SGreg Roach			return $translated_month_names[-7];
171a25f0a04SGreg Roach		} else {
172a25f0a04SGreg Roach			return $translated_month_names[$month_number];
173a25f0a04SGreg Roach		}
174a25f0a04SGreg Roach	}
175a25f0a04SGreg Roach
176a25f0a04SGreg Roach	/** {@inheritdoc} */
177a25f0a04SGreg Roach	protected static function monthNameInstrumentalCase($month_number, $leap_year) {
178a25f0a04SGreg Roach		static $translated_month_names;
179a25f0a04SGreg Roach
180a25f0a04SGreg Roach		if ($translated_month_names === null) {
181a25f0a04SGreg Roach			$translated_month_names = array(
182a25f0a04SGreg Roach				0  => '',
183a25f0a04SGreg Roach				1  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('INSTRUMENTAL', 'Tishrei'),
184a25f0a04SGreg Roach				2  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('INSTRUMENTAL', 'Heshvan'),
185a25f0a04SGreg Roach				3  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('INSTRUMENTAL', 'Kislev'),
186a25f0a04SGreg Roach				4  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('INSTRUMENTAL', 'Tevet'),
187a25f0a04SGreg Roach				5  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('INSTRUMENTAL', 'Shevat'),
188a25f0a04SGreg Roach				6  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('INSTRUMENTAL', 'Adar I'),
189a25f0a04SGreg Roach				7  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('INSTRUMENTAL', 'Adar'),
190a25f0a04SGreg Roach				-7 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('INSTRUMENTAL', 'Adar II'),
191a25f0a04SGreg Roach				8  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('INSTRUMENTAL', 'Nissan'),
192a25f0a04SGreg Roach				9  => /* I18N: a month in the Jewish calendar */ I18N::translate_c('INSTRUMENTAL', 'Iyar'),
193a25f0a04SGreg Roach				10 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('INSTRUMENTAL', 'Sivan'),
194a25f0a04SGreg Roach				11 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('INSTRUMENTAL', 'Tamuz'),
195a25f0a04SGreg Roach				12 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('INSTRUMENTAL', 'Av'),
196a25f0a04SGreg Roach				13 => /* I18N: a month in the Jewish calendar */ I18N::translate_c('INSTRUMENTAL', 'Elul'),
197a25f0a04SGreg Roach			);
198a25f0a04SGreg Roach		}
199a25f0a04SGreg Roach
200a25f0a04SGreg Roach		if ($month_number === 7 && $leap_year) {
201a25f0a04SGreg Roach			return $translated_month_names[-7];
202a25f0a04SGreg Roach		} else {
203a25f0a04SGreg Roach			return $translated_month_names[$month_number];
204a25f0a04SGreg Roach		}
205a25f0a04SGreg Roach	}
206a25f0a04SGreg Roach
207a25f0a04SGreg Roach	/** {@inheritdoc} */
208a25f0a04SGreg Roach	protected static function monthNameAbbreviated($month_number, $leap_year) {
209a25f0a04SGreg Roach		return self::monthNameNominativeCase($month_number, $leap_year);
210a25f0a04SGreg Roach	}
211a25f0a04SGreg Roach
212a25f0a04SGreg Roach	/** {@inheritdoc} */
213a25f0a04SGreg Roach	protected function nextMonth() {
214a25f0a04SGreg Roach		if ($this->m == 6 && !$this->isLeapYear()) {
215a25f0a04SGreg Roach			return array($this->y, 8);
216a25f0a04SGreg Roach		} else {
217a25f0a04SGreg Roach			return array($this->y + ($this->m == 13 ? 1 : 0), ($this->m % 13) + 1);
218a25f0a04SGreg Roach		}
219a25f0a04SGreg Roach	}
220a25f0a04SGreg Roach
221a25f0a04SGreg Roach	/**
222a25f0a04SGreg Roach	 * Convert a decimal number to hebrew - like roman numerals, but with extra punctuation and special rules.
223a25f0a04SGreg Roach	 *
224a25f0a04SGreg Roach	 * @param integer $num
225a25f0a04SGreg Roach	 *
226a25f0a04SGreg Roach	 * @return string
227a25f0a04SGreg Roach	 */
228a25f0a04SGreg Roach	protected static function numberToHebrewNumerals($num) {
229a25f0a04SGreg Roach		$DISPLAY_JEWISH_THOUSANDS = false;
230a25f0a04SGreg Roach
231a25f0a04SGreg Roach		static $jHundreds = array("", "ק", "ר", "ש", "ת", "תק", "תר", "תש", "תת", "תתק");
232a25f0a04SGreg Roach		static $jTens = array("", "י", "כ", "ל", "מ", "נ", "ס", "ע", "פ", "צ");
233a25f0a04SGreg Roach		static $jTenEnds = array("", "י", "ך", "ל", "ם", "ן", "ס", "ע", "ף", "ץ");
234a25f0a04SGreg Roach		static $tavTaz = array("ט״ו", "ט״ז");
235a25f0a04SGreg Roach		static $jOnes = array("", "א", "ב", "ג", "ד", "ה", "ו", "ז", "ח", "ט");
236a25f0a04SGreg Roach
237a25f0a04SGreg Roach		$shortYear = $num % 1000; //discard thousands
238a25f0a04SGreg Roach		//next check for all possible single Hebrew digit years
239a25f0a04SGreg Roach		$singleDigitYear = ($shortYear < 11 || ($shortYear < 100 && $shortYear % 10 == 0) || ($shortYear <= 400 && $shortYear % 100 == 0));
240a25f0a04SGreg Roach		$thousands       = (int) ($num / 1000); //get # thousands
241a25f0a04SGreg Roach		$sb              = "";
242a25f0a04SGreg Roach		//append thousands to String
243a25f0a04SGreg Roach		if ($num % 1000 == 0) {
244a25f0a04SGreg Roach			// in year is 5000, 4000 etc
245a25f0a04SGreg Roach			$sb .= $jOnes[$thousands];
246a25f0a04SGreg Roach			$sb .= self::GERSH;
247a25f0a04SGreg Roach			$sb .= " ";
248a25f0a04SGreg Roach			$sb .= self::ALAFIM; //add # of thousands plus word thousand (overide alafim boolean)
249a25f0a04SGreg Roach		} elseif ($DISPLAY_JEWISH_THOUSANDS) {
250a25f0a04SGreg Roach			// if alafim boolean display thousands
251a25f0a04SGreg Roach			$sb .= $jOnes[$thousands];
252a25f0a04SGreg Roach			$sb .= self::GERSH; //append thousands quote
253a25f0a04SGreg Roach			$sb .= " ";
254a25f0a04SGreg Roach		}
255a25f0a04SGreg Roach		$num      = $num % 1000; //remove 1000s
256a25f0a04SGreg Roach		$hundreds = (int) ($num / 100); // # of hundreds
257a25f0a04SGreg Roach		$sb .= $jHundreds[$hundreds]; //add hundreds to String
258a25f0a04SGreg Roach		$num = $num % 100; //remove 100s
259a25f0a04SGreg Roach		if ($num == 15) {
260a25f0a04SGreg Roach			$sb .= $tavTaz[0];
261a25f0a04SGreg Roach		} else if ($num == 16) {
262a25f0a04SGreg Roach			$sb .= $tavTaz[1];
263a25f0a04SGreg Roach		} else {
264a25f0a04SGreg Roach			$tens = (int) ($num / 10);
265a25f0a04SGreg Roach			if ($num % 10 == 0) {
266a25f0a04SGreg Roach				if ($singleDigitYear == false) {
267a25f0a04SGreg Roach					$sb .= $jTenEnds[$tens]; // use end letters so that for example 5750 will end with an end nun
268a25f0a04SGreg Roach				} else {
269a25f0a04SGreg Roach					$sb .= $jTens[$tens]; // use standard letters so that for example 5050 will end with a regular nun
270a25f0a04SGreg Roach				}
271a25f0a04SGreg Roach			} else {
272a25f0a04SGreg Roach				$sb .= $jTens[$tens];
273a25f0a04SGreg Roach				$num = $num % 10;
274a25f0a04SGreg Roach				$sb .= $jOnes[$num];
275a25f0a04SGreg Roach			}
276a25f0a04SGreg Roach		}
277a25f0a04SGreg Roach		if ($singleDigitYear == true) {
278a25f0a04SGreg Roach			// Append single quote
279a25f0a04SGreg Roach			$sb .= self::GERSH;
280a25f0a04SGreg Roach		} else {
281a25f0a04SGreg Roach			// Append double quote before last digit
282a25f0a04SGreg Roach			$pos1 = strlen($sb) - 2;
283a25f0a04SGreg Roach			$sb   = substr($sb, 0, $pos1) . self::GERSHAYIM . substr($sb, $pos1);
284a25f0a04SGreg Roach			// Replace double gershayim with single instance
285a25f0a04SGreg Roach			$sb   = str_replace(self::GERSHAYIM . self::GERSHAYIM, self::GERSHAYIM, $sb);
286a25f0a04SGreg Roach		}
287a25f0a04SGreg Roach
288a25f0a04SGreg Roach		return $sb;
289a25f0a04SGreg Roach	}
290a25f0a04SGreg Roach}
291