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