xref: /webtrees/app/Date/JulianDate.php (revision 4e6225d53a8a7b56681ba35a19239662ca20d718)
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	protected static function nextYear($year) {
39		if ($year == -1) {
40			return 1;
41		} else {
42			return $year + 1;
43		}
44	}
45
46	/**
47	 * Process new-style/old-style years and years BC
48	 *
49	 * {@inheritdoc}
50	 */
51	protected function extractYear($year) {
52		if (preg_match('/^(\d\d\d\d)\/\d{1,4}$/', $year, $match)) {
53			// Assume the first year is correct
54			$this->new_old_style = true;
55
56			return $match[1] + 1;
57		} else if (preg_match('/^(\d+) B\.C\.$/', $year, $match)) {
58			return -$match[1];
59		} else {
60			return (int) $year;
61		}
62	}
63
64	/** {@inheritdoc} */
65	protected function formatLongYear() {
66		if ($this->y < 0) {
67			return /*  I18N: BCE=Before the Common Era, for Julian years < 0.  See http://en.wikipedia.org/wiki/Common_Era */
68				I18N::translate('%s&nbsp;BCE', I18N::digits(-$this->y));
69		} else {
70			if ($this->new_old_style) {
71				return I18N::translate('%s&nbsp;CE', I18N::digits(sprintf('%d/%02d', $this->y - 1, $this->y % 100)));
72			} else {
73				return /* I18N: CE=Common Era, for Julian years > 0.  See http://en.wikipedia.org/wiki/Common_Era */
74					I18N::translate('%s&nbsp;CE', I18N::digits($this->y));
75			}
76		}
77	}
78
79	/** {@inheritdoc} */
80	protected function formatGedcomYear() {
81		if ($this->y < 0) {
82			return sprintf('%04d B.C.', -$this->y);
83		} else {
84			if ($this->new_old_style) {
85				return sprintf('%04d/%02d', $this->y - 1, $this->y % 100);
86			} else {
87				return sprintf('%04d', $this->y);
88			}
89		}
90	}
91}
92