Passed
Branch master (380e00)
by Greg
20:17
created

Date::minimumDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * webtrees: online genealogy
4
 * Copyright (C) 2017 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
 */
16
namespace Fisharebest\Webtrees;
17
18
use Fisharebest\ExtCalendar\GregorianCalendar;
19
use Fisharebest\Webtrees\Date\CalendarDate;
20
use Fisharebest\Webtrees\Date\FrenchDate;
21
use Fisharebest\Webtrees\Date\GregorianDate;
22
use Fisharebest\Webtrees\Date\HijriDate;
23
use Fisharebest\Webtrees\Date\JalaliDate;
24
use Fisharebest\Webtrees\Date\JewishDate;
25
use Fisharebest\Webtrees\Date\JulianDate;
26
use 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
 */
39
class 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
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use GregorianDate|JulianDate...e|FrenchDate|JalaliDate.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
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 View Code Duplication
			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 View Code Duplication
			} 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 View Code Duplication
		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[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array<string,string>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
		global $WT_TREE;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
218
219
		$CALENDAR_FORMAT = $WT_TREE->getPreference('CALENDAR_FORMAT');
220
221
		if ($date_format === null) {
222
			$date_format = I18N::dateFormat();
223
		}
224
225
		if ($convert_calendars) {
226
			$calendar_format = explode('_and_', $CALENDAR_FORMAT);
227
		} else {
228
			$calendar_format = [];
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 from 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) . '" rel="nofollow">' . $d1tmp . '</a>)</span>';
267
						} else {
268
							$conv1 .= ' <span dir="' . I18N::direction() . '"><br><a href="' . $d1conv->calendarUrl($date_format) . '" rel="nofollow">' . $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) . '" rel="nofollow">' . $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) . '" rel="nofollow">' . $d1 . '</a>';
