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