xref: /webtrees/app/Date.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
1a25f0a04SGreg Roach<?php
2*3976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17e7f56f2aSGreg Roachdeclare(strict_types=1);
18e7f56f2aSGreg Roach
1976692c8bSGreg Roachnamespace Fisharebest\Webtrees;
20a25f0a04SGreg Roach
21a80c4362SGreg Roachuse DomainException;
22a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
234a83f5d7SGreg Roachuse Fisharebest\Webtrees\Date\AbstractCalendarDate;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\FrenchDate;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\HijriDate;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JalaliDate;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate;
290e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JulianDate;
300e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\RomanDate;
31a25f0a04SGreg Roach
32a25f0a04SGreg Roach/**
3376692c8bSGreg Roach * A representation of GEDCOM dates and date ranges.
3476692c8bSGreg Roach *
3576692c8bSGreg Roach * Since different calendars start their days at different times, (civil
36a25f0a04SGreg Roach * midnight, solar midnight, sunset, sunrise, etc.), we convert on the basis of
37a25f0a04SGreg Roach * midday.
38a25f0a04SGreg Roach *
3976692c8bSGreg Roach * We assume that years start on the first day of the first month. Where
40a25f0a04SGreg Roach * this is not the case (e.g. England prior to 1752), we need to use modified
41a25f0a04SGreg Roach * years or the OS/NS notation "4 FEB 1750/51".
42a25f0a04SGreg Roach */
43c1010edaSGreg Roachclass Date
44c1010edaSGreg Roach{
45a25f0a04SGreg Roach    /** @var string Optional qualifier, such as BEF, FROM, ABT */
46fe11e66dSGreg Roach    public $qual1 = '';
47a25f0a04SGreg Roach
484a83f5d7SGreg Roach    /** @var AbstractCalendarDate  The first (or only) date */
49f5b60decSGreg Roach    private $date1;
50a25f0a04SGreg Roach
51a25f0a04SGreg Roach    /** @var string  Optional qualifier, such as TO, AND */
52fe11e66dSGreg Roach    public $qual2 = '';
53a25f0a04SGreg Roach
544a83f5d7SGreg Roach    /** @var AbstractCalendarDate|null Optional second date */
557e96c925SGreg Roach    private $date2 = null;
56a25f0a04SGreg Roach
57a25f0a04SGreg Roach    /** @var string ptional text, as included with an INTerpreted date */
587e96c925SGreg Roach    private $text = '';
59a25f0a04SGreg Roach
60a25f0a04SGreg Roach    /**
61a25f0a04SGreg Roach     * Create a date, from GEDCOM data.
62a25f0a04SGreg Roach     *
63a25f0a04SGreg Roach     * @param string $date A date in GEDCOM format
64a25f0a04SGreg Roach     */
65cfe766afSGreg Roach    public function __construct(string $date)
66c1010edaSGreg Roach    {
67a25f0a04SGreg Roach        // Extract any explanatory text
68a25f0a04SGreg Roach        if (preg_match('/^(.*) ?[(](.*)[)]/', $date, $match)) {
69a25f0a04SGreg Roach            $date       = $match[1];
70a25f0a04SGreg Roach            $this->text = $match[2];
71a25f0a04SGreg Roach        }
72a25f0a04SGreg Roach        if (preg_match('/^(FROM|BET) (.+) (AND|TO) (.+)/', $date, $match)) {
73a25f0a04SGreg Roach            $this->qual1 = $match[1];
74a25f0a04SGreg Roach            $this->date1 = $this->parseDate($match[2]);
75a25f0a04SGreg Roach            $this->qual2 = $match[3];
76a25f0a04SGreg Roach            $this->date2 = $this->parseDate($match[4]);
773506b2e5SGreg Roach        } elseif (preg_match('/^(TO|FROM|BEF|AFT|CAL|EST|INT|ABT) (.+)/', $date, $match)) {
78a25f0a04SGreg Roach            $this->qual1 = $match[1];
79a25f0a04SGreg Roach            $this->date1 = $this->parseDate($match[2]);
80a25f0a04SGreg Roach        } else {
81a25f0a04SGreg Roach            $this->date1 = $this->parseDate($date);
82a25f0a04SGreg Roach        }
83a25f0a04SGreg Roach    }
84a25f0a04SGreg Roach
85a25f0a04SGreg Roach    /**
86a25f0a04SGreg Roach     * When we copy a date object, we need to create copies of
87a25f0a04SGreg Roach     * its child objects.
88a25f0a04SGreg Roach     */
89c1010edaSGreg Roach    public function __clone()
90c1010edaSGreg Roach    {
91a25f0a04SGreg Roach        $this->date1 = clone $this->date1;
927e96c925SGreg Roach        if ($this->date2 !== null) {
93a25f0a04SGreg Roach            $this->date2 = clone $this->date2;
94a25f0a04SGreg Roach        }
95a25f0a04SGreg Roach    }
96a25f0a04SGreg Roach
97a25f0a04SGreg Roach    /**
98a25f0a04SGreg Roach     * Convert a calendar date, such as "12 JUN 1943" into calendar date object.
99a25f0a04SGreg Roach     * A GEDCOM date range may have two calendar dates.
100a25f0a04SGreg Roach     *
101a25f0a04SGreg Roach     * @param string $date
102a25f0a04SGreg Roach     *
103a80c4362SGreg Roach     * @throws DomainException
1044a83f5d7SGreg Roach     * @return AbstractCalendarDate
105a25f0a04SGreg Roach     */
1064a83f5d7SGreg Roach    private function parseDate($date): AbstractCalendarDate
107c1010edaSGreg Roach    {
108a25f0a04SGreg Roach        // Valid calendar escape specified? - use it
109bee1f90bSGreg Roach        if (preg_match('/^(@#D(?:GREGORIAN|JULIAN|HEBREW|HIJRI|JALALI|FRENCH R|ROMAN)+@) ?(.*)/', $date, $match)) {
110a25f0a04SGreg Roach            $cal  = $match[1];
111a25f0a04SGreg Roach            $date = $match[2];
112a25f0a04SGreg Roach        } else {
113a25f0a04SGreg Roach            $cal = '';
114a25f0a04SGreg Roach        }
115a25f0a04SGreg Roach        // A date with a month: DM, M, MY or DMY
116a25f0a04SGreg Roach        if (preg_match('/^(\d?\d?) ?(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|TSH|CSH|KSL|TVT|SHV|ADR|ADS|NSN|IYR|SVN|TMZ|AAV|ELL|VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP|MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH|FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN) ?((?:\d{1,4}(?: B\.C\.)?|\d\d\d\d\/\d\d)?)$/', $date, $match)) {
117a25f0a04SGreg Roach            $d = $match[1];
118a25f0a04SGreg Roach            $m = $match[2];
119a25f0a04SGreg Roach            $y = $match[3];
120471edd97SGreg Roach        } elseif (preg_match('/^(\d{1,4}(?: B\.C\.)?|\d\d\d\d\/\d\d)$/', $date, $match)) {
121471edd97SGreg Roach            // A date with just a year
122a25f0a04SGreg Roach            $d = '';
123a25f0a04SGreg Roach            $m = '';
124a25f0a04SGreg Roach            $y = $match[1];
125a25f0a04SGreg Roach        } else {
126a25f0a04SGreg Roach            // An invalid date - do the best we can.
127a25f0a04SGreg Roach            $d = '';
128a25f0a04SGreg Roach            $m = '';
129a25f0a04SGreg Roach            $y = '';
130a25f0a04SGreg Roach            // Look for a 3/4 digit year anywhere in the date
131a25f0a04SGreg Roach            if (preg_match('/\b(\d{3,4})\b/', $date, $match)) {
132a25f0a04SGreg Roach                $y = $match[1];
133a25f0a04SGreg Roach            }
134a25f0a04SGreg Roach            // Look for a month anywhere in the date
135a25f0a04SGreg Roach            if (preg_match('/(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|TSH|CSH|KSL|TVT|SHV|ADR|ADS|NSN|IYR|SVN|TMZ|AAV|ELL|VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP|MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH|FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN)/', $date, $match)) {
136a25f0a04SGreg Roach                $m = $match[1];
137a25f0a04SGreg Roach                // Look for a day number anywhere in the date
138a25f0a04SGreg Roach                if (preg_match('/\b(\d\d?)\b/', $date, $match)) {
139a25f0a04SGreg Roach                    $d = $match[1];
140a25f0a04SGreg Roach                }
141a25f0a04SGreg Roach            }
142a25f0a04SGreg Roach        }
143a25f0a04SGreg Roach
144a25f0a04SGreg Roach        // Unambiguous dates - override calendar escape
145a25f0a04SGreg Roach        if (preg_match('/^(TSH|CSH|KSL|TVT|SHV|ADR|ADS|NSN|IYR|SVN|TMZ|AAV|ELL)$/', $m)) {
1464a83f5d7SGreg Roach            $cal = JewishDate::ESCAPE;
147e364afe4SGreg Roach        } elseif (preg_match('/^(VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP)$/', $m)) {
1484a83f5d7SGreg Roach            $cal = FrenchDate::ESCAPE;
149e364afe4SGreg Roach        } elseif (preg_match('/^(MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH)$/', $m)) {
1504a83f5d7SGreg Roach            $cal = HijriDate::ESCAPE; // This is a WT extension
151e364afe4SGreg Roach        } elseif (preg_match('/^(FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN)$/', $m)) {
1524a83f5d7SGreg Roach            $cal = JalaliDate::ESCAPE; // This is a WT extension
153a25f0a04SGreg Roach        } elseif (preg_match('/^\d{1,4}( B\.C\.)|\d\d\d\d\/\d\d$/', $y)) {
1544a83f5d7SGreg Roach            $cal = JulianDate::ESCAPE;
155a25f0a04SGreg Roach        }
156a25f0a04SGreg Roach
157a25f0a04SGreg Roach        // Ambiguous dates - don't override calendar escape
158e364afe4SGreg Roach        if ($cal === '') {
159a25f0a04SGreg Roach            if (preg_match('/^(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)$/', $m)) {
1604a83f5d7SGreg Roach                $cal =  GregorianDate::ESCAPE;
161e364afe4SGreg Roach            } elseif (preg_match('/^[345]\d\d\d$/', $y)) {
162a25f0a04SGreg Roach                // Year 3000-5999
1634a83f5d7SGreg Roach                $cal = JewishDate::ESCAPE;
164a25f0a04SGreg Roach            } else {
1654a83f5d7SGreg Roach                $cal = GregorianDate::ESCAPE;
166a25f0a04SGreg Roach            }
167a25f0a04SGreg Roach        }
168a25f0a04SGreg Roach        // Now construct an object of the correct type
169a25f0a04SGreg Roach        switch ($cal) {
1704a83f5d7SGreg Roach            case GregorianDate::ESCAPE:
171c1010edaSGreg Roach                return new GregorianDate([
172c1010edaSGreg Roach                    $y,
173c1010edaSGreg Roach                    $m,
174c1010edaSGreg Roach                    $d,
175c1010edaSGreg Roach                ]);
1764a83f5d7SGreg Roach            case JulianDate::ESCAPE:
177c1010edaSGreg Roach                return new JulianDate([
178c1010edaSGreg Roach                    $y,
179c1010edaSGreg Roach                    $m,
180c1010edaSGreg Roach                    $d,
181c1010edaSGreg Roach                ]);
1824a83f5d7SGreg Roach            case JewishDate::ESCAPE:
183c1010edaSGreg Roach                return new JewishDate([
184c1010edaSGreg Roach                    $y,
185c1010edaSGreg Roach                    $m,
186c1010edaSGreg Roach                    $d,
187c1010edaSGreg Roach                ]);
1884a83f5d7SGreg Roach            case HijriDate::ESCAPE:
189c1010edaSGreg Roach                return new HijriDate([
190c1010edaSGreg Roach                    $y,
191c1010edaSGreg Roach                    $m,
192c1010edaSGreg Roach                    $d,
193c1010edaSGreg Roach                ]);
1944a83f5d7SGreg Roach            case FrenchDate::ESCAPE:
195c1010edaSGreg Roach                return new FrenchDate([
196c1010edaSGreg Roach                    $y,
197c1010edaSGreg Roach                    $m,
198c1010edaSGreg Roach                    $d,
199c1010edaSGreg Roach                ]);
2004a83f5d7SGreg Roach            case JalaliDate::ESCAPE:
201c1010edaSGreg Roach                return new JalaliDate([
202c1010edaSGreg Roach                    $y,
203c1010edaSGreg Roach                    $m,
204c1010edaSGreg Roach                    $d,
205c1010edaSGreg Roach                ]);
2064a83f5d7SGreg Roach            case RomanDate::ESCAPE:
207c1010edaSGreg Roach                return new RomanDate([
208c1010edaSGreg Roach                    $y,
209c1010edaSGreg Roach                    $m,
210c1010edaSGreg Roach                    $d,
211c1010edaSGreg Roach                ]);
212a25f0a04SGreg Roach            default:
213a80c4362SGreg Roach                throw new DomainException('Invalid calendar');
214a25f0a04SGreg Roach        }
215a25f0a04SGreg Roach    }
216a25f0a04SGreg Roach
2174e6225d5SGreg Roach    /**
2184e6225d5SGreg Roach     * A list of supported calendars and their names.
2194e6225d5SGreg Roach     *
2204e6225d5SGreg Roach     * @return string[]
2214e6225d5SGreg Roach     */
2228f53f488SRico Sonntag    public static function calendarNames(): array
223c1010edaSGreg Roach    {
22413abd6f3SGreg Roach        return [
225bbb76c12SGreg Roach            /* I18N: The gregorian calendar */
226bbb76c12SGreg Roach            'gregorian' => I18N::translate('Gregorian'),
227bbb76c12SGreg Roach            /* I18N: The julian calendar */
228bbb76c12SGreg Roach            'julian'    => I18N::translate('Julian'),
229bbb76c12SGreg Roach            /* I18N: The French calendar */
230bbb76c12SGreg Roach            'french'    => I18N::translate('French'),
231bbb76c12SGreg Roach            /* I18N: The Hebrew/Jewish calendar */
232bbb76c12SGreg Roach            'jewish'    => I18N::translate('Jewish'),
233bbb76c12SGreg Roach            /* I18N: The Arabic/Hijri calendar */
234bbb76c12SGreg Roach            'hijri'     => I18N::translate('Hijri'),
235bbb76c12SGreg Roach            /* I18N: The Persian/Jalali calendar */
236bbb76c12SGreg Roach            'jalali'    => I18N::translate('Jalali'),
23713abd6f3SGreg Roach        ];
2384e6225d5SGreg Roach    }
2394e6225d5SGreg Roach
240a25f0a04SGreg Roach    /**
241a25f0a04SGreg Roach     * Convert a date to the preferred format and calendar(s) display.
242a25f0a04SGreg Roach     *
243cbc1590aSGreg Roach     * @param bool|null   $url               Wrap the date in a link to calendar.php
244a25f0a04SGreg Roach     * @param string|null $date_format       Override the default date format
245cbc1590aSGreg Roach     * @param bool|null   $convert_calendars Convert the date into other calendars
246a25f0a04SGreg Roach     *
247a25f0a04SGreg Roach     * @return string
248a25f0a04SGreg Roach     */
249e364afe4SGreg Roach    public function display($url = false, $date_format = null, $convert_calendars = true): string
250c1010edaSGreg Roach    {
2513a5741c7SGreg Roach        // Do we need a new DateFormatterService class?
252cab242e7SGreg Roach        $tree = app(Tree::class);
2535d51864bSGreg Roach
25412dcbc32SGreg Roach        $CALENDAR_FORMAT = $tree->getPreference('CALENDAR_FORMAT');
255a25f0a04SGreg Roach
256a25f0a04SGreg Roach        if ($date_format === null) {
257dfeee0a8SGreg Roach            $date_format = I18N::dateFormat();
258a25f0a04SGreg Roach        }
259a25f0a04SGreg Roach
260a25f0a04SGreg Roach        if ($convert_calendars) {
261a25f0a04SGreg Roach            $calendar_format = explode('_and_', $CALENDAR_FORMAT);
262a25f0a04SGreg Roach        } else {
26313abd6f3SGreg Roach            $calendar_format = [];
264a25f0a04SGreg Roach        }
265a25f0a04SGreg Roach
266a25f0a04SGreg Roach        // Two dates with text before, between and after
267a25f0a04SGreg Roach        $q1 = $this->qual1;
268a25f0a04SGreg Roach        $d1 = $this->date1->format($date_format, $this->qual1);
269a25f0a04SGreg Roach        $q2 = $this->qual2;
2708f038c36SRico Sonntag        if ($this->date2 === null) {
271a25f0a04SGreg Roach            $d2 = '';
272a25f0a04SGreg Roach        } else {
273a25f0a04SGreg Roach            $d2 = $this->date2->format($date_format, $this->qual2);
274a25f0a04SGreg Roach        }
275a25f0a04SGreg Roach        // Con vert to other calendars, if requested
276a25f0a04SGreg Roach        $conv1 = '';
277a25f0a04SGreg Roach        $conv2 = '';
278a25f0a04SGreg Roach        foreach ($calendar_format as $cal_fmt) {
279e364afe4SGreg Roach            if ($cal_fmt !== 'none') {
280a25f0a04SGreg Roach                $d1conv = $this->date1->convertToCalendar($cal_fmt);
281a25f0a04SGreg Roach                if ($d1conv->inValidRange()) {
282a25f0a04SGreg Roach                    $d1tmp = $d1conv->format($date_format, $this->qual1);
283a25f0a04SGreg Roach                } else {
284a25f0a04SGreg Roach                    $d1tmp = '';
285a25f0a04SGreg Roach                }
2868f038c36SRico Sonntag                if ($this->date2 === null) {
287a25f0a04SGreg Roach                    $d2conv = null;
288a25f0a04SGreg Roach                    $d2tmp  = '';
289a25f0a04SGreg Roach                } else {
290a25f0a04SGreg Roach                    $d2conv = $this->date2->convertToCalendar($cal_fmt);
291a25f0a04SGreg Roach                    if ($d2conv->inValidRange()) {
292a25f0a04SGreg Roach                        $d2tmp = $d2conv->format($date_format, $this->qual2);
293a25f0a04SGreg Roach                    } else {
294a25f0a04SGreg Roach                        $d2tmp = '';
295a25f0a04SGreg Roach                    }
296a25f0a04SGreg Roach                }
297d4f10423SGreg Roach                // If the date is different from the unconverted date, add it to the date string.
2983cfaf201SGreg Roach                if ($d1 != $d1tmp && $d1tmp !== '') {
299a25f0a04SGreg Roach                    if ($url) {
3003cfaf201SGreg Roach                        if ($CALENDAR_FORMAT !== 'none') {
30149d5f1d7SGreg Roach                            $conv1 .= ' <span dir="' . I18N::direction() . '">(<a href="' . $d1conv->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d1tmp . '</a>)</span>';
302a25f0a04SGreg Roach                        } else {
30349d5f1d7SGreg Roach                            $conv1 .= ' <span dir="' . I18N::direction() . '"><br><a href="' . $d1conv->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d1tmp . '</a></span>';
304a25f0a04SGreg Roach                        }
305a25f0a04SGreg Roach                    } else {
306c0af647eSGreg Roach                        $conv1 .= ' <span dir="' . I18N::direction() . '">(' . $d1tmp . ')</span>';
307a25f0a04SGreg Roach                    }
308a25f0a04SGreg Roach                }
3098f038c36SRico Sonntag                if ($this->date2 !== null && $d2 != $d2tmp && $d1tmp != '') {
310a25f0a04SGreg Roach                    if ($url) {
31149d5f1d7SGreg Roach                        $conv2 .= ' <span dir="' . I18N::direction() . '">(<a href="' . $d2conv->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d2tmp . '</a>)</span>';
312a25f0a04SGreg Roach                    } else {
313c0af647eSGreg Roach                        $conv2 .= ' <span dir="' . I18N::direction() . '">(' . $d2tmp . ')</span>';
314a25f0a04SGreg Roach                    }
315a25f0a04SGreg Roach                }
316a25f0a04SGreg Roach            }
317a25f0a04SGreg Roach        }
318a25f0a04SGreg Roach
319a25f0a04SGreg Roach        // Add URLs, if requested
320a25f0a04SGreg Roach        if ($url) {
32149d5f1d7SGreg Roach            $d1 = '<a href="' . $this->date1->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d1 . '</a>';
3224a83f5d7SGreg Roach            if ($this->date2 instanceof AbstractCalendarDate) {
32349d5f1d7SGreg Roach                $d2 = '<a href="' . $this->date2->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d2 . '</a>';
324a25f0a04SGreg Roach            }
325a25f0a04SGreg Roach        }
326a25f0a04SGreg Roach
327a25f0a04SGreg Roach        // Localise the date
328a25f0a04SGreg Roach        switch ($q1 . $q2) {
329a25f0a04SGreg Roach            case '':
330a25f0a04SGreg Roach                $tmp = $d1 . $conv1;
331a25f0a04SGreg Roach                break;
332a25f0a04SGreg Roach            case 'ABT':
333bbb76c12SGreg Roach                /* I18N: Gedcom ABT dates */
334bbb76c12SGreg Roach                $tmp = I18N::translate('about %s', $d1 . $conv1);
335a25f0a04SGreg Roach                break;
336a25f0a04SGreg Roach            case 'CAL':
337bbb76c12SGreg Roach                /* I18N: Gedcom CAL dates */
338bbb76c12SGreg Roach                $tmp = I18N::translate('calculated %s', $d1 . $conv1);
339a25f0a04SGreg Roach                break;
340a25f0a04SGreg Roach            case 'EST':
341bbb76c12SGreg Roach                /* I18N: Gedcom EST dates */
342bbb76c12SGreg Roach                $tmp = I18N::translate('estimated %s', $d1 . $conv1);
343a25f0a04SGreg Roach                break;
344a25f0a04SGreg Roach            case 'INT':
345bbb76c12SGreg Roach                /* I18N: Gedcom INT dates */
346bbb76c12SGreg Roach                $tmp = I18N::translate('interpreted %s (%s)', $d1 . $conv1, e($this->text));
347a25f0a04SGreg Roach                break;
348a25f0a04SGreg Roach            case 'BEF':
349bbb76c12SGreg Roach                /* I18N: Gedcom BEF dates */
350bbb76c12SGreg Roach                $tmp = I18N::translate('before %s', $d1 . $conv1);
351a25f0a04SGreg Roach                break;
352a25f0a04SGreg Roach            case 'AFT':
353bbb76c12SGreg Roach                /* I18N: Gedcom AFT dates */
354bbb76c12SGreg Roach                $tmp = I18N::translate('after %s', $d1 . $conv1);
355a25f0a04SGreg Roach                break;
356a25f0a04SGreg Roach            case 'FROM':
357bbb76c12SGreg Roach                /* I18N: Gedcom FROM dates */
358bbb76c12SGreg Roach                $tmp = I18N::translate('from %s', $d1 . $conv1);
359a25f0a04SGreg Roach                break;
360a25f0a04SGreg Roach            case 'TO':
361bbb76c12SGreg Roach                /* I18N: Gedcom TO dates */
362bbb76c12SGreg Roach                $tmp = I18N::translate('to %s', $d1 . $conv1);
363a25f0a04SGreg Roach                break;
364a25f0a04SGreg Roach            case 'BETAND':
365bbb76c12SGreg Roach                /* I18N: Gedcom BET-AND dates */
366bbb76c12SGreg Roach                $tmp = I18N::translate('between %s and %s', $d1 . $conv1, $d2 . $conv2);
367a25f0a04SGreg Roach                break;
368a25f0a04SGreg Roach            case 'FROMTO':
369bbb76c12SGreg Roach                /* I18N: Gedcom FROM-TO dates */
370bbb76c12SGreg Roach                $tmp = I18N::translate('from %s to %s', $d1 . $conv1, $d2 . $conv2);
371a25f0a04SGreg Roach                break;
372a25f0a04SGreg Roach            default:
373a25f0a04SGreg Roach                $tmp = I18N::translate('Invalid date');
374bbb76c12SGreg Roach                break;
375a25f0a04SGreg Roach        }
376a25f0a04SGreg Roach
3775fec6c0aSGreg Roach        if (strip_tags($tmp) === '') {
3785fec6c0aSGreg Roach            return '';
379a25f0a04SGreg Roach        }
380b2ce94c6SRico Sonntag
381b2ce94c6SRico Sonntag        return '<span class="date">' . $tmp . '</span>';
382a25f0a04SGreg Roach    }
383a25f0a04SGreg Roach
384a25f0a04SGreg Roach    /**
385a25f0a04SGreg Roach     * Get the earliest calendar date from this GEDCOM date.
386a25f0a04SGreg Roach     *
387a25f0a04SGreg Roach     * In the date “FROM 1900 TO 1910”, this would be 1900.
388a25f0a04SGreg Roach     *
3894a83f5d7SGreg Roach     * @return AbstractCalendarDate
390a25f0a04SGreg Roach     */
3914a83f5d7SGreg Roach    public function minimumDate(): AbstractCalendarDate
392c1010edaSGreg Roach    {
393a25f0a04SGreg Roach        return $this->date1;
394a25f0a04SGreg Roach    }
395a25f0a04SGreg Roach
396a25f0a04SGreg Roach    /**
397a25f0a04SGreg Roach     * Get the latest calendar date from this GEDCOM date.
398a25f0a04SGreg Roach     *
399a25f0a04SGreg Roach     * In the date “FROM 1900 TO 1910”, this would be 1910.
400a25f0a04SGreg Roach     *
4014a83f5d7SGreg Roach     * @return AbstractCalendarDate
402a25f0a04SGreg Roach     */
403e364afe4SGreg Roach    public function maximumDate(): AbstractCalendarDate
404c1010edaSGreg Roach    {
405af2489e5SGreg Roach        return $this->date2 ?? $this->date1;
406a25f0a04SGreg Roach    }
407a25f0a04SGreg Roach
408a25f0a04SGreg Roach    /**
409a25f0a04SGreg Roach     * Get the earliest Julian day number from this GEDCOM date.
410a25f0a04SGreg Roach     *
411cbc1590aSGreg Roach     * @return int
412a25f0a04SGreg Roach     */
4138f53f488SRico Sonntag    public function minimumJulianDay(): int
414c1010edaSGreg Roach    {
415af2489e5SGreg Roach        return $this->minimumDate()->minimumJulianDay();
416a25f0a04SGreg Roach    }
417a25f0a04SGreg Roach
418a25f0a04SGreg Roach    /**
419a25f0a04SGreg Roach     * Get the latest Julian day number from this GEDCOM date.
420a25f0a04SGreg Roach     *
421cbc1590aSGreg Roach     * @return int
422a25f0a04SGreg Roach     */
4238f53f488SRico Sonntag    public function maximumJulianDay(): int
424c1010edaSGreg Roach    {
425af2489e5SGreg Roach        return $this->maximumDate()->maximumJulianDay();
426a25f0a04SGreg Roach    }
427a25f0a04SGreg Roach
428a25f0a04SGreg Roach    /**
429a25f0a04SGreg Roach     * Get the middle Julian day number from the GEDCOM date.
430a25f0a04SGreg Roach     *
431f5b60decSGreg Roach     * For a month-only date, this would be somewhere around the 16th day.
432a25f0a04SGreg Roach     * For a year-only date, this would be somewhere around 1st July.
433a25f0a04SGreg Roach     *
434cbc1590aSGreg Roach     * @return int
435a25f0a04SGreg Roach     */
4368f53f488SRico Sonntag    public function julianDay(): int
437c1010edaSGreg Roach    {
438cdaafeeeSGreg Roach        return intdiv($this->minimumJulianDay() + $this->maximumJulianDay(), 2);
439a25f0a04SGreg Roach    }
440a25f0a04SGreg Roach
441a25f0a04SGreg Roach    /**
442a25f0a04SGreg Roach     * Offset this date by N years, and round to the whole year.
443a25f0a04SGreg Roach     *
444a25f0a04SGreg Roach     * This is typically used to create an estimated death date,
445a25f0a04SGreg Roach     * which is before a certain number of years after the birth date.
446a25f0a04SGreg Roach     *
447cbc1590aSGreg Roach     * @param int    $years     a number of years, positive or negative
448cbc1590aSGreg Roach     * @param string $qualifier typically “BEF” or “AFT”
449a25f0a04SGreg Roach     *
450a25f0a04SGreg Roach     * @return Date
451a25f0a04SGreg Roach     */
4528f53f488SRico Sonntag    public function addYears(int $years, string $qualifier = ''): Date
453c1010edaSGreg Roach    {
454a25f0a04SGreg Roach        $tmp               = clone $this;
4554a83f5d7SGreg Roach        $tmp->date1->year  += $years;
4564a83f5d7SGreg Roach        $tmp->date1->month = 0;
4574a83f5d7SGreg Roach        $tmp->date1->day   = 0;
458a25f0a04SGreg Roach        $tmp->date1->setJdFromYmd();
459a25f0a04SGreg Roach        $tmp->qual1 = $qualifier;
460a25f0a04SGreg Roach        $tmp->qual2 = '';
461a25f0a04SGreg Roach        $tmp->date2 = null;
462a25f0a04SGreg Roach
463a25f0a04SGreg Roach        return $tmp;
464a25f0a04SGreg Roach    }
465a25f0a04SGreg Roach
466a25f0a04SGreg Roach    /**
4678fe32d0dSGreg Roach     * Calculate the the age of a person (n years), on a given date.
468a25f0a04SGreg Roach     *
469a25f0a04SGreg Roach     * @param Date $d1
470a25f0a04SGreg Roach     * @param Date $d2
471cbc1590aSGreg Roach     *
4728fe32d0dSGreg Roach     * @return int
473a25f0a04SGreg Roach     */
4748fe32d0dSGreg Roach    public static function getAgeYears(Date $d1, Date $d2): int
475c1010edaSGreg Roach    {
4768af3e5c1SGreg Roach        if (self::compare($d1, $d2) === 0) {
4778fe32d0dSGreg Roach            // Overlapping dates
4788fe32d0dSGreg Roach            $jd = $d1->minimumJulianDay();
4798fe32d0dSGreg Roach        } else {
4808fe32d0dSGreg Roach            // Non-overlapping dates
4818fe32d0dSGreg Roach            $jd = $d2->minimumJulianDay();
4828fe32d0dSGreg Roach        }
4838fe32d0dSGreg Roach
4848fe32d0dSGreg Roach        if ($jd && $d1->minimumJulianDay() && $d1->minimumJulianDay() <= $jd) {
4858fe32d0dSGreg Roach            return $d1->minimumDate()->getAge($jd);
4868fe32d0dSGreg Roach        }
487e364afe4SGreg Roach
488e364afe4SGreg Roach        return -1;
4898fe32d0dSGreg Roach    }
4908fe32d0dSGreg Roach
4918fe32d0dSGreg Roach    /**
4928fe32d0dSGreg Roach     * Calculate the the age of a person (n days), on a given date.
4938fe32d0dSGreg Roach     *
4948fe32d0dSGreg Roach     * @param Date $d1
4958fe32d0dSGreg Roach     * @param Date $d2
4968fe32d0dSGreg Roach     *
4978fe32d0dSGreg Roach     * @return int
4988fe32d0dSGreg Roach     */
4998fe32d0dSGreg Roach    public static function getAgeDays(Date $d1, Date $d2): int
5008fe32d0dSGreg Roach    {
5011631e7f7SGreg Roach        if ($d2->maximumJulianDay() >= $d1->minimumJulianDay() && $d2->minimumJulianDay() <= $d1->maximumJulianDay()) {
5028fe32d0dSGreg Roach            // Overlapping dates
5038fe32d0dSGreg Roach            $jd = $d1->minimumJulianDay();
5048fe32d0dSGreg Roach        } else {
5058fe32d0dSGreg Roach            // Non-overlapping dates
5068fe32d0dSGreg Roach            $jd = $d2->minimumJulianDay();
5078fe32d0dSGreg Roach        }
5088fe32d0dSGreg Roach
5098fe32d0dSGreg Roach        // Days - integer only (for sorting, rather than for display)
5108fe32d0dSGreg Roach        if ($jd && $d1->minimumJulianDay()) {
5118fe32d0dSGreg Roach            return $jd - $d1->minimumJulianDay();
5128fe32d0dSGreg Roach        }
513e364afe4SGreg Roach
514e364afe4SGreg Roach        return -1;
5158fe32d0dSGreg Roach    }
5168fe32d0dSGreg Roach
5178fe32d0dSGreg Roach    /**
5188fe32d0dSGreg Roach     * Calculate the the age of a person, on a date.
5198fe32d0dSGreg Roach     *
5208fe32d0dSGreg Roach     * @param Date      $d1
5218fe32d0dSGreg Roach     * @param Date|null $d2
5228fe32d0dSGreg Roach     *
5238fe32d0dSGreg Roach     * @return string
5248fe32d0dSGreg Roach     */
5258fe32d0dSGreg Roach    public static function getAge(Date $d1, Date $d2 = null): string
5268fe32d0dSGreg Roach    {
527e364afe4SGreg Roach        if ($d2 instanceof self) {
5281631e7f7SGreg Roach            if ($d2->maximumJulianDay() >= $d1->minimumJulianDay() && $d2->minimumJulianDay() <= $d1->maximumJulianDay()) {
529a25f0a04SGreg Roach                // Overlapping dates
530f5b60decSGreg Roach                $jd = $d1->minimumJulianDay();
531a25f0a04SGreg Roach            } else {
532a25f0a04SGreg Roach                // Non-overlapping dates
533f5b60decSGreg Roach                $jd = $d2->minimumJulianDay();
534a25f0a04SGreg Roach            }
535a25f0a04SGreg Roach        } else {
536a25f0a04SGreg Roach            // If second date not specified, use today’s date
5374459dc9aSGreg Roach            $jd = Carbon::now()->julianDay();
538a25f0a04SGreg Roach        }
539a25f0a04SGreg Roach
5405fec6c0aSGreg Roach        // Just years, in local digits, with warning for negative/
541f5b60decSGreg Roach        if ($jd && $d1->minimumJulianDay()) {
542f5b60decSGreg Roach            if ($d1->minimumJulianDay() > $jd) {
543e39fd5c6SGreg Roach                return view('icons/warning');
544e364afe4SGreg Roach            }
545e364afe4SGreg Roach
5468fe32d0dSGreg Roach            $years = $d1->minimumDate()->getAge($jd);
54755664801SGreg Roach
54855664801SGreg Roach            return I18N::number($years);
549a25f0a04SGreg Roach        }
550e364afe4SGreg Roach
55166b90994SGreg Roach        return '';
552a25f0a04SGreg Roach    }
553a25f0a04SGreg Roach
554a25f0a04SGreg Roach    /**
555a25f0a04SGreg Roach     * Calculate the years/months/days between two events
556a25f0a04SGreg Roach     * Return a gedcom style age string: "1y 2m 3d" (for fact details)
557a25f0a04SGreg Roach     *
558a25f0a04SGreg Roach     * @param Date      $d1
559a25f0a04SGreg Roach     * @param Date|null $d2
560a25f0a04SGreg Roach     *
561a25f0a04SGreg Roach     * @return string
562a25f0a04SGreg Roach     */
563e364afe4SGreg Roach    public static function getAgeGedcom(Date $d1, Date $d2 = null): string
564c1010edaSGreg Roach    {
5658f038c36SRico Sonntag        if ($d2 === null) {
5664459dc9aSGreg Roach            return $d1->date1->getAgeFull(Carbon::now()->julianDay());
5674e8e93daSGreg Roach        }
5684e8e93daSGreg Roach
5694e8e93daSGreg Roach        if (self::compare($d1, $d2) !== 0) {
5708fe32d0dSGreg Roach            return $d1->date1->getAgeFull($d2->minimumJulianDay());
5714e8e93daSGreg Roach        }
5724e8e93daSGreg Roach
5734e8e93daSGreg Roach        if ($d1->minimumJulianDay() == $d2->minimumJulianDay()) {
574a25f0a04SGreg Roach            return '0d';
5754e8e93daSGreg Roach        }
5764e8e93daSGreg Roach
577a25f0a04SGreg Roach        return '';
578a25f0a04SGreg Roach    }
579a25f0a04SGreg Roach
580a25f0a04SGreg Roach    /**
581a25f0a04SGreg Roach     * Compare two dates, so they can be sorted.
582a25f0a04SGreg Roach     *
583a25f0a04SGreg Roach     * return <0 if $a<$b
584a25f0a04SGreg Roach     * return >0 if $b>$a
585a25f0a04SGreg Roach     * return  0 if dates same/overlap
586a25f0a04SGreg Roach     * BEF/AFT sort as the day before/after
587a25f0a04SGreg Roach     *
588a25f0a04SGreg Roach     * @param Date $a
589a25f0a04SGreg Roach     * @param Date $b
590a25f0a04SGreg Roach     *
591cbc1590aSGreg Roach     * @return int
592a25f0a04SGreg Roach     */
593e364afe4SGreg Roach    public static function compare(Date $a, Date $b): int
594c1010edaSGreg Roach    {
595a25f0a04SGreg Roach        // Get min/max JD for each date.
596a25f0a04SGreg Roach        switch ($a->qual1) {
597a25f0a04SGreg Roach            case 'BEF':
598f5b60decSGreg Roach                $amin = $a->minimumJulianDay() - 1;
599a25f0a04SGreg Roach                $amax = $amin;
600a25f0a04SGreg Roach                break;
601a25f0a04SGreg Roach            case 'AFT':
602f5b60decSGreg Roach                $amax = $a->maximumJulianDay() + 1;
603a25f0a04SGreg Roach                $amin = $amax;
604a25f0a04SGreg Roach                break;
605a25f0a04SGreg Roach            default:
606f5b60decSGreg Roach                $amin = $a->minimumJulianDay();
607f5b60decSGreg Roach                $amax = $a->maximumJulianDay();
608a25f0a04SGreg Roach                break;
609a25f0a04SGreg Roach        }
610a25f0a04SGreg Roach        switch ($b->qual1) {
611a25f0a04SGreg Roach            case 'BEF':
612f5b60decSGreg Roach                $bmin = $b->minimumJulianDay() - 1;
613a25f0a04SGreg Roach                $bmax = $bmin;
614a25f0a04SGreg Roach                break;
615a25f0a04SGreg Roach            case 'AFT':
616f5b60decSGreg Roach                $bmax = $b->maximumJulianDay() + 1;
617a25f0a04SGreg Roach                $bmin = $bmax;
618a25f0a04SGreg Roach                break;
619a25f0a04SGreg Roach            default:
620f5b60decSGreg Roach                $bmin = $b->minimumJulianDay();
621f5b60decSGreg Roach                $bmax = $b->maximumJulianDay();
622a25f0a04SGreg Roach                break;
623a25f0a04SGreg Roach        }
624a25f0a04SGreg Roach        if ($amax < $bmin) {
625a25f0a04SGreg Roach            return -1;
626a25f0a04SGreg Roach        }
627b2ce94c6SRico Sonntag
628b2ce94c6SRico Sonntag        if ($amin > $bmax && $bmax > 0) {
629b2ce94c6SRico Sonntag            return 1;
630b2ce94c6SRico Sonntag        }
631b2ce94c6SRico Sonntag
632b2ce94c6SRico Sonntag        if ($amin < $bmin && $amax <= $bmax) {
633b2ce94c6SRico Sonntag            return -1;
634b2ce94c6SRico Sonntag        }
635b2ce94c6SRico Sonntag
636b2ce94c6SRico Sonntag        if ($amin > $bmin && $amax >= $bmax && $bmax > 0) {
637b2ce94c6SRico Sonntag            return 1;
638b2ce94c6SRico Sonntag        }
639b2ce94c6SRico Sonntag
640b2ce94c6SRico Sonntag        return 0;
641a25f0a04SGreg Roach    }
642a25f0a04SGreg Roach
643a25f0a04SGreg Roach    /**
644a25f0a04SGreg Roach     * Check whether a gedcom date contains usable calendar date(s).
645a25f0a04SGreg Roach     *
646a25f0a04SGreg Roach     * An incomplete date such as "12 AUG" would be invalid, as
647a25f0a04SGreg Roach     * we cannot sort it.
648a25f0a04SGreg Roach     *
649cbc1590aSGreg Roach     * @return bool
650a25f0a04SGreg Roach     */
6518f53f488SRico Sonntag    public function isOK(): bool
652c1010edaSGreg Roach    {
653f5b60decSGreg Roach        return $this->minimumJulianDay() && $this->maximumJulianDay();
654a25f0a04SGreg Roach    }
655a25f0a04SGreg Roach
656a25f0a04SGreg Roach    /**
657a25f0a04SGreg Roach     * Calculate the gregorian year for a date. This should NOT be used internally
658a25f0a04SGreg Roach     * within WT - we should keep the code "calendar neutral" to allow support for
659a25f0a04SGreg Roach     * jewish/arabic users. This is only for interfacing with external entities,
660a25f0a04SGreg Roach     * such as the ancestry.com search interface or the dated fact icons.
661a25f0a04SGreg Roach     *
662cbc1590aSGreg Roach     * @return int
663a25f0a04SGreg Roach     */
664e364afe4SGreg Roach    public function gregorianYear(): int
665c1010edaSGreg Roach    {
666a25f0a04SGreg Roach        if ($this->isOK()) {
66759f2f229SGreg Roach            $gregorian_calendar = new GregorianCalendar();
66865e02381SGreg Roach            [$year] = $gregorian_calendar->jdToYmd($this->julianDay());
669a25f0a04SGreg Roach
670a25f0a04SGreg Roach            return $year;
671a25f0a04SGreg Roach        }
672b2ce94c6SRico Sonntag
673b2ce94c6SRico Sonntag        return 0;
674a25f0a04SGreg Roach    }
675a25f0a04SGreg Roach}
676