xref: /webtrees/app/Date/AbstractCalendarDate.php (revision dec352c1d7404cdd35c9b1a1b5d97f29e7c4ebb5)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Date;
21
22use Fisharebest\ExtCalendar\CalendarInterface;
23use Fisharebest\ExtCalendar\JewishCalendar;
24use Fisharebest\Webtrees\Carbon;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Tree;
27use InvalidArgumentException;
28
29use function array_key_exists;
30use function array_search;
31use function get_class;
32use function intdiv;
33use function is_array;
34use function is_int;
35use function max;
36use function preg_match;
37use function preg_replace;
38use function route;
39use function sprintf;
40use function str_replace;
41use function strpbrk;
42use function strtr;
43use function trigger_error;
44use function trim;
45use function view;
46
47use const E_USER_DEPRECATED;
48
49/**
50 * Classes for Gedcom Date/Calendar functionality.
51 *
52 * CalendarDate is a base class for classes such as GregorianDate, etc.
53 * + All supported calendars have non-zero days/months/years.
54 * + We store dates as both Y/M/D and Julian Days.
55 * + For imprecise dates such as "JAN 2000" we store the start/end julian day.
56 *
57 * NOTE: Since different calendars start their days at different times, (civil
58 * midnight, solar midnight, sunset, sunrise, etc.), we convert on the basis of
59 * midday.
60 */
61abstract class AbstractCalendarDate
62{
63    // GEDCOM calendar escape
64    public const ESCAPE = '@#DUNKNOWN@';
65
66    // Convert GEDCOM month names to month numbers.
67    protected const MONTH_ABBREVIATIONS = [];
68
69    /** @var CalendarInterface The calendar system used to represent this date */
70    protected $calendar;
71
72    /** @var int Year number */
73    public $year;
74
75    /** @var int Month number */
76    public $month;
77
78    /** @var int Day number */
79    public $day;
80
81    /** @var int Earliest Julian day number (start of month/year for imprecise dates) */
82    private $minimum_julian_day;
83
84    /** @var int Latest Julian day number (end of month/year for imprecise dates) */
85    private $maximum_julian_day;
86
87    /**
88     * Create a date from either:
89     * a Julian day number
90     * day/month/year strings from a GEDCOM date
91     * another CalendarDate object
92     *
93     * @param array<string>|int|AbstractCalendarDate $date
94     */
95    protected function __construct($date)
96    {
97        // Construct from an integer (a julian day number)
98        if (is_int($date)) {
99            $this->minimum_julian_day = $date;
100            $this->maximum_julian_day = $date;
101            [$this->year, $this->month, $this->day] = $this->calendar->jdToYmd($date);
102
103            return;
104        }
105
106        // Construct from an array (of three gedcom-style strings: "1900", "FEB", "4")
107        if (is_array($date)) {
108            $this->day = (int) $date[2];
109            if (array_key_exists($date[1], static::MONTH_ABBREVIATIONS)) {
110                $this->month = static::MONTH_ABBREVIATIONS[$date[1]];
111            } else {
112                $this->month = 0;
113                $this->day   = 0;
114            }
115            $this->year = $this->extractYear($date[0]);
116
117            // Our simple lookup table above does not take into account Adar and leap-years.
118            if ($this->month === 6 && $this->calendar instanceof JewishCalendar && !$this->calendar->isLeapYear($this->year)) {
119                $this->month = 7;
120            }
121
122            $this->setJdFromYmd();
123
124            return;
125        }
126
127        // Contruct from a CalendarDate
128        $this->minimum_julian_day = $date->minimum_julian_day;
129        $this->maximum_julian_day = $date->maximum_julian_day;
130
131        // Construct from an equivalent xxxxDate object
132        if (get_class($this) === get_class($date)) {
133            $this->year  = $date->year;
134            $this->month = $date->month;
135            $this->day   = $date->day;
136
137            return;
138        }
139
140        // Not all dates can be converted
141        if (!$this->inValidRange()) {
142            $this->year  = 0;
143            $this->month = 0;
144            $this->day   = 0;
145
146            return;
147        }
148
149        // ...else construct an inequivalent xxxxDate object
150        if ($date->year === 0) {
151            // Incomplete date - convert on basis of anniversary in current year
152            $today = $date->calendar->jdToYmd(Carbon::now()->julianDay());
153            $jd    = $date->calendar->ymdToJd($today[0], $date->month, $date->day === 0 ? $today[2] : $date->day);
154        } else {
155            // Complete date
156            $jd = intdiv($date->maximum_julian_day + $date->minimum_julian_day, 2);
157        }
158        [$this->year, $this->month, $this->day] = $this->calendar->jdToYmd($jd);
159        // New date has same precision as original date
160        if ($date->year === 0) {
161            $this->year = 0;
162        }
163        if ($date->month === 0) {
164            $this->month = 0;
165        }
166        if ($date->day === 0) {
167            $this->day = 0;
168        }
169        $this->setJdFromYmd();
170    }
171
172    /**
173     * @return int
174     */
175    public function maximumJulianDay(): int
176    {
177        return $this->maximum_julian_day;
178    }
179
180    /**
181     * @return int
182     */
183    public function year(): int
184    {
185        return $this->year;
186    }
187
188    /**
189     * @return int
190     */
191    public function month(): int
192    {
193        return $this->month;
194    }
195
196    /**
197     * @return int
198     */
199    public function day(): int
200    {
201        return $this->day;
202    }
203
204    /**
205     * @return int
206     */
207    public function minimumJulianDay(): int
208    {
209        return $this->minimum_julian_day;
210    }
211
212    /**
213     * Is the current year a leap year?
214     *
215     * @return bool
216     */
217    public function isLeapYear(): bool
218    {
219        return $this->calendar->isLeapYear($this->year);
220    }
221
222    /**
223     * Set the object’s Julian day number from a potentially incomplete year/month/day
224     *
225     * @return void
226     */
227    public function setJdFromYmd(): void
228    {
229        if ($this->year === 0) {
230            $this->minimum_julian_day = 0;
231            $this->maximum_julian_day = 0;
232        } elseif ($this->month === 0) {
233            $this->minimum_julian_day = $this->calendar->ymdToJd($this->year, 1, 1);
234            $this->maximum_julian_day = $this->calendar->ymdToJd($this->nextYear($this->year), 1, 1) - 1;
235        } elseif ($this->day === 0) {
236            [$ny, $nm] = $this->nextMonth();
237            $this->minimum_julian_day = $this->calendar->ymdToJd($this->year, $this->month, 1);
238            $this->maximum_julian_day = $this->calendar->ymdToJd($ny, $nm, 1) - 1;
239        } else {
240            $this->minimum_julian_day = $this->calendar->ymdToJd($this->year, $this->month, $this->day);
241            $this->maximum_julian_day = $this->minimum_julian_day;
242        }
243    }
244
245    /**
246     * Full day of the week
247     *
248     * @param int $day_number
249     *
250     * @return string
251     */
252    public function dayNames(int $day_number): string
253    {
254        static $translated_day_names;
255
256        if ($translated_day_names === null) {
257            $translated_day_names = [
258                0 => I18N::translate('Monday'),
259                1 => I18N::translate('Tuesday'),
260                2 => I18N::translate('Wednesday'),
261                3 => I18N::translate('Thursday'),
262                4 => I18N::translate('Friday'),
263                5 => I18N::translate('Saturday'),
264                6 => I18N::translate('Sunday'),
265            ];
266        }
267
268        return $translated_day_names[$day_number];
269    }
270
271    /**
272     * Abbreviated day of the week
273     *
274     * @param int $day_number
275     *
276     * @return string
277     */
278    protected function dayNamesAbbreviated(int $day_number): string
279    {
280        static $translated_day_names;
281
282        if ($translated_day_names === null) {
283            $translated_day_names = [
284                /* I18N: abbreviation for Monday */
285                0 => I18N::translate('Mon'),
286                /* I18N: abbreviation for Tuesday */
287                1 => I18N::translate('Tue'),
288                /* I18N: abbreviation for Wednesday */
289                2 => I18N::translate('Wed'),
290                /* I18N: abbreviation for Thursday */
291                3 => I18N::translate('Thu'),
292                /* I18N: abbreviation for Friday */
293                4 => I18N::translate('Fri'),
294                /* I18N: abbreviation for Saturday */
295                5 => I18N::translate('Sat'),
296                /* I18N: abbreviation for Sunday */
297                6 => I18N::translate('Sun'),
298            ];
299        }
300
301        return $translated_day_names[$day_number];
302    }
303
304    /**
305     * Most years are 1 more than the previous, but not always (e.g. 1BC->1AD)
306     *
307     * @param int $year
308     *
309     * @return int
310     */
311    protected function nextYear(int $year): int
312    {
313        return $year + 1;
314    }
315
316    /**
317     * Calendars that use suffixes, etc. (e.g. “B.C.”) or OS/NS notation should redefine this.
318     *
319     * @param string $year
320     *
321     * @return int
322     */
323    protected function extractYear(string $year): int
324    {
325        return (int) $year;
326    }
327
328    /**
329     * Compare two dates, for sorting
330     *
331     * @param AbstractCalendarDate $d1
332     * @param AbstractCalendarDate $d2
333     *
334     * @return int
335     */
336    public static function compare(AbstractCalendarDate $d1, AbstractCalendarDate $d2): int
337    {
338        if ($d1->maximum_julian_day < $d2->minimum_julian_day) {
339            return -1;
340        }
341
342        if ($d2->maximum_julian_day < $d1->minimum_julian_day) {
343            return 1;
344        }
345
346        return 0;
347    }
348
349    /**
350     * Calculate the years/months/days between this date and another date.
351     * Results assume you add the days first, then the months.
352     * 4 February -> 3 July is 27 days (3 March) and 4 months.
353     * It is not 4 months (4 June) and 29 days.
354     *
355     * @param AbstractCalendarDate $date
356     *
357     * @return int[] Age in years/months/days
358     */
359    public function ageDifference(AbstractCalendarDate $date): array
360    {
361        // Incomplete dates
362        if ($this->year === 0 || $date->year === 0) {
363            return [-1, -1, -1];
364        }
365
366        // Overlapping dates
367        if (self::compare($this, $date) === 0) {
368            return [0, 0, 0];
369        }
370
371        // Perform all calculations using the calendar of the first date
372        [$year1, $month1, $day1] = $this->calendar->jdToYmd($this->minimum_julian_day);
373        [$year2, $month2, $day2] = $this->calendar->jdToYmd($date->minimum_julian_day);
374
375        $years  = $year2 - $year1;
376        $months = $month2 - $month1;
377        $days   = $day2 - $day1;
378
379        if ($days < 0) {
380            $days += $this->calendar->daysInMonth($year1, $month1);
381            $months--;
382        }
383
384        if ($months < 0) {
385            $months += $this->calendar->monthsInYear($year2);
386            $years--;
387        }
388
389        return [$years, $months, $days];
390    }
391
392    /**
393     * How long between an event and a given julian day
394     * Return result as a number of years.
395     *
396     * @param int $jd date for calculation
397     *
398     * @return int
399     *
400     * @deprecated since 2.0.4.  Will be removed in 2.1.0
401     */
402    public function getAge(int $jd): int
403    {
404        trigger_error('AbstractCalendarDate::getAge() is deprecated. Use class Age instead.', E_USER_DEPRECATED);
405
406        if ($this->year === 0 || $jd === 0) {
407            return 0;
408        }
409        if ($this->minimum_julian_day < $jd && $this->maximum_julian_day > $jd) {
410            return 0;
411        }
412        if ($this->minimum_julian_day === $jd) {
413            return 0;
414        }
415        [$y, $m, $d] = $this->calendar->jdToYmd($jd);
416        $dy = $y - $this->year;
417        $dm = $m - max($this->month, 1);
418        $dd = $d - max($this->day, 1);
419        if ($dd < 0) {
420            $dm--;
421        }
422        if ($dm < 0) {
423            $dy--;
424        }
425
426        // Not a full age? Then just the years
427        return $dy;
428    }
429
430    /**
431     * How long between an event and a given julian day
432     * Return result as a gedcom-style age string.
433     *
434     * @param int $jd date for calculation
435     *
436     * @return string
437     *
438     * @deprecated since 2.0.4.  Will be removed in 2.1.0
439     */
440    public function getAgeFull(int $jd): string
441    {
442        trigger_error('AbstractCalendarDate::getAge() is deprecated. Use class Age instead.', E_USER_DEPRECATED);
443
444        if ($this->year === 0 || $jd === 0) {
445            return '';
446        }
447        if ($this->minimum_julian_day < $jd && $this->maximum_julian_day > $jd) {
448            return '';
449        }
450        if ($this->minimum_julian_day === $jd) {
451            return '';
452        }
453        if ($jd < $this->minimum_julian_day) {
454            return view('icons/warning');
455        }
456        [$y, $m, $d] = $this->calendar->jdToYmd($jd);
457        $dy = $y - $this->year;
458        $dm = $m - max($this->month, 1);
459        $dd = $d - max($this->day, 1);
460        if ($dd < 0) {
461            $dm--;
462        }
463        if ($dm < 0) {
464            $dm += $this->calendar->monthsInYear();
465            $dy--;
466        }
467        // Age in years?
468        if ($dy > 1) {
469            return $dy . 'y';
470        }
471        $dm += $dy * $this->calendar->monthsInYear();
472        // Age in months?
473        if ($dm > 1) {
474            return $dm . 'm';
475        }
476
477        // Age in days?
478        return ($jd - $this->minimum_julian_day) . 'd';
479    }
480
481    /**
482     * Convert a date from one calendar to another.
483     *
484     * @param string $calendar
485     *
486     * @return AbstractCalendarDate
487     */
488    public function convertToCalendar(string $calendar): AbstractCalendarDate
489    {
490        switch ($calendar) {
491            case 'gregorian':
492                return new GregorianDate($this);
493            case 'julian':
494                return new JulianDate($this);
495            case 'jewish':
496                return new JewishDate($this);
497            case 'french':
498                return new FrenchDate($this);
499            case 'hijri':
500                return new HijriDate($this);
501            case 'jalali':
502                return new JalaliDate($this);
503            default:
504                return $this;
505        }
506    }
507
508    /**
509     * Is this date within the valid range of the calendar
510     *
511     * @return bool
512     */
513    public function inValidRange(): bool
514    {
515        return $this->minimum_julian_day >= $this->calendar->jdStart() && $this->maximum_julian_day <= $this->calendar->jdEnd();
516    }
517
518    /**
519     * How many months in a year
520     *
521     * @return int
522     */
523    public function monthsInYear(): int
524    {
525        return $this->calendar->monthsInYear();
526    }
527
528    /**
529     * How many days in the current month
530     *
531     * @return int
532     */
533    public function daysInMonth(): int
534    {
535        try {
536            return $this->calendar->daysInMonth($this->year, $this->month);
537        } catch (InvalidArgumentException $ex) {
538            // calendar.php calls this with "DD MMM" dates, for which we cannot calculate
539            // the length of a month. Should we validate this before calling this function?
540            return 0;
541        }
542    }
543
544    /**
545     * How many days in the current week
546     *
547     * @return int
548     */
549    public function daysInWeek(): int
550    {
551        return $this->calendar->daysInWeek();
552    }
553
554    /**
555     * Format a date, using similar codes to the PHP date() function.
556     *
557     * @param string $format    See http://php.net/date
558     * @param string $qualifier GEDCOM qualifier, so we can choose the right case for the month name.
559     *
560     * @return string
561     */
562    public function format(string $format, string $qualifier = ''): string
563    {
564        // Dates can include additional punctuation and symbols.
565        // e.g. "%Y年 %n月 %j日", "Y.M.D" and "M D, Y".
566        // The logic here is inflexible, and should be replaced with
567        // specific translations for each abbreviated format.
568
569        // Don’t show exact details for inexact dates
570        if (!$this->day) {
571            $format = preg_replace('/%[djlDNSwz][,日]?/u', '', $format);
572            $format = str_replace(['%d,', '%j日', '%j,', '%j', '%l', '%D', '%N', '%S', '%w', '%z'], '', $format);
573        }
574        if (!$this->month) {
575            $format = str_replace(['%F', '%m', '%M', '年 %n月', '%n', '%t'], '', $format);
576        }
577        if (!$this->year) {
578            $format = str_replace(['%t', '%L', '%G', '%y', '%Y年', '%Y'], '', $format);
579        }
580        // If we’ve trimmed the format, also trim the punctuation
581        if (!$this->day || !$this->month || !$this->year) {
582            $format = trim($format, ',. ;/-');
583        }
584        if ($this->day && preg_match('/%[djlDNSwz]/', $format)) {
585            // If we have a day-number *and* we are being asked to display it, then genitive
586            $case = 'GENITIVE';
587        } else {
588            switch ($qualifier) {
589                case 'TO':
590                case 'ABT':
591                case 'FROM':
592                    $case = 'GENITIVE';
593                    break;
594                case 'AFT':
595                    $case = 'LOCATIVE';
596                    break;
597                case 'BEF':
598                case 'BET':
599                case 'AND':
600                    $case = 'INSTRUMENTAL';
601                    break;
602                case '':
603                case 'INT':
604                case 'EST':
605                case 'CAL':
606                default: // There shouldn't be any other options...
607                    $case = 'NOMINATIVE';
608                    break;
609            }
610        }
611
612        return strtr($format, [
613            '%d' => $this->formatDayZeros(),
614            '%j' => $this->formatDay(),
615            '%l' => $this->formatLongWeekday(),
616            '%D' => $this->formatShortWeekday(),
617            '%N' => $this->formatIsoWeekday(),
618            '%w' => $this->formatNumericWeekday(),
619            '%z' => $this->formatDayOfYear(),
620            '%F' => $this->formatLongMonth($case),
621            '%m' => $this->formatMonthZeros(),
622            '%M' => $this->formatShortMonth(),
623            '%n' => $this->formatMonth(),
624            '%t' => (string) $this->daysInMonth(),
625            '%L' => $this->isLeapYear() ? '1' : '0',
626            '%Y' => $this->formatLongYear(),
627            '%y' => $this->formatShortYear(),
628            // These 4 extensions are useful for re-formatting gedcom dates.
629            '%@' => $this->formatGedcomCalendarEscape(),
630            '%A' => $this->formatGedcomDay(),
631            '%O' => $this->formatGedcomMonth(),
632            '%E' => $this->formatGedcomYear(),
633        ]);
634    }
635
636    /**
637     * Generate the %d format for a date.
638     *
639     * @return string
640     */
641    protected function formatDayZeros(): string
642    {
643        if ($this->day > 9) {
644            return I18N::digits($this->day);
645        }
646
647        return I18N::digits('0' . $this->day);
648    }
649
650    /**
651     * Generate the %j format for a date.
652     *
653     * @return string
654     */
655    protected function formatDay(): string
656    {
657        return I18N::digits($this->day);
658    }
659
660    /**
661     * Generate the %l format for a date.
662     *
663     * @return string
664     */
665    protected function formatLongWeekday(): string
666    {
667        return $this->dayNames($this->minimum_julian_day % $this->calendar->daysInWeek());
668    }
669
670    /**
671     * Generate the %D format for a date.
672     *
673     * @return string
674     */
675    protected function formatShortWeekday(): string
676    {
677        return $this->dayNamesAbbreviated($this->minimum_julian_day % $this->calendar->daysInWeek());
678    }
679
680    /**
681     * Generate the %N format for a date.
682     *
683     * @return string
684     */
685    protected function formatIsoWeekday(): string
686    {
687        return I18N::digits($this->minimum_julian_day % 7 + 1);
688    }
689
690    /**
691     * Generate the %w format for a date.
692     *
693     * @return string
694     */
695    protected function formatNumericWeekday(): string
696    {
697        return I18N::digits(($this->minimum_julian_day + 1) % $this->calendar->daysInWeek());
698    }
699
700    /**
701     * Generate the %z format for a date.
702     *
703     * @return string
704     */
705    protected function formatDayOfYear(): string
706    {
707        return I18N::digits($this->minimum_julian_day - $this->calendar->ymdToJd($this->year, 1, 1));
708    }
709
710    /**
711     * Generate the %n format for a date.
712     *
713     * @return string
714     */
715    protected function formatMonth(): string
716    {
717        return I18N::digits($this->month);
718    }
719
720    /**
721     * Generate the %m format for a date.
722     *
723     * @return string
724     */
725    protected function formatMonthZeros(): string
726    {
727        if ($this->month > 9) {
728            return I18N::digits($this->month);
729        }
730
731        return I18N::digits('0' . $this->month);
732    }
733
734    /**
735     * Generate the %F format for a date.
736     *
737     * @param string $case Which grammatical case shall we use
738     *
739     * @return string
740     */
741    protected function formatLongMonth($case = 'NOMINATIVE'): string
742    {
743        switch ($case) {
744            case 'GENITIVE':
745                return $this->monthNameGenitiveCase($this->month, $this->isLeapYear());
746            case 'NOMINATIVE':
747                return $this->monthNameNominativeCase($this->month, $this->isLeapYear());
748            case 'LOCATIVE':
749                return $this->monthNameLocativeCase($this->month, $this->isLeapYear());
750            case 'INSTRUMENTAL':
751                return $this->monthNameInstrumentalCase($this->month, $this->isLeapYear());
752            default:
753                throw new InvalidArgumentException($case);
754        }
755    }
756
757    /**
758     * Full month name in genitive case.
759     *
760     * @param int  $month
761     * @param bool $leap_year Some calendars use leap months
762     *
763     * @return string
764     */
765    abstract protected function monthNameGenitiveCase(int $month, bool $leap_year): string;
766
767    /**
768     * Full month name in nominative case.
769     *
770     * @param int  $month
771     * @param bool $leap_year Some calendars use leap months
772     *
773     * @return string
774     */
775    abstract protected function monthNameNominativeCase(int $month, bool $leap_year): string;
776
777    /**
778     * Full month name in locative case.
779     *
780     * @param int  $month
781     * @param bool $leap_year Some calendars use leap months
782     *
783     * @return string
784     */
785    abstract protected function monthNameLocativeCase(int $month, bool $leap_year): string;
786
787    /**
788     * Full month name in instrumental case.
789     *
790     * @param int  $month
791     * @param bool $leap_year Some calendars use leap months
792     *
793     * @return string
794     */
795    abstract protected function monthNameInstrumentalCase(int $month, bool $leap_year): string;
796
797    /**
798     * Abbreviated month name
799     *
800     * @param int  $month
801     * @param bool $leap_year Some calendars use leap months
802     *
803     * @return string
804     */
805    abstract protected function monthNameAbbreviated(int $month, bool $leap_year): string;
806
807    /**
808     * Generate the %M format for a date.
809     *
810     * @return string
811     */
812    protected function formatShortMonth(): string
813    {
814        return $this->monthNameAbbreviated($this->month, $this->isLeapYear());
815    }
816
817    /**
818     * Generate the %y format for a date.
819     * NOTE Short year is NOT a 2-digit year. It is for calendars such as hebrew
820     * which have a 3-digit form of 4-digit years.
821     *
822     * @return string
823     */
824    protected function formatShortYear(): string
825    {
826        return $this->formatLongYear();
827    }
828
829    /**
830     * Generate the %A format for a date.
831     *
832     * @return string
833     */
834    protected function formatGedcomDay(): string
835    {
836        if ($this->day === 0) {
837            return '';
838        }
839
840        return sprintf('%02d', $this->day);
841    }
842
843    /**
844     * Generate the %O format for a date.
845     *
846     * @return string
847     */
848    protected function formatGedcomMonth(): string
849    {
850        // Our simple lookup table doesn't work correctly for Adar on leap years
851        if ($this->month === 7 && $this->calendar instanceof JewishCalendar && !$this->calendar->isLeapYear($this->year)) {
852            return 'ADR';
853        }
854
855        return array_search($this->month, static::MONTH_ABBREVIATIONS, true);
856    }
857
858    /**
859     * Generate the %E format for a date.
860     *
861     * @return string
862     */
863    protected function formatGedcomYear(): string
864    {
865        if ($this->year === 0) {
866            return '';
867        }
868
869        return sprintf('%04d', $this->year);
870    }
871
872    /**
873     * Generate the %@ format for a calendar escape.
874     *
875     * @return string
876     */
877    protected function formatGedcomCalendarEscape(): string
878    {
879        return static::ESCAPE;
880    }
881
882    /**
883     * Generate the %Y format for a date.
884     *
885     * @return string
886     */
887    protected function formatLongYear(): string
888    {
889        return I18N::digits($this->year);
890    }
891
892    /**
893     * Which months follows this one? Calendars with leap-months should provide their own implementation.
894     *
895     * @return int[]
896     */
897    protected function nextMonth(): array
898    {
899        return [
900            $this->month === $this->calendar->monthsInYear() ? $this->nextYear($this->year) : $this->year,
901            $this->month % $this->calendar->monthsInYear() + 1,
902        ];
903    }
904
905    /**
906     * Get today’s date in the current calendar.
907     *
908     * @return int[]
909     */
910    public function todayYmd(): array
911    {
912        return $this->calendar->jdToYmd(Carbon::now()->julianDay());
913    }
914
915    /**
916     * Convert to today’s date.
917     *
918     * @return AbstractCalendarDate
919     */
920    public function today(): AbstractCalendarDate
921    {
922        $tmp        = clone $this;
923        $ymd        = $tmp->todayYmd();
924        $tmp->year  = $ymd[0];
925        $tmp->month = $ymd[1];
926        $tmp->day   = $ymd[2];
927        $tmp->setJdFromYmd();
928
929        return $tmp;
930    }
931
932    /**
933     * Create a URL that links this date to the WT calendar
934     *
935     * @param string $date_format
936     * @param Tree   $tree
937     *
938     * @return string
939     */
940    public function calendarUrl(string $date_format, Tree $tree): string
941    {
942        if ($this->day !== 0 && strpbrk($date_format, 'dDj')) {
943            // If the format includes a day, and the date also includes a day, then use the day view
944            $view = 'day';
945        } elseif ($this->month !== 0 && strpbrk($date_format, 'FMmn')) {
946            // If the format includes a month, and the date also includes a month, then use the month view
947            $view = 'month';
948        } else {
949            // Use the year view
950            $view = 'year';
951        }
952
953        return route('calendar', [
954            'cal'   => $this->calendar->gedcomCalendarEscape(),
955            'year'  => $this->formatGedcomYear(),
956            'month' => $this->formatGedcomMonth(),
957            'day'   => $this->formatGedcomDay(),
958            'view'  => $view,
959            'tree'  => $tree->name(),
960        ]);
961    }
962}
963