xref: /webtrees/app/Date/JulianDate.php (revision 1e71bdc0ba6fc5add8fed9a3beb51cfca09e47dd)
1<?php
2namespace Fisharebest\Webtrees;
3
4/**
5 * webtrees: online genealogy
6 * Copyright (C) 2015 webtrees development team
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19use Fisharebest\ExtCalendar\JulianCalendar;
20
21/**
22 * Class JulianDate - Definitions for the Julian Proleptic calendar
23 * (Proleptic means we extend it backwards, prior to its introduction in 46BC)
24 */
25class JulianDate extends CalendarDate {
26	const CALENDAR_ESCAPE = '@#DJULIAN@';
27
28	/** @var boolean True for dates recorded in new-style/old-style format, e.g. 2 FEB 1743/44 */
29	private $new_old_style = false;
30
31	/** {@inheritdoc} */
32	public function __construct($date) {
33		$this->calendar = new JulianCalendar;
34		parent::__construct($date);
35	}
36
37	/** {@inheritdoc} */
38	public static function calendarName() {
39		return /* I18N: The julian calendar */
40			I18N::translate('Julian');
41	}
42
43	/** {@inheritdoc} */
44	protected static function nextYear($year) {
45		if ($year == -1) {
46			return 1;
47		} else {
48			return $year + 1;
49		}
50	}
51
52	/**
53	 * Process new-style/old-style years and years BC
54	 *
55	 * {@inheritdoc}
56	 */
57	protected function extractYear($year) {
58		if (preg_match('/^(\d\d\d\d)\/\d{1,4}$/', $year, $match)) {
59			// Assume the first year is correct
60			$this->new_old_style = true;
61
62			return $match[1] + 1;
63		} else if (preg_match('/^(\d+) B\.C\.$/', $year, $match)) {
64			return -$match[1];
65		} else {
66			return (int) $year;
67		}
68	}
69
70	/** {@inheritdoc} */
71	protected function formatLongYear() {
72		if ($this->y < 0) {
73			return /*  I18N: BCE=Before the Common Era, for Julian years < 0.  See http://en.wikipedia.org/wiki/Common_Era */
74				I18N::translate('%s&nbsp;BCE', I18N::digits(-$this->y));
75		} else {
76			if ($this->new_old_style) {
77				return I18N::translate('%s&nbsp;CE', I18N::digits(sprintf('%d/%02d', $this->y - 1, $this->y % 100)));
78			} else {
79				return /* I18N: CE=Common Era, for Julian years > 0.  See http://en.wikipedia.org/wiki/Common_Era */
80					I18N::translate('%s&nbsp;CE', I18N::digits($this->y));
81			}
82		}
83	}
84
85	/** {@inheritdoc} */
86	protected function formatGedcomYear() {
87		if ($this->y < 0) {
88			return sprintf('%04d B.C.', -$this->y);
89		} else {
90			if ($this->new_old_style) {
91				return sprintf('%04d/%02d', $this->y - 1, $this->y % 100);
92			} else {
93				return sprintf('%04d', $this->y);
94			}
95		}
96	}
97}
98