xref: /webtrees/app/Date/JulianDate.php (revision 4a83f5d742e43c37a641392fe50a3db201bffb30)
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 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Date;
19a25f0a04SGreg Roach
20a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\JulianCalendar;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
22a25f0a04SGreg Roach
23a25f0a04SGreg Roach/**
24*4a83f5d7SGreg Roach * Definitions for proleptic Julian dates.
25a25f0a04SGreg Roach */
26*4a83f5d7SGreg Roachclass JulianDate extends AbstractGregorianJulianDate
27c1010edaSGreg Roach{
28*4a83f5d7SGreg Roach    // GEDCOM calendar escape
29*4a83f5d7SGreg Roach    const ESCAPE = '@#DJULIAN@';
30*4a83f5d7SGreg Roach
31cbc1590aSGreg Roach    /** @var bool True for dates recorded in new-style/old-style format, e.g. 2 FEB 1743/44 */
32a25f0a04SGreg Roach    private $new_old_style = false;
33a25f0a04SGreg Roach
3476692c8bSGreg Roach    /**
3576692c8bSGreg Roach     * Create a date from either:
3676692c8bSGreg Roach     * a Julian day number
3776692c8bSGreg Roach     * day/month/year strings from a GEDCOM date
3876692c8bSGreg Roach     * another CalendarDate object
3976692c8bSGreg Roach     *
40*4a83f5d7SGreg Roach     * @param array|int|AbstractCalendarDate $date
4176692c8bSGreg Roach     */
42c1010edaSGreg Roach    public function __construct($date)
43c1010edaSGreg Roach    {
4459f2f229SGreg Roach        $this->calendar = new JulianCalendar();
45a25f0a04SGreg Roach        parent::__construct($date);
46a25f0a04SGreg Roach    }
47a25f0a04SGreg Roach
4876692c8bSGreg Roach    /**
4976692c8bSGreg Roach     * Most years are 1 more than the previous, but not always (e.g. 1BC->1AD)
5076692c8bSGreg Roach     *
5176692c8bSGreg Roach     * @param int $year
5276692c8bSGreg Roach     *
5376692c8bSGreg Roach     * @return int
5476692c8bSGreg Roach     */
55fe11e66dSGreg Roach    protected function nextYear(int $year): int
56c1010edaSGreg Roach    {
57a25f0a04SGreg Roach        if ($year == -1) {
58a25f0a04SGreg Roach            return 1;
59a25f0a04SGreg Roach        }
60b2ce94c6SRico Sonntag
61b2ce94c6SRico Sonntag        return $year + 1;
62a25f0a04SGreg Roach    }
63a25f0a04SGreg Roach
64a25f0a04SGreg Roach    /**
65a25f0a04SGreg Roach     * Process new-style/old-style years and years BC
66a25f0a04SGreg Roach     *
6776692c8bSGreg Roach     * @param string $year
6876692c8bSGreg Roach     *
6976692c8bSGreg Roach     * @return int
70a25f0a04SGreg Roach     */
71fe11e66dSGreg Roach    protected function extractYear(string $year): int
72c1010edaSGreg Roach    {
73a25f0a04SGreg Roach        if (preg_match('/^(\d\d\d\d)\/\d{1,4}$/', $year, $match)) {
74a25f0a04SGreg Roach            // Assume the first year is correct
75a25f0a04SGreg Roach            $this->new_old_style = true;
76a25f0a04SGreg Roach
77db7bb364SGreg Roach            return (int) $match[1] + 1;
78a25f0a04SGreg Roach        }
79a0cead57SGreg Roach
80a0cead57SGreg Roach        if (preg_match('/^(\d+) B\.C\.$/', $year, $match)) {
81a0cead57SGreg Roach            return - (int) $match[1];
82a0cead57SGreg Roach        }
83a0cead57SGreg Roach
84a0cead57SGreg Roach        return (int) $year;
85a25f0a04SGreg Roach    }
86a25f0a04SGreg Roach
8776692c8bSGreg Roach    /**
8876692c8bSGreg Roach     * Generate the %Y format for a date.
8976692c8bSGreg Roach     *
9076692c8bSGreg Roach     * @return string
9176692c8bSGreg Roach     */
92fe11e66dSGreg Roach    protected function formatLongYear(): string
93c1010edaSGreg Roach    {
94*4a83f5d7SGreg Roach        if ($this->year < 0) {
95a25f0a04SGreg Roach            return /*  I18N: BCE=Before the Common Era, for Julian years < 0. See http://en.wikipedia.org/wiki/Common_Era */
96*4a83f5d7SGreg Roach                I18N::translate('%s&nbsp;BCE', I18N::digits(-$this->year));
97b2ce94c6SRico Sonntag        }
98b2ce94c6SRico Sonntag
99a25f0a04SGreg Roach        if ($this->new_old_style) {
100*4a83f5d7SGreg Roach            return I18N::translate('%s&nbsp;CE', I18N::digits(sprintf('%d/%02d', $this->year - 1, $this->year % 100)));
101b2ce94c6SRico Sonntag        }
102b2ce94c6SRico Sonntag
103bbb76c12SGreg Roach        /* I18N: CE=Common Era, for Julian years > 0. See http://en.wikipedia.org/wiki/Common_Era */
104*4a83f5d7SGreg Roach        return I18N::translate('%s&nbsp;CE', I18N::digits($this->year));
105a25f0a04SGreg Roach    }
106a25f0a04SGreg Roach
10776692c8bSGreg Roach    /**
10876692c8bSGreg Roach     * Generate the %E format for a date.
10976692c8bSGreg Roach     *
11076692c8bSGreg Roach     * @return string
11176692c8bSGreg Roach     */
112fe11e66dSGreg Roach    protected function formatGedcomYear(): string
113c1010edaSGreg Roach    {
114*4a83f5d7SGreg Roach        if ($this->year < 0) {
115*4a83f5d7SGreg Roach            return sprintf('%04d B.C.', -$this->year);
116b2ce94c6SRico Sonntag        }
117b2ce94c6SRico Sonntag
118a25f0a04SGreg Roach        if ($this->new_old_style) {
119*4a83f5d7SGreg Roach            return sprintf('%04d/%02d', $this->year - 1, $this->year % 100);
120b2ce94c6SRico Sonntag        }
121b2ce94c6SRico Sonntag
122*4a83f5d7SGreg Roach        return sprintf('%04d', $this->year);
123a25f0a04SGreg Roach    }
124a25f0a04SGreg Roach}
125