xref: /webtrees/app/Date.php (revision b4250c5cea6f65a6cd2b476c0a937173ca8c6b13)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use DomainException;
23use Fisharebest\ExtCalendar\GregorianCalendar;
24use Fisharebest\Webtrees\Date\AbstractCalendarDate;
25use Fisharebest\Webtrees\Date\FrenchDate;
26use Fisharebest\Webtrees\Date\GregorianDate;
27use Fisharebest\Webtrees\Date\HijriDate;
28use Fisharebest\Webtrees\Date\JalaliDate;
29use Fisharebest\Webtrees\Date\JewishDate;
30use Fisharebest\Webtrees\Date\JulianDate;
31use Fisharebest\Webtrees\Date\RomanDate;
32
33use function app;
34use function trigger_error;
35
36use const E_USER_DEPRECATED;
37
38/**
39 * A representation of GEDCOM dates and date ranges.
40 *
41 * Since different calendars start their days at different times, (civil
42 * midnight, solar midnight, sunset, sunrise, etc.), we convert on the basis of
43 * midday.
44 *
45 * We assume that years start on the first day of the first month. Where
46 * this is not the case (e.g. England prior to 1752), we need to use modified
47 * years or the OS/NS notation "4 FEB 1750/51".
48 */
49class Date
50{
51    /** @var string Optional qualifier, such as BEF, FROM, ABT */
52    public $qual1 = '';
53
54    /** @var AbstractCalendarDate  The first (or only) date */
55    private $date1;
56
57    /** @var string  Optional qualifier, such as TO, AND */
58    public $qual2 = '';
59
60    /** @var AbstractCalendarDate|null Optional second date */
61    private $date2;
62
63    /** @var string Optional text, as included with an INTerpreted date */
64    private $text = '';
65
66    /**
67     * Create a date, from GEDCOM data.
68     *
69     * @param string $date A date in GEDCOM format
70     */
71    public function __construct(string $date)
72    {
73        // Extract any explanatory text
74        if (preg_match('/^(.*) ?[(](.*)[)]/', $date, $match)) {
75            $date       = $match[1];
76            $this->text = $match[2];
77        }
78        if (preg_match('/^(FROM|BET) (.+) (AND|TO) (.+)/', $date, $match)) {
79            $this->qual1 = $match[1];
80            $this->date1 = $this->parseDate($match[2]);
81            $this->qual2 = $match[3];
82            $this->date2 = $this->parseDate($match[4]);
83        } elseif (preg_match('/^(TO|FROM|BEF|AFT|CAL|EST|INT|ABT) (.+)/', $date, $match)) {
84            $this->qual1 = $match[1];
85            $this->date1 = $this->parseDate($match[2]);
86        } else {
87            $this->date1 = $this->parseDate($date);
88        }
89    }
90
91    /**
92     * When we copy a date object, we need to create copies of
93     * its child objects.
94     */
95    public function __clone()
96    {
97        $this->date1 = clone $this->date1;
98        if ($this->date2 !== null) {
99            $this->date2 = clone $this->date2;
100        }
101    }
102
103    /**
104     * Convert a calendar date, such as "12 JUN 1943" into calendar date object.
105     * A GEDCOM date range may have two calendar dates.
106     *
107     * @param string $date
108     *
109     * @throws DomainException
110     * @return AbstractCalendarDate
111     */
112    private function parseDate($date): AbstractCalendarDate
113    {
114        // Valid calendar escape specified? - use it
115        if (preg_match('/^(@#D(?:GREGORIAN|JULIAN|HEBREW|HIJRI|JALALI|FRENCH R|ROMAN)+@) ?(.*)/', $date, $match)) {
116            $cal  = $match[1];
117            $date = $match[2];
118        } else {
119            $cal = '';
120        }
121        // A date with a month: DM, M, MY or DMY
122        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)) {
123            $d = $match[1];
124            $m = $match[2];
125            $y = $match[3];
126        } elseif (preg_match('/^(\d{1,4}(?: B\.C\.)?|\d\d\d\d\/\d\d)$/', $date, $match)) {
127            // A date with just a year
128            $d = '';
129            $m = '';
130            $y = $match[1];
131        } else {
132            // An invalid date - do the best we can.
133            $d = '';
134            $m = '';
135            $y = '';
136            // Look for a 3/4 digit year anywhere in the date
137            if (preg_match('/\b(\d{3,4})\b/', $date, $match)) {
138                $y = $match[1];
139            }
140            // Look for a month anywhere in the date
141            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)) {
142                $m = $match[1];
143                // Look for a day number anywhere in the date
144                if (preg_match('/\b(\d\d?)\b/', $date, $match)) {
145                    $d = $match[1];
146                }
147            }
148        }
149
150        // Unambiguous dates - override calendar escape
151        if (preg_match('/^(TSH|CSH|KSL|TVT|SHV|ADR|ADS|NSN|IYR|SVN|TMZ|AAV|ELL)$/', $m)) {
152            $cal = JewishDate::ESCAPE;
153        } elseif (preg_match('/^(VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP)$/', $m)) {
154            $cal = FrenchDate::ESCAPE;
155        } elseif (preg_match('/^(MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH)$/', $m)) {
156            $cal = HijriDate::ESCAPE; // This is a WT extension
157        } elseif (preg_match('/^(FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN)$/', $m)) {
158            $cal = JalaliDate::ESCAPE; // This is a WT extension
159        } elseif (preg_match('/^\d{1,4}( B\.C\.)|\d\d\d\d\/\d\d$/', $y)) {
160            $cal = JulianDate::ESCAPE;
161        }
162
163        // Ambiguous dates - don't override calendar escape
164        if ($cal === '') {
165            if (preg_match('/^(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)$/', $m)) {
166                $cal =  GregorianDate::ESCAPE;
167            } elseif (preg_match('/^[345]\d\d\d$/', $y)) {
168                // Year 3000-5999
169                $cal = JewishDate::ESCAPE;
170            } else {
171                $cal = GregorianDate::ESCAPE;
172            }
173        }
174        // Now construct an object of the correct type
175        switch ($cal) {
176            case GregorianDate::ESCAPE:
177                return new GregorianDate([
178                    $y,
179                    $m,
180                    $d,
181                ]);
182            case JulianDate::ESCAPE:
183                return new JulianDate([
184                    $y,
185                    $m,
186                    $d,
187                ]);
188            case JewishDate::ESCAPE:
189                return new JewishDate([
190                    $y,
191                    $m,
192                    $d,
193                ]);
194            case HijriDate::ESCAPE:
195                return new HijriDate([
196                    $y,
197                    $m,
198                    $d,
199                ]);
200            case FrenchDate::ESCAPE:
201                return new FrenchDate([
202                    $y,
203                    $m,
204                    $d,
205                ]);
206            case JalaliDate::ESCAPE:
207                return new JalaliDate([
208                    $y,
209                    $m,
210                    $d,
211                ]);
212            case RomanDate::ESCAPE:
213                return new RomanDate([
214                    $y,
215                    $m,
216                    $d,
217                ]);
218            default:
219                throw new DomainException('Invalid calendar');
220        }
221    }
222
223    /**
224     * A list of supported calendars and their names.
225     *
226     * @return string[]
227     */
228    public static function calendarNames(): array
229    {
230        return [
231            /* I18N: The gregorian calendar */
232            'gregorian' => I18N::translate('Gregorian'),
233            /* I18N: The julian calendar */
234            'julian'    => I18N::translate('Julian'),
235            /* I18N: The French calendar */
236            'french'    => I18N::translate('French'),
237            /* I18N: The Hebrew/Jewish calendar */
238            'jewish'    => I18N::translate('Jewish'),
239            /* I18N: The Arabic/Hijri calendar */
240            'hijri'     => I18N::translate('Hijri'),
241            /* I18N: The Persian/Jalali calendar */
242            'jalali'    => I18N::translate('Jalali'),
243        ];
244    }
245
246    /**
247     * Convert a date to the preferred format and calendar(s) display.
248     *
249     * @param bool|null   $url               Wrap the date in a link to calendar.php
250     * @param string|null $date_format       Override the default date format
251     * @param bool|null   $convert_calendars Convert the date into other calendars
252     *
253     * @return string
254     */
255    public function display($url = false, $date_format = null, $convert_calendars = true): string
256    {
257        // Do we need a new DateFormatterService class?
258        if (app()->has(Tree::class)) {
259            $tree            = app(Tree::class);
260            $CALENDAR_FORMAT = $tree->getPreference('CALENDAR_FORMAT');
261        } else {
262            $tree            = null;
263            $CALENDAR_FORMAT = '';
264        }
265
266        if ($date_format === null) {
267            $date_format = I18N::dateFormat();
268        }
269
270        if ($convert_calendars) {
271            $calendar_format = explode('_and_', $CALENDAR_FORMAT);
272        } else {
273            $calendar_format = [];
274        }
275
276        // Two dates with text before, between and after
277        $q1 = $this->qual1;
278        $d1 = $this->date1->format($date_format, $this->qual1);
279        $q2 = $this->qual2;
280        if ($this->date2 === null) {
281            $d2 = '';
282        } else {
283            $d2 = $this->date2->format($date_format, $this->qual2);
284        }
285        // Con vert to other calendars, if requested
286        $conv1 = '';
287        $conv2 = '';
288        foreach ($calendar_format as $cal_fmt) {
289            if ($cal_fmt !== 'none') {
290                $d1conv = $this->date1->convertToCalendar($cal_fmt);
291                if ($d1conv->inValidRange()) {
292                    $d1tmp = $d1conv->format($date_format, $this->qual1);
293                } else {
294                    $d1tmp = '';
295                }
296                if ($this->date2 === null) {
297                    $d2conv = null;
298                    $d2tmp  = '';
299                } else {
300                    $d2conv = $this->date2->convertToCalendar($cal_fmt);
301                    if ($d2conv->inValidRange()) {
302                        $d2tmp = $d2conv->format($date_format, $this->qual2);
303                    } else {
304                        $d2tmp = '';
305                    }
306                }
307                // If the date is different from the unconverted date, add it to the date string.
308                if ($d1 != $d1tmp && $d1tmp !== '') {
309                    if ($url) {
310                        if ($CALENDAR_FORMAT !== 'none') {
311                            $conv1 .= ' <span dir="' . I18N::direction() . '">(<a href="' . $d1conv->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d1tmp . '</a>)</span>';
312                        } else {
313                            $conv1 .= ' <span dir="' . I18N::direction() . '"><br><a href="' . $d1conv->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d1tmp . '</a></span>';
314                        }
315                    } else {
316                        $conv1 .= ' <span dir="' . I18N::direction() . '">(' . $d1tmp . ')</span>';
317                    }
318                }
319                if ($this->date2 !== null && $d2 != $d2tmp && $d1tmp != '') {
320                    if ($url) {
321                        $conv2 .= ' <span dir="' . I18N::direction() . '">(<a href="' . $d2conv->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d2tmp . '</a>)</span>';
322                    } else {
323                        $conv2 .= ' <span dir="' . I18N::direction() . '">(' . $d2tmp . ')</span>';
324                    }
325                }
326            }
327        }
328
329        // Add URLs, if requested
330        if ($url) {
331            $d1 = '<a href="' . $this->date1->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d1 . '</a>';
332            if ($this->date2 instanceof AbstractCalendarDate) {
333                $d2 = '<a href="' . $this->date2->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d2 . '</a>';
334            }
335        }
336
337        // Localise the date
338        switch ($q1 . $q2) {
339            case '':
340                $tmp = $d1 . $conv1;
341                break;
342            case 'ABT':
343                /* I18N: Gedcom ABT dates */
344                $tmp = I18N::translate('about %s', $d1 . $conv1);
345                break;
346            case 'CAL':
347                /* I18N: Gedcom CAL dates */
348                $tmp = I18N::translate('calculated %s', $d1 . $conv1);
349                break;
350            case 'EST':
351                /* I18N: Gedcom EST dates */
352                $tmp = I18N::translate('estimated %s', $d1 . $conv1);
353                break;
354            case 'INT':
355                /* I18N: Gedcom INT dates */
356                $tmp = I18N::translate('interpreted %s (%s)', $d1 . $conv1, e($this->text));
357                break;
358            case 'BEF':
359                /* I18N: Gedcom BEF dates */
360                $tmp = I18N::translate('before %s', $d1 . $conv1);
361                break;
362            case 'AFT':
363                /* I18N: Gedcom AFT dates */
364                $tmp = I18N::translate('after %s', $d1 . $conv1);
365                break;
366            case 'FROM':
367                /* I18N: Gedcom FROM dates */
368                $tmp = I18N::translate('from %s', $d1 . $conv1);
369                break;
370            case 'TO':
371                /* I18N: Gedcom TO dates */
372                $tmp = I18N::translate('to %s', $d1 . $conv1);
373                break;
374            case 'BETAND':
375                /* I18N: Gedcom BET-AND dates */
376                $tmp = I18N::translate('between %s and %s', $d1 . $conv1, $d2 . $conv2);
377                break;
378            case 'FROMTO':
379                /* I18N: Gedcom FROM-TO dates */
380                $tmp = I18N::translate('from %s to %s', $d1 . $conv1, $d2 . $conv2);
381                break;
382            default:
383                $tmp = I18N::translate('Invalid date');
384                break;
385        }
386
387        if (strip_tags($tmp) === '') {
388            return '';
389        }
390
391        return '<span class="date">' . $tmp . '</span>';
392    }
393
394    /**
395     * Get the earliest calendar date from this GEDCOM date.
396     *
397     * In the date “FROM 1900 TO 1910”, this would be 1900.
398     *
399     * @return AbstractCalendarDate
400     */
401    public function minimumDate(): AbstractCalendarDate
402    {
403        return $this->date1;
404    }
405
406    /**
407     * Get the latest calendar date from this GEDCOM date.
408     *
409     * In the date “FROM 1900 TO 1910”, this would be 1910.
410     *
411     * @return AbstractCalendarDate
412     */
413    public function maximumDate(): AbstractCalendarDate
414    {
415        return $this->date2 ?? $this->date1;
416    }
417
418    /**
419     * Get the earliest Julian day number from this GEDCOM date.
420     *
421     * @return int
422     */
423    public function minimumJulianDay(): int
424    {
425        return $this->minimumDate()->minimumJulianDay();
426    }
427
428    /**
429     * Get the latest Julian day number from this GEDCOM date.
430     *
431     * @return int
432     */
433    public function maximumJulianDay(): int
434    {
435        return $this->maximumDate()->maximumJulianDay();
436    }
437
438    /**
439     * Get the middle Julian day number from the GEDCOM date.
440     *
441     * For a month-only date, this would be somewhere around the 16th day.
442     * For a year-only date, this would be somewhere around 1st July.
443     *
444     * @return int
445     */
446    public function julianDay(): int
447    {
448        return intdiv($this->minimumJulianDay() + $this->maximumJulianDay(), 2);
449    }
450
451    /**
452     * Offset this date by N years, and round to the whole year.
453     *
454     * This is typically used to create an estimated death date,
455     * which is before a certain number of years after the birth date.
456     *
457     * @param int    $years     a number of years, positive or negative
458     * @param string $qualifier typically “BEF” or “AFT”
459     *
460     * @return Date
461     */
462    public function addYears(int $years, string $qualifier = ''): Date
463    {
464        $tmp               = clone $this;
465        $tmp->date1->year  += $years;
466        $tmp->date1->month = 0;
467        $tmp->date1->day   = 0;
468        $tmp->date1->setJdFromYmd();
469        $tmp->qual1 = $qualifier;
470        $tmp->qual2 = '';
471        $tmp->date2 = null;
472
473        return $tmp;
474    }
475
476    /**
477     * Calculate the the age of a person (n years), on a given date.
478     *
479     * @param Date $d1
480     * @param Date $d2
481     *
482     * @return int
483     *
484     * @deprecated since 2.0.4.  Will be removed in 2.1.0
485     */
486    public static function getAgeYears(Date $d1, Date $d2): int
487    {
488        trigger_error('Date::getAgeYears() is deprecated. Use class Age instead.', E_USER_DEPRECATED);
489
490        if (self::compare($d1, $d2) === 0) {
491            // Overlapping dates
492            $jd = $d1->minimumJulianDay();
493        } else {
494            // Non-overlapping dates
495            $jd = $d2->minimumJulianDay();
496        }
497
498        if ($jd && $d1->minimumJulianDay() && $d1->minimumJulianDay() <= $jd) {
499            return $d1->minimumDate()->getAge($jd);
500        }
501
502        return -1;
503    }
504
505    /**
506     * Calculate the the age of a person (n days), on a given date.
507     *
508     * @param Date $d1
509     * @param Date $d2
510     *
511     * @return int
512     *
513     * @deprecated since 2.0.4.  Will be removed in 2.1.0
514     */
515    public static function getAgeDays(Date $d1, Date $d2): int
516    {
517        trigger_error('Date::getAgeDays() is deprecated. Use class Age instead.', E_USER_DEPRECATED);
518
519        if ($d2->maximumJulianDay() >= $d1->minimumJulianDay() && $d2->minimumJulianDay() <= $d1->maximumJulianDay()) {
520            // Overlapping dates
521            $jd = $d1->minimumJulianDay();
522        } else {
523            // Non-overlapping dates
524            $jd = $d2->minimumJulianDay();
525        }
526
527        // Days - integer only (for sorting, rather than for display)
528        if ($jd && $d1->minimumJulianDay()) {
529            return $jd - $d1->minimumJulianDay();
530        }
531
532        return -1;
533    }
534
535    /**
536     * Calculate the the age of a person, on a date.
537     *
538     * @param Date      $d1
539     * @param Date|null $d2
540     *
541     * @return string
542     *
543     * @deprecated since 2.0.4.  Will be removed in 2.1.0
544     */
545    public static function getAge(Date $d1, Date $d2 = null): string
546    {
547        trigger_error('Date::getAge() is deprecated. Use class Age instead.', E_USER_DEPRECATED);
548
549        if ($d2 instanceof self) {
550            if ($d2->maximumJulianDay() >= $d1->minimumJulianDay() && $d2->minimumJulianDay() <= $d1->maximumJulianDay()) {
551                // Overlapping dates
552                $jd = $d1->minimumJulianDay();
553            } else {
554                // Non-overlapping dates
555                $jd = $d2->minimumJulianDay();
556            }
557        } else {
558            // If second date not specified, use today’s date
559            $jd = Carbon::now()->julianDay();
560        }
561
562        // Just years, in local digits, with warning for negative/
563        if ($jd && $d1->minimumJulianDay()) {
564            if ($d1->minimumJulianDay() > $jd) {
565                return view('icons/warning');
566            }
567
568            $years = $d1->minimumDate()->getAge($jd);
569
570            return I18N::number($years);
571        }
572
573        return '';
574    }
575
576    /**
577     * Calculate the years/months/days between two events
578     * Return a gedcom style age string: "1y 2m 3d" (for fact details)
579     *
580     * @param Date      $d1
581     * @param Date|null $d2
582     *
583     * @return string
584     *
585     * @deprecated since 2.0.4.  Will be removed in 2.1.0
586     */
587    public static function getAgeGedcom(Date $d1, Date $d2 = null): string
588    {
589        trigger_error('Date::getAgeGedcom() is deprecated. Use class Age instead.', E_USER_DEPRECATED);
590
591        if ($d2 === null) {
592            return $d1->date1->getAgeFull(Carbon::now()->julianDay());
593        }
594
595        if (self::compare($d1, $d2) !== 0) {
596            return $d1->date1->getAgeFull($d2->minimumJulianDay());
597        }
598
599        if ($d1->minimumJulianDay() == $d2->minimumJulianDay()) {
600            return '0d';
601        }
602
603        return '';
604    }
605
606    /**
607     * Compare two dates, so they can be sorted.
608     *
609     * return -1 if $a<$b
610     * return +1 if $b>$a
611     * return  0 if dates same/overlap
612     * BEF/AFT sort as the day before/after
613     *
614     * @param Date $a
615     * @param Date $b
616     *
617     * @return int
618     */
619    public static function compare(Date $a, Date $b): int
620    {
621        // Get min/max JD for each date.
622        switch ($a->qual1) {
623            case 'BEF':
624                $amin = $a->minimumJulianDay() - 1;
625                $amax = $amin;
626                break;
627            case 'AFT':
628                $amax = $a->maximumJulianDay() + 1;
629                $amin = $amax;
630                break;
631            default:
632                $amin = $a->minimumJulianDay();
633                $amax = $a->maximumJulianDay();
634                break;
635        }
636        switch ($b->qual1) {
637            case 'BEF':
638                $bmin = $b->minimumJulianDay() - 1;
639                $bmax = $bmin;
640                break;
641            case 'AFT':
642                $bmax = $b->maximumJulianDay() + 1;
643                $bmin = $bmax;
644                break;
645            default:
646                $bmin = $b->minimumJulianDay();
647                $bmax = $b->maximumJulianDay();
648                break;
649        }
650        if ($amax < $bmin) {
651            return -1;
652        }
653
654        if ($amin > $bmax && $bmax > 0) {
655            return 1;
656        }
657
658        if ($amin < $bmin && $amax <= $bmax) {
659            return -1;
660        }
661
662        if ($amin > $bmin && $amax >= $bmax && $bmax > 0) {
663            return 1;
664        }
665
666        return 0;
667    }
668
669    /**
670     * Check whether a gedcom date contains usable calendar date(s).
671     *
672     * An incomplete date such as "12 AUG" would be invalid, as
673     * we cannot sort it.
674     *
675     * @return bool
676     */
677    public function isOK(): bool
678    {
679        return $this->minimumJulianDay() && $this->maximumJulianDay();
680    }
681
682    /**
683     * Calculate the gregorian year for a date. This should NOT be used internally
684     * within WT - we should keep the code "calendar neutral" to allow support for
685     * jewish/arabic users. This is only for interfacing with external entities,
686     * such as the ancestry.com search interface or the dated fact icons.
687     *
688     * @return int
689     */
690    public function gregorianYear(): int
691    {
692        if ($this->isOK()) {
693            $gregorian_calendar = new GregorianCalendar();
694            [$year] = $gregorian_calendar->jdToYmd($this->julianDay());
695
696            return $year;
697        }
698
699        return 0;
700    }
701}
702