xref: /webtrees/app/Date.php (revision 7e96c9252136dcd930d82ed9a75fc86423075cb7)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees;
17a25f0a04SGreg Roach
18a80c4362SGreg Roachuse DomainException;
19a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\CalendarDate;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\FrenchDate;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\HijriDate;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JalaliDate;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JulianDate;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\RomanDate;
28a25f0a04SGreg Roach
29a25f0a04SGreg Roach/**
3076692c8bSGreg Roach * A representation of GEDCOM dates and date ranges.
3176692c8bSGreg Roach *
3276692c8bSGreg Roach * Since different calendars start their days at different times, (civil
33a25f0a04SGreg Roach * midnight, solar midnight, sunset, sunrise, etc.), we convert on the basis of
34a25f0a04SGreg Roach * midday.
35a25f0a04SGreg Roach *
3676692c8bSGreg Roach * We assume that years start on the first day of the first month. Where
37a25f0a04SGreg Roach * this is not the case (e.g. England prior to 1752), we need to use modified
38a25f0a04SGreg Roach * years or the OS/NS notation "4 FEB 1750/51".
39a25f0a04SGreg Roach */
40c1010edaSGreg Roachclass Date
41c1010edaSGreg Roach{
42a25f0a04SGreg Roach    /** @var string Optional qualifier, such as BEF, FROM, ABT */
43fe11e66dSGreg Roach    public $qual1 = '';
44a25f0a04SGreg Roach
45a25f0a04SGreg Roach    /** @var CalendarDate  The first (or only) date */
46f5b60decSGreg Roach    private $date1;
47a25f0a04SGreg Roach
48a25f0a04SGreg Roach    /** @var string  Optional qualifier, such as TO, AND */
49fe11e66dSGreg Roach    public $qual2 = '';
50a25f0a04SGreg Roach
51*7e96c925SGreg Roach    /** @var CalendarDate|null Optional second date */
52*7e96c925SGreg Roach    private $date2 = null;
53a25f0a04SGreg Roach
54a25f0a04SGreg Roach    /** @var string ptional text, as included with an INTerpreted date */
55*7e96c925SGreg Roach    private $text = '';
56a25f0a04SGreg Roach
57a25f0a04SGreg Roach    /**
58a25f0a04SGreg Roach     * Create a date, from GEDCOM data.
59a25f0a04SGreg Roach     *
60a25f0a04SGreg Roach     * @param string $date A date in GEDCOM format
61a25f0a04SGreg Roach     */
62cfe766afSGreg Roach    public function __construct(string $date)
63c1010edaSGreg Roach    {
64a25f0a04SGreg Roach        // Extract any explanatory text
65a25f0a04SGreg Roach        if (preg_match('/^(.*) ?[(](.*)[)]/', $date, $match)) {
66a25f0a04SGreg Roach            $date       = $match[1];
67a25f0a04SGreg Roach            $this->text = $match[2];
68a25f0a04SGreg Roach        }
69a25f0a04SGreg Roach        if (preg_match('/^(FROM|BET) (.+) (AND|TO) (.+)/', $date, $match)) {
70a25f0a04SGreg Roach            $this->qual1 = $match[1];
71a25f0a04SGreg Roach            $this->date1 = $this->parseDate($match[2]);
72a25f0a04SGreg Roach            $this->qual2 = $match[3];
73a25f0a04SGreg Roach            $this->date2 = $this->parseDate($match[4]);
743506b2e5SGreg Roach        } elseif (preg_match('/^(TO|FROM|BEF|AFT|CAL|EST|INT|ABT) (.+)/', $date, $match)) {
75a25f0a04SGreg Roach            $this->qual1 = $match[1];
76a25f0a04SGreg Roach            $this->date1 = $this->parseDate($match[2]);
77a25f0a04SGreg Roach        } else {
78a25f0a04SGreg Roach            $this->date1 = $this->parseDate($date);
79a25f0a04SGreg Roach        }
80a25f0a04SGreg Roach    }
81a25f0a04SGreg Roach
82a25f0a04SGreg Roach    /**
83a25f0a04SGreg Roach     * When we copy a date object, we need to create copies of
84a25f0a04SGreg Roach     * its child objects.
85a25f0a04SGreg Roach     */
86c1010edaSGreg Roach    public function __clone()
87c1010edaSGreg Roach    {
88a25f0a04SGreg Roach        $this->date1 = clone $this->date1;
89*7e96c925SGreg Roach        if ($this->date2 !== null) {
90a25f0a04SGreg Roach            $this->date2 = clone $this->date2;
91a25f0a04SGreg Roach        }
92a25f0a04SGreg Roach    }
93a25f0a04SGreg Roach
94a25f0a04SGreg Roach    /**
95a25f0a04SGreg Roach     * Convert a calendar date, such as "12 JUN 1943" into calendar date object.
96a25f0a04SGreg Roach     *
97a25f0a04SGreg Roach     * A GEDCOM date range may have two calendar dates.
98a25f0a04SGreg Roach     *
99a25f0a04SGreg Roach     * @param string $date
100a25f0a04SGreg Roach     *
101a80c4362SGreg Roach     * @throws DomainException
102cbc1590aSGreg Roach     *
103cbc1590aSGreg Roach     * @return CalendarDate
104a25f0a04SGreg Roach     */
105a80c4362SGreg Roach    private function parseDate($date): CalendarDate
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)) {
145a25f0a04SGreg Roach            $cal = '@#DHEBREW@';
146a25f0a04SGreg Roach        } else {
147a25f0a04SGreg Roach            if (preg_match('/^(VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP)$/', $m)) {
148a25f0a04SGreg Roach                $cal = '@#DFRENCH R@';
149a25f0a04SGreg Roach            } else {
150a25f0a04SGreg Roach                if (preg_match('/^(MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH)$/', $m)) {
151a25f0a04SGreg Roach                    $cal = '@#DHIJRI@'; // This is a WT extension
152a25f0a04SGreg Roach                } else {
153a25f0a04SGreg Roach                    if (preg_match('/^(FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN)$/', $m)) {
154a25f0a04SGreg Roach                        $cal = '@#DJALALI@'; // This is a WT extension
155a25f0a04SGreg Roach                    } elseif (preg_match('/^\d{1,4}( B\.C\.)|\d\d\d\d\/\d\d$/', $y)) {
156a25f0a04SGreg Roach                        $cal = '@#DJULIAN@';
157a25f0a04SGreg Roach                    }
158a25f0a04SGreg Roach                }
159a25f0a04SGreg Roach            }
160a25f0a04SGreg Roach        }
161a25f0a04SGreg Roach
162a25f0a04SGreg Roach        // Ambiguous dates - don't override calendar escape
163a25f0a04SGreg Roach        if ($cal == '') {
164a25f0a04SGreg Roach            if (preg_match('/^(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)$/', $m)) {
165a25f0a04SGreg Roach                $cal = '@#DGREGORIAN@';
166a25f0a04SGreg Roach            } else {
167a25f0a04SGreg Roach                if (preg_match('/^[345]\d\d\d$/', $y)) {
168a25f0a04SGreg Roach                    // Year 3000-5999
169a25f0a04SGreg Roach                    $cal = '@#DHEBREW@';
170a25f0a04SGreg Roach                } else {
171a25f0a04SGreg Roach                    $cal = '@#DGREGORIAN@';
172a25f0a04SGreg Roach                }
173a25f0a04SGreg Roach            }
174a25f0a04SGreg Roach        }
175a25f0a04SGreg Roach        // Now construct an object of the correct type
176a25f0a04SGreg Roach        switch ($cal) {
177a25f0a04SGreg Roach            case '@#DGREGORIAN@':
178c1010edaSGreg Roach                return new GregorianDate([
179c1010edaSGreg Roach                    $y,
180c1010edaSGreg Roach                    $m,
181c1010edaSGreg Roach                    $d,
182c1010edaSGreg Roach                ]);
183a25f0a04SGreg Roach            case '@#DJULIAN@':
184c1010edaSGreg Roach                return new JulianDate([
185c1010edaSGreg Roach                    $y,
186c1010edaSGreg Roach                    $m,
187c1010edaSGreg Roach                    $d,
188c1010edaSGreg Roach                ]);
189a25f0a04SGreg Roach            case '@#DHEBREW@':
190c1010edaSGreg Roach                return new JewishDate([
191c1010edaSGreg Roach                    $y,
192c1010edaSGreg Roach                    $m,
193c1010edaSGreg Roach                    $d,
194c1010edaSGreg Roach                ]);
195a25f0a04SGreg Roach            case '@#DHIJRI@':
196c1010edaSGreg Roach                return new HijriDate([
197c1010edaSGreg Roach                    $y,
198c1010edaSGreg Roach                    $m,
199c1010edaSGreg Roach                    $d,
200c1010edaSGreg Roach                ]);
201a25f0a04SGreg Roach            case '@#DFRENCH R@':
202c1010edaSGreg Roach                return new FrenchDate([
203c1010edaSGreg Roach                    $y,
204c1010edaSGreg Roach                    $m,
205c1010edaSGreg Roach                    $d,
206c1010edaSGreg Roach                ]);
207a25f0a04SGreg Roach            case '@#DJALALI@':
208c1010edaSGreg Roach                return new JalaliDate([
209c1010edaSGreg Roach                    $y,
210c1010edaSGreg Roach                    $m,
211c1010edaSGreg Roach                    $d,
212c1010edaSGreg Roach                ]);
213a25f0a04SGreg Roach            case '@#DROMAN@':
214c1010edaSGreg Roach                return new RomanDate([
215c1010edaSGreg Roach                    $y,
216c1010edaSGreg Roach                    $m,
217c1010edaSGreg Roach                    $d,
218c1010edaSGreg Roach                ]);
219a25f0a04SGreg Roach            default:
220a80c4362SGreg Roach                throw new DomainException('Invalid calendar');
221a25f0a04SGreg Roach        }
222a25f0a04SGreg Roach    }
223a25f0a04SGreg Roach
2244e6225d5SGreg Roach    /**
2254e6225d5SGreg Roach     * A list of supported calendars and their names.
2264e6225d5SGreg Roach     *
2274e6225d5SGreg Roach     * @return string[]
2284e6225d5SGreg Roach     */
2298f53f488SRico Sonntag    public static function calendarNames(): array
230c1010edaSGreg Roach    {
23113abd6f3SGreg Roach        return [
232bbb76c12SGreg Roach            /* I18N: The gregorian calendar */
233bbb76c12SGreg Roach            'gregorian' => I18N::translate('Gregorian'),
234bbb76c12SGreg Roach            /* I18N: The julian calendar */
235bbb76c12SGreg Roach            'julian'    => I18N::translate('Julian'),
236bbb76c12SGreg Roach            /* I18N: The French calendar */
237bbb76c12SGreg Roach            'french'    => I18N::translate('French'),
238bbb76c12SGreg Roach            /* I18N: The Hebrew/Jewish calendar */
239bbb76c12SGreg Roach            'jewish'    => I18N::translate('Jewish'),
240bbb76c12SGreg Roach            /* I18N: The Arabic/Hijri calendar */
241bbb76c12SGreg Roach            'hijri'     => I18N::translate('Hijri'),
242bbb76c12SGreg Roach            /* I18N: The Persian/Jalali calendar */
243bbb76c12SGreg Roach            'jalali'    => I18N::translate('Jalali'),
24413abd6f3SGreg Roach        ];
2454e6225d5SGreg Roach    }
2464e6225d5SGreg Roach
247a25f0a04SGreg Roach    /**
248a25f0a04SGreg Roach     * Convert a date to the preferred format and calendar(s) display.
249a25f0a04SGreg Roach     *
250cbc1590aSGreg Roach     * @param bool|null   $url               Wrap the date in a link to calendar.php
251a25f0a04SGreg Roach     * @param string|null $date_format       Override the default date format
252cbc1590aSGreg Roach     * @param bool|null   $convert_calendars Convert the date into other calendars
253a25f0a04SGreg Roach     *
254a25f0a04SGreg Roach     * @return string
255a25f0a04SGreg Roach     */
256c1010edaSGreg Roach    public function display($url = false, $date_format = null, $convert_calendars = true)
257c1010edaSGreg Roach    {
25812dcbc32SGreg Roach        // @TODO, This is set in index.php - but it is not safe to rely on globals.
259a80c4362SGreg Roach        // We need a new DateFormatterService class
26012dcbc32SGreg Roach        global $tree;
2615d51864bSGreg Roach
26212dcbc32SGreg Roach        $CALENDAR_FORMAT = $tree->getPreference('CALENDAR_FORMAT');
263a25f0a04SGreg Roach
264a25f0a04SGreg Roach        if ($date_format === null) {
265dfeee0a8SGreg Roach            $date_format = I18N::dateFormat();
266a25f0a04SGreg Roach        }
267a25f0a04SGreg Roach
268a25f0a04SGreg Roach        if ($convert_calendars) {
269a25f0a04SGreg Roach            $calendar_format = explode('_and_', $CALENDAR_FORMAT);
270a25f0a04SGreg Roach        } else {
27113abd6f3SGreg Roach            $calendar_format = [];
272a25f0a04SGreg Roach        }
273a25f0a04SGreg Roach
274a25f0a04SGreg Roach        // Two dates with text before, between and after
275a25f0a04SGreg Roach        $q1 = $this->qual1;
276a25f0a04SGreg Roach        $d1 = $this->date1->format($date_format, $this->qual1);
277a25f0a04SGreg Roach        $q2 = $this->qual2;
2788f038c36SRico Sonntag        if ($this->date2 === null) {
279a25f0a04SGreg Roach            $d2 = '';
280a25f0a04SGreg Roach        } else {
281a25f0a04SGreg Roach            $d2 = $this->date2->format($date_format, $this->qual2);
282a25f0a04SGreg Roach        }
283a25f0a04SGreg Roach        // Con vert to other calendars, if requested
284a25f0a04SGreg Roach        $conv1 = '';
285a25f0a04SGreg Roach        $conv2 = '';
286a25f0a04SGreg Roach        foreach ($calendar_format as $cal_fmt) {
287a25f0a04SGreg Roach            if ($cal_fmt != 'none') {
288a25f0a04SGreg Roach                $d1conv = $this->date1->convertToCalendar($cal_fmt);
289a25f0a04SGreg Roach                if ($d1conv->inValidRange()) {
290a25f0a04SGreg Roach                    $d1tmp = $d1conv->format($date_format, $this->qual1);
291a25f0a04SGreg Roach                } else {
292a25f0a04SGreg Roach                    $d1tmp = '';
293a25f0a04SGreg Roach                }
2948f038c36SRico Sonntag                if ($this->date2 === null) {
295a25f0a04SGreg Roach                    $d2conv = null;
296a25f0a04SGreg Roach                    $d2tmp  = '';
297a25f0a04SGreg Roach                } else {
298a25f0a04SGreg Roach                    $d2conv = $this->date2->convertToCalendar($cal_fmt);
299a25f0a04SGreg Roach                    if ($d2conv->inValidRange()) {
300a25f0a04SGreg Roach                        $d2tmp = $d2conv->format($date_format, $this->qual2);
301a25f0a04SGreg Roach                    } else {
302a25f0a04SGreg Roach                        $d2tmp = '';
303a25f0a04SGreg Roach                    }
304a25f0a04SGreg Roach                }
305d4f10423SGreg Roach                // If the date is different from the unconverted date, add it to the date string.
3063cfaf201SGreg Roach                if ($d1 != $d1tmp && $d1tmp !== '') {
307a25f0a04SGreg Roach                    if ($url) {
3083cfaf201SGreg Roach                        if ($CALENDAR_FORMAT !== 'none') {
309f5b8f123SGreg Roach                            $conv1 .= ' <span dir="' . I18N::direction() . '">(<a href="' . $d1conv->calendarUrl($date_format) . '" rel="nofollow">' . $d1tmp . '</a>)</span>';
310a25f0a04SGreg Roach                        } else {
311f5b8f123SGreg Roach                            $conv1 .= ' <span dir="' . I18N::direction() . '"><br><a href="' . $d1conv->calendarUrl($date_format) . '" rel="nofollow">' . $d1tmp . '</a></span>';
312a25f0a04SGreg Roach                        }
313a25f0a04SGreg Roach                    } else {
314c0af647eSGreg Roach                        $conv1 .= ' <span dir="' . I18N::direction() . '">(' . $d1tmp . ')</span>';
315a25f0a04SGreg Roach                    }
316a25f0a04SGreg Roach                }
3178f038c36SRico Sonntag                if ($this->date2 !== null && $d2 != $d2tmp && $d1tmp != '') {
318a25f0a04SGreg Roach                    if ($url) {
319f5b8f123SGreg Roach                        $conv2 .= ' <span dir="' . I18N::direction() . '">(<a href="' . $d2conv->calendarUrl($date_format) . '" rel="nofollow">' . $d2tmp . '</a>)</span>';
320a25f0a04SGreg Roach                    } else {
321c0af647eSGreg Roach                        $conv2 .= ' <span dir="' . I18N::direction() . '">(' . $d2tmp . ')</span>';
322a25f0a04SGreg Roach                    }
323a25f0a04SGreg Roach                }
324a25f0a04SGreg Roach            }
325a25f0a04SGreg Roach        }
326a25f0a04SGreg Roach
327a25f0a04SGreg Roach        // Add URLs, if requested
328a25f0a04SGreg Roach        if ($url) {
329f5b8f123SGreg Roach            $d1 = '<a href="' . $this->date1->calendarUrl($date_format) . '" rel="nofollow">' . $d1 . '</a>';
3308f038c36SRico Sonntag            if ($this->date2 !== null) {
331f5b8f123SGreg Roach                $d2 = '<a href="' . $this->date2->calendarUrl($date_format) . '" rel="nofollow">' . $d2 . '</a>';
332a25f0a04SGreg Roach            }
333a25f0a04SGreg Roach        }
334a25f0a04SGreg Roach
335a25f0a04SGreg Roach        // Localise the date
336a25f0a04SGreg Roach        switch ($q1 . $q2) {
337a25f0a04SGreg Roach            case '':
338a25f0a04SGreg Roach                $tmp = $d1 . $conv1;
339a25f0a04SGreg Roach                break;
340a25f0a04SGreg Roach            case 'ABT':
341bbb76c12SGreg Roach                /* I18N: Gedcom ABT dates */
342bbb76c12SGreg Roach                $tmp = I18N::translate('about %s', $d1 . $conv1);
343a25f0a04SGreg Roach                break;
344a25f0a04SGreg Roach            case 'CAL':
345bbb76c12SGreg Roach                /* I18N: Gedcom CAL dates */
346bbb76c12SGreg Roach                $tmp = I18N::translate('calculated %s', $d1 . $conv1);
347a25f0a04SGreg Roach                break;
348a25f0a04SGreg Roach            case 'EST':
349bbb76c12SGreg Roach                /* I18N: Gedcom EST dates */
350bbb76c12SGreg Roach                $tmp = I18N::translate('estimated %s', $d1 . $conv1);
351a25f0a04SGreg Roach                break;
352a25f0a04SGreg Roach            case 'INT':
353bbb76c12SGreg Roach                /* I18N: Gedcom INT dates */
354bbb76c12SGreg Roach                $tmp = I18N::translate('interpreted %s (%s)', $d1 . $conv1, e($this->text));
355a25f0a04SGreg Roach                break;
356a25f0a04SGreg Roach            case 'BEF':
357bbb76c12SGreg Roach                /* I18N: Gedcom BEF dates */
358bbb76c12SGreg Roach                $tmp = I18N::translate('before %s', $d1 . $conv1);
359a25f0a04SGreg Roach                break;
360a25f0a04SGreg Roach            case 'AFT':
361bbb76c12SGreg Roach                /* I18N: Gedcom AFT dates */
362bbb76c12SGreg Roach                $tmp = I18N::translate('after %s', $d1 . $conv1);
363a25f0a04SGreg Roach                break;
364a25f0a04SGreg Roach            case 'FROM':
365bbb76c12SGreg Roach                /* I18N: Gedcom FROM dates */
366bbb76c12SGreg Roach                $tmp = I18N::translate('from %s', $d1 . $conv1);
367a25f0a04SGreg Roach                break;
368a25f0a04SGreg Roach            case 'TO':
369bbb76c12SGreg Roach                /* I18N: Gedcom TO dates */
370bbb76c12SGreg Roach                $tmp = I18N::translate('to %s', $d1 . $conv1);
371a25f0a04SGreg Roach                break;
372a25f0a04SGreg Roach            case 'BETAND':
373bbb76c12SGreg Roach                /* I18N: Gedcom BET-AND dates */
374bbb76c12SGreg Roach                $tmp = I18N::translate('between %s and %s', $d1 . $conv1, $d2 . $conv2);
375a25f0a04SGreg Roach                break;
376a25f0a04SGreg Roach            case 'FROMTO':
377bbb76c12SGreg Roach                /* I18N: Gedcom FROM-TO dates */
378bbb76c12SGreg Roach                $tmp = I18N::translate('from %s to %s', $d1 . $conv1, $d2 . $conv2);
379a25f0a04SGreg Roach                break;
380a25f0a04SGreg Roach            default:
381a25f0a04SGreg Roach                $tmp = I18N::translate('Invalid date');
382bbb76c12SGreg Roach                break;
383a25f0a04SGreg Roach        }
384a25f0a04SGreg Roach
3855fec6c0aSGreg Roach        if (strip_tags($tmp) === '') {
3865fec6c0aSGreg Roach            return '';
387a25f0a04SGreg Roach        } else {
3885fec6c0aSGreg Roach            return '<span class="date">' . $tmp . '</span>';
389a25f0a04SGreg Roach        }
390a25f0a04SGreg Roach    }
391a25f0a04SGreg Roach
392a25f0a04SGreg Roach    /**
393a25f0a04SGreg Roach     * Get the earliest calendar date from this GEDCOM date.
394a25f0a04SGreg Roach     *
395a25f0a04SGreg Roach     * In the date “FROM 1900 TO 1910”, this would be 1900.
396a25f0a04SGreg Roach     *
397a25f0a04SGreg Roach     * @return CalendarDate
398a25f0a04SGreg Roach     */
3998f53f488SRico Sonntag    public function minimumDate(): CalendarDate
400c1010edaSGreg Roach    {
401a25f0a04SGreg Roach        return $this->date1;
402a25f0a04SGreg Roach    }
403a25f0a04SGreg Roach
404a25f0a04SGreg Roach    /**
405a25f0a04SGreg Roach     * Get the latest calendar date from this GEDCOM date.
406a25f0a04SGreg Roach     *
407a25f0a04SGreg Roach     * In the date “FROM 1900 TO 1910”, this would be 1910.
408a25f0a04SGreg Roach     *
409a25f0a04SGreg Roach     * @return CalendarDate
410a25f0a04SGreg Roach     */
411c1010edaSGreg Roach    public function maximumDate()
412c1010edaSGreg Roach    {
4138f038c36SRico Sonntag        if ($this->date2 === null) {
414a25f0a04SGreg Roach            return $this->date1;
415a25f0a04SGreg Roach        } else {
416a25f0a04SGreg Roach            return $this->date2;
417a25f0a04SGreg Roach        }
418a25f0a04SGreg Roach    }
419a25f0a04SGreg Roach
420a25f0a04SGreg Roach    /**
421a25f0a04SGreg Roach     * Get the earliest Julian day number from this GEDCOM date.
422a25f0a04SGreg Roach     *
423cbc1590aSGreg Roach     * @return int
424a25f0a04SGreg Roach     */
4258f53f488SRico Sonntag    public function minimumJulianDay(): int
426c1010edaSGreg Roach    {
427f5b60decSGreg Roach        return $this->minimumDate()->minJD;
428a25f0a04SGreg Roach    }
429a25f0a04SGreg Roach
430a25f0a04SGreg Roach    /**
431a25f0a04SGreg Roach     * Get the latest Julian day number from this GEDCOM date.
432a25f0a04SGreg Roach     *
433cbc1590aSGreg Roach     * @return int
434a25f0a04SGreg Roach     */
4358f53f488SRico Sonntag    public function maximumJulianDay(): int
436c1010edaSGreg Roach    {
437f5b60decSGreg Roach        return $this->maximumDate()->maxJD;
438a25f0a04SGreg Roach    }
439a25f0a04SGreg Roach
440a25f0a04SGreg Roach    /**
441a25f0a04SGreg Roach     * Get the middle Julian day number from the GEDCOM date.
442a25f0a04SGreg Roach     *
443f5b60decSGreg Roach     * For a month-only date, this would be somewhere around the 16th day.
444a25f0a04SGreg Roach     * For a year-only date, this would be somewhere around 1st July.
445a25f0a04SGreg Roach     *
446cbc1590aSGreg Roach     * @return int
447a25f0a04SGreg Roach     */
4488f53f488SRico Sonntag    public function julianDay(): int
449c1010edaSGreg Roach    {
450f5b60decSGreg Roach        return (int)(($this->minimumJulianDay() + $this->maximumJulianDay()) / 2);
451a25f0a04SGreg Roach    }
452a25f0a04SGreg Roach
453a25f0a04SGreg Roach    /**
454a25f0a04SGreg Roach     * Offset this date by N years, and round to the whole year.
455a25f0a04SGreg Roach     *
456a25f0a04SGreg Roach     * This is typically used to create an estimated death date,
457a25f0a04SGreg Roach     * which is before a certain number of years after the birth date.
458a25f0a04SGreg Roach     *
459cbc1590aSGreg Roach     * @param int    $years     a number of years, positive or negative
460cbc1590aSGreg Roach     * @param string $qualifier typically “BEF” or “AFT”
461a25f0a04SGreg Roach     *
462a25f0a04SGreg Roach     * @return Date
463a25f0a04SGreg Roach     */
4648f53f488SRico Sonntag    public function addYears(int $years, string $qualifier = ''): Date
465c1010edaSGreg Roach    {
466a25f0a04SGreg Roach        $tmp           = clone $this;
467a25f0a04SGreg Roach        $tmp->date1->y += $years;
468a25f0a04SGreg Roach        $tmp->date1->m = 0;
469a25f0a04SGreg Roach        $tmp->date1->d = 0;
470a25f0a04SGreg Roach        $tmp->date1->setJdFromYmd();
471a25f0a04SGreg Roach        $tmp->qual1 = $qualifier;
472a25f0a04SGreg Roach        $tmp->qual2 = '';
473a25f0a04SGreg Roach        $tmp->date2 = null;
474a25f0a04SGreg Roach
475a25f0a04SGreg Roach        return $tmp;
476a25f0a04SGreg Roach    }
477a25f0a04SGreg Roach
478a25f0a04SGreg Roach    /**
4798fe32d0dSGreg Roach     * Calculate the the age of a person (n years), on a given date.
480a25f0a04SGreg Roach     *
481a25f0a04SGreg Roach     * @param Date $d1
482a25f0a04SGreg Roach     * @param Date $d2
483cbc1590aSGreg Roach     *
4848fe32d0dSGreg Roach     * @return int
485a25f0a04SGreg Roach     */
4868fe32d0dSGreg Roach    public static function getAgeYears(Date $d1, Date $d2): int
487c1010edaSGreg Roach    {
4888fe32d0dSGreg Roach        if ($d2->maximumJulianDay() >= $d1->minimumJulianDay() && $d2->minimumJulianDay() <= $d1->minimumJulianDay()) {
4898fe32d0dSGreg Roach            // Overlapping dates
4908fe32d0dSGreg Roach            $jd = $d1->minimumJulianDay();
4918fe32d0dSGreg Roach        } else {
4928fe32d0dSGreg Roach            // Non-overlapping dates
4938fe32d0dSGreg Roach            $jd = $d2->minimumJulianDay();
4948fe32d0dSGreg Roach        }
4958fe32d0dSGreg Roach
4968fe32d0dSGreg Roach        if ($jd && $d1->minimumJulianDay() && $d1->minimumJulianDay() <= $jd) {
4978fe32d0dSGreg Roach            return $d1->minimumDate()->getAge($jd);
4988fe32d0dSGreg Roach        } else {
4998fe32d0dSGreg Roach            return -1;
5008fe32d0dSGreg Roach        }
5018fe32d0dSGreg Roach    }
5028fe32d0dSGreg Roach
5038fe32d0dSGreg Roach    /**
5048fe32d0dSGreg Roach     * Calculate the the age of a person (n days), on a given date.
5058fe32d0dSGreg Roach     *
5068fe32d0dSGreg Roach     * @param Date $d1
5078fe32d0dSGreg Roach     * @param Date $d2
5088fe32d0dSGreg Roach     *
5098fe32d0dSGreg Roach     * @return int
5108fe32d0dSGreg Roach     */
5118fe32d0dSGreg Roach    public static function getAgeDays(Date $d1, Date $d2): int
5128fe32d0dSGreg Roach    {
5138fe32d0dSGreg Roach        if ($d2->maximumJulianDay() >= $d1->minimumJulianDay() && $d2->minimumJulianDay() <= $d1->minimumJulianDay()) {
5148fe32d0dSGreg Roach            // Overlapping dates
5158fe32d0dSGreg Roach            $jd = $d1->minimumJulianDay();
5168fe32d0dSGreg Roach        } else {
5178fe32d0dSGreg Roach            // Non-overlapping dates
5188fe32d0dSGreg Roach            $jd = $d2->minimumJulianDay();
5198fe32d0dSGreg Roach        }
5208fe32d0dSGreg Roach
5218fe32d0dSGreg Roach        // Days - integer only (for sorting, rather than for display)
5228fe32d0dSGreg Roach        if ($jd && $d1->minimumJulianDay()) {
5238fe32d0dSGreg Roach            return $jd - $d1->minimumJulianDay();
5248fe32d0dSGreg Roach        } else {
5258fe32d0dSGreg Roach            return -1;
5268fe32d0dSGreg Roach        }
5278fe32d0dSGreg Roach    }
5288fe32d0dSGreg Roach
5298fe32d0dSGreg Roach    /**
5308fe32d0dSGreg Roach     * Calculate the the age of a person, on a date.
5318fe32d0dSGreg Roach     *
5328fe32d0dSGreg Roach     * @param Date      $d1
5338fe32d0dSGreg Roach     * @param Date|null $d2
5348fe32d0dSGreg Roach     *
5358fe32d0dSGreg Roach     * @return string
5368fe32d0dSGreg Roach     */
5378fe32d0dSGreg Roach    public static function getAge(Date $d1, Date $d2 = null): string
5388fe32d0dSGreg Roach    {
5398fe32d0dSGreg Roach        if ($d2 !== null) {
540f5b60decSGreg Roach            if ($d2->maximumJulianDay() >= $d1->minimumJulianDay() && $d2->minimumJulianDay() <= $d1->minimumJulianDay()) {
541a25f0a04SGreg Roach                // Overlapping dates
542f5b60decSGreg Roach                $jd = $d1->minimumJulianDay();
543a25f0a04SGreg Roach            } else {
544a25f0a04SGreg Roach                // Non-overlapping dates
545f5b60decSGreg Roach                $jd = $d2->minimumJulianDay();
546a25f0a04SGreg Roach            }
547a25f0a04SGreg Roach        } else {
548a25f0a04SGreg Roach            // If second date not specified, use today’s date
549a25f0a04SGreg Roach            $jd = WT_CLIENT_JD;
550a25f0a04SGreg Roach        }
551a25f0a04SGreg Roach
5525fec6c0aSGreg Roach        // Just years, in local digits, with warning for negative/
553f5b60decSGreg Roach        if ($jd && $d1->minimumJulianDay()) {
554f5b60decSGreg Roach            if ($d1->minimumJulianDay() > $jd) {
555a25f0a04SGreg Roach                return '<i class="icon-warning"></i>';
556a25f0a04SGreg Roach            } else {
5578fe32d0dSGreg Roach                $years = $d1->minimumDate()->getAge($jd);
55855664801SGreg Roach
55955664801SGreg Roach                return I18N::number($years);
560a25f0a04SGreg Roach            }
561a25f0a04SGreg Roach        } else {
56266b90994SGreg Roach            return '';
563a25f0a04SGreg Roach        }
564a25f0a04SGreg Roach    }
565a25f0a04SGreg Roach
566a25f0a04SGreg Roach    /**
567a25f0a04SGreg Roach     * Calculate the years/months/days between two events
568a25f0a04SGreg Roach     * Return a gedcom style age string: "1y 2m 3d" (for fact details)
569a25f0a04SGreg Roach     *
570a25f0a04SGreg Roach     * @param Date      $d1
571a25f0a04SGreg Roach     * @param Date|null $d2
572a25f0a04SGreg Roach     *
573a25f0a04SGreg Roach     * @return string
574a25f0a04SGreg Roach     */
575c1010edaSGreg Roach    public static function getAgeGedcom(Date $d1, Date $d2 = null)
576c1010edaSGreg Roach    {
5778f038c36SRico Sonntag        if ($d2 === null) {
5788fe32d0dSGreg Roach            return $d1->date1->getAgeFull(WT_CLIENT_JD);
579a25f0a04SGreg Roach        } else {
580a25f0a04SGreg Roach            // If dates overlap, then can’t calculate age.
581f5b60decSGreg Roach            if (self::compare($d1, $d2)) {
5828fe32d0dSGreg Roach                return $d1->date1->getAgeFull($d2->minimumJulianDay());
583b851b295SGreg Roach            } elseif (self::compare($d1, $d2) == 0 && $d1->minimumJulianDay() == $d2->minimumJulianDay()) {
584a25f0a04SGreg Roach                return '0d';
585a25f0a04SGreg Roach            } else {
586a25f0a04SGreg Roach                return '';
587a25f0a04SGreg Roach            }
588a25f0a04SGreg Roach        }
589a25f0a04SGreg Roach    }
590a25f0a04SGreg Roach
591a25f0a04SGreg Roach    /**
592a25f0a04SGreg Roach     * Compare two dates, so they can be sorted.
593a25f0a04SGreg Roach     *
594a25f0a04SGreg Roach     * return <0 if $a<$b
595a25f0a04SGreg Roach     * return >0 if $b>$a
596a25f0a04SGreg Roach     * return  0 if dates same/overlap
597a25f0a04SGreg Roach     * BEF/AFT sort as the day before/after
598a25f0a04SGreg Roach     *
599a25f0a04SGreg Roach     * @param Date $a
600a25f0a04SGreg Roach     * @param Date $b
601a25f0a04SGreg Roach     *
602cbc1590aSGreg Roach     * @return int
603a25f0a04SGreg Roach     */
604c1010edaSGreg Roach    public static function compare(Date $a, Date $b)
605c1010edaSGreg Roach    {
606a25f0a04SGreg Roach        // Get min/max JD for each date.
607a25f0a04SGreg Roach        switch ($a->qual1) {
608a25f0a04SGreg Roach            case 'BEF':
609f5b60decSGreg Roach                $amin = $a->minimumJulianDay() - 1;
610a25f0a04SGreg Roach                $amax = $amin;
611a25f0a04SGreg Roach                break;
612a25f0a04SGreg Roach            case 'AFT':
613f5b60decSGreg Roach                $amax = $a->maximumJulianDay() + 1;
614a25f0a04SGreg Roach                $amin = $amax;
615a25f0a04SGreg Roach                break;
616a25f0a04SGreg Roach            default:
617f5b60decSGreg Roach                $amin = $a->minimumJulianDay();
618f5b60decSGreg Roach                $amax = $a->maximumJulianDay();
619a25f0a04SGreg Roach                break;
620a25f0a04SGreg Roach        }
621a25f0a04SGreg Roach        switch ($b->qual1) {
622a25f0a04SGreg Roach            case 'BEF':
623f5b60decSGreg Roach                $bmin = $b->minimumJulianDay() - 1;
624a25f0a04SGreg Roach                $bmax = $bmin;
625a25f0a04SGreg Roach                break;
626a25f0a04SGreg Roach            case 'AFT':
627f5b60decSGreg Roach                $bmax = $b->maximumJulianDay() + 1;
628a25f0a04SGreg Roach                $bmin = $bmax;
629a25f0a04SGreg Roach                break;
630a25f0a04SGreg Roach            default:
631f5b60decSGreg Roach                $bmin = $b->minimumJulianDay();
632f5b60decSGreg Roach                $bmax = $b->maximumJulianDay();
633a25f0a04SGreg Roach                break;
634a25f0a04SGreg Roach        }
635a25f0a04SGreg Roach        if ($amax < $bmin) {
636a25f0a04SGreg Roach            return -1;
637a25f0a04SGreg Roach        } elseif ($amin > $bmax && $bmax > 0) {
638a25f0a04SGreg Roach            return 1;
639a25f0a04SGreg Roach        } elseif ($amin < $bmin && $amax <= $bmax) {
640a25f0a04SGreg Roach            return -1;
641a25f0a04SGreg Roach        } elseif ($amin > $bmin && $amax >= $bmax && $bmax > 0) {
642a25f0a04SGreg Roach            return 1;
643a25f0a04SGreg Roach        } else {
644a25f0a04SGreg Roach            return 0;
645a25f0a04SGreg Roach        }
646a25f0a04SGreg Roach    }
647a25f0a04SGreg Roach
648a25f0a04SGreg Roach    /**
649a25f0a04SGreg Roach     * Check whether a gedcom date contains usable calendar date(s).
650a25f0a04SGreg Roach     *
651a25f0a04SGreg Roach     * An incomplete date such as "12 AUG" would be invalid, as
652a25f0a04SGreg Roach     * we cannot sort it.
653a25f0a04SGreg Roach     *
654cbc1590aSGreg Roach     * @return bool
655a25f0a04SGreg Roach     */
6568f53f488SRico Sonntag    public function isOK(): bool
657c1010edaSGreg Roach    {
658f5b60decSGreg Roach        return $this->minimumJulianDay() && $this->maximumJulianDay();
659a25f0a04SGreg Roach    }
660a25f0a04SGreg Roach
661a25f0a04SGreg Roach    /**
662a25f0a04SGreg Roach     * Calculate the gregorian year for a date. This should NOT be used internally
663a25f0a04SGreg Roach     * within WT - we should keep the code "calendar neutral" to allow support for
664a25f0a04SGreg Roach     * jewish/arabic users. This is only for interfacing with external entities,
665a25f0a04SGreg Roach     * such as the ancestry.com search interface or the dated fact icons.
666a25f0a04SGreg Roach     *
667cbc1590aSGreg Roach     * @return int
668a25f0a04SGreg Roach     */
669c1010edaSGreg Roach    public function gregorianYear()
670c1010edaSGreg Roach    {
671a25f0a04SGreg Roach        if ($this->isOK()) {
67259f2f229SGreg Roach            $gregorian_calendar = new GregorianCalendar();
673f5b60decSGreg Roach            list($year) = $gregorian_calendar->jdToYmd($this->julianDay());
674a25f0a04SGreg Roach
675a25f0a04SGreg Roach            return $year;
676a25f0a04SGreg Roach        } else {
677a25f0a04SGreg Roach            return 0;
678a25f0a04SGreg Roach        }
679a25f0a04SGreg Roach    }
680a25f0a04SGreg Roach}
681