xref: /webtrees/app/Date/AbstractCalendarDate.php (revision 91495569c5a00349a68e84e2297e0c2b3ffb458e)
14a83f5d7SGreg Roach<?php
24a83f5d7SGreg Roach/**
34a83f5d7SGreg Roach * webtrees: online genealogy
44a83f5d7SGreg Roach * Copyright (C) 2018 webtrees development team
54a83f5d7SGreg Roach * This program is free software: you can redistribute it and/or modify
64a83f5d7SGreg Roach * it under the terms of the GNU General Public License as published by
74a83f5d7SGreg Roach * the Free Software Foundation, either version 3 of the License, or
84a83f5d7SGreg Roach * (at your option) any later version.
94a83f5d7SGreg Roach * This program is distributed in the hope that it will be useful,
104a83f5d7SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
114a83f5d7SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
124a83f5d7SGreg Roach * GNU General Public License for more details.
134a83f5d7SGreg Roach * You should have received a copy of the GNU General Public License
144a83f5d7SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
154a83f5d7SGreg Roach */
164a83f5d7SGreg Roachdeclare(strict_types=1);
174a83f5d7SGreg Roach
184a83f5d7SGreg Roachnamespace Fisharebest\Webtrees\Date;
194a83f5d7SGreg Roach
204a83f5d7SGreg Roachuse Fisharebest\ExtCalendar\CalendarInterface;
214a83f5d7SGreg Roachuse Fisharebest\ExtCalendar\JewishCalendar;
224a83f5d7SGreg Roachuse Fisharebest\Webtrees\DebugBar;
234a83f5d7SGreg Roachuse Fisharebest\Webtrees\I18N;
2449d5f1d7SGreg Roachuse Fisharebest\Webtrees\Tree;
25*91495569SGreg Roachuse InvalidArgumentException;
264a83f5d7SGreg Roach
274a83f5d7SGreg Roach/**
284a83f5d7SGreg Roach * Classes for Gedcom Date/Calendar functionality.
294a83f5d7SGreg Roach *
304a83f5d7SGreg Roach * CalendarDate is a base class for classes such as GregorianDate, etc.
314a83f5d7SGreg Roach * + All supported calendars have non-zero days/months/years.
324a83f5d7SGreg Roach * + We store dates as both Y/M/D and Julian Days.
334a83f5d7SGreg Roach * + For imprecise dates such as "JAN 2000" we store the start/end julian day.
344a83f5d7SGreg Roach *
354a83f5d7SGreg Roach * NOTE: Since different calendars start their days at different times, (civil
364a83f5d7SGreg Roach * midnight, solar midnight, sunset, sunrise, etc.), we convert on the basis of
374a83f5d7SGreg Roach * midday.
384a83f5d7SGreg Roach */
397bb2eb25SGreg Roachabstract class AbstractCalendarDate
404a83f5d7SGreg Roach{
414a83f5d7SGreg Roach    // GEDCOM calendar escape
424a83f5d7SGreg Roach    const ESCAPE = '@#DUNKNOWN@';
434a83f5d7SGreg Roach
444a83f5d7SGreg Roach    // Convert GEDCOM month names to month numbers.
454a83f5d7SGreg Roach    const MONTH_ABBREVIATIONS = [];
464a83f5d7SGreg Roach
474a83f5d7SGreg Roach    /** @var CalendarInterface The calendar system used to represent this date */
484a83f5d7SGreg Roach    protected $calendar;
494a83f5d7SGreg Roach
504a83f5d7SGreg Roach    /** @var int Year number */
514a83f5d7SGreg Roach    public $year;
524a83f5d7SGreg Roach
534a83f5d7SGreg Roach    /** @var int Month number */
544a83f5d7SGreg Roach    public $month;
554a83f5d7SGreg Roach
564a83f5d7SGreg Roach    /** @var int Day number */
574a83f5d7SGreg Roach    public $day;
584a83f5d7SGreg Roach
594a83f5d7SGreg Roach    /** @var int Earliest Julian day number (start of month/year for imprecise dates) */
604a83f5d7SGreg Roach    private $minimum_julian_day;
614a83f5d7SGreg Roach
624a83f5d7SGreg Roach    /** @var int Latest Julian day number (end of month/year for imprecise dates) */
634a83f5d7SGreg Roach    private $maximum_julian_day;
644a83f5d7SGreg Roach
654a83f5d7SGreg Roach    /**
664a83f5d7SGreg Roach     * Create a date from either:
674a83f5d7SGreg Roach     * a Julian day number
684a83f5d7SGreg Roach     * day/month/year strings from a GEDCOM date
694a83f5d7SGreg Roach     * another CalendarDate object
704a83f5d7SGreg Roach     *
714a83f5d7SGreg Roach     * @param array|int|AbstractCalendarDate $date
724a83f5d7SGreg Roach     */
734a83f5d7SGreg Roach    protected function __construct($date)
744a83f5d7SGreg Roach    {
754a83f5d7SGreg Roach        // Construct from an integer (a julian day number)
764a83f5d7SGreg Roach        if (is_int($date)) {
774a83f5d7SGreg Roach            $this->minimum_julian_day = $date;
784a83f5d7SGreg Roach            $this->maximum_julian_day = $date;
7965e02381SGreg Roach            [$this->year, $this->month, $this->day] = $this->calendar->jdToYmd($date);
804a83f5d7SGreg Roach
814a83f5d7SGreg Roach            return;
824a83f5d7SGreg Roach        }
834a83f5d7SGreg Roach
844a83f5d7SGreg Roach        // Construct from an array (of three gedcom-style strings: "1900", "FEB", "4")
854a83f5d7SGreg Roach        if (is_array($date)) {
864a83f5d7SGreg Roach            $this->day = (int) $date[2];
874a83f5d7SGreg Roach            if (array_key_exists($date[1], static::MONTH_ABBREVIATIONS)) {
884a83f5d7SGreg Roach                $this->month = static::MONTH_ABBREVIATIONS[$date[1]];
894a83f5d7SGreg Roach            } else {
904a83f5d7SGreg Roach                $this->month = 0;
914a83f5d7SGreg Roach                $this->day   = 0;
924a83f5d7SGreg Roach            }
934a83f5d7SGreg Roach            $this->year = $this->extractYear($date[0]);
944a83f5d7SGreg Roach
954a83f5d7SGreg Roach            // Our simple lookup table above does not take into account Adar and leap-years.
964a83f5d7SGreg Roach            if ($this->month === 6 && $this->calendar instanceof JewishCalendar && !$this->calendar->isLeapYear($this->year)) {
974a83f5d7SGreg Roach                $this->month = 7;
984a83f5d7SGreg Roach            }
994a83f5d7SGreg Roach
1004a83f5d7SGreg Roach            $this->setJdFromYmd();
1014a83f5d7SGreg Roach
1024a83f5d7SGreg Roach            return;
1034a83f5d7SGreg Roach        }
1044a83f5d7SGreg Roach
1054a83f5d7SGreg Roach        // Contruct from a CalendarDate
1064a83f5d7SGreg Roach        $this->minimum_julian_day = $date->minimum_julian_day;
1074a83f5d7SGreg Roach        $this->maximum_julian_day = $date->maximum_julian_day;
1084a83f5d7SGreg Roach
1094a83f5d7SGreg Roach        // Construct from an equivalent xxxxDate object
1104a83f5d7SGreg Roach        if (get_class($this) == get_class($date)) {
1114a83f5d7SGreg Roach            $this->year  = $date->year;
1124a83f5d7SGreg Roach            $this->month = $date->month;
1134a83f5d7SGreg Roach            $this->day   = $date->day;
1144a83f5d7SGreg Roach
1154a83f5d7SGreg Roach            return;
1164a83f5d7SGreg Roach        }
1174a83f5d7SGreg Roach
1184a83f5d7SGreg Roach        // Not all dates can be converted
1194a83f5d7SGreg Roach        if (!$this->inValidRange()) {
1204a83f5d7SGreg Roach            $this->year  = 0;
1214a83f5d7SGreg Roach            $this->month = 0;
1224a83f5d7SGreg Roach            $this->day   = 0;
1234a83f5d7SGreg Roach
1244a83f5d7SGreg Roach            return;
1254a83f5d7SGreg Roach        }
1264a83f5d7SGreg Roach
1274a83f5d7SGreg Roach        // ...else construct an inequivalent xxxxDate object
1284a83f5d7SGreg Roach        if ($date->year == 0) {
1294a83f5d7SGreg Roach            // Incomplete date - convert on basis of anniversary in current year
1304a83f5d7SGreg Roach            $today = $date->calendar->jdToYmd(unixtojd());
1314a83f5d7SGreg Roach            $jd    = $date->calendar->ymdToJd($today[0], $date->month, $date->day == 0 ? $today[2] : $date->day);
1324a83f5d7SGreg Roach        } else {
1334a83f5d7SGreg Roach            // Complete date
1344a83f5d7SGreg Roach            $jd = intdiv($date->maximum_julian_day + $date->minimum_julian_day, 2);
1354a83f5d7SGreg Roach        }
13665e02381SGreg Roach        [$this->year, $this->month, $this->day] = $this->calendar->jdToYmd($jd);
1374a83f5d7SGreg Roach        // New date has same precision as original date
1384a83f5d7SGreg Roach        if ($date->year == 0) {
1394a83f5d7SGreg Roach            $this->year = 0;
1404a83f5d7SGreg Roach        }
1414a83f5d7SGreg Roach        if ($date->month == 0) {
1424a83f5d7SGreg Roach            $this->month = 0;
1434a83f5d7SGreg Roach        }
1444a83f5d7SGreg Roach        if ($date->day == 0) {
1454a83f5d7SGreg Roach            $this->day = 0;
1464a83f5d7SGreg Roach        }
1474a83f5d7SGreg Roach        $this->setJdFromYmd();
1484a83f5d7SGreg Roach    }
1494a83f5d7SGreg Roach
1504a83f5d7SGreg Roach    /**
1514a83f5d7SGreg Roach     * @return CalendarInterface
1524a83f5d7SGreg Roach     */
1534a83f5d7SGreg Roach    public function calendar(): CalendarInterface
1544a83f5d7SGreg Roach    {
1554a83f5d7SGreg Roach        return $this->calendar();
1564a83f5d7SGreg Roach    }
1574a83f5d7SGreg Roach
1584a83f5d7SGreg Roach    /**
1594a83f5d7SGreg Roach     * @return int
1604a83f5d7SGreg Roach     */
1614a83f5d7SGreg Roach    public function maximumJulianDay(): int
1624a83f5d7SGreg Roach    {
1634a83f5d7SGreg Roach        return $this->maximum_julian_day;
1644a83f5d7SGreg Roach    }
1654a83f5d7SGreg Roach
1664a83f5d7SGreg Roach    /**
1674a83f5d7SGreg Roach     * @return int
1684a83f5d7SGreg Roach     */
1694a83f5d7SGreg Roach    public function year(): int
1704a83f5d7SGreg Roach    {
1714a83f5d7SGreg Roach        return $this->year;
1724a83f5d7SGreg Roach    }
1734a83f5d7SGreg Roach
1744a83f5d7SGreg Roach    /**
1754a83f5d7SGreg Roach     * @return int
1764a83f5d7SGreg Roach     */
1774a83f5d7SGreg Roach    public function month(): int
1784a83f5d7SGreg Roach    {
1794a83f5d7SGreg Roach        return $this->month;
1804a83f5d7SGreg Roach    }
1814a83f5d7SGreg Roach
1824a83f5d7SGreg Roach    /**
1834a83f5d7SGreg Roach     * @return int
1844a83f5d7SGreg Roach     */
1854a83f5d7SGreg Roach    public function day(): int
1864a83f5d7SGreg Roach    {
1874a83f5d7SGreg Roach        return $this->day;
1884a83f5d7SGreg Roach    }
1894a83f5d7SGreg Roach
1904a83f5d7SGreg Roach    /**
1914a83f5d7SGreg Roach     * @return int
1924a83f5d7SGreg Roach     */
1934a83f5d7SGreg Roach    public function minimumJulianDay(): int
1944a83f5d7SGreg Roach    {
1954a83f5d7SGreg Roach        return $this->minimum_julian_day;
1964a83f5d7SGreg Roach    }
1974a83f5d7SGreg Roach
1984a83f5d7SGreg Roach    /**
1994a83f5d7SGreg Roach     * Is the current year a leap year?
2004a83f5d7SGreg Roach     *
2014a83f5d7SGreg Roach     * @return bool
2024a83f5d7SGreg Roach     */
2034a83f5d7SGreg Roach    public function isLeapYear(): bool
2044a83f5d7SGreg Roach    {
2054a83f5d7SGreg Roach        return $this->calendar->isLeapYear($this->year);
2064a83f5d7SGreg Roach    }
2074a83f5d7SGreg Roach
2084a83f5d7SGreg Roach    /**
2094a83f5d7SGreg Roach     * Set the object’s Julian day number from a potentially incomplete year/month/day
2104a83f5d7SGreg Roach     *
2114a83f5d7SGreg Roach     * @return void
2124a83f5d7SGreg Roach     */
2134a83f5d7SGreg Roach    public function setJdFromYmd()
2144a83f5d7SGreg Roach    {
2154a83f5d7SGreg Roach        if ($this->year == 0) {
2164a83f5d7SGreg Roach            $this->minimum_julian_day = 0;
2174a83f5d7SGreg Roach            $this->maximum_julian_day = 0;
2184a83f5d7SGreg Roach        } elseif ($this->month == 0) {
2194a83f5d7SGreg Roach            $this->minimum_julian_day = $this->calendar->ymdToJd($this->year, 1, 1);
2204a83f5d7SGreg Roach            $this->maximum_julian_day = $this->calendar->ymdToJd($this->nextYear($this->year), 1, 1) - 1;
2214a83f5d7SGreg Roach        } elseif ($this->day == 0) {
22265e02381SGreg Roach            [$ny, $nm] = $this->nextMonth();
2234a83f5d7SGreg Roach            $this->minimum_julian_day = $this->calendar->ymdToJd($this->year, $this->month, 1);
2244a83f5d7SGreg Roach            $this->maximum_julian_day = $this->calendar->ymdToJd($ny, $nm, 1) - 1;
2254a83f5d7SGreg Roach        } else {
2264a83f5d7SGreg Roach            $this->minimum_julian_day = $this->calendar->ymdToJd($this->year, $this->month, $this->day);
2274a83f5d7SGreg Roach            $this->maximum_julian_day = $this->minimum_julian_day;
2284a83f5d7SGreg Roach        }
2294a83f5d7SGreg Roach    }
2304a83f5d7SGreg Roach
2314a83f5d7SGreg Roach    /**
2324a83f5d7SGreg Roach     * Full day of the week
2334a83f5d7SGreg Roach     *
2344a83f5d7SGreg Roach     * @param int $day_number
2354a83f5d7SGreg Roach     *
2364a83f5d7SGreg Roach     * @return string
2374a83f5d7SGreg Roach     */
2384a83f5d7SGreg Roach    public function dayNames(int $day_number): string
2394a83f5d7SGreg Roach    {
2404a83f5d7SGreg Roach        static $translated_day_names;
2414a83f5d7SGreg Roach
2424a83f5d7SGreg Roach        if ($translated_day_names === null) {
2434a83f5d7SGreg Roach            $translated_day_names = [
2444a83f5d7SGreg Roach                0 => I18N::translate('Monday'),
2454a83f5d7SGreg Roach                1 => I18N::translate('Tuesday'),
2464a83f5d7SGreg Roach                2 => I18N::translate('Wednesday'),
2474a83f5d7SGreg Roach                3 => I18N::translate('Thursday'),
2484a83f5d7SGreg Roach                4 => I18N::translate('Friday'),
2494a83f5d7SGreg Roach                5 => I18N::translate('Saturday'),
2504a83f5d7SGreg Roach                6 => I18N::translate('Sunday'),
2514a83f5d7SGreg Roach            ];
2524a83f5d7SGreg Roach        }
2534a83f5d7SGreg Roach
2544a83f5d7SGreg Roach        return $translated_day_names[$day_number];
2554a83f5d7SGreg Roach    }
2564a83f5d7SGreg Roach
2574a83f5d7SGreg Roach    /**
2584a83f5d7SGreg Roach     * Abbreviated day of the week
2594a83f5d7SGreg Roach     *
2604a83f5d7SGreg Roach     * @param int $day_number
2614a83f5d7SGreg Roach     *
2624a83f5d7SGreg Roach     * @return string
2634a83f5d7SGreg Roach     */
2644a83f5d7SGreg Roach    protected function dayNamesAbbreviated(int $day_number): string
2654a83f5d7SGreg Roach    {
2664a83f5d7SGreg Roach        static $translated_day_names;
2674a83f5d7SGreg Roach
2684a83f5d7SGreg Roach        if ($translated_day_names === null) {
2694a83f5d7SGreg Roach            $translated_day_names = [
2704a83f5d7SGreg Roach                /* I18N: abbreviation for Monday */
2714a83f5d7SGreg Roach                0 => I18N::translate('Mon'),
2724a83f5d7SGreg Roach                /* I18N: abbreviation for Tuesday */
2734a83f5d7SGreg Roach                1 => I18N::translate('Tue'),
2744a83f5d7SGreg Roach                /* I18N: abbreviation for Wednesday */
2754a83f5d7SGreg Roach                2 => I18N::translate('Wed'),
2764a83f5d7SGreg Roach                /* I18N: abbreviation for Thursday */
2774a83f5d7SGreg Roach                3 => I18N::translate('Thu'),
2784a83f5d7SGreg Roach                /* I18N: abbreviation for Friday */
2794a83f5d7SGreg Roach                4 => I18N::translate('Fri'),
2804a83f5d7SGreg Roach                /* I18N: abbreviation for Saturday */
2814a83f5d7SGreg Roach                5 => I18N::translate('Sat'),
2824a83f5d7SGreg Roach                /* I18N: abbreviation for Sunday */
2834a83f5d7SGreg Roach                6 => I18N::translate('Sun'),
2844a83f5d7SGreg Roach            ];
2854a83f5d7SGreg Roach        }
2864a83f5d7SGreg Roach
2874a83f5d7SGreg Roach        return $translated_day_names[$day_number];
2884a83f5d7SGreg Roach    }
2894a83f5d7SGreg Roach
2904a83f5d7SGreg Roach    /**
2914a83f5d7SGreg Roach     * Most years are 1 more than the previous, but not always (e.g. 1BC->1AD)
2924a83f5d7SGreg Roach     *
2934a83f5d7SGreg Roach     * @param int $year
2944a83f5d7SGreg Roach     *
2954a83f5d7SGreg Roach     * @return int
2964a83f5d7SGreg Roach     */
2974a83f5d7SGreg Roach    protected function nextYear(int $year): int
2984a83f5d7SGreg Roach    {
2994a83f5d7SGreg Roach        return $year + 1;
3004a83f5d7SGreg Roach    }
3014a83f5d7SGreg Roach
3024a83f5d7SGreg Roach    /**
3034a83f5d7SGreg Roach     * Calendars that use suffixes, etc. (e.g. “B.C.”) or OS/NS notation should redefine this.
3044a83f5d7SGreg Roach     *
3054a83f5d7SGreg Roach     * @param string $year
3064a83f5d7SGreg Roach     *
3074a83f5d7SGreg Roach     * @return int
3084a83f5d7SGreg Roach     */
3094a83f5d7SGreg Roach    protected function extractYear(string $year): int
3104a83f5d7SGreg Roach    {
3114a83f5d7SGreg Roach        return (int) $year;
3124a83f5d7SGreg Roach    }
3134a83f5d7SGreg Roach
3144a83f5d7SGreg Roach    /**
3154a83f5d7SGreg Roach     * Compare two dates, for sorting
3164a83f5d7SGreg Roach     *
3174a83f5d7SGreg Roach     * @param AbstractCalendarDate $d1
3184a83f5d7SGreg Roach     * @param AbstractCalendarDate $d2
3194a83f5d7SGreg Roach     *
3204a83f5d7SGreg Roach     * @return int
3214a83f5d7SGreg Roach     */
3224a83f5d7SGreg Roach    public static function compare(AbstractCalendarDate $d1, AbstractCalendarDate $d2): int
3234a83f5d7SGreg Roach    {
3244a83f5d7SGreg Roach        if ($d1->maximum_julian_day < $d2->minimum_julian_day) {
3254a83f5d7SGreg Roach            return -1;
3264a83f5d7SGreg Roach        }
3274a83f5d7SGreg Roach
3284a83f5d7SGreg Roach        if ($d2->maximum_julian_day < $d1->minimum_julian_day) {
3294a83f5d7SGreg Roach            return 1;
3304a83f5d7SGreg Roach        }
3314a83f5d7SGreg Roach
3324a83f5d7SGreg Roach        return 0;
3334a83f5d7SGreg Roach    }
3344a83f5d7SGreg Roach
3354a83f5d7SGreg Roach    /**
3364a83f5d7SGreg Roach     * Calculate the years/months/days between this date and another date.
3374a83f5d7SGreg Roach     * Results assume you add the days first, then the months.
3384a83f5d7SGreg Roach     * 4 February -> 3 July is 27 days (3 March) and 4 months.
3394a83f5d7SGreg Roach     * It is not 4 months (4 June) and 29 days.
3404a83f5d7SGreg Roach     *
3414a83f5d7SGreg Roach     * @param AbstractCalendarDate $date
3424a83f5d7SGreg Roach     *
3434a83f5d7SGreg Roach     * @return int[] Age in years/months/days
3444a83f5d7SGreg Roach     */
3454a83f5d7SGreg Roach    public function ageDifference(AbstractCalendarDate $date): array
3464a83f5d7SGreg Roach    {
3474a83f5d7SGreg Roach        // Incomplete dates
3484a83f5d7SGreg Roach        if ($this->year === 0 || $date->year === 0) {
3494a83f5d7SGreg Roach            return [-1, -1, -1];
3504a83f5d7SGreg Roach        }
3514a83f5d7SGreg Roach
3524a83f5d7SGreg Roach        // Overlapping dates
3534a83f5d7SGreg Roach        if (self::compare($this, $date) === 0) {
3544a83f5d7SGreg Roach            return [0, 0, 0];
3554a83f5d7SGreg Roach        }
3564a83f5d7SGreg Roach
3574a83f5d7SGreg Roach        // Perform all calculations using the calendar of the first date
35865e02381SGreg Roach        [$year1, $month1, $day1] = $this->calendar->jdToYmd($this->minimum_julian_day);
35965e02381SGreg Roach        [$year2, $month2, $day2] = $this->calendar->jdToYmd($date->minimum_julian_day);
3604a83f5d7SGreg Roach
3614a83f5d7SGreg Roach        $years  = $year2 - $year1;
3624a83f5d7SGreg Roach        $months = $month2 - $month1;
3634a83f5d7SGreg Roach        $days   = $day2 - $day1;
3644a83f5d7SGreg Roach
3654a83f5d7SGreg Roach        if ($days < 0) {
3664a83f5d7SGreg Roach            $days += $this->calendar->daysInMonth($year1, $month1);
3674a83f5d7SGreg Roach            $months--;
3684a83f5d7SGreg Roach        }
3694a83f5d7SGreg Roach
3704a83f5d7SGreg Roach        if ($months < 0) {
3714a83f5d7SGreg Roach            $months += $this->calendar->monthsInYear($year2);
3724a83f5d7SGreg Roach            $years--;
3734a83f5d7SGreg Roach        }
3744a83f5d7SGreg Roach
3754a83f5d7SGreg Roach        return [$years, $months, $days];
3764a83f5d7SGreg Roach    }
3774a83f5d7SGreg Roach
3784a83f5d7SGreg Roach    /**
3794a83f5d7SGreg Roach     * How long between an event and a given julian day
3804a83f5d7SGreg Roach     * Return result as a number of years.
3814a83f5d7SGreg Roach     *
3824a83f5d7SGreg Roach     * @param int $jd date for calculation
3834a83f5d7SGreg Roach     *
3844a83f5d7SGreg Roach     * @return int
3854a83f5d7SGreg Roach     */
3864a83f5d7SGreg Roach    public function getAge(int $jd): int
3874a83f5d7SGreg Roach    {
3884a83f5d7SGreg Roach        if ($this->year == 0 || $jd == 0) {
3894a83f5d7SGreg Roach            return 0;
3904a83f5d7SGreg Roach        }
3914a83f5d7SGreg Roach        if ($this->minimum_julian_day < $jd && $this->maximum_julian_day > $jd) {
3924a83f5d7SGreg Roach            return 0;
3934a83f5d7SGreg Roach        }
3944a83f5d7SGreg Roach        if ($this->minimum_julian_day == $jd) {
3954a83f5d7SGreg Roach            return 0;
3964a83f5d7SGreg Roach        }
39765e02381SGreg Roach        [$y, $m, $d] = $this->calendar->jdToYmd($jd);
3984a83f5d7SGreg Roach        $dy = $y - $this->year;
3994a83f5d7SGreg Roach        $dm = $m - max($this->month, 1);
4004a83f5d7SGreg Roach        $dd = $d - max($this->day, 1);
4014a83f5d7SGreg Roach        if ($dd < 0) {
4024a83f5d7SGreg Roach            $dm--;
4034a83f5d7SGreg Roach        }
4044a83f5d7SGreg Roach        if ($dm < 0) {
4054a83f5d7SGreg Roach            $dy--;
4064a83f5d7SGreg Roach        }
4074a83f5d7SGreg Roach
4084a83f5d7SGreg Roach        // Not a full age? Then just the years
4094a83f5d7SGreg Roach        return $dy;
4104a83f5d7SGreg Roach    }
4114a83f5d7SGreg Roach
4124a83f5d7SGreg Roach    /**
4134a83f5d7SGreg Roach     * How long between an event and a given julian day
4144a83f5d7SGreg Roach     * Return result as a gedcom-style age string.
4154a83f5d7SGreg Roach     *
4164a83f5d7SGreg Roach     * @param int $jd date for calculation
4174a83f5d7SGreg Roach     *
4184a83f5d7SGreg Roach     * @return string
4194a83f5d7SGreg Roach     */
4204a83f5d7SGreg Roach    public function getAgeFull(int $jd): string
4214a83f5d7SGreg Roach    {
4224a83f5d7SGreg Roach        if ($this->year == 0 || $jd == 0) {
4234a83f5d7SGreg Roach            return '';
4244a83f5d7SGreg Roach        }
4254a83f5d7SGreg Roach        if ($this->minimum_julian_day < $jd && $this->maximum_julian_day > $jd) {
4264a83f5d7SGreg Roach            return '';
4274a83f5d7SGreg Roach        }
4284a83f5d7SGreg Roach        if ($this->minimum_julian_day == $jd) {
4294a83f5d7SGreg Roach            return '';
4304a83f5d7SGreg Roach        }
4314a83f5d7SGreg Roach        if ($jd < $this->minimum_julian_day) {
4324a83f5d7SGreg Roach            return '<i class="icon-warning"></i>';
4334a83f5d7SGreg Roach        }
43465e02381SGreg Roach        [$y, $m, $d] = $this->calendar->jdToYmd($jd);
4354a83f5d7SGreg Roach        $dy = $y - $this->year;
4364a83f5d7SGreg Roach        $dm = $m - max($this->month, 1);
4374a83f5d7SGreg Roach        $dd = $d - max($this->day, 1);
4384a83f5d7SGreg Roach        if ($dd < 0) {
4394a83f5d7SGreg Roach            $dm--;
4404a83f5d7SGreg Roach        }
4414a83f5d7SGreg Roach        if ($dm < 0) {
4424a83f5d7SGreg Roach            $dm += $this->calendar->monthsInYear();
4434a83f5d7SGreg Roach            $dy--;
4444a83f5d7SGreg Roach        }
4454a83f5d7SGreg Roach        // Age in years?
4464a83f5d7SGreg Roach        if ($dy > 1) {
4474a83f5d7SGreg Roach            return $dy . 'y';
4484a83f5d7SGreg Roach        }
4494a83f5d7SGreg Roach        $dm += $dy * $this->calendar->monthsInYear();
4504a83f5d7SGreg Roach        // Age in months?
4514a83f5d7SGreg Roach        if ($dm > 1) {
4524a83f5d7SGreg Roach            return $dm . 'm';
4534a83f5d7SGreg Roach        }
4544a83f5d7SGreg Roach
4554a83f5d7SGreg Roach        // Age in days?
4564a83f5d7SGreg Roach        return ($jd - $this->minimum_julian_day) . 'd';
4574a83f5d7SGreg Roach    }
4584a83f5d7SGreg Roach
4594a83f5d7SGreg Roach    /**
4604a83f5d7SGreg Roach     * Convert a date from one calendar to another.
4614a83f5d7SGreg Roach     *
4624a83f5d7SGreg Roach     * @param string $calendar
4634a83f5d7SGreg Roach     *
4644a83f5d7SGreg Roach     * @return AbstractCalendarDate
4654a83f5d7SGreg Roach     */
4664a83f5d7SGreg Roach    public function convertToCalendar(string $calendar): AbstractCalendarDate
4674a83f5d7SGreg Roach    {
4684a83f5d7SGreg Roach        switch ($calendar) {
4694a83f5d7SGreg Roach            case 'gregorian':
4704a83f5d7SGreg Roach                return new GregorianDate($this);
4714a83f5d7SGreg Roach            case 'julian':
4724a83f5d7SGreg Roach                return new JulianDate($this);
4734a83f5d7SGreg Roach            case 'jewish':
4744a83f5d7SGreg Roach                return new JewishDate($this);
4754a83f5d7SGreg Roach            case 'french':
4764a83f5d7SGreg Roach                return new FrenchDate($this);
4774a83f5d7SGreg Roach            case 'hijri':
4784a83f5d7SGreg Roach                return new HijriDate($this);
4794a83f5d7SGreg Roach            case 'jalali':
4804a83f5d7SGreg Roach                return new JalaliDate($this);
4814a83f5d7SGreg Roach            default:
4824a83f5d7SGreg Roach                return $this;
4834a83f5d7SGreg Roach        }
4844a83f5d7SGreg Roach    }
4854a83f5d7SGreg Roach
4864a83f5d7SGreg Roach    /**
4874a83f5d7SGreg Roach     * Is this date within the valid range of the calendar
4884a83f5d7SGreg Roach     *
4894a83f5d7SGreg Roach     * @return bool
4904a83f5d7SGreg Roach     */
4914a83f5d7SGreg Roach    public function inValidRange(): bool
4924a83f5d7SGreg Roach    {
4934a83f5d7SGreg Roach        return $this->minimum_julian_day >= $this->calendar->jdStart() && $this->maximum_julian_day <= $this->calendar->jdEnd();
4944a83f5d7SGreg Roach    }
4954a83f5d7SGreg Roach
4964a83f5d7SGreg Roach    /**
4974a83f5d7SGreg Roach     * How many months in a year
4984a83f5d7SGreg Roach     *
4994a83f5d7SGreg Roach     * @return int
5004a83f5d7SGreg Roach     */
5014a83f5d7SGreg Roach    public function monthsInYear(): int
5024a83f5d7SGreg Roach    {
5034a83f5d7SGreg Roach        return $this->calendar->monthsInYear();
5044a83f5d7SGreg Roach    }
5054a83f5d7SGreg Roach
5064a83f5d7SGreg Roach    /**
5074a83f5d7SGreg Roach     * How many days in the current month
5084a83f5d7SGreg Roach     *
5094a83f5d7SGreg Roach     * @return int
5104a83f5d7SGreg Roach     */
5114a83f5d7SGreg Roach    public function daysInMonth(): int
5124a83f5d7SGreg Roach    {
5134a83f5d7SGreg Roach        try {
5144a83f5d7SGreg Roach            return $this->calendar->daysInMonth($this->year, $this->month);
515*91495569SGreg Roach        } catch (InvalidArgumentException $ex) {
5164a83f5d7SGreg Roach            // calendar.php calls this with "DD MMM" dates, for which we cannot calculate
5174a83f5d7SGreg Roach            // the length of a month. Should we validate this before calling this function?
5184a83f5d7SGreg Roach            return 0;
5194a83f5d7SGreg Roach        }
5204a83f5d7SGreg Roach    }
5214a83f5d7SGreg Roach
5224a83f5d7SGreg Roach    /**
5234a83f5d7SGreg Roach     * How many days in the current week
5244a83f5d7SGreg Roach     *
5254a83f5d7SGreg Roach     * @return int
5264a83f5d7SGreg Roach     */
5274a83f5d7SGreg Roach    public function daysInWeek(): int
5284a83f5d7SGreg Roach    {
5294a83f5d7SGreg Roach        return $this->calendar->daysInWeek();
5304a83f5d7SGreg Roach    }
5314a83f5d7SGreg Roach
5324a83f5d7SGreg Roach    /**
5334a83f5d7SGreg Roach     * Format a date, using similar codes to the PHP date() function.
5344a83f5d7SGreg Roach     *
5354a83f5d7SGreg Roach     * @param string $format    See http://php.net/date
5364a83f5d7SGreg Roach     * @param string $qualifier GEDCOM qualifier, so we can choose the right case for the month name.
5374a83f5d7SGreg Roach     *
5384a83f5d7SGreg Roach     * @return string
5394a83f5d7SGreg Roach     */
5404a83f5d7SGreg Roach    public function format(string $format, string $qualifier = ''): string
5414a83f5d7SGreg Roach    {
5424a83f5d7SGreg Roach        // Don’t show exact details for inexact dates
5434a83f5d7SGreg Roach        if (!$this->day) {
5444a83f5d7SGreg Roach            // The comma is for US "M D, Y" dates
5454a83f5d7SGreg Roach            $format = preg_replace('/%[djlDNSwz][,]?/', '', $format);
5464a83f5d7SGreg Roach        }
5474a83f5d7SGreg Roach        if (!$this->month) {
5484a83f5d7SGreg Roach            $format = str_replace([
5494a83f5d7SGreg Roach                '%F',
5504a83f5d7SGreg Roach                '%m',
5514a83f5d7SGreg Roach                '%M',
5524a83f5d7SGreg Roach                '%n',
5534a83f5d7SGreg Roach                '%t',
5544a83f5d7SGreg Roach            ], '', $format);
5554a83f5d7SGreg Roach        }
5564a83f5d7SGreg Roach        if (!$this->year) {
5574a83f5d7SGreg Roach            $format = str_replace([
5584a83f5d7SGreg Roach                '%t',
5594a83f5d7SGreg Roach                '%L',
5604a83f5d7SGreg Roach                '%G',
5614a83f5d7SGreg Roach                '%y',
5624a83f5d7SGreg Roach                '%Y',
5634a83f5d7SGreg Roach            ], '', $format);
5644a83f5d7SGreg Roach        }
5654a83f5d7SGreg Roach        // If we’ve trimmed the format, also trim the punctuation
5664a83f5d7SGreg Roach        if (!$this->day || !$this->month || !$this->year) {
5674a83f5d7SGreg Roach            $format = trim($format, ',. ;/-');
5684a83f5d7SGreg Roach        }
5694a83f5d7SGreg Roach        if ($this->day && preg_match('/%[djlDNSwz]/', $format)) {
5704a83f5d7SGreg Roach            // If we have a day-number *and* we are being asked to display it, then genitive
5714a83f5d7SGreg Roach            $case = 'GENITIVE';
5724a83f5d7SGreg Roach        } else {
5734a83f5d7SGreg Roach            switch ($qualifier) {
5744a83f5d7SGreg Roach                case 'TO':
5754a83f5d7SGreg Roach                case 'ABT':
5764a83f5d7SGreg Roach                case 'FROM':
5774a83f5d7SGreg Roach                    $case = 'GENITIVE';
5784a83f5d7SGreg Roach                    break;
5794a83f5d7SGreg Roach                case 'AFT':
5804a83f5d7SGreg Roach                    $case = 'LOCATIVE';
5814a83f5d7SGreg Roach                    break;
5824a83f5d7SGreg Roach                case 'BEF':
5834a83f5d7SGreg Roach                case 'BET':
5844a83f5d7SGreg Roach                case 'AND':
5854a83f5d7SGreg Roach                    $case = 'INSTRUMENTAL';
5864a83f5d7SGreg Roach                    break;
5874a83f5d7SGreg Roach                case '':
5884a83f5d7SGreg Roach                case 'INT':
5894a83f5d7SGreg Roach                case 'EST':
5904a83f5d7SGreg Roach                case 'CAL':
5914a83f5d7SGreg Roach                default: // There shouldn't be any other options...
5924a83f5d7SGreg Roach                    $case = 'NOMINATIVE';
5934a83f5d7SGreg Roach                    break;
5944a83f5d7SGreg Roach            }
5954a83f5d7SGreg Roach        }
5964a83f5d7SGreg Roach        // Build up the formatted date, character at a time
5974a83f5d7SGreg Roach        preg_match_all('/%[^%]/', $format, $matches);
5984a83f5d7SGreg Roach        foreach ($matches[0] as $match) {
5994a83f5d7SGreg Roach            switch ($match) {
6004a83f5d7SGreg Roach                case '%d':
6014a83f5d7SGreg Roach                    $format = str_replace($match, $this->formatDayZeros(), $format);
6024a83f5d7SGreg Roach                    break;
6034a83f5d7SGreg Roach                case '%j':
6044a83f5d7SGreg Roach                    $format = str_replace($match, $this->formatDay(), $format);
6054a83f5d7SGreg Roach                    break;
6064a83f5d7SGreg Roach                case '%l':
6074a83f5d7SGreg Roach                    $format = str_replace($match, $this->formatLongWeekday(), $format);
6084a83f5d7SGreg Roach                    break;
6094a83f5d7SGreg Roach                case '%D':
6104a83f5d7SGreg Roach                    $format = str_replace($match, $this->formatShortWeekday(), $format);
6114a83f5d7SGreg Roach                    break;
6124a83f5d7SGreg Roach                case '%N':
6134a83f5d7SGreg Roach                    $format = str_replace($match, $this->formatIsoWeekday(), $format);
6144a83f5d7SGreg Roach                    break;
6154a83f5d7SGreg Roach                case '%w':
6164a83f5d7SGreg Roach                    $format = str_replace($match, $this->formatNumericWeekday(), $format);
6174a83f5d7SGreg Roach                    break;
6184a83f5d7SGreg Roach                case '%z':
6194a83f5d7SGreg Roach                    $format = str_replace($match, $this->formatDayOfYear(), $format);
6204a83f5d7SGreg Roach                    break;
6214a83f5d7SGreg Roach                case '%F':
6224a83f5d7SGreg Roach                    $format = str_replace($match, $this->formatLongMonth($case), $format);
6234a83f5d7SGreg Roach                    break;
6244a83f5d7SGreg Roach                case '%m':
6254a83f5d7SGreg Roach                    $format = str_replace($match, $this->formatMonthZeros(), $format);
6264a83f5d7SGreg Roach                    break;
6274a83f5d7SGreg Roach                case '%M':
6284a83f5d7SGreg Roach                    $format = str_replace($match, $this->formatShortMonth(), $format);
6294a83f5d7SGreg Roach                    break;
6304a83f5d7SGreg Roach                case '%n':
6314a83f5d7SGreg Roach                    $format = str_replace($match, $this->formatMonth(), $format);
6324a83f5d7SGreg Roach                    break;
6334a83f5d7SGreg Roach                case '%t':
6344a83f5d7SGreg Roach                    $format = str_replace($match, (string) $this->daysInMonth(), $format);
6354a83f5d7SGreg Roach                    break;
6364a83f5d7SGreg Roach                case '%L':
6374a83f5d7SGreg Roach                    $format = str_replace($match, $this->isLeapYear() ? '1' : '0', $format);
6384a83f5d7SGreg Roach                    break;
6394a83f5d7SGreg Roach                case '%Y':
6404a83f5d7SGreg Roach                    $format = str_replace($match, $this->formatLongYear(), $format);
6414a83f5d7SGreg Roach                    break;
6424a83f5d7SGreg Roach                case '%y':
6434a83f5d7SGreg Roach                    $format = str_replace($match, $this->formatShortYear(), $format);
6444a83f5d7SGreg Roach                    break;
6454a83f5d7SGreg Roach                // These 4 extensions are useful for re-formatting gedcom dates.
6464a83f5d7SGreg Roach                case '%@':
6474a83f5d7SGreg Roach                    $format = str_replace($match, $this->formatGedcomCalendarEscape(), $format);
6484a83f5d7SGreg Roach                    break;
6494a83f5d7SGreg Roach                case '%A':
6504a83f5d7SGreg Roach                    $format = str_replace($match, $this->formatGedcomDay(), $format);
6514a83f5d7SGreg Roach                    break;
6524a83f5d7SGreg Roach                case '%O':
6534a83f5d7SGreg Roach                    $format = str_replace($match, $this->formatGedcomMonth(), $format);
6544a83f5d7SGreg Roach                    break;
6554a83f5d7SGreg Roach                case '%E':
6564a83f5d7SGreg Roach                    $format = str_replace($match, $this->formatGedcomYear(), $format);
6574a83f5d7SGreg Roach                    break;
6584a83f5d7SGreg Roach            }
6594a83f5d7SGreg Roach        }
6604a83f5d7SGreg Roach
6614a83f5d7SGreg Roach        return $format;
6624a83f5d7SGreg Roach    }
6634a83f5d7SGreg Roach
6644a83f5d7SGreg Roach    /**
6654a83f5d7SGreg Roach     * Generate the %d format for a date.
6664a83f5d7SGreg Roach     *
6674a83f5d7SGreg Roach     * @return string
6684a83f5d7SGreg Roach     */
6694a83f5d7SGreg Roach    protected function formatDayZeros(): string
6704a83f5d7SGreg Roach    {
6714a83f5d7SGreg Roach        if ($this->day > 9) {
6724a83f5d7SGreg Roach            return I18N::digits($this->day);
6734a83f5d7SGreg Roach        }
6744a83f5d7SGreg Roach
6754a83f5d7SGreg Roach        return I18N::digits('0' . $this->day);
6764a83f5d7SGreg Roach    }
6774a83f5d7SGreg Roach
6784a83f5d7SGreg Roach    /**
6794a83f5d7SGreg Roach     * Generate the %j format for a date.
6804a83f5d7SGreg Roach     *
6814a83f5d7SGreg Roach     * @return string
6824a83f5d7SGreg Roach     */
6834a83f5d7SGreg Roach    protected function formatDay(): string
6844a83f5d7SGreg Roach    {
6854a83f5d7SGreg Roach        return I18N::digits($this->day);
6864a83f5d7SGreg Roach    }
6874a83f5d7SGreg Roach
6884a83f5d7SGreg Roach    /**
6894a83f5d7SGreg Roach     * Generate the %l format for a date.
6904a83f5d7SGreg Roach     *
6914a83f5d7SGreg Roach     * @return string
6924a83f5d7SGreg Roach     */
6934a83f5d7SGreg Roach    protected function formatLongWeekday(): string
6944a83f5d7SGreg Roach    {
6954a83f5d7SGreg Roach        return $this->dayNames($this->minimum_julian_day % $this->calendar->daysInWeek());
6964a83f5d7SGreg Roach    }
6974a83f5d7SGreg Roach
6984a83f5d7SGreg Roach    /**
6994a83f5d7SGreg Roach     * Generate the %D format for a date.
7004a83f5d7SGreg Roach     *
7014a83f5d7SGreg Roach     * @return string
7024a83f5d7SGreg Roach     */
7034a83f5d7SGreg Roach    protected function formatShortWeekday(): string
7044a83f5d7SGreg Roach    {
7054a83f5d7SGreg Roach        return $this->dayNamesAbbreviated($this->minimum_julian_day % $this->calendar->daysInWeek());
7064a83f5d7SGreg Roach    }
7074a83f5d7SGreg Roach
7084a83f5d7SGreg Roach    /**
7094a83f5d7SGreg Roach     * Generate the %N format for a date.
7104a83f5d7SGreg Roach     *
7114a83f5d7SGreg Roach     * @return string
7124a83f5d7SGreg Roach     */
7134a83f5d7SGreg Roach    protected function formatIsoWeekday(): string
7144a83f5d7SGreg Roach    {
7154a83f5d7SGreg Roach        return I18N::digits($this->minimum_julian_day % 7 + 1);
7164a83f5d7SGreg Roach    }
7174a83f5d7SGreg Roach
7184a83f5d7SGreg Roach    /**
7194a83f5d7SGreg Roach     * Generate the %w format for a date.
7204a83f5d7SGreg Roach     *
7214a83f5d7SGreg Roach     * @return string
7224a83f5d7SGreg Roach     */
7234a83f5d7SGreg Roach    protected function formatNumericWeekday(): string
7244a83f5d7SGreg Roach    {
7254a83f5d7SGreg Roach        return I18N::digits(($this->minimum_julian_day + 1) % $this->calendar->daysInWeek());
7264a83f5d7SGreg Roach    }
7274a83f5d7SGreg Roach
7284a83f5d7SGreg Roach    /**
7294a83f5d7SGreg Roach     * Generate the %z format for a date.
7304a83f5d7SGreg Roach     *
7314a83f5d7SGreg Roach     * @return string
7324a83f5d7SGreg Roach     */
7334a83f5d7SGreg Roach    protected function formatDayOfYear(): string
7344a83f5d7SGreg Roach    {
7354a83f5d7SGreg Roach        return I18N::digits($this->minimum_julian_day - $this->calendar->ymdToJd($this->year, 1, 1));
7364a83f5d7SGreg Roach    }
7374a83f5d7SGreg Roach
7384a83f5d7SGreg Roach    /**
7394a83f5d7SGreg Roach     * Generate the %n format for a date.
7404a83f5d7SGreg Roach     *
7414a83f5d7SGreg Roach     * @return string
7424a83f5d7SGreg Roach     */
7434a83f5d7SGreg Roach    protected function formatMonth(): string
7444a83f5d7SGreg Roach    {
7454a83f5d7SGreg Roach        return I18N::digits($this->month);
7464a83f5d7SGreg Roach    }
7474a83f5d7SGreg Roach
7484a83f5d7SGreg Roach    /**
7494a83f5d7SGreg Roach     * Generate the %m format for a date.
7504a83f5d7SGreg Roach     *
7514a83f5d7SGreg Roach     * @return string
7524a83f5d7SGreg Roach     */
7534a83f5d7SGreg Roach    protected function formatMonthZeros(): string
7544a83f5d7SGreg Roach    {
7554a83f5d7SGreg Roach        if ($this->month > 9) {
7564a83f5d7SGreg Roach            return I18N::digits($this->month);
7574a83f5d7SGreg Roach        }
7584a83f5d7SGreg Roach
7594a83f5d7SGreg Roach        return I18N::digits('0' . $this->month);
7604a83f5d7SGreg Roach    }
7614a83f5d7SGreg Roach
7624a83f5d7SGreg Roach    /**
7634a83f5d7SGreg Roach     * Generate the %F format for a date.
7644a83f5d7SGreg Roach     *
7654a83f5d7SGreg Roach     * @param string $case Which grammatical case shall we use
7664a83f5d7SGreg Roach     *
7674a83f5d7SGreg Roach     * @return string
7684a83f5d7SGreg Roach     */
7694a83f5d7SGreg Roach    protected function formatLongMonth($case = 'NOMINATIVE'): string
7704a83f5d7SGreg Roach    {
7714a83f5d7SGreg Roach        switch ($case) {
7724a83f5d7SGreg Roach            case 'GENITIVE':
7734a83f5d7SGreg Roach                return $this->monthNameGenitiveCase($this->month, $this->isLeapYear());
7744a83f5d7SGreg Roach            case 'NOMINATIVE':
7754a83f5d7SGreg Roach                return $this->monthNameNominativeCase($this->month, $this->isLeapYear());
7764a83f5d7SGreg Roach            case 'LOCATIVE':
7774a83f5d7SGreg Roach                return $this->monthNameLocativeCase($this->month, $this->isLeapYear());
7784a83f5d7SGreg Roach            case 'INSTRUMENTAL':
7794a83f5d7SGreg Roach                return $this->monthNameInstrumentalCase($this->month, $this->isLeapYear());
7804a83f5d7SGreg Roach            default:
781*91495569SGreg Roach                throw new InvalidArgumentException($case);
7824a83f5d7SGreg Roach        }
7834a83f5d7SGreg Roach    }
7844a83f5d7SGreg Roach
7854a83f5d7SGreg Roach    /**
7867bb2eb25SGreg Roach     * Full month name in genitive case.
7877bb2eb25SGreg Roach     *
7887bb2eb25SGreg Roach     * @param int  $month
7897bb2eb25SGreg Roach     * @param bool $leap_year Some calendars use leap months
7907bb2eb25SGreg Roach     *
7917bb2eb25SGreg Roach     * @return string
7927bb2eb25SGreg Roach     */
7937bb2eb25SGreg Roach    abstract protected function monthNameGenitiveCase(int $month, bool $leap_year): string;
7947bb2eb25SGreg Roach
7957bb2eb25SGreg Roach    /**
7967bb2eb25SGreg Roach     * Full month name in nominative case.
7977bb2eb25SGreg Roach     *
7987bb2eb25SGreg Roach     * @param int  $month
7997bb2eb25SGreg Roach     * @param bool $leap_year Some calendars use leap months
8007bb2eb25SGreg Roach     *
8017bb2eb25SGreg Roach     * @return string
8027bb2eb25SGreg Roach     */
8037bb2eb25SGreg Roach    abstract protected function monthNameNominativeCase(int $month, bool $leap_year): string;
8047bb2eb25SGreg Roach
8057bb2eb25SGreg Roach    /**
8067bb2eb25SGreg Roach     * Full month name in locative case.
8077bb2eb25SGreg Roach     *
8087bb2eb25SGreg Roach     * @param int  $month
8097bb2eb25SGreg Roach     * @param bool $leap_year Some calendars use leap months
8107bb2eb25SGreg Roach     *
8117bb2eb25SGreg Roach     * @return string
8127bb2eb25SGreg Roach     */
8137bb2eb25SGreg Roach    abstract protected function monthNameLocativeCase(int $month, bool $leap_year): string;
8147bb2eb25SGreg Roach
8157bb2eb25SGreg Roach    /**
8167bb2eb25SGreg Roach     * Full month name in instrumental case.
8177bb2eb25SGreg Roach     *
8187bb2eb25SGreg Roach     * @param int  $month
8197bb2eb25SGreg Roach     * @param bool $leap_year Some calendars use leap months
8207bb2eb25SGreg Roach     *
8217bb2eb25SGreg Roach     * @return string
8227bb2eb25SGreg Roach     */
8237bb2eb25SGreg Roach    abstract protected function monthNameInstrumentalCase(int $month, bool $leap_year): string;
8247bb2eb25SGreg Roach
8257bb2eb25SGreg Roach    /**
8267bb2eb25SGreg Roach     * Abbreviated month name
8277bb2eb25SGreg Roach     *
8287bb2eb25SGreg Roach     * @param int  $month
8297bb2eb25SGreg Roach     * @param bool $leap_year Some calendars use leap months
8307bb2eb25SGreg Roach     *
8317bb2eb25SGreg Roach     * @return string
8327bb2eb25SGreg Roach     */
8337bb2eb25SGreg Roach    abstract protected function monthNameAbbreviated(int $month, bool $leap_year): string;
8347bb2eb25SGreg Roach
8357bb2eb25SGreg Roach    /**
8364a83f5d7SGreg Roach     * Generate the %M format for a date.
8374a83f5d7SGreg Roach     *
8384a83f5d7SGreg Roach     * @return string
8394a83f5d7SGreg Roach     */
8404a83f5d7SGreg Roach    protected function formatShortMonth(): string
8414a83f5d7SGreg Roach    {
8424a83f5d7SGreg Roach        return $this->monthNameAbbreviated($this->month, $this->isLeapYear());
8434a83f5d7SGreg Roach    }
8444a83f5d7SGreg Roach
8454a83f5d7SGreg Roach    /**
8464a83f5d7SGreg Roach     * Generate the %y format for a date.
8474a83f5d7SGreg Roach     * NOTE Short year is NOT a 2-digit year. It is for calendars such as hebrew
8484a83f5d7SGreg Roach     * which have a 3-digit form of 4-digit years.
8494a83f5d7SGreg Roach     *
8504a83f5d7SGreg Roach     * @return string
8514a83f5d7SGreg Roach     */
8524a83f5d7SGreg Roach    protected function formatShortYear(): string
8534a83f5d7SGreg Roach    {
8544a83f5d7SGreg Roach        return $this->formatLongYear();
8554a83f5d7SGreg Roach    }
8564a83f5d7SGreg Roach
8574a83f5d7SGreg Roach    /**
8584a83f5d7SGreg Roach     * Generate the %A format for a date.
8594a83f5d7SGreg Roach     *
8604a83f5d7SGreg Roach     * @return string
8614a83f5d7SGreg Roach     */
8624a83f5d7SGreg Roach    protected function formatGedcomDay(): string
8634a83f5d7SGreg Roach    {
8644a83f5d7SGreg Roach        if ($this->day == 0) {
8654a83f5d7SGreg Roach            return '';
8664a83f5d7SGreg Roach        }
8674a83f5d7SGreg Roach
8684a83f5d7SGreg Roach        return sprintf('%02d', $this->day);
8694a83f5d7SGreg Roach    }
8704a83f5d7SGreg Roach
8714a83f5d7SGreg Roach    /**
8724a83f5d7SGreg Roach     * Generate the %O format for a date.
8734a83f5d7SGreg Roach     *
8744a83f5d7SGreg Roach     * @return string
8754a83f5d7SGreg Roach     */
8764a83f5d7SGreg Roach    protected function formatGedcomMonth(): string
8774a83f5d7SGreg Roach    {
8784a83f5d7SGreg Roach        // Our simple lookup table doesn't work correctly for Adar on leap years
8794a83f5d7SGreg Roach        if ($this->month == 7 && $this->calendar instanceof JewishCalendar && !$this->calendar->isLeapYear($this->year)) {
8804a83f5d7SGreg Roach            return 'ADR';
8814a83f5d7SGreg Roach        }
8824a83f5d7SGreg Roach
8834a83f5d7SGreg Roach        return array_search($this->month, static::MONTH_ABBREVIATIONS);
8844a83f5d7SGreg Roach    }
8854a83f5d7SGreg Roach
8864a83f5d7SGreg Roach    /**
8874a83f5d7SGreg Roach     * Generate the %E format for a date.
8884a83f5d7SGreg Roach     *
8894a83f5d7SGreg Roach     * @return string
8904a83f5d7SGreg Roach     */
8914a83f5d7SGreg Roach    protected function formatGedcomYear(): string
8924a83f5d7SGreg Roach    {
8934a83f5d7SGreg Roach        if ($this->year == 0) {
8944a83f5d7SGreg Roach            return '';
8954a83f5d7SGreg Roach        }
8964a83f5d7SGreg Roach
8974a83f5d7SGreg Roach        return sprintf('%04d', $this->year);
8984a83f5d7SGreg Roach    }
8994a83f5d7SGreg Roach
9004a83f5d7SGreg Roach    /**
9014a83f5d7SGreg Roach     * Generate the %@ format for a calendar escape.
9024a83f5d7SGreg Roach     *
9034a83f5d7SGreg Roach     * @return string
9044a83f5d7SGreg Roach     */
9054a83f5d7SGreg Roach    protected function formatGedcomCalendarEscape(): string
9064a83f5d7SGreg Roach    {
9074a83f5d7SGreg Roach        return static::ESCAPE;
9084a83f5d7SGreg Roach    }
9094a83f5d7SGreg Roach
9104a83f5d7SGreg Roach    /**
9114a83f5d7SGreg Roach     * Generate the %Y format for a date.
9124a83f5d7SGreg Roach     *
9134a83f5d7SGreg Roach     * @return string
9144a83f5d7SGreg Roach     */
9154a83f5d7SGreg Roach    protected function formatLongYear(): string
9164a83f5d7SGreg Roach    {
9174a83f5d7SGreg Roach        return I18N::digits($this->year);
9184a83f5d7SGreg Roach    }
9194a83f5d7SGreg Roach
9204a83f5d7SGreg Roach    /**
9214a83f5d7SGreg Roach     * Which months follows this one? Calendars with leap-months should provide their own implementation.
9224a83f5d7SGreg Roach     *
9234a83f5d7SGreg Roach     * @return int[]
9244a83f5d7SGreg Roach     */
9254a83f5d7SGreg Roach    protected function nextMonth(): array
9264a83f5d7SGreg Roach    {
9274a83f5d7SGreg Roach        return [
9284a83f5d7SGreg Roach            $this->month === $this->calendar->monthsInYear() ? $this->nextYear($this->year) : $this->year,
9294a83f5d7SGreg Roach            ($this->month % $this->calendar->monthsInYear()) + 1,
9304a83f5d7SGreg Roach        ];
9314a83f5d7SGreg Roach    }
9324a83f5d7SGreg Roach
9334a83f5d7SGreg Roach    /**
9344a83f5d7SGreg Roach     * Get today’s date in the current calendar.
9354a83f5d7SGreg Roach     *
9364a83f5d7SGreg Roach     * @return int[]
9374a83f5d7SGreg Roach     */
9384a83f5d7SGreg Roach    public function todayYmd(): array
9394a83f5d7SGreg Roach    {
9404a83f5d7SGreg Roach        return $this->calendar->jdToYmd(unixtojd());
9414a83f5d7SGreg Roach    }
9424a83f5d7SGreg Roach
9434a83f5d7SGreg Roach    /**
9444a83f5d7SGreg Roach     * Convert to today’s date.
9454a83f5d7SGreg Roach     *
9464a83f5d7SGreg Roach     * @return AbstractCalendarDate
9474a83f5d7SGreg Roach     */
9484a83f5d7SGreg Roach    public function today(): AbstractCalendarDate
9494a83f5d7SGreg Roach    {
9504a83f5d7SGreg Roach        $tmp        = clone $this;
9514a83f5d7SGreg Roach        $ymd        = $tmp->todayYmd();
9524a83f5d7SGreg Roach        $tmp->year  = $ymd[0];
9534a83f5d7SGreg Roach        $tmp->month = $ymd[1];
9544a83f5d7SGreg Roach        $tmp->day   = $ymd[2];
9554a83f5d7SGreg Roach        $tmp->setJdFromYmd();
9564a83f5d7SGreg Roach
9574a83f5d7SGreg Roach        return $tmp;
9584a83f5d7SGreg Roach    }
9594a83f5d7SGreg Roach
9604a83f5d7SGreg Roach    /**
9614a83f5d7SGreg Roach     * Create a URL that links this date to the WT calendar
9624a83f5d7SGreg Roach     *
9634a83f5d7SGreg Roach     * @param string $date_format
96449d5f1d7SGreg Roach     * @param Tree   $tree
9654a83f5d7SGreg Roach     *
9664a83f5d7SGreg Roach     * @return string
9674a83f5d7SGreg Roach     */
96849d5f1d7SGreg Roach    public function calendarUrl(string $date_format, Tree $tree): string
9694a83f5d7SGreg Roach    {
9704a83f5d7SGreg Roach        if (strpbrk($date_format, 'dDj') && $this->day) {
9714a83f5d7SGreg Roach            // If the format includes a day, and the date also includes a day, then use the day view
9724a83f5d7SGreg Roach            $view = 'day';
9734a83f5d7SGreg Roach        } elseif (strpbrk($date_format, 'FMmn') && $this->month) {
9744a83f5d7SGreg Roach            // If the format includes a month, and the date also includes a month, then use the month view
9754a83f5d7SGreg Roach            $view = 'month';
9764a83f5d7SGreg Roach        } else {
9774a83f5d7SGreg Roach            // Use the year view
9784a83f5d7SGreg Roach            $view = 'year';
9794a83f5d7SGreg Roach        }
9804a83f5d7SGreg Roach
9814a83f5d7SGreg Roach        return route('calendar', [
9824a83f5d7SGreg Roach            'cal'   => $this->calendar->gedcomCalendarEscape(),
9834a83f5d7SGreg Roach            'year'  => $this->formatGedcomYear(),
9844a83f5d7SGreg Roach            'month' => $this->formatGedcomMonth(),
9854a83f5d7SGreg Roach            'day'   => $this->formatGedcomDay(),
9864a83f5d7SGreg Roach            'view'  => $view,
987aa6f03bbSGreg Roach            'ged'   => $tree->name(),
9884a83f5d7SGreg Roach        ]);
9894a83f5d7SGreg Roach    }
9904a83f5d7SGreg Roach}
991