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 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 $TEXT_DIRECTION, $WT_TREE; 213 214 $CALENDAR_FORMAT = $WT_TREE->getPreference('CALENDAR_FORMAT'); 215 $DATE_FORMAT = $WT_TREE->getPreference('DATE_FORMAT'); 216 217 if ($date_format === null) { 218 $date_format = $DATE_FORMAT; 219 } 220 221 if ($convert_calendars) { 222 $calendar_format = explode('_and_', $CALENDAR_FORMAT); 223 } else { 224 $calendar_format = array(); 225 } 226 227 // Two dates with text before, between and after 228 $q1 = $this->qual1; 229 $d1 = $this->date1->format($date_format, $this->qual1); 230 $q2 = $this->qual2; 231 if (is_null($this->date2)) { 232 $d2 = ''; 233 } else { 234 $d2 = $this->date2->format($date_format, $this->qual2); 235 } 236 // Con vert to other calendars, if requested 237 $conv1 = ''; 238 $conv2 = ''; 239 foreach ($calendar_format as $cal_fmt) { 240 if ($cal_fmt != 'none') { 241 $d1conv = $this->date1->convertToCalendar($cal_fmt); 242 if ($d1conv->inValidRange()) { 243 $d1tmp = $d1conv->format($date_format, $this->qual1); 244 } else { 245 $d1tmp = ''; 246 } 247 if (is_null($this->date2)) { 248 $d2conv = null; 249 $d2tmp = ''; 250 } else { 251 $d2conv = $this->date2->convertToCalendar($cal_fmt); 252 if ($d2conv->inValidRange()) { 253 $d2tmp = $d2conv->format($date_format, $this->qual2); 254 } else { 255 $d2tmp = ''; 256 } 257 } 258 // If the date is different to the unconverted date, add it to the date string. 259 if ($d1 != $d1tmp && $d1tmp != '') { 260 if ($url) { 261 if ($CALENDAR_FORMAT != "none") { 262 $conv1 .= ' <span dir="' . $TEXT_DIRECTION . '">(<a href="' . $d1conv->calendarUrl($date_format) . '">' . $d1tmp . '</a>)</span>'; 263 } else { 264 $conv1 .= ' <span dir="' . $TEXT_DIRECTION . '"><br><a href="' . $d1conv->calendarUrl($date_format) . '">' . $d1tmp . '</a></span>'; 265 } 266 } else { 267 $conv1 .= ' <span dir="' . $TEXT_DIRECTION . '">(' . $d1tmp . ')</span>'; 268 } 269 } 270 if (!is_null($this->date2) && $d2 != $d2tmp && $d1tmp != '') { 271 if ($url) { 272 $conv2 .= ' <span dir="' . $TEXT_DIRECTION . '">(<a href="' . $d2conv->calendarUrl($date_format) . '">' . $d2tmp . '</a>)</span>'; 273 } else { 274 $conv2 .= ' <span dir="' . $TEXT_DIRECTION . '">(' . $d2tmp . ')</span>'; 275 } 276 } 277 } 278 } 279 280 // Add URLs, if requested 281 if ($url) { 282 $d1 = '<a href="' . $this->date1->calendarUrl($date_format) . '">' . $d1 . '</a>'; 283 if (!is_null($this->date2)) { 284 $d2 = '<a href="' . $this->date2->calendarUrl($date_format) . '">' . $d2 . '</a>'; 285 } 286 } 287 288 // Localise the date 289 switch ($q1 . $q2) { 290 case '': 291 $tmp = $d1 . $conv1; 292 break; 293 case 'ABT': 294 $tmp = /* I18N: Gedcom ABT dates */ I18N::translate('about %s', $d1 . $conv1); 295 break; 296 case 'CAL': 297 $tmp = /* I18N: Gedcom CAL dates */ I18N::translate('calculated %s', $d1 . $conv1); 298 break; 299 case 'EST': 300 $tmp = /* I18N: Gedcom EST dates */ I18N::translate('estimated %s', $d1 . $conv1); 301 break; 302 case 'INT': 303 $tmp = /* I18N: Gedcom INT dates */ I18N::translate('interpreted %s (%s)', $d1 . $conv1, $this->text); 304 break; 305 case 'BEF': 306 $tmp = /* I18N: Gedcom BEF dates */ I18N::translate('before %s', $d1 . $conv1); 307 break; 308 case 'AFT': 309 $tmp = /* I18N: Gedcom AFT dates */ I18N::translate('after %s', $d1 . $conv1); 310 break; 311 case 'FROM': 312 $tmp = /* I18N: Gedcom FROM dates */ I18N::translate('from %s', $d1 . $conv1); 313 break; 314 case 'TO': 315 $tmp = /* I18N: Gedcom TO dates */ I18N::translate('to %s', $d1 . $conv1); 316 break; 317 case 'BETAND': 318 $tmp = /* I18N: Gedcom BET-AND dates */ I18N::translate('between %s and %s', $d1 . $conv1, $d2 . $conv2); 319 break; 320 case 'FROMTO': 321 $tmp = /* I18N: Gedcom FROM-TO dates */ I18N::translate('from %s to %s', $d1 . $conv1, $d2 . $conv2); 322 break; 323 default: 324 $tmp = I18N::translate('Invalid date'); 325 break; // e.g. BET without AND 326 } 327 if ($this->text && !$q1) { 328 $tmp = I18N::translate('%1$s (%2$s)', $tmp, $this->text); 329 } 330 331 // Return at least one printable character, for better formatting in tables. 332 if (strip_tags($tmp) == '') { 333 return ' '; 334 } else { 335 return "<span class=\"date\">{$tmp}</span>"; 336 } 337 } 338 339 /** 340 * Get the earliest calendar date from this GEDCOM date. 341 * 342 * In the date “FROM 1900 TO 1910”, this would be 1900. 343 * 344 * @return CalendarDate 345 */ 346 function MinDate() { 347 return $this->date1; 348 } 349 350 /** 351 * Get the latest calendar date from this GEDCOM date. 352 * 353 * In the date “FROM 1900 TO 1910”, this would be 1910. 354 * 355 * @return CalendarDate 356 */ 357 function MaxDate() { 358 if (is_null($this->date2)) { 359 return $this->date1; 360 } else { 361 return $this->date2; 362 } 363 } 364 365 /** 366 * Get the earliest Julian day number from this GEDCOM date. 367 * 368 * @return integer 369 */ 370 function MinJD() { 371 $tmp = $this->MinDate(); 372 373 return $tmp->minJD; 374 } 375 376 /** 377 * Get the latest Julian day number from this GEDCOM date. 378 * 379 * @return integer 380 */ 381 function MaxJD() { 382 $tmp = $this->MaxDate(); 383 384 return $tmp->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 16 day. 391 * For a year-only date, this would be somewhere around 1st July. 392 * 393 * @return integer 394 */ 395 function JD() { 396 return (int) (($this->MinJD() + $this->MaxJD()) / 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 integer $years - a number of years, positive or negative 406 * @param string $qualifier - typically “BEF” or “AFT” 407 * 408 * @return Date 409 */ 410 function AddYears($years, $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 integer $format 429 * 430 * @return int|string 431 * @throws \InvalidArgumentException 432 */ 433 static function getAge(Date $d1, Date $d2 = null, $format = 0) { 434 if ($d2) { 435 if ($d2->MaxJD() >= $d1->MinJD() && $d2->MinJD() <= $d1->MinJD()) { 436 // Overlapping dates 437 $jd = $d1->MinJD(); 438 } else { 439 // Non-overlapping dates 440 $jd = $d2->MinJD(); 441 } 442 } else { 443 // If second date not specified, use today’s date 444 $jd = WT_CLIENT_JD; 445 } 446 447 switch ($format) { 448 case 0: // Years - integer only (for statistics, rather than for display) 449 if ($jd && $d1->MinJD() && $d1->MinJD() <= $jd) { 450 return $d1->MinDate()->getAge(false, $jd, false); 451 } else { 452 return -1; 453 } 454 case 1: // Days - integer only (for sorting, rather than for display) 455 if ($jd && $d1->MinJD()) { 456 return $jd - $d1->MinJD(); 457 } else { 458 return -1; 459 } 460 case 2: // Just years, in local digits, with warning for negative/ 461 if ($jd && $d1->MinJD()) { 462 if ($d1->MinJD() > $jd) { 463 return '<i class="icon-warning"></i>'; 464 } else { 465 return I18N::number($d1->MinDate()->getAge(false, $jd)); 466 } 467 } else { 468 return ' '; 469 } 470 default: 471 throw new \InvalidArgumentException('format: ' . $format); 472 // TODO: combine GetAgeGedcom() into this function 473 } 474 } 475 476 /** 477 * Calculate the years/months/days between two events 478 * Return a gedcom style age string: "1y 2m 3d" (for fact details) 479 * 480 * @param Date $d1 481 * @param Date|null $d2 482 * @param boolean $warn_on_negative 483 * 484 * @return string 485 */ 486 static function GetAgeGedcom(Date $d1, Date $d2 = null, $warn_on_negative = true) { 487 if (is_null($d2)) { 488 return $d1->date1->GetAge(true, WT_CLIENT_JD, $warn_on_negative); 489 } else { 490 // If dates overlap, then can’t calculate age. 491 if (self::Compare($d1, $d2)) { 492 return $d1->date1->GetAge(true, $d2->MinJD(), $warn_on_negative); 493 } elseif (self::Compare($d1, $d2) == 0 && $d1->date1->minJD == $d2->MinJD()) { 494 return '0d'; 495 } else { 496 return ''; 497 } 498 } 499 } 500 501 /** 502 * Compare two dates, so they can be sorted. 503 * 504 * return <0 if $a<$b 505 * return >0 if $b>$a 506 * return 0 if dates same/overlap 507 * BEF/AFT sort as the day before/after 508 * 509 * @param Date $a 510 * @param Date $b 511 * 512 * @return integer 513 */ 514 static function Compare(Date $a, Date $b) { 515 // Get min/max JD for each date. 516 switch ($a->qual1) { 517 case 'BEF': 518 $amin = $a->MinJD() - 1; 519 $amax = $amin; 520 break; 521 case 'AFT': 522 $amax = $a->MaxJD() + 1; 523 $amin = $amax; 524 break; 525 default: 526 $amin = $a->MinJD(); 527 $amax = $a->MaxJD(); 528 break; 529 } 530 switch ($b->qual1) { 531 case 'BEF': 532 $bmin = $b->MinJD() - 1; 533 $bmax = $bmin; 534 break; 535 case 'AFT': 536 $bmax = $b->MaxJD() + 1; 537 $bmin = $bmax; 538 break; 539 default: 540 $bmin = $b->MinJD(); 541 $bmax = $b->MaxJD(); 542 break; 543 } 544 if ($amax < $bmin) { 545 return -1; 546 } elseif ($amin > $bmax && $bmax > 0) { 547 return 1; 548 } elseif ($amin < $bmin && $amax <= $bmax) { 549 return -1; 550 } elseif ($amin > $bmin && $amax >= $bmax && $bmax > 0) { 551 return 1; 552 } else { 553 return 0; 554 } 555 } 556 557 /** 558 * Check whether a gedcom date contains usable calendar date(s). 559 * 560 * An incomplete date such as "12 AUG" would be invalid, as 561 * we cannot sort it. 562 * 563 * @return boolean 564 */ 565 function isOK() { 566 return $this->MinJD() && $this->MaxJD(); 567 } 568 569 /** 570 * Calculate the gregorian year for a date. This should NOT be used internally 571 * within WT - we should keep the code "calendar neutral" to allow support for 572 * jewish/arabic users. This is only for interfacing with external entities, 573 * such as the ancestry.com search interface or the dated fact icons. 574 * 575 * @return integer 576 */ 577 function gregorianYear() { 578 if ($this->isOK()) { 579 $gregorian_calendar = new GregorianCalendar; 580 list($year) = $gregorian_calendar->jdToYmd($this->JD()); 581 582 return $year; 583 } else { 584 return 0; 585 } 586 } 587} 588