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