287
			if (!is_null($this->date2)) {
288
				$d2 = '<a href="' . $this->date2->calendarUrl($date_format) . '" rel="nofollow">' . $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
	public function minimumDate() {
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
	public function maximumDate() {
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 int
372
	 */
373
	public function minimumJulianDay() {
374
		return $this->minimumDate()->minJD;
375
	}
376
377
	/**
378
	 * Get the latest Julian day number from this GEDCOM date.
379
	 *
380
	 * @return int
381
	 */
382
	public function maximumJulianDay() {
383
		return $this->maximumDate()->maxJD;
384
	}
385
386
	/**
387
	 * Get the middle Julian day number from the GEDCOM date.
388
	 *
389
	 * For a month-only date, this would be somewhere around the 16th day.
390
	 * For a year-only date, this would be somewhere around 1st July.
391
	 *
392
	 * @return int
393
	 */
394
	public function julianDay() {
395
		return (int) (($this->minimumJulianDay() + $this->maximumJulianDay()) / 2);
396
	}
397
398
	/**
399
	 * Offset this date by N years, and round to the whole year.
400
	 *
401
	 * This is typically used to create an estimated death date,
402
	 * which is before a certain number of years after the birth date.
403
	 *
404
	 * @param int     $years     a number of years, positive or negative
405
	 * @param string  $qualifier typically “BEF” or “AFT”
406
	 *
407
	 * @return Date
408
	 */
409
	public function addYears(int $years, string $qualifier = '') {
410
		$tmp = clone $this;
411
		$tmp->date1->y += $years;
412
		$tmp->date1->m = 0;
413
		$tmp->date1->d = 0;
414
		$tmp->date1->setJdFromYmd();
415
		$tmp->qual1 = $qualifier;
416
		$tmp->qual2 = '';
417
		$tmp->date2 = null;
418
419
		return $tmp;
420
	}
421
422
	/**
423
	 * Calculate the the age of a person, on a date.
424
	 *
425
	 * @param Date $d1
426
	 * @param Date $d2
0 ignored issues
show
Documentation introduced by
Should the type for parameter $d2 not be null|Date?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
427
	 * @param int  $format
428
	 *
429
	 * @throws \InvalidArgumentException
430
	 *
431
	 * @return int|string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
432
	 */
433
	public static function getAge(Date $d1, Date $d2 = null, $format = 0) {
434
		if ($d2) {
435
			if ($d2->maximumJulianDay() >= $d1->minimumJulianDay() && $d2->minimumJulianDay() <= $d1->minimumJulianDay()) {
436
				// Overlapping dates
437
				$jd = $d1->minimumJulianDay();
438
			} else {
439
				// Non-overlapping dates
440
				$jd = $d2->minimumJulianDay();
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:
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
449
				// Years - integer only (for statistics, rather than for display)
450
				if ($jd && $d1->minimumJulianDay() && $d1->minimumJulianDay() <= $jd) {
451
					return $d1->minimumDate()->getAge(false, $jd, false);
452
				} else {
453
					return -1;
454
				}
455
			case 1:
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
456
				// Days - integer only (for sorting, rather than for display)
457
				if ($jd && $d1->minimumJulianDay()) {
458
					return $jd - $d1->minimumJulianDay();
459
				} else {
460
					return -1;
461
				}
462
			case 2:
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
463
				// Just years, in local digits, with warning for negative/
464
				if ($jd && $d1->minimumJulianDay()) {
465
					if ($d1->minimumJulianDay() > $jd) {
466
						return '<i class="icon-warning"></i>';
467
					} else {
468
						return I18N::number($d1->minimumDate()->getAge(false, $jd));
469
					}
470
				} else {
471
					return '';
472
				}
473
			default:
474
				throw new \InvalidArgumentException('format: ' . $format);
475
		}
476
	}
477
478
	/**
479
	 * Calculate the years/months/days between two events
480
	 * Return a gedcom style age string: "1y 2m 3d" (for fact details)
481
	 *
482
	 * @param Date      $d1
483
	 * @param Date|null $d2
484
	 *
485
	 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
486
	 */
487
	public static function getAgeGedcom(Date $d1, Date $d2 = null) {
488
		if (is_null($d2)) {
489
			return $d1->date1->getAge(true, WT_CLIENT_JD, true);
490
		} else {
491
			// If dates overlap, then can’t calculate age.
492
			if (self::compare($d1, $d2)) {
493
				return $d1->date1->getAge(true, $d2->minimumJulianDay(), true);
494
			} elseif (self::compare($d1, $d2) == 0 && $d1->minimumJulianDay() == $d2->minimumJulianDay()) {
495
				return '0d';
496
			} else {
497
				return '';
498
			}
499
		}
500
	}
501
502
	/**
503
	 * Compare two dates, so they can be sorted.
504
	 *
505
	 * return <0 if $a<$b
506
	 * return >0 if $b>$a
507
	 * return  0 if dates same/overlap
508
	 * BEF/AFT sort as the day before/after
509
	 *
510
	 * @param Date $a
511
	 * @param Date $b
512
	 *
513
	 * @return int
514
	 */
515
	public static function compare(Date $a, Date $b) {
516
		// Get min/max JD for each date.
517 View Code Duplication
		switch ($a->qual1) {
518
			case 'BEF':
519
				$amin = $a->minimumJulianDay() - 1;
520
				$amax = $amin;
521
				break;
522
			case 'AFT':
523
				$amax = $a->maximumJulianDay() + 1;
524
				$amin = $amax;
525
				break;
526
			default:
527
				$amin = $a->minimumJulianDay();
528
				$amax = $a->maximumJulianDay();
529
				break;
530
		}
531 View Code Duplication
		switch ($b->qual1) {
532
			case 'BEF':
533
				$bmin = $b->minimumJulianDay() - 1;
534
				$bmax = $bmin;
535
				break;
536
			case 'AFT':
537
				$bmax = $b->maximumJulianDay() + 1;
538
				$bmin = $bmax;
539
				break;
540
			default:
541
				$bmin = $b->minimumJulianDay();
542
				$bmax = $b->maximumJulianDay();
543
				break;
544
		}
545
		if ($amax < $bmin) {
546
			return -1;
547
		} elseif ($amin > $bmax && $bmax > 0) {
548
			return 1;
549
		} elseif ($amin < $bmin && $amax <= $bmax) {
550
			return -1;
551
		} elseif ($amin > $bmin && $amax >= $bmax && $bmax > 0) {
552
			return 1;
553
		} else {
554
			return 0;
555
		}
556
	}
557
558
	/**
559
	 * Check whether a gedcom date contains usable calendar date(s).
560
	 *
561
	 * An incomplete date such as "12 AUG" would be invalid, as
562
	 * we cannot sort it.
563
	 *
564
	 * @return bool
565
	 */
566
	public function isOK() {
567
		return $this->minimumJulianDay() && $this->maximumJulianDay();
568
	}
569
570
	/**
571
	 * Calculate the gregorian year for a date. This should NOT be used internally
572
	 * within WT - we should keep the code "calendar neutral" to allow support for
573
	 * jewish/arabic users. This is only for interfacing with external entities,
574
	 * such as the ancestry.com search interface or the dated fact icons.
575
	 *
576
	 * @return int
577
	 */
578
	public function gregorianYear() {
579
		if ($this->isOK()) {
580
			$gregorian_calendar = new GregorianCalendar;
581
			list($year)         = $gregorian_calendar->jdToYmd($this->julianDay());
582
583
			return $year;
584
		} else {
585
			return 0;
586
		}
587
	}
588
}
589