xref: /webtrees/app/Date/JulianDate.php (revision 0e62c4b8d0ec6901bfaffd1dc763db37489518a4)
1<?php
2namespace Fisharebest\Webtrees\Date;
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;
20use Fisharebest\Webtrees\I18N;
21
22/**
23 * Class JulianDate - Definitions for the Julian Proleptic calendar
24 * (Proleptic means we extend it backwards, prior to its introduction in 46BC)
25 */
26class JulianDate extends CalendarDate {
27	/** @var bool True for dates recorded in new-style/old-style format, e.g. 2 FEB 1743/44 */
28	private $new_old_style = false;
29
30	/** {@inheritdoc} */
31	public function __construct($date) {
32		$this->calendar = new JulianCalendar;
33		parent::__construct($date);
34	}
35
36	/** {@inheritdoc} */
37	protected function nextYear($year) {
38		if ($year == -1) {
39			return 1;
40		} else {
41			return $year + 1;
42		}
43	}
44
45	/**
46	 * Process new-style/old-style years and years BC
47	 *
48	 * {@inheritdoc}
49	 */
50	protected function extractYear($year) {
51		if (preg_match('/^(\d\d\d\d)\/\d{1,4}$/', $year, $match)) {
52			// Assume the first year is correct
53			$this->new_old_style = true;
54
55			return $match[1] + 1;
56		} elseif (preg_match('/^(\d+) B\.C\.$/', $year, $match)) {
57			return -$match[1];
58		} else {
59			return (int) $year;
60		}
61	}
62
63	/** {@inheritdoc} */
64	protected function formatLongYear() {
65		if ($this->y < 0) {
66			return /*  I18N: BCE=Before the Common Era, for Julian years < 0.  See http://en.wikipedia.org/wiki/Common_Era */
67				I18N::translate('%s&nbsp;BCE', I18N::digits(-$this->y));
68		} else {
69			if ($this->new_old_style) {
70				return I18N::translate('%s&nbsp;CE', I18N::digits(sprintf('%d/%02d', $this->y - 1, $this->y % 100)));
71			} else {
72				return /* I18N: CE=Common Era, for Julian years > 0.  See http://en.wikipedia.org/wiki/Common_Era */
73					I18N::translate('%s&nbsp;CE', I18N::digits($this->y));
74			}
75		}
76	}
77
78	/** {@inheritdoc} */
79	protected function formatGedcomYear() {
80		if ($this->y < 0) {
81			return sprintf('%04d B.C.', -$this->y);
82		} else {
83			if ($this->new_old_style) {
84				return sprintf('%04d/%02d', $this->y - 1, $this->y % 100);
85			} else {
86				return sprintf('%04d', $this->y);
87			}
88		}
89	}
90}
91