xref: /webtrees/app/Date/JulianDate.php (revision bbb76c12bd7338ebbb054916678efe20cb71ce1f)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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 */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Date;
17a25f0a04SGreg Roach
18a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\JulianCalendar;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
20a25f0a04SGreg Roach
21a25f0a04SGreg Roach/**
2276692c8bSGreg Roach * Definitions for the Julian Proleptic calendar
23a25f0a04SGreg Roach * (Proleptic means we extend it backwards, prior to its introduction in 46BC)
24a25f0a04SGreg Roach */
25c1010edaSGreg Roachclass JulianDate extends CalendarDate
26c1010edaSGreg Roach{
27cbc1590aSGreg Roach    /** @var bool True for dates recorded in new-style/old-style format, e.g. 2 FEB 1743/44 */
28a25f0a04SGreg Roach    private $new_old_style = false;
29a25f0a04SGreg Roach
3076692c8bSGreg Roach    /**
3176692c8bSGreg Roach     * Create a date from either:
3276692c8bSGreg Roach     * a Julian day number
3376692c8bSGreg Roach     * day/month/year strings from a GEDCOM date
3476692c8bSGreg Roach     * another CalendarDate object
3576692c8bSGreg Roach     *
3676692c8bSGreg Roach     * @param array|int|CalendarDate $date
3776692c8bSGreg Roach     */
38c1010edaSGreg Roach    public function __construct($date)
39c1010edaSGreg Roach    {
40a25f0a04SGreg Roach        $this->calendar = new JulianCalendar;
41a25f0a04SGreg Roach        parent::__construct($date);
42a25f0a04SGreg Roach    }
43a25f0a04SGreg Roach
4476692c8bSGreg Roach    /**
4576692c8bSGreg Roach     * Most years are 1 more than the previous, but not always (e.g. 1BC->1AD)
4676692c8bSGreg Roach     *
4776692c8bSGreg Roach     * @param int $year
4876692c8bSGreg Roach     *
4976692c8bSGreg Roach     * @return int
5076692c8bSGreg Roach     */
51c1010edaSGreg Roach    protected function nextYear($year)
52c1010edaSGreg Roach    {
53a25f0a04SGreg Roach        if ($year == -1) {
54a25f0a04SGreg Roach            return 1;
55a25f0a04SGreg Roach        } else {
56a25f0a04SGreg Roach            return $year + 1;
57a25f0a04SGreg Roach        }
58a25f0a04SGreg Roach    }
59a25f0a04SGreg Roach
60a25f0a04SGreg Roach    /**
61a25f0a04SGreg Roach     * Process new-style/old-style years and years BC
62a25f0a04SGreg Roach     *
6376692c8bSGreg Roach     * @param string $year
6476692c8bSGreg Roach     *
6576692c8bSGreg Roach     * @return int
66a25f0a04SGreg Roach     */
67c1010edaSGreg Roach    protected function extractYear($year)
68c1010edaSGreg Roach    {
69a25f0a04SGreg Roach        if (preg_match('/^(\d\d\d\d)\/\d{1,4}$/', $year, $match)) {
70a25f0a04SGreg Roach            // Assume the first year is correct
71a25f0a04SGreg Roach            $this->new_old_style = true;
72a25f0a04SGreg Roach
73a25f0a04SGreg Roach            return $match[1] + 1;
74a25f0a04SGreg Roach        } elseif (preg_match('/^(\d+) B\.C\.$/', $year, $match)) {
75a25f0a04SGreg Roach            return -$match[1];
76a25f0a04SGreg Roach        } else {
77a25f0a04SGreg Roach            return (int)$year;
78a25f0a04SGreg Roach        }
79a25f0a04SGreg Roach    }
80a25f0a04SGreg Roach
8176692c8bSGreg Roach    /**
8276692c8bSGreg Roach     * Generate the %Y format for a date.
8376692c8bSGreg Roach     *
8476692c8bSGreg Roach     * @return string
8576692c8bSGreg Roach     */
86c1010edaSGreg Roach    protected function formatLongYear()
87c1010edaSGreg Roach    {
88a25f0a04SGreg Roach        if ($this->y < 0) {
89a25f0a04SGreg Roach            return /*  I18N: BCE=Before the Common Era, for Julian years < 0. See http://en.wikipedia.org/wiki/Common_Era */
90a25f0a04SGreg Roach                I18N::translate('%s&nbsp;BCE', I18N::digits(-$this->y));
91a25f0a04SGreg Roach        } else {
92a25f0a04SGreg Roach            if ($this->new_old_style) {
93a25f0a04SGreg Roach                return I18N::translate('%s&nbsp;CE', I18N::digits(sprintf('%d/%02d', $this->y - 1, $this->y % 100)));
94a25f0a04SGreg Roach            } else {
95*bbb76c12SGreg Roach                /* I18N: CE=Common Era, for Julian years > 0. See http://en.wikipedia.org/wiki/Common_Era */
96*bbb76c12SGreg Roach                return I18N::translate('%s&nbsp;CE', I18N::digits($this->y));
97a25f0a04SGreg Roach            }
98a25f0a04SGreg Roach        }
99a25f0a04SGreg Roach    }
100a25f0a04SGreg Roach
10176692c8bSGreg Roach    /**
10276692c8bSGreg Roach     * Generate the %E format for a date.
10376692c8bSGreg Roach     *
10476692c8bSGreg Roach     * @return string
10576692c8bSGreg Roach     */
106c1010edaSGreg Roach    protected function formatGedcomYear()
107c1010edaSGreg Roach    {
108a25f0a04SGreg Roach        if ($this->y < 0) {
109a25f0a04SGreg Roach            return sprintf('%04d B.C.', -$this->y);
110a25f0a04SGreg Roach        } else {
111a25f0a04SGreg Roach            if ($this->new_old_style) {
112a25f0a04SGreg Roach                return sprintf('%04d/%02d', $this->y - 1, $this->y % 100);
113a25f0a04SGreg Roach            } else {
114a25f0a04SGreg Roach                return sprintf('%04d', $this->y);
115a25f0a04SGreg Roach            }
116a25f0a04SGreg Roach        }
117a25f0a04SGreg Roach    }
118a25f0a04SGreg Roach}
119