1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 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_match_all; 38use function preg_replace; 39use function route; 40use function sprintf; 41use function str_replace; 42use function strpbrk; 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 // Build up the formatted date, character at a time 612 preg_match_all('/%[^%]/', $format, $matches); 613 foreach ($matches[0] as $match) { 614 switch ($match) { 615 case '%d': 616 $format = str_replace($match, $this->formatDayZeros(), $format); 617 break; 618 case '%j': 619 $format = str_replace($match, $this->formatDay(), $format); 620 break; 621 case '%l': 622 $format = str_replace($match, $this->formatLongWeekday(), $format); 623 break; 624 case '%D': 625 $format = str_replace($match, $this->formatShortWeekday(), $format); 626 break; 627 case '%N': 628 $format = str_replace($match, $this->formatIsoWeekday(), $format); 629 break; 630 case '%w': 631 $format = str_replace($match, $this->formatNumericWeekday(), $format); 632 break; 633 case '%z': 634 $format = str_replace($match, $this->formatDayOfYear(), $format); 635 break; 636 case '%F': 637 $format = str_replace($match, $this->formatLongMonth($case), $format); 638 break; 639 case '%m': 640 $format = str_replace($match, $this->formatMonthZeros(), $format); 641 break; 642 case '%M': 643 $format = str_replace($match, $this->formatShortMonth(), $format); 644 break; 645 case '%n': 646 $format = str_replace($match, $this->formatMonth(), $format); 647 break; 648 case '%t': 649 $format = str_replace($match, (string) $this->daysInMonth(), $format); 650 break; 651 case '%L': 652 $format = str_replace($match, $this->isLeapYear() ? '1' : '0', $format); 653 break; 654 case '%Y': 655 $format = str_replace($match, $this->formatLongYear(), $format); 656 break; 657 case '%y': 658 $format = str_replace($match, $this->formatShortYear(), $format); 659 break; 660 // These 4 extensions are useful for re-formatting gedcom dates. 661 case '%@': 662 $format = str_replace($match, $this->formatGedcomCalendarEscape(), $format); 663 break; 664 case '%A': 665 $format = str_replace($match, $this->formatGedcomDay(), $format); 666 break; 667 case '%O': 668 $format = str_replace($match, $this->formatGedcomMonth(), $format); 669 break; 670 case '%E': 671 $format = str_replace($match, $this->formatGedcomYear(), $format); 672 break; 673 } 674 } 675 676 return $format; 677 } 678 679 /** 680 * Generate the %d format for a date. 681 * 682 * @return string 683 */ 684 protected function formatDayZeros(): string 685 { 686 if ($this->day > 9) { 687 return I18N::digits($this->day); 688 } 689 690 return I18N::digits('0' . $this->day); 691 } 692 693 /** 694 * Generate the %j format for a date. 695 * 696 * @return string 697 */ 698 protected function formatDay(): string 699 { 700 return I18N::digits($this->day); 701 } 702 703 /** 704 * Generate the %l format for a date. 705 * 706 * @return string 707 */ 708 protected function formatLongWeekday(): string 709 { 710 return $this->dayNames($this->minimum_julian_day % $this->calendar->daysInWeek()); 711 } 712 713 /** 714 * Generate the %D format for a date. 715 * 716 * @return string 717 */ 718 protected function formatShortWeekday(): string 719 { 720 return $this->dayNamesAbbreviated($this->minimum_julian_day % $this->calendar->daysInWeek()); 721 } 722 723 /** 724 * Generate the %N format for a date. 725 * 726 * @return string 727 */ 728 protected function formatIsoWeekday(): string 729 { 730 return I18N::digits($this->minimum_julian_day % 7 + 1); 731 } 732 733 /** 734 * Generate the %w format for a date. 735 * 736 * @return string 737 */ 738 protected function formatNumericWeekday(): string 739 { 740 return I18N::digits(($this->minimum_julian_day + 1) % $this->calendar->daysInWeek()); 741 } 742 743 /** 744 * Generate the %z format for a date. 745 * 746 * @return string 747 */ 748 protected function formatDayOfYear(): string 749 { 750 return I18N::digits($this->minimum_julian_day - $this->calendar->ymdToJd($this->year, 1, 1)); 751 } 752 753 /** 754 * Generate the %n format for a date. 755 * 756 * @return string 757 */ 758 protected function formatMonth(): string 759 { 760 return I18N::digits($this->month); 761 } 762 763 /** 764 * Generate the %m format for a date. 765 * 766 * @return string 767 */ 768 protected function formatMonthZeros(): string 769 { 770 if ($this->month > 9) { 771 return I18N::digits($this->month); 772 } 773 774 return I18N::digits('0' . $this->month); 775 } 776 777 /** 778 * Generate the %F format for a date. 779 * 780 * @param string $case Which grammatical case shall we use 781 * 782 * @return string 783 */ 784 protected function formatLongMonth($case = 'NOMINATIVE'): string 785 { 786 switch ($case) { 787 case 'GENITIVE': 788 return $this->monthNameGenitiveCase($this->month, $this->isLeapYear()); 789 case 'NOMINATIVE': 790 return $this->monthNameNominativeCase($this->month, $this->isLeapYear()); 791 case 'LOCATIVE': 792 return $this->monthNameLocativeCase($this->month, $this->isLeapYear()); 793 case 'INSTRUMENTAL': 794 return $this->monthNameInstrumentalCase($this->month, $this->isLeapYear()); 795 default: 796 throw new InvalidArgumentException($case); 797 } 798 } 799 800 /** 801 * Full month name in genitive case. 802 * 803 * @param int $month 804 * @param bool $leap_year Some calendars use leap months 805 * 806 * @return string 807 */ 808 abstract protected function monthNameGenitiveCase(int $month, bool $leap_year): string; 809 810 /** 811 * Full month name in nominative case. 812 * 813 * @param int $month 814 * @param bool $leap_year Some calendars use leap months 815 * 816 * @return string 817 */ 818 abstract protected function monthNameNominativeCase(int $month, bool $leap_year): string; 819 820 /** 821 * Full month name in locative case. 822 * 823 * @param int $month 824 * @param bool $leap_year Some calendars use leap months 825 * 826 * @return string 827 */ 828 abstract protected function monthNameLocativeCase(int $month, bool $leap_year): string; 829 830 /** 831 * Full month name in instrumental case. 832 * 833 * @param int $month 834 * @param bool $leap_year Some calendars use leap months 835 * 836 * @return string 837 */ 838 abstract protected function monthNameInstrumentalCase(int $month, bool $leap_year): string; 839 840 /** 841 * Abbreviated month name 842 * 843 * @param int $month 844 * @param bool $leap_year Some calendars use leap months 845 * 846 * @return string 847 */ 848 abstract protected function monthNameAbbreviated(int $month, bool $leap_year): string; 849 850 /** 851 * Generate the %M format for a date. 852 * 853 * @return string 854 */ 855 protected function formatShortMonth(): string 856 { 857 return $this->monthNameAbbreviated($this->month, $this->isLeapYear()); 858 } 859 860 /** 861 * Generate the %y format for a date. 862 * NOTE Short year is NOT a 2-digit year. It is for calendars such as hebrew 863 * which have a 3-digit form of 4-digit years. 864 * 865 * @return string 866 */ 867 protected function formatShortYear(): string 868 { 869 return $this->formatLongYear(); 870 } 871 872 /** 873 * Generate the %A format for a date. 874 * 875 * @return string 876 */ 877 protected function formatGedcomDay(): string 878 { 879 if ($this->day === 0) { 880 return ''; 881 } 882 883 return sprintf('%02d', $this->day); 884 } 885 886 /** 887 * Generate the %O format for a date. 888 * 889 * @return string 890 */ 891 protected function formatGedcomMonth(): string 892 { 893 // Our simple lookup table doesn't work correctly for Adar on leap years 894 if ($this->month === 7 && $this->calendar instanceof JewishCalendar && !$this->calendar->isLeapYear($this->year)) { 895 return 'ADR'; 896 } 897 898 return array_search($this->month, static::MONTH_ABBREVIATIONS, true); 899 } 900 901 /** 902 * Generate the %E format for a date. 903 * 904 * @return string 905 */ 906 protected function formatGedcomYear(): string 907 { 908 if ($this->year === 0) { 909 return ''; 910 } 911 912 return sprintf('%04d', $this->year); 913 } 914 915 /** 916 * Generate the %@ format for a calendar escape. 917 * 918 * @return string 919 */ 920 protected function formatGedcomCalendarEscape(): string 921 { 922 return static::ESCAPE; 923 } 924 925 /** 926 * Generate the %Y format for a date. 927 * 928 * @return string 929 */ 930 protected function formatLongYear(): string 931 { 932 return I18N::digits($this->year); 933 } 934 935 /** 936 * Which months follows this one? Calendars with leap-months should provide their own implementation. 937 * 938 * @return int[] 939 */ 940 protected function nextMonth(): array 941 { 942 return [ 943 $this->month === $this->calendar->monthsInYear() ? $this->nextYear($this->year) : $this->year, 944 $this->month % $this->calendar->monthsInYear() + 1, 945 ]; 946 } 947 948 /** 949 * Get today’s date in the current calendar. 950 * 951 * @return int[] 952 */ 953 public function todayYmd(): array 954 { 955 return $this->calendar->jdToYmd(Carbon::now()->julianDay()); 956 } 957 958 /** 959 * Convert to today’s date. 960 * 961 * @return AbstractCalendarDate 962 */ 963 public function today(): AbstractCalendarDate 964 { 965 $tmp = clone $this; 966 $ymd = $tmp->todayYmd(); 967 $tmp->year = $ymd[0]; 968 $tmp->month = $ymd[1]; 969 $tmp->day = $ymd[2]; 970 $tmp->setJdFromYmd(); 971 972 return $tmp; 973 } 974 975 /** 976 * Create a URL that links this date to the WT calendar 977 * 978 * @param string $date_format 979 * @param Tree $tree 980 * 981 * @return string 982 */ 983 public function calendarUrl(string $date_format, Tree $tree): string 984 { 985 if ($this->day !== 0 && strpbrk($date_format, 'dDj')) { 986 // If the format includes a day, and the date also includes a day, then use the day view 987 $view = 'day'; 988 } elseif ($this->month !== 0 && strpbrk($date_format, 'FMmn')) { 989 // If the format includes a month, and the date also includes a month, then use the month view 990 $view = 'month'; 991 } else { 992 // Use the year view 993 $view = 'year'; 994 } 995 996 return route('calendar', [ 997 'cal' => $this->calendar->gedcomCalendarEscape(), 998 'year' => $this->formatGedcomYear(), 999 'month' => $this->formatGedcomMonth(), 1000 'day' => $this->formatGedcomDay(), 1001 'view' => $view, 1002 'tree' => $tree->name(), 1003 ]); 1004 } 1005} 1006