xref: /webtrees/app/Date.php (revision e364afe4ae4e316fc4ebd53eccbaff2d29e419a5)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 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;
19a25f0a04SGreg Roach
20a80c4362SGreg Roachuse DomainException;
21a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
224a83f5d7SGreg Roachuse Fisharebest\Webtrees\Date\AbstractCalendarDate;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\FrenchDate;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\HijriDate;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JalaliDate;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JulianDate;
290e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\RomanDate;
30a25f0a04SGreg Roach
31a25f0a04SGreg Roach/**
3276692c8bSGreg Roach * A representation of GEDCOM dates and date ranges.
3376692c8bSGreg Roach *
3476692c8bSGreg Roach * Since different calendars start their days at different times, (civil
35a25f0a04SGreg Roach * midnight, solar midnight, sunset, sunrise, etc.), we convert on the basis of
36a25f0a04SGreg Roach * midday.
37a25f0a04SGreg Roach *
3876692c8bSGreg Roach * We assume that years start on the first day of the first month. Where
39a25f0a04SGreg Roach * this is not the case (e.g. England prior to 1752), we need to use modified
40a25f0a04SGreg Roach * years or the OS/NS notation "4 FEB 1750/51".
41a25f0a04SGreg Roach */
42c1010edaSGreg Roachclass Date
43c1010edaSGreg Roach{
44a25f0a04SGreg Roach    /** @var string Optional qualifier, such as BEF, FROM, ABT */
45fe11e66dSGreg Roach    public $qual1 = '';
46a25f0a04SGreg Roach
474a83f5d7SGreg Roach    /** @var AbstractCalendarDate  The first (or only) date */
48f5b60decSGreg Roach    private $date1;
49a25f0a04SGreg Roach
50a25f0a04SGreg Roach    /** @var string  Optional qualifier, such as TO, AND */
51fe11e66dSGreg Roach    public $qual2 = '';
52a25f0a04SGreg Roach
534a83f5d7SGreg Roach    /** @var AbstractCalendarDate|null Optional second date */
547e96c925SGreg Roach    private $date2 = null;
55a25f0a04SGreg Roach
56a25f0a04SGreg Roach    /** @var string ptional text, as included with an INTerpreted date */
577e96c925SGreg Roach    private $text = '';
58a25f0a04SGreg Roach
59a25f0a04SGreg Roach    /**
60a25f0a04SGreg Roach     * Create a date, from GEDCOM data.
61a25f0a04SGreg Roach     *
62a25f0a04SGreg Roach     * @param string $date A date in GEDCOM format
63a25f0a04SGreg Roach     */
64cfe766afSGreg Roach    public function __construct(string $date)
65c1010edaSGreg Roach    {
66a25f0a04SGreg Roach        // Extract any explanatory text
67a25f0a04SGreg Roach        if (preg_match('/^(.*) ?[(](.*)[)]/', $date, $match)) {
68a25f0a04SGreg Roach            $date       = $match[1];
69a25f0a04SGreg Roach            $this->text = $match[2];
70a25f0a04SGreg Roach        }
71a25f0a04SGreg Roach        if (preg_match('/^(FROM|BET) (.+) (AND|TO) (.+)/', $date, $match)) {
72a25f0a04SGreg Roach            $this->qual1 = $match[1];
73a25f0a04SGreg Roach            $this->date1 = $this->parseDate($match[2]);
74a25f0a04SGreg Roach            $this->qual2 = $match[3];
75a25f0a04SGreg Roach            $this->date2 = $this->parseDate($match[4]);
763506b2e5SGreg Roach        } elseif (preg_match('/^(TO|FROM|BEF|AFT|CAL|EST|INT|ABT) (.+)/', $date, $match)) {
77a25f0a04SGreg Roach            $this->qual1 = $match[1];
78a25f0a04SGreg Roach            $this->date1 = $this->parseDate($match[2]);
79a25f0a04SGreg Roach        } else {
80a25f0a04SGreg Roach            $this->date1 = $this->parseDate($date);
81a25f0a04SGreg Roach        }
82a25f0a04SGreg Roach    }
83a25f0a04SGreg Roach
84a25f0a04SGreg Roach    /**
85a25f0a04SGreg Roach     * When we copy a date object, we need to create copies of
86a25f0a04SGreg Roach     * its child objects.
87a25f0a04SGreg Roach     */
88c1010edaSGreg Roach    public function __clone()
89c1010edaSGreg Roach    {
90a25f0a04SGreg Roach        $this->date1 = clone $this->date1;
917e96c925SGreg Roach        if ($this->date2 !== null) {
92a25f0a04SGreg Roach            $this->date2 = clone $this->date2;
93a25f0a04SGreg Roach        }
94a25f0a04SGreg Roach    }
95a25f0a04SGreg Roach
96a25f0a04SGreg Roach    /**
97a25f0a04SGreg Roach     * Convert a calendar date, such as "12 JUN 1943" into calendar date object.
98a25f0a04SGreg Roach     * A GEDCOM date range may have two calendar dates.
99a25f0a04SGreg Roach     *
100a25f0a04SGreg Roach     * @param string $date
101a25f0a04SGreg Roach     *
102a80c4362SGreg Roach     * @throws DomainException
1034a83f5d7SGreg Roach     * @return AbstractCalendarDate
104a25f0a04SGreg Roach     */
1054a83f5d7SGreg Roach    private function parseDate($date): AbstractCalendarDate
106c1010edaSGreg Roach    {
107a25f0a04SGreg Roach        // Valid calendar escape specified? - use it
108bee1f90bSGreg Roach        if (preg_match('/^(@#D(?:GREGORIAN|JULIAN|HEBREW|HIJRI|JALALI|FRENCH R|ROMAN)+@) ?(.*)/', $date, $match)) {
109a25f0a04SGreg Roach            $cal  = $match[1];
110a25f0a04SGreg Roach            $date = $match[2];
111a25f0a04SGreg Roach        } else {
112a25f0a04SGreg Roach            $cal = '';
113a25f0a04SGreg Roach        }
114a25f0a04SGreg Roach        // A date with a month: DM, M, MY or DMY
115a25f0a04SGreg 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)) {
116a25f0a04SGreg Roach            $d = $match[1];
117a25f0a04SGreg Roach            $m = $match[2];
118a25f0a04SGreg Roach            $y = $match[3];
119471edd97SGreg Roach        } elseif (preg_match('/^(\d{1,4}(?: B\.C\.)?|\d\d\d\d\/\d\d)$/', $date, $match)) {
120471edd97SGreg Roach            // A date with just a year
121a25f0a04SGreg Roach            $d = '';
122a25f0a04SGreg Roach            $m = '';
123a25f0a04SGreg Roach            $y = $match[1];
124a25f0a04SGreg Roach        } else {
125a25f0a04SGreg Roach            // An invalid date - do the best we can.
126a25f0a04SGreg Roach            $d = '';
127a25f0a04SGreg Roach            $m = '';
128a25f0a04SGreg Roach            $y = '';
129a25f0a04SGreg Roach            // Look for a 3/4 digit year anywhere in the date
130a25f0a04SGreg Roach            if (preg_match('/\b(\d{3,4})\b/', $date, $match)) {
131a25f0a04SGreg Roach                $y = $match[1];
132a25f0a04SGreg Roach            }
133a25f0a04SGreg Roach            // Look for a month anywhere in the date
134a25f0a04SGreg 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)) {
135a25f0a04SGreg Roach                $m = $match[1];
136a25f0a04SGreg Roach                // Look for a day number anywhere in the date
137a25f0a04SGreg Roach                if (preg_match('/\b(\d\d?)\b/', $date, $match)) {
138a25f0a04SGreg Roach                    $d = $match[1];
139a25f0a04SGreg Roach                }
140a25f0a04SGreg Roach            }
141a25f0a04SGreg Roach        }
142a25f0a04SGreg Roach
143a25f0a04SGreg Roach        // Unambiguous dates - override calendar escape
144a25f0a04SGreg Roach        if (preg_match('/^(TSH|CSH|KSL|TVT|SHV|ADR|ADS|NSN|IYR|SVN|TMZ|AAV|ELL)$/', $m)) {
1454a83f5d7SGreg Roach            $cal = JewishDate::ESCAPE;
146*e364afe4SGreg Roach        } elseif (preg_match('/^(VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP)$/', $m)) {
1474a83f5d7SGreg Roach            $cal = FrenchDate::ESCAPE;
148*e364afe4SGreg Roach        } elseif (preg_match('/^(MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH)$/', $m)) {
1494a83f5d7SGreg Roach            $cal = HijriDate::ESCAPE; // This is a WT extension
150*e364afe4SGreg Roach        } elseif (preg_match('/^(FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN)$/', $m)) {
1514a83f5d7SGreg Roach            $cal = JalaliDate::ESCAPE; // This is a WT extension
152a25f0a04SGreg Roach        } elseif (preg_match('/^\d{1,4}( B\.C\.)|\d\d\d\d\/\d\d$/', $y)) {
1534a83f5d7SGreg Roach            $cal = JulianDate::ESCAPE;
154a25f0a04SGreg Roach        }
155a25f0a04SGreg Roach
156a25f0a04SGreg Roach        // Ambiguous dates - don't override calendar escape
157*e364afe4SGreg Roach        if ($cal === '') {
158a25f0a04SGreg Roach            if (preg_match('/^(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)$/', $m)) {
1594a83f5d7SGreg Roach                $cal =  GregorianDate::ESCAPE;
160*e364afe4SGreg Roach            } elseif (preg_match('/^[345]\d\d\d$/', $y)) {
161a25f0a04SGreg Roach                // Year 3000-5999
1624a83f5d7SGreg Roach                $cal = JewishDate::ESCAPE;
163a25f0a04SGreg Roach            } else {
1644a83f5d7SGreg Roach                $cal = GregorianDate::ESCAPE;
165a25f0a04SGreg Roach            }
166a25f0a04SGreg Roach        }
167a25f0a04SGreg Roach        // Now construct an object of the correct type
168a25f0a04SGreg Roach        switch ($cal) {
1694a83f5d7SGreg Roach            case GregorianDate::ESCAPE:
170c1010edaSGreg Roach                return new GregorianDate([
171c1010edaSGreg Roach                    $y,
172c1010edaSGreg Roach                    $m,
173c1010edaSGreg Roach                    $d,
174c1010edaSGreg Roach                ]);
1754a83f5d7SGreg Roach            case JulianDate::ESCAPE:
176c1010edaSGreg Roach                return new JulianDate([
177c1010edaSGreg Roach                    $y,
178c1010edaSGreg Roach                    $m,
179c1010edaSGreg Roach                    $d,
180c1010edaSGreg Roach                ]);
1814a83f5d7SGreg Roach            case JewishDate::ESCAPE:
182c1010edaSGreg Roach                return new JewishDate([
183c1010edaSGreg Roach                    $y,
184c1010edaSGreg Roach                    $m,
185c1010edaSGreg Roach                    $d,
186c1010edaSGreg Roach                ]);
1874a83f5d7SGreg Roach            case HijriDate::ESCAPE:
188c1010edaSGreg Roach                return new HijriDate([
189c1010edaSGreg Roach                    $y,
190c1010edaSGreg Roach                    $m,
191c1010edaSGreg Roach                    $d,
192c1010edaSGreg Roach                ]);
1934a83f5d7SGreg Roach            case FrenchDate::ESCAPE:
194c1010edaSGreg Roach                return new FrenchDate([
195c1010edaSGreg Roach                    $y,
196c1010edaSGreg Roach                    $m,
197c1010edaSGreg Roach                    $d,
198c1010edaSGreg Roach                ]);
1994a83f5d7SGreg Roach            case JalaliDate::ESCAPE:
200c1010edaSGreg Roach                return new JalaliDate([
201c1010edaSGreg Roach                    $y,
202c1010edaSGreg Roach                    $m,
203c1010edaSGreg Roach                    $d,
204c1010edaSGreg Roach                ]);
2054a83f5d7SGreg Roach            case RomanDate::ESCAPE:
206c1010edaSGreg Roach                return new RomanDate([
207c1010edaSGreg Roach                    $y,
208c1010edaSGreg Roach                    $m,
209c1010edaSGreg Roach                    $d,
210c1010edaSGreg Roach                ]);
211a25f0a04SGreg Roach            default:
212a80c4362SGreg Roach                throw new DomainException('Invalid calendar');
213a25f0a04SGreg Roach        }
214a25f0a04SGreg Roach    }
215a25f0a04SGreg Roach
2164e6225d5SGreg Roach    /**
2174e6225d5SGreg Roach     * A list of supported calendars and their names.
2184e6225d5SGreg Roach     *
2194e6225d5SGreg Roach     * @return string[]
2204e6225d5SGreg Roach     */
2218f53f488SRico Sonntag    public static function calendarNames(): array
222c1010edaSGreg Roach    {
22313abd6f3SGreg Roach        return [
224bbb76c12SGreg Roach            /* I18N: The gregorian calendar */
225bbb76c12SGreg Roach            'gregorian' => I18N::translate('Gregorian'),
226bbb76c12SGreg Roach            /* I18N: The julian calendar */
227bbb76c12SGreg Roach            'julian'    => I18N::translate('Julian'),
228bbb76c12SGreg Roach            /* I18N: The French calendar */
229bbb76c12SGreg Roach            'french'    => I18N::translate('French'),
230bbb76c12SGreg Roach            /* I18N: The Hebrew/Jewish calendar */
231bbb76c12SGreg Roach            'jewish'    => I18N::translate('Jewish'),
232bbb76c12SGreg Roach            /* I18N: The Arabic/Hijri calendar */
233bbb76c12SGreg Roach            'hijri'     => I18N::translate('Hijri'),
234bbb76c12SGreg Roach            /* I18N: The Persian/Jalali calendar */
235bbb76c12SGreg Roach            'jalali'    => I18N::translate('Jalali'),
23613abd6f3SGreg Roach        ];
2374e6225d5SGreg Roach    }
2384e6225d5SGreg Roach
239a25f0a04SGreg Roach    /**
240a25f0a04SGreg Roach     * Convert a date to the preferred format and calendar(s) display.
241a25f0a04SGreg Roach     *
242cbc1590aSGreg Roach     * @param bool|null   $url               Wrap the date in a link to calendar.php
243a25f0a04SGreg Roach     * @param string|null $date_format       Override the default date format
244cbc1590aSGreg Roach     * @param bool|null   $convert_calendars Convert the date into other calendars
245a25f0a04SGreg Roach     *
246a25f0a04SGreg Roach     * @return string
247a25f0a04SGreg Roach     */
248*e364afe4SGreg Roach    public function display($url = false, $date_format = null, $convert_calendars = true): string
249c1010edaSGreg Roach    {
2503a5741c7SGreg Roach        // Do we need a new DateFormatterService class?
251cab242e7SGreg Roach        $tree = app(Tree::class);
2525d51864bSGreg Roach
25312dcbc32SGreg Roach        $CALENDAR_FORMAT = $tree->getPreference('CALENDAR_FORMAT');
254a25f0a04SGreg Roach
255a25f0a04SGreg Roach        if ($date_format === null) {
256dfeee0a8SGreg Roach            $date_format = I18N::dateFormat();
257a25f0a04SGreg Roach        }
258a25f0a04SGreg Roach
259a25f0a04SGreg Roach        if ($convert_calendars) {
260a25f0a04SGreg Roach            $calendar_format = explode('_and_', $CALENDAR_FORMAT);
261a25f0a04SGreg Roach        } else {
26213abd6f3SGreg Roach            $calendar_format = [];
263a25f0a04SGreg Roach        }
264a25f0a04SGreg Roach
265a25f0a04SGreg Roach        // Two dates with text before, between and after
266a25f0a04SGreg Roach        $q1 = $this->qual1;
267a25f0a04SGreg Roach        $d1 = $this->date1->format($date_format, $this->qual1);
268a25f0a04SGreg Roach        $q2 = $this->qual2;
2698f038c36SRico Sonntag        if ($this->date2 === null) {
270a25f0a04SGreg Roach            $d2 = '';
271a25f0a04SGreg Roach        } else {
272a25f0a04SGreg Roach            $d2 = $this->date2->format($date_format, $this->qual2);
273a25f0a04SGreg Roach        }
274a25f0a04SGreg Roach        // Con vert to other calendars, if requested
275a25f0a04SGreg Roach        $conv1 = '';
276a25f0a04SGreg Roach        $conv2 = '';
277a25f0a04SGreg Roach        foreach ($calendar_format as $cal_fmt) {
278*e364afe4SGreg Roach            if ($cal_fmt !== 'none') {
279a25f0a04SGreg Roach                $d1conv = $this->date1->convertToCalendar($cal_fmt);
280a25f0a04SGreg Roach                if ($d1conv->inValidRange()) {
281a25f0a04SGreg Roach                    $d1tmp = $d1conv->format($date_format, $this->qual1);
282a25f0a04SGreg Roach                } else {
283a25f0a04SGreg Roach                    $d1tmp = '';
284a25f0a04SGreg Roach                }
2858f038c36SRico Sonntag                if ($this->date2 === null) {
286a25f0a04SGreg Roach                    $d2conv = null;
287a25f0a04SGreg Roach                    $d2tmp  = '';
288a25f0a04SGreg Roach                } else {
289a25f0a04SGreg Roach                    $d2conv = $this->date2->convertToCalendar($cal_fmt);
290a25f0a04SGreg Roach                    if ($d2conv->inValidRange()) {
291a25f0a04SGreg Roach                        $d2tmp = $d2conv->format($date_format, $this->qual2);
292a25f0a04SGreg Roach                    } else {
293a25f0a04SGreg Roach                        $d2tmp = '';
294a25f0a04SGreg Roach                    }
295a25f0a04SGreg Roach                }
296d4f10423SGreg Roach                // If the date is different from the unconverted date, add it to the date string.
2973cfaf201SGreg Roach                if ($d1 != $d1tmp && $d1tmp !== '') {
298a25f0a04SGreg Roach                    if ($url) {
2993cfaf201SGreg Roach                        if ($CALENDAR_FORMAT !== 'none') {
30049d5f1d7SGreg Roach                            $conv1 .= ' <span dir="' . I18N::direction() . '">(<a href="' . $d1conv->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d1tmp . '</a>)</span>';
301a25f0a04SGreg Roach                        } else {
30249d5f1d7SGreg Roach                            $conv1 .= ' <span dir="' . I18N::direction() . '"><br><a href="' . $d1conv->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d1tmp . '</a></span>';
303a25f0a04SGreg Roach                        }
304a25f0a04SGreg Roach                    } else {
305c0af647eSGreg Roach                        $conv1 .= ' <span dir="' . I18N::direction() . '">(' . $d1tmp . ')</span>';
306a25f0a04SGreg Roach                    }
307a25f0a04SGreg Roach                }
3088f038c36SRico Sonntag                if ($this->date2 !== null && $d2 != $d2tmp && $d1tmp != '') {
309a25f0a04SGreg Roach                    if ($url) {
31049d5f1d7SGreg Roach                        $conv2 .= ' <span dir="' . I18N::direction() . '">(<a href="' . $d2conv->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d2tmp . '</a>)</span>';
311a25f0a04SGreg Roach                    } else {
312c0af647eSGreg Roach                        $conv2 .= ' <span dir="' . I18N::direction() . '">(' . $d2tmp . ')</span>';
313a25f0a04SGreg Roach                    }
314a25f0a04SGreg Roach                }
315a25f0a04SGreg Roach            }
316a25f0a04SGreg Roach        }
317a25f0a04SGreg Roach
318a25f0a04SGreg Roach        // Add URLs, if requested
319a25f0a04SGreg Roach        if ($url) {
32049d5f1d7SGreg Roach            $d1 = '<a href="' . $this->date1->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d1 . '</a>';
3214a83f5d7SGreg Roach            if ($this->date2 instanceof AbstractCalendarDate) {
32249d5f1d7SGreg Roach                $d2 = '<a href="' . $this->date2->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d2 . '</a>';
323a25f0a04SGreg Roach            }
324a25f0a04SGreg Roach        }
325a25f0a04SGreg Roach
326a25f0a04SGreg Roach        // Localise the date
327a25f0a04SGreg Roach        switch ($q1 . $q2) {
328a25f0a04SGreg Roach            case '':
329a25f0a04SGreg Roach                $tmp = $d1 . $conv1;
330a25f0a04SGreg Roach                break;
331a25f0a04SGreg Roach            case 'ABT':
332bbb76c12SGreg Roach                /* I18N: Gedcom ABT dates */
333bbb76c12SGreg Roach                $tmp = I18N::translate('about %s', $d1 . $conv1);
334a25f0a04SGreg Roach                break;
335a25f0a04SGreg Roach            case 'CAL':
336bbb76c12SGreg Roach                /* I18N: Gedcom CAL dates */
337bbb76c12SGreg Roach                $tmp = I18N::translate('calculated %s', $d1 . $conv1);
338a25f0a04SGreg Roach                break;
339a25f0a04SGreg Roach            case 'EST':
340bbb76c12SGreg Roach                /* I18N: Gedcom EST dates */
341bbb76c12SGreg Roach                $tmp = I18N::translate('estimated %s', $d1 . $conv1);
342a25f0a04SGreg Roach                break;
343a25f0a04SGreg Roach            case 'INT':
344bbb76c12SGreg Roach                /* I18N: Gedcom INT dates */
345bbb76c12SGreg Roach                $tmp = I18N::translate('interpreted %s (%s)', $d1 . $conv1, e($this->text));
346a25f0a04SGreg Roach                break;
347a25f0a04SGreg Roach            case 'BEF':
348bbb76c12SGreg Roach                /* I18N: Gedcom BEF dates */
349bbb76c12SGreg Roach                $tmp = I18N::translate('before %s', $d1 . $conv1);
350a25f0a04SGreg Roach                break;
351a25f0a04SGreg Roach            case 'AFT':
352bbb76c12SGreg Roach                /* I18N: Gedcom AFT dates */
353bbb76c12SGreg Roach                $tmp = I18N::translate('after %s', $d1 . $conv1);
354a25f0a04SGreg Roach                break;
355a25f0a04SGreg Roach            case 'FROM':
356bbb76c12SGreg Roach                /* I18N: Gedcom FROM dates */
357bbb76c12SGreg Roach                $tmp = I18N::translate('from %s', $d1 . $conv1);
358a25f0a04SGreg Roach                break;
359a25f0a04SGreg Roach            case 'TO':
360bbb76c12SGreg Roach                /* I18N: Gedcom TO dates */
361bbb76c12SGreg Roach                $tmp = I18N::translate('to %s', $d1 . $conv1);
362a25f0a04SGreg Roach                break;
363a25f0a04SGreg Roach            case 'BETAND':
364bbb76c12SGreg Roach                /* I18N: Gedcom BET-AND dates */
365bbb76c12SGreg Roach                $tmp = I18N::translate('between %s and %s', $d1 . $conv1, $d2 . $conv2);
366a25f0a04SGreg Roach                break;
367a25f0a04SGreg Roach            case 'FROMTO':
368bbb76c12SGreg Roach                /* I18N: Gedcom FROM-TO dates */
369bbb76c12SGreg Roach                $tmp = I18N::translate('from %s to %s', $d1 . $conv1, $d2 . $conv2);
370a25f0a04SGreg Roach                break;
371a25f0a04SGreg Roach            default:
372a25f0a04SGreg Roach                $tmp = I18N::translate('Invalid date');
373bbb76c12SGreg Roach                break;
374a25f0a04SGreg Roach        }
375a25f0a04SGreg Roach
3765fec6c0aSGreg Roach        if (strip_tags($tmp) === '') {
3775fec6c0aSGreg Roach            return '';
378a25f0a04SGreg Roach        }
379b2ce94c6SRico Sonntag
380b2ce94c6SRico Sonntag        return '<span class="date">' . $tmp . '</span>';
381a25f0a04SGreg Roach    }
382a25f0a04SGreg Roach
383a25f0a04SGreg Roach    /**
384a25f0a04SGreg Roach     * Get the earliest calendar date from this GEDCOM date.
385a25f0a04SGreg Roach     *
386a25f0a04SGreg Roach     * In the date “FROM 1900 TO 1910”, this would be 1900.
387a25f0a04SGreg Roach     *
3884a83f5d7SGreg Roach     * @return AbstractCalendarDate
389a25f0a04SGreg Roach     */
3904a83f5d7SGreg Roach    public function minimumDate(): AbstractCalendarDate
391c1010edaSGreg Roach    {
392a25f0a04SGreg Roach        return $this->date1;
393a25f0a04SGreg Roach    }
394a25f0a04SGreg Roach
395a25f0a04SGreg Roach    /**
396a25f0a04SGreg Roach     * Get the latest calendar date from this GEDCOM date.
397a25f0a04SGreg Roach     *
398a25f0a04SGreg Roach     * In the date “FROM 1900 TO 1910”, this would be 1910.
399a25f0a04SGreg Roach     *
4004a83f5d7SGreg Roach     * @return AbstractCalendarDate
401a25f0a04SGreg Roach     */
402*e364afe4SGreg Roach    public function maximumDate(): AbstractCalendarDate
403c1010edaSGreg Roach    {
404af2489e5SGreg Roach        return $this->date2 ?? $this->date1;
405a25f0a04SGreg Roach    }
406a25f0a04SGreg Roach
407a25f0a04SGreg Roach    /**
408a25f0a04SGreg Roach     * Get the earliest Julian day number from this GEDCOM date.
409a25f0a04SGreg Roach     *
410cbc1590aSGreg Roach     * @return int
411a25f0a04SGreg Roach     */
4128f53f488SRico Sonntag    public function minimumJulianDay(): int
413c1010edaSGreg Roach    {
414af2489e5SGreg Roach        return $this->minimumDate()->minimumJulianDay();
415a25f0a04SGreg Roach    }
416a25f0a04SGreg Roach
417a25f0a04SGreg Roach    /**
418a25f0a04SGreg Roach     * Get the latest Julian day number from this GEDCOM date.
419a25f0a04SGreg Roach     *
420cbc1590aSGreg Roach     * @return int
421a25f0a04SGreg Roach     */
4228f53f488SRico Sonntag    public function maximumJulianDay(): int
423c1010edaSGreg Roach    {
424af2489e5SGreg Roach        return $this->maximumDate()->maximumJulianDay();
425a25f0a04SGreg Roach    }
426a25f0a04SGreg Roach
427a25f0a04SGreg Roach    /**
428a25f0a04SGreg Roach     * Get the middle Julian day number from the GEDCOM date.
429a25f0a04SGreg Roach     *
430f5b60decSGreg Roach     * For a month-only date, this would be somewhere around the 16th day.
431a25f0a04SGreg Roach     * For a year-only date, this would be somewhere around 1st July.
432a25f0a04SGreg Roach     *
433cbc1590aSGreg Roach     * @return int
434a25f0a04SGreg Roach     */
4358f53f488SRico Sonntag    public function julianDay(): int
436c1010edaSGreg Roach    {
437cdaafeeeSGreg Roach        return intdiv($this->minimumJulianDay() + $this->maximumJulianDay(), 2);
438a25f0a04SGreg Roach    }
439a25f0a04SGreg Roach
440a25f0a04SGreg Roach    /**
441a25f0a04SGreg Roach     * Offset this date by N years, and round to the whole year.
442a25f0a04SGreg Roach     *
443a25f0a04SGreg Roach     * This is typically used to create an estimated death date,
444a25f0a04SGreg Roach     * which is before a certain number of years after the birth date.
445a25f0a04SGreg Roach     *
446cbc1590aSGreg Roach     * @param int    $years     a number of years, positive or negative
447cbc1590aSGreg Roach     * @param string $qualifier typically “BEF” or “AFT”
448a25f0a04SGreg Roach     *
449a25f0a04SGreg Roach     * @return Date
450a25f0a04SGreg Roach     */
4518f53f488SRico Sonntag    public function addYears(int $years, string $qualifier = ''): Date
452c1010edaSGreg Roach    {
453a25f0a04SGreg Roach        $tmp               = clone $this;
4544a83f5d7SGreg Roach        $tmp->date1->year  += $years;
4554a83f5d7SGreg Roach        $tmp->date1->month = 0;
4564a83f5d7SGreg Roach        $tmp->date1->day   = 0;
457a25f0a04SGreg Roach        $tmp->date1->setJdFromYmd();
458a25f0a04SGreg Roach        $tmp->qual1 = $qualifier;
459a25f0a04SGreg Roach        $tmp->qual2 = '';
460a25f0a04SGreg Roach        $tmp->date2 = null;
461a25f0a04SGreg Roach
462a25f0a04SGreg Roach        return $tmp;
463a25f0a04SGreg Roach    }
464a25f0a04SGreg Roach
465a25f0a04SGreg Roach    /**
4668fe32d0dSGreg Roach     * Calculate the the age of a person (n years), on a given date.
467a25f0a04SGreg Roach     *
468a25f0a04SGreg Roach     * @param Date $d1
469a25f0a04SGreg Roach     * @param Date $d2
470cbc1590aSGreg Roach     *
4718fe32d0dSGreg Roach     * @return int
472a25f0a04SGreg Roach     */
4738fe32d0dSGreg Roach    public static function getAgeYears(Date $d1, Date $d2): int
474c1010edaSGreg Roach    {
475*e364afe4SGreg Roach        if (Date::compare($d1, $d2) === 0) {
4768fe32d0dSGreg Roach            // Overlapping dates
4778fe32d0dSGreg Roach            $jd = $d1->minimumJulianDay();
4788fe32d0dSGreg Roach        } else {
4798fe32d0dSGreg Roach            // Non-overlapping dates
4808fe32d0dSGreg Roach            $jd = $d2->minimumJulianDay();
4818fe32d0dSGreg Roach        }
4828fe32d0dSGreg Roach
4838fe32d0dSGreg Roach        if ($jd && $d1->minimumJulianDay() && $d1->minimumJulianDay() <= $jd) {
4848fe32d0dSGreg Roach            return $d1->minimumDate()->getAge($jd);
4858fe32d0dSGreg Roach        }
486*e364afe4SGreg Roach
487*e364afe4SGreg Roach        return -1;
4888fe32d0dSGreg Roach    }
4898fe32d0dSGreg Roach
4908fe32d0dSGreg Roach    /**
4918fe32d0dSGreg Roach     * Calculate the the age of a person (n days), on a given date.
4928fe32d0dSGreg Roach     *
4938fe32d0dSGreg Roach     * @param Date $d1
4948fe32d0dSGreg Roach     * @param Date $d2
4958fe32d0dSGreg Roach     *
4968fe32d0dSGreg Roach     * @return int
4978fe32d0dSGreg Roach     */
4988fe32d0dSGreg Roach    public static function getAgeDays(Date $d1, Date $d2): int
4998fe32d0dSGreg Roach    {
5008fe32d0dSGreg Roach        if ($d2->maximumJulianDay() >= $d1->minimumJulianDay() && $d2->minimumJulianDay() <= $d1->minimumJulianDay()) {
5018fe32d0dSGreg Roach            // Overlapping dates
5028fe32d0dSGreg Roach            $jd = $d1->minimumJulianDay();
5038fe32d0dSGreg Roach        } else {
5048fe32d0dSGreg Roach            // Non-overlapping dates
5058fe32d0dSGreg Roach            $jd = $d2->minimumJulianDay();
5068fe32d0dSGreg Roach        }
5078fe32d0dSGreg Roach
5088fe32d0dSGreg Roach        // Days - integer only (for sorting, rather than for display)
5098fe32d0dSGreg Roach        if ($jd && $d1->minimumJulianDay()) {
5108fe32d0dSGreg Roach            return $jd - $d1->minimumJulianDay();
5118fe32d0dSGreg Roach        }
512*e364afe4SGreg Roach
513*e364afe4SGreg Roach        return -1;
5148fe32d0dSGreg Roach    }
5158fe32d0dSGreg Roach
5168fe32d0dSGreg Roach    /**
5178fe32d0dSGreg Roach     * Calculate the the age of a person, on a date.
5188fe32d0dSGreg Roach     *
5198fe32d0dSGreg Roach     * @param Date      $d1
5208fe32d0dSGreg Roach     * @param Date|null $d2
5218fe32d0dSGreg Roach     *
5228fe32d0dSGreg Roach     * @return string
5238fe32d0dSGreg Roach     */
5248fe32d0dSGreg Roach    public static function getAge(Date $d1, Date $d2 = null): string
5258fe32d0dSGreg Roach    {
526*e364afe4SGreg Roach        if ($d2 instanceof self) {
527f5b60decSGreg Roach            if ($d2->maximumJulianDay() >= $d1->minimumJulianDay() && $d2->minimumJulianDay() <= $d1->minimumJulianDay()) {
528a25f0a04SGreg Roach                // Overlapping dates
529f5b60decSGreg Roach                $jd = $d1->minimumJulianDay();
530a25f0a04SGreg Roach            } else {
531a25f0a04SGreg Roach                // Non-overlapping dates
532f5b60decSGreg Roach                $jd = $d2->minimumJulianDay();
533a25f0a04SGreg Roach            }
534a25f0a04SGreg Roach        } else {
535a25f0a04SGreg Roach            // If second date not specified, use today’s date
5364459dc9aSGreg Roach            $jd = Carbon::now()->julianDay();
537a25f0a04SGreg Roach        }
538a25f0a04SGreg Roach
5395fec6c0aSGreg Roach        // Just years, in local digits, with warning for negative/
540f5b60decSGreg Roach        if ($jd && $d1->minimumJulianDay()) {
541f5b60decSGreg Roach            if ($d1->minimumJulianDay() > $jd) {
542e39fd5c6SGreg Roach                return view('icons/warning');
543*e364afe4SGreg Roach            }
544*e364afe4SGreg Roach
5458fe32d0dSGreg Roach            $years = $d1->minimumDate()->getAge($jd);
54655664801SGreg Roach
54755664801SGreg Roach            return I18N::number($years);
548a25f0a04SGreg Roach        }
549*e364afe4SGreg Roach
55066b90994SGreg Roach        return '';
551a25f0a04SGreg Roach    }
552a25f0a04SGreg Roach
553a25f0a04SGreg Roach    /**
554a25f0a04SGreg Roach     * Calculate the years/months/days between two events
555a25f0a04SGreg Roach     * Return a gedcom style age string: "1y 2m 3d" (for fact details)
556a25f0a04SGreg Roach     *
557a25f0a04SGreg Roach     * @param Date      $d1
558a25f0a04SGreg Roach     * @param Date|null $d2
559a25f0a04SGreg Roach     *
560a25f0a04SGreg Roach     * @return string
561a25f0a04SGreg Roach     */
562*e364afe4SGreg Roach    public static function getAgeGedcom(Date $d1, Date $d2 = null): string
563c1010edaSGreg Roach    {
5648f038c36SRico Sonntag        if ($d2 === null) {
5654459dc9aSGreg Roach            return $d1->date1->getAgeFull(Carbon::now()->julianDay());
5664e8e93daSGreg Roach        }
5674e8e93daSGreg Roach
5684e8e93daSGreg Roach        if (self::compare($d1, $d2) !== 0) {
5698fe32d0dSGreg Roach            return $d1->date1->getAgeFull($d2->minimumJulianDay());
5704e8e93daSGreg Roach        }
5714e8e93daSGreg Roach
5724e8e93daSGreg Roach        if ($d1->minimumJulianDay() == $d2->minimumJulianDay()) {
573a25f0a04SGreg Roach            return '0d';
5744e8e93daSGreg Roach        }
5754e8e93daSGreg Roach
576a25f0a04SGreg Roach        return '';
577a25f0a04SGreg Roach    }
578a25f0a04SGreg Roach
579a25f0a04SGreg Roach    /**
580a25f0a04SGreg Roach     * Compare two dates, so they can be sorted.
581a25f0a04SGreg Roach     *
582a25f0a04SGreg Roach     * return <0 if $a<$b
583a25f0a04SGreg Roach     * return >0 if $b>$a
584a25f0a04SGreg Roach     * return  0 if dates same/overlap
585a25f0a04SGreg Roach     * BEF/AFT sort as the day before/after
586a25f0a04SGreg Roach     *
587a25f0a04SGreg Roach     * @param Date $a
588a25f0a04SGreg Roach     * @param Date $b
589a25f0a04SGreg Roach     *
590cbc1590aSGreg Roach     * @return int
591a25f0a04SGreg Roach     */
592*e364afe4SGreg Roach    public static function compare(Date $a, Date $b): int
593c1010edaSGreg Roach    {
594a25f0a04SGreg Roach        // Get min/max JD for each date.
595a25f0a04SGreg Roach        switch ($a->qual1) {
596a25f0a04SGreg Roach            case 'BEF':
597f5b60decSGreg Roach                $amin = $a->minimumJulianDay() - 1;
598a25f0a04SGreg Roach                $amax = $amin;
599a25f0a04SGreg Roach                break;
600a25f0a04SGreg Roach            case 'AFT':
601f5b60decSGreg Roach                $amax = $a->maximumJulianDay() + 1;
602a25f0a04SGreg Roach                $amin = $amax;
603a25f0a04SGreg Roach                break;
604a25f0a04SGreg Roach            default:
605f5b60decSGreg Roach                $amin = $a->minimumJulianDay();
606f5b60decSGreg Roach                $amax = $a->maximumJulianDay();
607a25f0a04SGreg Roach                break;
608a25f0a04SGreg Roach        }
609a25f0a04SGreg Roach        switch ($b->qual1) {
610a25f0a04SGreg Roach            case 'BEF':
611f5b60decSGreg Roach                $bmin = $b->minimumJulianDay() - 1;
612a25f0a04SGreg Roach                $bmax = $bmin;
613a25f0a04SGreg Roach                break;
614a25f0a04SGreg Roach            case 'AFT':
615f5b60decSGreg Roach                $bmax = $b->maximumJulianDay() + 1;
616a25f0a04SGreg Roach                $bmin = $bmax;
617a25f0a04SGreg Roach                break;
618a25f0a04SGreg Roach            default:
619f5b60decSGreg Roach                $bmin = $b->minimumJulianDay();
620f5b60decSGreg Roach                $bmax = $b->maximumJulianDay();
621a25f0a04SGreg Roach                break;
622a25f0a04SGreg Roach        }
623a25f0a04SGreg Roach        if ($amax < $bmin) {
624a25f0a04SGreg Roach            return -1;
625a25f0a04SGreg Roach        }
626b2ce94c6SRico Sonntag
627b2ce94c6SRico Sonntag        if ($amin > $bmax && $bmax > 0) {
628b2ce94c6SRico Sonntag            return 1;
629b2ce94c6SRico Sonntag        }
630b2ce94c6SRico Sonntag
631b2ce94c6SRico Sonntag        if ($amin < $bmin && $amax <= $bmax) {
632b2ce94c6SRico Sonntag            return -1;
633b2ce94c6SRico Sonntag        }
634b2ce94c6SRico Sonntag
635b2ce94c6SRico Sonntag        if ($amin > $bmin && $amax >= $bmax && $bmax > 0) {
636b2ce94c6SRico Sonntag            return 1;
637b2ce94c6SRico Sonntag        }
638b2ce94c6SRico Sonntag
639b2ce94c6SRico Sonntag        return 0;
640a25f0a04SGreg Roach    }
641a25f0a04SGreg Roach
642a25f0a04SGreg Roach    /**
643a25f0a04SGreg Roach     * Check whether a gedcom date contains usable calendar date(s).
644a25f0a04SGreg Roach     *
645a25f0a04SGreg Roach     * An incomplete date such as "12 AUG" would be invalid, as
646a25f0a04SGreg Roach     * we cannot sort it.
647a25f0a04SGreg Roach     *
648cbc1590aSGreg Roach     * @return bool
649a25f0a04SGreg Roach     */
6508f53f488SRico Sonntag    public function isOK(): bool
651c1010edaSGreg Roach    {
652f5b60decSGreg Roach        return $this->minimumJulianDay() && $this->maximumJulianDay();
653a25f0a04SGreg Roach    }
654a25f0a04SGreg Roach
655a25f0a04SGreg Roach    /**
656a25f0a04SGreg Roach     * Calculate the gregorian year for a date. This should NOT be used internally
657a25f0a04SGreg Roach     * within WT - we should keep the code "calendar neutral" to allow support for
658a25f0a04SGreg Roach     * jewish/arabic users. This is only for interfacing with external entities,
659a25f0a04SGreg Roach     * such as the ancestry.com search interface or the dated fact icons.
660a25f0a04SGreg Roach     *
661cbc1590aSGreg Roach     * @return int
662a25f0a04SGreg Roach     */
663*e364afe4SGreg Roach    public function gregorianYear(): int
664c1010edaSGreg Roach    {
665a25f0a04SGreg Roach        if ($this->isOK()) {
66659f2f229SGreg Roach            $gregorian_calendar = new GregorianCalendar();
66765e02381SGreg Roach            [$year] = $gregorian_calendar->jdToYmd($this->julianDay());
668a25f0a04SGreg Roach
669a25f0a04SGreg Roach            return $year;
670a25f0a04SGreg Roach        }
671b2ce94c6SRico Sonntag
672b2ce94c6SRico Sonntag        return 0;
673a25f0a04SGreg Roach    }
674a25f0a04SGreg Roach}
675