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