xref: /webtrees/app/Date/JulianDate.php (revision 1062a1429914c995339f502856821457aa975a5a)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
4*1062a142SGreg Roach * Copyright (C) 2018 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Date;
17a25f0a04SGreg Roach
18a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\JulianCalendar;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
20a25f0a04SGreg Roach
21a25f0a04SGreg Roach/**
2276692c8bSGreg Roach * Definitions for the Julian Proleptic calendar
23a25f0a04SGreg Roach * (Proleptic means we extend it backwards, prior to its introduction in 46BC)
24a25f0a04SGreg Roach */
25a25f0a04SGreg Roachclass JulianDate extends CalendarDate {
26cbc1590aSGreg Roach	/** @var bool True for dates recorded in new-style/old-style format, e.g. 2 FEB 1743/44 */
27a25f0a04SGreg Roach	private $new_old_style = false;
28a25f0a04SGreg Roach
2976692c8bSGreg Roach	/**
3076692c8bSGreg Roach	 * Create a date from either:
3176692c8bSGreg Roach	 * a Julian day number
3276692c8bSGreg Roach	 * day/month/year strings from a GEDCOM date
3376692c8bSGreg Roach	 * another CalendarDate object
3476692c8bSGreg Roach	 *
3576692c8bSGreg Roach	 * @param array|int|CalendarDate $date
3676692c8bSGreg Roach	 */
37a25f0a04SGreg Roach	public function __construct($date) {
38a25f0a04SGreg Roach		$this->calendar = new JulianCalendar;
39a25f0a04SGreg Roach		parent::__construct($date);
40a25f0a04SGreg Roach	}
41a25f0a04SGreg Roach
4276692c8bSGreg Roach	/**
4376692c8bSGreg Roach	 * Most years are 1 more than the previous, but not always (e.g. 1BC->1AD)
4476692c8bSGreg Roach	 *
4576692c8bSGreg Roach	 * @param int $year
4676692c8bSGreg Roach	 *
4776692c8bSGreg Roach	 * @return int
4876692c8bSGreg Roach	 */
4917920f94SGreg Roach	protected function nextYear($year) {
50a25f0a04SGreg Roach		if ($year == -1) {
51a25f0a04SGreg Roach			return 1;
52a25f0a04SGreg Roach		} else {
53a25f0a04SGreg Roach			return $year + 1;
54a25f0a04SGreg Roach		}
55a25f0a04SGreg Roach	}
56a25f0a04SGreg Roach
57a25f0a04SGreg Roach	/**
58a25f0a04SGreg Roach	 * Process new-style/old-style years and years BC
59a25f0a04SGreg Roach	 *
6076692c8bSGreg Roach	 * @param string $year
6176692c8bSGreg Roach	 *
6276692c8bSGreg Roach	 * @return int
63a25f0a04SGreg Roach	 */
64a25f0a04SGreg Roach	protected function extractYear($year) {
65a25f0a04SGreg Roach		if (preg_match('/^(\d\d\d\d)\/\d{1,4}$/', $year, $match)) {
66a25f0a04SGreg Roach			// Assume the first year is correct
67a25f0a04SGreg Roach			$this->new_old_style = true;
68a25f0a04SGreg Roach
69a25f0a04SGreg Roach			return $match[1] + 1;
70a25f0a04SGreg Roach		} elseif (preg_match('/^(\d+) B\.C\.$/', $year, $match)) {
71a25f0a04SGreg Roach			return -$match[1];
72a25f0a04SGreg Roach		} else {
73a25f0a04SGreg Roach			return (int) $year;
74a25f0a04SGreg Roach		}
75a25f0a04SGreg Roach	}
76a25f0a04SGreg Roach
7776692c8bSGreg Roach	/**
7876692c8bSGreg Roach	 * Generate the %Y format for a date.
7976692c8bSGreg Roach	 *
8076692c8bSGreg Roach	 * @return string
8176692c8bSGreg Roach	 */
82a25f0a04SGreg Roach	protected function formatLongYear() {
83a25f0a04SGreg Roach		if ($this->y < 0) {
84a25f0a04SGreg Roach			return /*  I18N: BCE=Before the Common Era, for Julian years < 0. See http://en.wikipedia.org/wiki/Common_Era */
85a25f0a04SGreg Roach				I18N::translate('%s&nbsp;BCE', I18N::digits(-$this->y));
86a25f0a04SGreg Roach		} else {
87a25f0a04SGreg Roach			if ($this->new_old_style) {
88a25f0a04SGreg Roach				return I18N::translate('%s&nbsp;CE', I18N::digits(sprintf('%d/%02d', $this->y - 1, $this->y % 100)));
89a25f0a04SGreg Roach			} else {
90a25f0a04SGreg Roach				return /* I18N: CE=Common Era, for Julian years > 0. See http://en.wikipedia.org/wiki/Common_Era */
91a25f0a04SGreg Roach					I18N::translate('%s&nbsp;CE', I18N::digits($this->y));
92a25f0a04SGreg Roach			}
93a25f0a04SGreg Roach		}
94a25f0a04SGreg Roach	}
95a25f0a04SGreg Roach
9676692c8bSGreg Roach	/**
9776692c8bSGreg Roach	 * Generate the %E format for a date.
9876692c8bSGreg Roach	 *
9976692c8bSGreg Roach	 * @return string
10076692c8bSGreg Roach	 */
101a25f0a04SGreg Roach	protected function formatGedcomYear() {
102a25f0a04SGreg Roach		if ($this->y < 0) {
103a25f0a04SGreg Roach			return sprintf('%04d B.C.', -$this->y);
104a25f0a04SGreg Roach		} else {
105a25f0a04SGreg Roach			if ($this->new_old_style) {
106a25f0a04SGreg Roach				return sprintf('%04d/%02d', $this->y - 1, $this->y % 100);
107a25f0a04SGreg Roach			} else {
108a25f0a04SGreg Roach				return sprintf('%04d', $this->y);
109a25f0a04SGreg Roach			}
110a25f0a04SGreg Roach		}
111a25f0a04SGreg Roach	}
112a25f0a04SGreg Roach}
113