xref: /webtrees/app/Date.php (revision 7560a51f7cf0285b29434543df1552751f1c6b32)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
22a80c4362SGreg Roachuse DomainException;
23a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
244a83f5d7SGreg Roachuse Fisharebest\Webtrees\Date\AbstractCalendarDate;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\FrenchDate;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\HijriDate;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JalaliDate;
290e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate;
300e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JulianDate;
310e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\RomanDate;
32a25f0a04SGreg Roach
333cccdb42SGreg Roachuse function app;
343cccdb42SGreg Roach
35a25f0a04SGreg Roach/**
3676692c8bSGreg Roach * A representation of GEDCOM dates and date ranges.
3776692c8bSGreg Roach *
3876692c8bSGreg Roach * Since different calendars start their days at different times, (civil
39a25f0a04SGreg Roach * midnight, solar midnight, sunset, sunrise, etc.), we convert on the basis of
40a25f0a04SGreg Roach * midday.
41a25f0a04SGreg Roach *
4276692c8bSGreg Roach * We assume that years start on the first day of the first month. Where
43a25f0a04SGreg Roach * this is not the case (e.g. England prior to 1752), we need to use modified
44a25f0a04SGreg Roach * years or the OS/NS notation "4 FEB 1750/51".
45a25f0a04SGreg Roach */
46c1010edaSGreg Roachclass Date
47c1010edaSGreg Roach{
48a25f0a04SGreg Roach    /** @var string Optional qualifier, such as BEF, FROM, ABT */
49fe11e66dSGreg Roach    public $qual1 = '';
50a25f0a04SGreg Roach
514a83f5d7SGreg Roach    /** @var AbstractCalendarDate  The first (or only) date */
52f5b60decSGreg Roach    private $date1;
53a25f0a04SGreg Roach
54a25f0a04SGreg Roach    /** @var string  Optional qualifier, such as TO, AND */
55fe11e66dSGreg Roach    public $qual2 = '';
56a25f0a04SGreg Roach
574a83f5d7SGreg Roach    /** @var AbstractCalendarDate|null Optional second date */
58d0810bd3SGreg Roach    private $date2;
59a25f0a04SGreg Roach
6048599914SGreg Roach    /** @var string Optional text, as included with an INTerpreted date */
617e96c925SGreg Roach    private $text = '';
62a25f0a04SGreg Roach
63a25f0a04SGreg Roach    /**
64a25f0a04SGreg Roach     * Create a date, from GEDCOM data.
65a25f0a04SGreg Roach     *
66a25f0a04SGreg Roach     * @param string $date A date in GEDCOM format
67a25f0a04SGreg Roach     */
68cfe766afSGreg Roach    public function __construct(string $date)
69c1010edaSGreg Roach    {
70a25f0a04SGreg Roach        // Extract any explanatory text
71a25f0a04SGreg Roach        if (preg_match('/^(.*) ?[(](.*)[)]/', $date, $match)) {
72a25f0a04SGreg Roach            $date       = $match[1];
73a25f0a04SGreg Roach            $this->text = $match[2];
74a25f0a04SGreg Roach        }
75a25f0a04SGreg Roach        if (preg_match('/^(FROM|BET) (.+) (AND|TO) (.+)/', $date, $match)) {
76a25f0a04SGreg Roach            $this->qual1 = $match[1];
77a25f0a04SGreg Roach            $this->date1 = $this->parseDate($match[2]);
78a25f0a04SGreg Roach            $this->qual2 = $match[3];
79a25f0a04SGreg Roach            $this->date2 = $this->parseDate($match[4]);
803506b2e5SGreg Roach        } elseif (preg_match('/^(TO|FROM|BEF|AFT|CAL|EST|INT|ABT) (.+)/', $date, $match)) {
81a25f0a04SGreg Roach            $this->qual1 = $match[1];
82a25f0a04SGreg Roach            $this->date1 = $this->parseDate($match[2]);
83a25f0a04SGreg Roach        } else {
84a25f0a04SGreg Roach            $this->date1 = $this->parseDate($date);
85a25f0a04SGreg Roach        }
86a25f0a04SGreg Roach    }
87a25f0a04SGreg Roach
88a25f0a04SGreg Roach    /**
89a25f0a04SGreg Roach     * When we copy a date object, we need to create copies of
90a25f0a04SGreg Roach     * its child objects.
91a25f0a04SGreg Roach     */
92c1010edaSGreg Roach    public function __clone()
93c1010edaSGreg Roach    {
94a25f0a04SGreg Roach        $this->date1 = clone $this->date1;
957e96c925SGreg Roach        if ($this->date2 !== null) {
96a25f0a04SGreg Roach            $this->date2 = clone $this->date2;
97a25f0a04SGreg Roach        }
98a25f0a04SGreg Roach    }
99a25f0a04SGreg Roach
100a25f0a04SGreg Roach    /**
101a25f0a04SGreg Roach     * Convert a calendar date, such as "12 JUN 1943" into calendar date object.
102a25f0a04SGreg Roach     * A GEDCOM date range may have two calendar dates.
103a25f0a04SGreg Roach     *
104a25f0a04SGreg Roach     * @param string $date
105a25f0a04SGreg Roach     *
1064a83f5d7SGreg Roach     * @return AbstractCalendarDate
10724f2a3afSGreg Roach     * @throws DomainException
108a25f0a04SGreg Roach     */
10924f2a3afSGreg Roach    private function parseDate(string $date): AbstractCalendarDate
110c1010edaSGreg Roach    {
111a25f0a04SGreg Roach        // Valid calendar escape specified? - use it
112bee1f90bSGreg Roach        if (preg_match('/^(@#D(?:GREGORIAN|JULIAN|HEBREW|HIJRI|JALALI|FRENCH R|ROMAN)+@) ?(.*)/', $date, $match)) {
113a25f0a04SGreg Roach            $cal  = $match[1];
114a25f0a04SGreg Roach            $date = $match[2];
115a25f0a04SGreg Roach        } else {
116a25f0a04SGreg Roach            $cal = '';
117a25f0a04SGreg Roach        }
118a25f0a04SGreg Roach        // A date with a month: DM, M, MY or DMY
119a25f0a04SGreg 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)) {
120a25f0a04SGreg Roach            $d = $match[1];
121a25f0a04SGreg Roach            $m = $match[2];
122a25f0a04SGreg Roach            $y = $match[3];
123471edd97SGreg Roach        } elseif (preg_match('/^(\d{1,4}(?: B\.C\.)?|\d\d\d\d\/\d\d)$/', $date, $match)) {
124471edd97SGreg Roach            // A date with just a year
125a25f0a04SGreg Roach            $d = '';
126a25f0a04SGreg Roach            $m = '';
127a25f0a04SGreg Roach            $y = $match[1];
128a25f0a04SGreg Roach        } else {
129a25f0a04SGreg Roach            // An invalid date - do the best we can.
130a25f0a04SGreg Roach            $d = '';
131a25f0a04SGreg Roach            $m = '';
132a25f0a04SGreg Roach            $y = '';
133a25f0a04SGreg Roach            // Look for a 3/4 digit year anywhere in the date
134a25f0a04SGreg Roach            if (preg_match('/\b(\d{3,4})\b/', $date, $match)) {
135a25f0a04SGreg Roach                $y = $match[1];
136a25f0a04SGreg Roach            }
137a25f0a04SGreg Roach            // Look for a month anywhere in the date
138a25f0a04SGreg 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)) {
139a25f0a04SGreg Roach                $m = $match[1];
140a25f0a04SGreg Roach                // Look for a day number anywhere in the date
141a25f0a04SGreg Roach                if (preg_match('/\b(\d\d?)\b/', $date, $match)) {
142a25f0a04SGreg Roach                    $d = $match[1];
143a25f0a04SGreg Roach                }
144a25f0a04SGreg Roach            }
145a25f0a04SGreg Roach        }
146a25f0a04SGreg Roach
147a25f0a04SGreg Roach        // Unambiguous dates - override calendar escape
148a25f0a04SGreg Roach        if (preg_match('/^(TSH|CSH|KSL|TVT|SHV|ADR|ADS|NSN|IYR|SVN|TMZ|AAV|ELL)$/', $m)) {
1494a83f5d7SGreg Roach            $cal = JewishDate::ESCAPE;
150e364afe4SGreg Roach        } elseif (preg_match('/^(VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP)$/', $m)) {
1514a83f5d7SGreg Roach            $cal = FrenchDate::ESCAPE;
152e364afe4SGreg Roach        } elseif (preg_match('/^(MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH)$/', $m)) {
1534a83f5d7SGreg Roach            $cal = HijriDate::ESCAPE; // This is a WT extension
154e364afe4SGreg Roach        } elseif (preg_match('/^(FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN)$/', $m)) {
1554a83f5d7SGreg Roach            $cal = JalaliDate::ESCAPE; // This is a WT extension
156a25f0a04SGreg Roach        } elseif (preg_match('/^\d{1,4}( B\.C\.)|\d\d\d\d\/\d\d$/', $y)) {
1574a83f5d7SGreg Roach            $cal = JulianDate::ESCAPE;
158a25f0a04SGreg Roach        }
159a25f0a04SGreg Roach
160a25f0a04SGreg Roach        // Ambiguous dates - don't override calendar escape
161e364afe4SGreg Roach        if ($cal === '') {
162a25f0a04SGreg Roach            if (preg_match('/^(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)$/', $m)) {
1634a83f5d7SGreg Roach                $cal =  GregorianDate::ESCAPE;
164e364afe4SGreg Roach            } elseif (preg_match('/^[345]\d\d\d$/', $y)) {
165a25f0a04SGreg Roach                // Year 3000-5999
1664a83f5d7SGreg Roach                $cal = JewishDate::ESCAPE;
167a25f0a04SGreg Roach            } else {
1684a83f5d7SGreg Roach                $cal = GregorianDate::ESCAPE;
169a25f0a04SGreg Roach            }
170a25f0a04SGreg Roach        }
171a25f0a04SGreg Roach        // Now construct an object of the correct type
172a25f0a04SGreg Roach        switch ($cal) {
1734a83f5d7SGreg Roach            case GregorianDate::ESCAPE:
174c1010edaSGreg Roach                return new GregorianDate([
175c1010edaSGreg Roach                    $y,
176c1010edaSGreg Roach                    $m,
177c1010edaSGreg Roach                    $d,
178c1010edaSGreg Roach                ]);
1794a83f5d7SGreg Roach            case JulianDate::ESCAPE:
180c1010edaSGreg Roach                return new JulianDate([
181c1010edaSGreg Roach                    $y,
182c1010edaSGreg Roach                    $m,
183c1010edaSGreg Roach                    $d,
184c1010edaSGreg Roach                ]);
1854a83f5d7SGreg Roach            case JewishDate::ESCAPE:
186c1010edaSGreg Roach                return new JewishDate([
187c1010edaSGreg Roach                    $y,
188c1010edaSGreg Roach                    $m,
189c1010edaSGreg Roach                    $d,
190c1010edaSGreg Roach                ]);
1914a83f5d7SGreg Roach            case HijriDate::ESCAPE:
192c1010edaSGreg Roach                return new HijriDate([
193c1010edaSGreg Roach                    $y,
194c1010edaSGreg Roach                    $m,
195c1010edaSGreg Roach                    $d,
196c1010edaSGreg Roach                ]);
1974a83f5d7SGreg Roach            case FrenchDate::ESCAPE:
198c1010edaSGreg Roach                return new FrenchDate([
199c1010edaSGreg Roach                    $y,
200c1010edaSGreg Roach                    $m,
201c1010edaSGreg Roach                    $d,
202c1010edaSGreg Roach                ]);
2034a83f5d7SGreg Roach            case JalaliDate::ESCAPE:
204c1010edaSGreg Roach                return new JalaliDate([
205c1010edaSGreg Roach                    $y,
206c1010edaSGreg Roach                    $m,
207c1010edaSGreg Roach                    $d,
208c1010edaSGreg Roach                ]);
2094a83f5d7SGreg Roach            case RomanDate::ESCAPE:
210c1010edaSGreg Roach                return new RomanDate([
211c1010edaSGreg Roach                    $y,
212c1010edaSGreg Roach                    $m,
213c1010edaSGreg Roach                    $d,
214c1010edaSGreg Roach                ]);
215a25f0a04SGreg Roach            default:
216a80c4362SGreg Roach                throw new DomainException('Invalid calendar');
217a25f0a04SGreg Roach        }
218a25f0a04SGreg Roach    }
219a25f0a04SGreg Roach
2204e6225d5SGreg Roach    /**
2214e6225d5SGreg Roach     * A list of supported calendars and their names.
2224e6225d5SGreg Roach     *
22324f2a3afSGreg Roach     * @return array<string>
2244e6225d5SGreg Roach     */
2258f53f488SRico Sonntag    public static function calendarNames(): array
226c1010edaSGreg Roach    {
22713abd6f3SGreg Roach        return [
228bbb76c12SGreg Roach            /* I18N: The gregorian calendar */
229bbb76c12SGreg Roach            'gregorian' => I18N::translate('Gregorian'),
230bbb76c12SGreg Roach            /* I18N: The julian calendar */
231bbb76c12SGreg Roach            'julian'    => I18N::translate('Julian'),
232bbb76c12SGreg Roach            /* I18N: The French calendar */
233bbb76c12SGreg Roach            'french'    => I18N::translate('French'),
234bbb76c12SGreg Roach            /* I18N: The Hebrew/Jewish calendar */
235bbb76c12SGreg Roach            'jewish'    => I18N::translate('Jewish'),
236bbb76c12SGreg Roach            /* I18N: The Arabic/Hijri calendar */
237bbb76c12SGreg Roach            'hijri'     => I18N::translate('Hijri'),
238bbb76c12SGreg Roach            /* I18N: The Persian/Jalali calendar */
239bbb76c12SGreg Roach            'jalali'    => I18N::translate('Jalali'),
24013abd6f3SGreg Roach        ];
2414e6225d5SGreg Roach    }
2424e6225d5SGreg Roach
243a25f0a04SGreg Roach    /**
244a25f0a04SGreg Roach     * Convert a date to the preferred format and calendar(s) display.
245a25f0a04SGreg Roach     *
246cbc1590aSGreg Roach     * @param bool|null   $url               Wrap the date in a link to calendar.php
247a25f0a04SGreg Roach     * @param string|null $date_format       Override the default date format
248cbc1590aSGreg Roach     * @param bool|null   $convert_calendars Convert the date into other calendars
249a25f0a04SGreg Roach     *
250a25f0a04SGreg Roach     * @return string
251a25f0a04SGreg Roach     */
252e364afe4SGreg Roach    public function display($url = false, $date_format = null, $convert_calendars = true): string
253c1010edaSGreg Roach    {
2543a5741c7SGreg Roach        // Do we need a new DateFormatterService class?
2553cccdb42SGreg Roach        if (app()->has(Tree::class)) {
256cab242e7SGreg Roach            $tree            = app(Tree::class);
25712dcbc32SGreg Roach            $CALENDAR_FORMAT = $tree->getPreference('CALENDAR_FORMAT');
2583cccdb42SGreg Roach        } else {
2593cccdb42SGreg Roach            $tree            = null;
2603cccdb42SGreg Roach            $CALENDAR_FORMAT = '';
2613cccdb42SGreg Roach        }
262a25f0a04SGreg Roach
2637c6a929fSGreg Roach        $date_format = $date_format ?? I18N::dateFormat();
264a25f0a04SGreg Roach
265a25f0a04SGreg Roach        if ($convert_calendars) {
266a25f0a04SGreg Roach            $calendar_format = explode('_and_', $CALENDAR_FORMAT);
267a25f0a04SGreg Roach        } else {
26813abd6f3SGreg Roach            $calendar_format = [];
269a25f0a04SGreg Roach        }
270a25f0a04SGreg Roach
271a25f0a04SGreg Roach        // Two dates with text before, between and after
272a25f0a04SGreg Roach        $q1 = $this->qual1;
273a25f0a04SGreg Roach        $d1 = $this->date1->format($date_format, $this->qual1);
274a25f0a04SGreg Roach        $q2 = $this->qual2;
2758f038c36SRico Sonntag        if ($this->date2 === null) {
276a25f0a04SGreg Roach            $d2 = '';
277a25f0a04SGreg Roach        } else {
278a25f0a04SGreg Roach            $d2 = $this->date2->format($date_format, $this->qual2);
279a25f0a04SGreg Roach        }
280a25f0a04SGreg Roach        // Con vert to other calendars, if requested
281a25f0a04SGreg Roach        $conv1 = '';
282a25f0a04SGreg Roach        $conv2 = '';
283a25f0a04SGreg Roach        foreach ($calendar_format as $cal_fmt) {
284e364afe4SGreg Roach            if ($cal_fmt !== 'none') {
285a25f0a04SGreg Roach                $d1conv = $this->date1->convertToCalendar($cal_fmt);
286a25f0a04SGreg Roach                if ($d1conv->inValidRange()) {
287a25f0a04SGreg Roach                    $d1tmp = $d1conv->format($date_format, $this->qual1);
288a25f0a04SGreg Roach                } else {
289a25f0a04SGreg Roach                    $d1tmp = '';
290a25f0a04SGreg Roach                }
2918f038c36SRico Sonntag                if ($this->date2 === null) {
292a25f0a04SGreg Roach                    $d2conv = null;
293a25f0a04SGreg Roach                    $d2tmp  = '';
294a25f0a04SGreg Roach                } else {
295a25f0a04SGreg Roach                    $d2conv = $this->date2->convertToCalendar($cal_fmt);
296a25f0a04SGreg Roach                    if ($d2conv->inValidRange()) {
297a25f0a04SGreg Roach                        $d2tmp = $d2conv->format($date_format, $this->qual2);
298a25f0a04SGreg Roach                    } else {
299a25f0a04SGreg Roach                        $d2tmp = '';
300a25f0a04SGreg Roach                    }
301a25f0a04SGreg Roach                }
302d4f10423SGreg Roach                // If the date is different from the unconverted date, add it to the date string.
3033cfaf201SGreg Roach                if ($d1 != $d1tmp && $d1tmp !== '') {
304a25f0a04SGreg Roach                    if ($url) {
3053cfaf201SGreg Roach                        if ($CALENDAR_FORMAT !== 'none') {
306*7560a51fSGreg Roach                            $conv1 .= ' <span dir="' . I18N::direction() . '">(<a href="' . e($d1conv->calendarUrl($date_format, $tree)) . '" rel="nofollow">' . $d1tmp . '</a>)</span>';
307a25f0a04SGreg Roach                        } else {
308*7560a51fSGreg Roach                            $conv1 .= ' <span dir="' . I18N::direction() . '"><br><a href="' . e($d1conv->calendarUrl($date_format, $tree)) . '" rel="nofollow">' . $d1tmp . '</a></span>';
309a25f0a04SGreg Roach                        }
310a25f0a04SGreg Roach                    } else {
311c0af647eSGreg Roach                        $conv1 .= ' <span dir="' . I18N::direction() . '">(' . $d1tmp . ')</span>';
312a25f0a04SGreg Roach                    }
313a25f0a04SGreg Roach                }
3148f038c36SRico Sonntag                if ($this->date2 !== null && $d2 != $d2tmp && $d1tmp != '') {
315a25f0a04SGreg Roach                    if ($url) {
316*7560a51fSGreg Roach                        $conv2 .= ' <span dir="' . I18N::direction() . '">(<a href="' . e($d2conv->calendarUrl($date_format, $tree)) . '" rel="nofollow">' . $d2tmp . '</a>)</span>';
317a25f0a04SGreg Roach                    } else {
318c0af647eSGreg Roach                        $conv2 .= ' <span dir="' . I18N::direction() . '">(' . $d2tmp . ')</span>';
319a25f0a04SGreg Roach                    }
320a25f0a04SGreg Roach                }
321a25f0a04SGreg Roach            }
322a25f0a04SGreg Roach        }
323a25f0a04SGreg Roach
324a25f0a04SGreg Roach        // Add URLs, if requested
325a25f0a04SGreg Roach        if ($url) {
326*7560a51fSGreg Roach            $d1 = '<a href="' . e($this->date1->calendarUrl($date_format, $tree)) . '" rel="nofollow">' . $d1 . '</a>';
3274a83f5d7SGreg Roach            if ($this->date2 instanceof AbstractCalendarDate) {
328*7560a51fSGreg Roach                $d2 = '<a href="' . e($this->date2->calendarUrl($date_format, $tree)) . '" rel="nofollow">' . $d2 . '</a>';
329a25f0a04SGreg Roach            }
330a25f0a04SGreg Roach        }
331a25f0a04SGreg Roach
332a25f0a04SGreg Roach        // Localise the date
333a25f0a04SGreg Roach        switch ($q1 . $q2) {
334a25f0a04SGreg Roach            case '':
335a25f0a04SGreg Roach                $tmp = $d1 . $conv1;
336a25f0a04SGreg Roach                break;
337a25f0a04SGreg Roach            case 'ABT':
338bbb76c12SGreg Roach                /* I18N: Gedcom ABT dates */
339bbb76c12SGreg Roach                $tmp = I18N::translate('about %s', $d1 . $conv1);
340a25f0a04SGreg Roach                break;
341a25f0a04SGreg Roach            case 'CAL':
342bbb76c12SGreg Roach                /* I18N: Gedcom CAL dates */
343bbb76c12SGreg Roach                $tmp = I18N::translate('calculated %s', $d1 . $conv1);
344a25f0a04SGreg Roach                break;
345a25f0a04SGreg Roach            case 'EST':
346bbb76c12SGreg Roach                /* I18N: Gedcom EST dates */
347bbb76c12SGreg Roach                $tmp = I18N::translate('estimated %s', $d1 . $conv1);
348a25f0a04SGreg Roach                break;
349a25f0a04SGreg Roach            case 'INT':
350bbb76c12SGreg Roach                /* I18N: Gedcom INT dates */
351bbb76c12SGreg Roach                $tmp = I18N::translate('interpreted %s (%s)', $d1 . $conv1, e($this->text));
352a25f0a04SGreg Roach                break;
353a25f0a04SGreg Roach            case 'BEF':
354bbb76c12SGreg Roach                /* I18N: Gedcom BEF dates */
355bbb76c12SGreg Roach                $tmp = I18N::translate('before %s', $d1 . $conv1);
356a25f0a04SGreg Roach                break;
357a25f0a04SGreg Roach            case 'AFT':
358bbb76c12SGreg Roach                /* I18N: Gedcom AFT dates */
359bbb76c12SGreg Roach                $tmp = I18N::translate('after %s', $d1 . $conv1);
360a25f0a04SGreg Roach                break;
361a25f0a04SGreg Roach            case 'FROM':
362bbb76c12SGreg Roach                /* I18N: Gedcom FROM dates */
363bbb76c12SGreg Roach                $tmp = I18N::translate('from %s', $d1 . $conv1);
364a25f0a04SGreg Roach                break;
365a25f0a04SGreg Roach            case 'TO':
366bbb76c12SGreg Roach                /* I18N: Gedcom TO dates */
367bbb76c12SGreg Roach                $tmp = I18N::translate('to %s', $d1 . $conv1);
368a25f0a04SGreg Roach                break;
369a25f0a04SGreg Roach            case 'BETAND':
370bbb76c12SGreg Roach                /* I18N: Gedcom BET-AND dates */
371bbb76c12SGreg Roach                $tmp = I18N::translate('between %s and %s', $d1 . $conv1, $d2 . $conv2);
372a25f0a04SGreg Roach                break;
373a25f0a04SGreg Roach            case 'FROMTO':
374bbb76c12SGreg Roach                /* I18N: Gedcom FROM-TO dates */
375bbb76c12SGreg Roach                $tmp = I18N::translate('from %s to %s', $d1 . $conv1, $d2 . $conv2);
376a25f0a04SGreg Roach                break;
377a25f0a04SGreg Roach            default:
378a25f0a04SGreg Roach                $tmp = I18N::translate('Invalid date');
379bbb76c12SGreg Roach                break;
380a25f0a04SGreg Roach        }
381a25f0a04SGreg Roach
3825fec6c0aSGreg Roach        if (strip_tags($tmp) === '') {
3835fec6c0aSGreg Roach            return '';
384a25f0a04SGreg Roach        }
385b2ce94c6SRico Sonntag
386b2ce94c6SRico Sonntag        return '<span class="date">' . $tmp . '</span>';
387a25f0a04SGreg Roach    }
388a25f0a04SGreg Roach
389a25f0a04SGreg Roach    /**
390a25f0a04SGreg Roach     * Get the earliest calendar date from this GEDCOM date.
391a25f0a04SGreg Roach     *
392a25f0a04SGreg Roach     * In the date “FROM 1900 TO 1910”, this would be 1900.
393a25f0a04SGreg Roach     *
3944a83f5d7SGreg Roach     * @return AbstractCalendarDate
395a25f0a04SGreg Roach     */
3964a83f5d7SGreg Roach    public function minimumDate(): AbstractCalendarDate
397c1010edaSGreg Roach    {
398a25f0a04SGreg Roach        return $this->date1;
399a25f0a04SGreg Roach    }
400a25f0a04SGreg Roach
401a25f0a04SGreg Roach    /**
402a25f0a04SGreg Roach     * Get the latest calendar date from this GEDCOM date.
403a25f0a04SGreg Roach     *
404a25f0a04SGreg Roach     * In the date “FROM 1900 TO 1910”, this would be 1910.
405a25f0a04SGreg Roach     *
4064a83f5d7SGreg Roach     * @return AbstractCalendarDate
407a25f0a04SGreg Roach     */
408e364afe4SGreg Roach    public function maximumDate(): AbstractCalendarDate
409c1010edaSGreg Roach    {
410af2489e5SGreg Roach        return $this->date2 ?? $this->date1;
411a25f0a04SGreg Roach    }
412a25f0a04SGreg Roach
413a25f0a04SGreg Roach    /**
414a25f0a04SGreg Roach     * Get the earliest Julian day number from this GEDCOM date.
415a25f0a04SGreg Roach     *
416cbc1590aSGreg Roach     * @return int
417a25f0a04SGreg Roach     */
4188f53f488SRico Sonntag    public function minimumJulianDay(): int
419c1010edaSGreg Roach    {
420af2489e5SGreg Roach        return $this->minimumDate()->minimumJulianDay();
421a25f0a04SGreg Roach    }
422a25f0a04SGreg Roach
423a25f0a04SGreg Roach    /**
424a25f0a04SGreg Roach     * Get the latest Julian day number from this GEDCOM date.
425a25f0a04SGreg Roach     *
426cbc1590aSGreg Roach     * @return int
427a25f0a04SGreg Roach     */
4288f53f488SRico Sonntag    public function maximumJulianDay(): int
429c1010edaSGreg Roach    {
430af2489e5SGreg Roach        return $this->maximumDate()->maximumJulianDay();
431a25f0a04SGreg Roach    }
432a25f0a04SGreg Roach
433a25f0a04SGreg Roach    /**
434a25f0a04SGreg Roach     * Get the middle Julian day number from the GEDCOM date.
435a25f0a04SGreg Roach     *
436f5b60decSGreg Roach     * For a month-only date, this would be somewhere around the 16th day.
437a25f0a04SGreg Roach     * For a year-only date, this would be somewhere around 1st July.
438a25f0a04SGreg Roach     *
439cbc1590aSGreg Roach     * @return int
440a25f0a04SGreg Roach     */
4418f53f488SRico Sonntag    public function julianDay(): int
442c1010edaSGreg Roach    {
443cdaafeeeSGreg Roach        return intdiv($this->minimumJulianDay() + $this->maximumJulianDay(), 2);
444a25f0a04SGreg Roach    }
445a25f0a04SGreg Roach
446a25f0a04SGreg Roach    /**
447a25f0a04SGreg Roach     * Offset this date by N years, and round to the whole year.
448a25f0a04SGreg Roach     *
449a25f0a04SGreg Roach     * This is typically used to create an estimated death date,
450a25f0a04SGreg Roach     * which is before a certain number of years after the birth date.
451a25f0a04SGreg Roach     *
452cbc1590aSGreg Roach     * @param int    $years     a number of years, positive or negative
453cbc1590aSGreg Roach     * @param string $qualifier typically “BEF” or “AFT”
454a25f0a04SGreg Roach     *
455a25f0a04SGreg Roach     * @return Date
456a25f0a04SGreg Roach     */
4578f53f488SRico Sonntag    public function addYears(int $years, string $qualifier = ''): Date
458c1010edaSGreg Roach    {
459a25f0a04SGreg Roach        $tmp               = clone $this;
4604a83f5d7SGreg Roach        $tmp->date1->year  += $years;
4614a83f5d7SGreg Roach        $tmp->date1->month = 0;
4624a83f5d7SGreg Roach        $tmp->date1->day   = 0;
463a25f0a04SGreg Roach        $tmp->date1->setJdFromYmd();
464a25f0a04SGreg Roach        $tmp->qual1 = $qualifier;
465a25f0a04SGreg Roach        $tmp->qual2 = '';
466a25f0a04SGreg Roach        $tmp->date2 = null;
467a25f0a04SGreg Roach
468a25f0a04SGreg Roach        return $tmp;
469a25f0a04SGreg Roach    }
470a25f0a04SGreg Roach
471a25f0a04SGreg Roach    /**
472a25f0a04SGreg Roach     * Compare two dates, so they can be sorted.
473a25f0a04SGreg Roach     *
474054771e9SGreg Roach     * return -1 if $a<$b
475054771e9SGreg Roach     * return +1 if $b>$a
476a25f0a04SGreg Roach     * return  0 if dates same/overlap
477a25f0a04SGreg Roach     * BEF/AFT sort as the day before/after
478a25f0a04SGreg Roach     *
479a25f0a04SGreg Roach     * @param Date $a
480a25f0a04SGreg Roach     * @param Date $b
481a25f0a04SGreg Roach     *
482cbc1590aSGreg Roach     * @return int
483a25f0a04SGreg Roach     */
484e364afe4SGreg Roach    public static function compare(Date $a, Date $b): int
485c1010edaSGreg Roach    {
486a25f0a04SGreg Roach        // Get min/max JD for each date.
487a25f0a04SGreg Roach        switch ($a->qual1) {
488a25f0a04SGreg Roach            case 'BEF':
489f5b60decSGreg Roach                $amin = $a->minimumJulianDay() - 1;
490a25f0a04SGreg Roach                $amax = $amin;
491a25f0a04SGreg Roach                break;
492a25f0a04SGreg Roach            case 'AFT':
493f5b60decSGreg Roach                $amax = $a->maximumJulianDay() + 1;
494a25f0a04SGreg Roach                $amin = $amax;
495a25f0a04SGreg Roach                break;
496a25f0a04SGreg Roach            default:
497f5b60decSGreg Roach                $amin = $a->minimumJulianDay();
498f5b60decSGreg Roach                $amax = $a->maximumJulianDay();
499a25f0a04SGreg Roach                break;
500a25f0a04SGreg Roach        }
501a25f0a04SGreg Roach        switch ($b->qual1) {
502a25f0a04SGreg Roach            case 'BEF':
503f5b60decSGreg Roach                $bmin = $b->minimumJulianDay() - 1;
504a25f0a04SGreg Roach                $bmax = $bmin;
505a25f0a04SGreg Roach                break;
506a25f0a04SGreg Roach            case 'AFT':
507f5b60decSGreg Roach                $bmax = $b->maximumJulianDay() + 1;
508a25f0a04SGreg Roach                $bmin = $bmax;
509a25f0a04SGreg Roach                break;
510a25f0a04SGreg Roach            default:
511f5b60decSGreg Roach                $bmin = $b->minimumJulianDay();
512f5b60decSGreg Roach                $bmax = $b->maximumJulianDay();
513a25f0a04SGreg Roach                break;
514a25f0a04SGreg Roach        }
515a25f0a04SGreg Roach        if ($amax < $bmin) {
516a25f0a04SGreg Roach            return -1;
517a25f0a04SGreg Roach        }
518b2ce94c6SRico Sonntag
519b2ce94c6SRico Sonntag        if ($amin > $bmax && $bmax > 0) {
520b2ce94c6SRico Sonntag            return 1;
521b2ce94c6SRico Sonntag        }
522b2ce94c6SRico Sonntag
523b2ce94c6SRico Sonntag        if ($amin < $bmin && $amax <= $bmax) {
524b2ce94c6SRico Sonntag            return -1;
525b2ce94c6SRico Sonntag        }
526b2ce94c6SRico Sonntag
527b2ce94c6SRico Sonntag        if ($amin > $bmin && $amax >= $bmax && $bmax > 0) {
528b2ce94c6SRico Sonntag            return 1;
529b2ce94c6SRico Sonntag        }
530b2ce94c6SRico Sonntag
531b2ce94c6SRico Sonntag        return 0;
532a25f0a04SGreg Roach    }
533a25f0a04SGreg Roach
534a25f0a04SGreg Roach    /**
535a25f0a04SGreg Roach     * Check whether a gedcom date contains usable calendar date(s).
536a25f0a04SGreg Roach     *
537a25f0a04SGreg Roach     * An incomplete date such as "12 AUG" would be invalid, as
538a25f0a04SGreg Roach     * we cannot sort it.
539a25f0a04SGreg Roach     *
540cbc1590aSGreg Roach     * @return bool
541a25f0a04SGreg Roach     */
5428f53f488SRico Sonntag    public function isOK(): bool
543c1010edaSGreg Roach    {
544f5b60decSGreg Roach        return $this->minimumJulianDay() && $this->maximumJulianDay();
545a25f0a04SGreg Roach    }
546a25f0a04SGreg Roach
547a25f0a04SGreg Roach    /**
548a25f0a04SGreg Roach     * Calculate the gregorian year for a date. This should NOT be used internally
549a25f0a04SGreg Roach     * within WT - we should keep the code "calendar neutral" to allow support for
550a25f0a04SGreg Roach     * jewish/arabic users. This is only for interfacing with external entities,
551a25f0a04SGreg Roach     * such as the ancestry.com search interface or the dated fact icons.
552a25f0a04SGreg Roach     *
553cbc1590aSGreg Roach     * @return int
554a25f0a04SGreg Roach     */
555e364afe4SGreg Roach    public function gregorianYear(): int
556c1010edaSGreg Roach    {
557a25f0a04SGreg Roach        if ($this->isOK()) {
55859f2f229SGreg Roach            $gregorian_calendar = new GregorianCalendar();
55965e02381SGreg Roach            [$year] = $gregorian_calendar->jdToYmd($this->julianDay());
560a25f0a04SGreg Roach
561a25f0a04SGreg Roach            return $year;
562a25f0a04SGreg Roach        }
563b2ce94c6SRico Sonntag
564b2ce94c6SRico Sonntag        return 0;
565a25f0a04SGreg Roach    }
566a25f0a04SGreg Roach}
567