xref: /webtrees/app/Date/JulianDate.php (revision 76692c8b291f16d9251d67f27078779f6737fe7e)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
4a25f0a04SGreg Roach * Copyright (C) 2015 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 */
16*76692c8bSGreg Roachnamespace Fisharebest\Webtrees\Date;
17a25f0a04SGreg Roach
18a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\JulianCalendar;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
20a25f0a04SGreg Roach
21a25f0a04SGreg Roach/**
22*76692c8bSGreg 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
29*76692c8bSGreg Roach	/**
30*76692c8bSGreg Roach	 * Create a date from either:
31*76692c8bSGreg Roach	 * a Julian day number
32*76692c8bSGreg Roach	 * day/month/year strings from a GEDCOM date
33*76692c8bSGreg Roach	 * another CalendarDate object
34*76692c8bSGreg Roach	 *
35*76692c8bSGreg Roach	 * @param array|int|CalendarDate $date
36*76692c8bSGreg Roach	 */
37a25f0a04SGreg Roach	public function __construct($date) {
38a25f0a04SGreg Roach		$this->calendar = new JulianCalendar;
39a25f0a04SGreg Roach		parent::__construct($date);
40a25f0a04SGreg Roach	}
41a25f0a04SGreg Roach
42*76692c8bSGreg Roach	/**
43*76692c8bSGreg Roach	 * Most years are 1 more than the previous, but not always (e.g. 1BC->1AD)
44*76692c8bSGreg Roach	 *
45*76692c8bSGreg Roach	 * @param int $year
46*76692c8bSGreg Roach	 *
47*76692c8bSGreg Roach	 * @return int
48*76692c8bSGreg 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	 *
60*76692c8bSGreg Roach	 * @param string $year
61*76692c8bSGreg Roach	 *
62*76692c8bSGreg 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
77*76692c8bSGreg Roach	/**
78*76692c8bSGreg Roach	 * Generate the %Y format for a date.
79*76692c8bSGreg Roach	 *
80*76692c8bSGreg Roach	 * @return string
81*76692c8bSGreg 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
96*76692c8bSGreg Roach	/**
97*76692c8bSGreg Roach	 * Generate the %E format for a date.
98*76692c8bSGreg Roach	 *
99*76692c8bSGreg Roach	 * @return string
100*76692c8bSGreg 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