-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_date.php
More file actions
1344 lines (1233 loc) · 40.9 KB
/
class_date.php
File metadata and controls
1344 lines (1233 loc) · 40.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Classes for Gedcom Date/Calendar functionality.
*
* phpGedView: Genealogy Viewer
* Copyright (C) 2007 to 2008 PGV Development Team. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @package PhpGedView
* @author Greg Roach
* @version $Id: class_date.php 6634 2009-12-28 14:15:49Z fisharebest $
*
* NOTE: Since different calendars start their days at different times, (civil
* midnight, solar midnight, sunset, sunrise, etc.), we convert on the basis of
* midday.
*
* NOTE: We assume that years start on the first day of the first month. Where
* this is not the case (e.g. England prior to 1752), we need to use modified
* years or the OS/NS notation "4 FEB 1750/51".
*
* NOTE: PGV should only be using the GedcomDate class. The other classes
* are all for internal use only.
*/
if (!defined('PGV_PHPGEDVIEW')) {
header('HTTP/1.0 403 Forbidden');
exit;
}
define('PGV_CLASS_DATE_PHP', '');
////////////////////////////////////////////////////////////////////////////////
//
// CalendarDate is a base class for classes such as GregorianDate, etc.
//
// + All supported calendars have non-zero days/months/years.
// + We store dates as both Y/M/D and Julian Days.
// + For imprecise dates such as "JAN 2000" we store the start/end julian day.
//
////////////////////////////////////////////////////////////////////////////////
class CalendarDate {
var $y, $m, $d; // Numeric year/month/day
var $minJD, $maxJD; // Julian Day numbers
function __construct($date) {
// Construct from an integer (a julian day number)
if (is_numeric($date)) {
$this->minJD=$date;
$this->maxJD=$date;
list($this->y, $this->m, $this->d)=$this->JDtoYMD($date);
return;
}
// Construct from an array (of three gedcom-style strings: "1900", "feb", "4")
if (is_array($date)) {
$this->d=(int)$date[2];
if (!is_null($this->MONTH_TO_NUM($date[1]))) {
$this->m=$this->MONTH_TO_NUM($date[1]);
} else {
$this->m=0;
$this->d=0;
}
$this->y=$this->ExtractYear($date[0]);
$this->SetJDfromYMD();
return;
}
// Construct from an equivalent xxxxDate object
if ($this->CALENDAR_ESCAPE()==$date->CALENDAR_ESCAPE()) {
// NOTE - can't copy whole object - need to be able to copy Hebrew to Jewish, etc.
$this->y=$date->y;
$this->m=$date->m;
$this->d=$date->d;
$this->minJD=$date->minJD;
$this->maxJD=$date->maxJD;
return;
}
// ...else construct an inequivalent xxxxDate object
if ($date->y==0) {
// Incomplete date - convert on basis of anniversary in current year
$today=$date->TodayYMD();
$jd=$date->YMDtoJD($today[0], $date->m, $date->d==0?$today[2]:$date->d);
} else {
// Complete date
$jd=floor(($date->maxJD+$date->minJD)/2);
}
list($this->y, $this->m, $this->d)=$this->JDtoYMD($jd);
// New date has same precision as original date
if ($date->y==0) $this->y=0;
if ($date->m==0) $this->m=0;
if ($date->d==0) $this->d=0;
$this->SetJDfromYMD();
}
// Set the object's JD from a potentially incomplete YMD
function SetJDfromYMD() {
if ($this->y==0) {
$this->minJD=0;
$this->maxJD=0;
} else
if ($this->m==0) {
$this->minJD=$this->YMDtoJD($this->y, 1, 1);
$this->maxJD=$this->YMDtoJD($this->NextYear($this->y), 1, 1)-1;
} else {
if ($this->d==0) {
list($ny,$nm)=$this->NextMonth();
$this->minJD=$this->YMDtoJD($this->y, $this->m, 1);
$this->maxJD=$this->YMDtoJD($ny, $nm, 1)-1;
} else {
$this->minJD=$this->YMDtoJD($this->y, $this->m, $this->d);
$this->maxJD=$this->minJD;
}
}
}
// Calendars are defined in terms of the following static functions.
// They should redefine them as necessary.
static function CALENDAR_ESCAPE() {
return '@#DUNKNOWN@';
}
static function NUM_MONTHS() {
return 12;
}
static function MONTH_TO_NUM($m) {
static $months=array(''=>0, 'jan'=>1, 'feb'=>2, 'mar'=>3, 'apr'=>4, 'may'=>5, 'jun'=>6, 'jul'=>7, 'aug'=>8, 'sep'=>9, 'oct'=>10, 'nov'=>11, 'dec'=>12);
if (isset($months[$m])) {
return $months[$m];
} else {
return null;
}
}
static function NUM_TO_MONTH($n) {
static $months=array(0=>'', 1=>'jan', 2=>'feb', 3=>'mar', 4=>'apr', 5=>'may', 6=>'jun', 7=>'jul', 8=>'aug', 9=>'sep', 10=>'oct', 11=>'nov', 12=>'dec');
if (isset($months[$n])) {
return $months[$n];
} else {
return null;
}
}
static function CAL_START_JD() {
return 0; // @#DJULIAN@ 01 JAN 4713B.C.
}
static function CAL_END_JD() {
return 99999999;
}
static function NUM_DAYS_OF_WEEK() {
return 7;
}
static function DAYS_OF_WEEK($n) {
static $days=array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday');
return $days[$n];
}
static function YMDtoJD($y, $m, $d) {
return 0;
}
static function JDtoYMD($j) {
return array(0, 0, 0);
}
// Most years are 1 more than the previous, but not always (e.g. 1BC->1AD)
static function NextYear($y) {
return $y+1;
}
// Calendars that use suffixes, etc. (e.g. 'B.C.') or OS/NS notation should redefine this.
function ExtractYear($year) {
return (int)$year;
}
// Leap years may have extra days, extra months, etc.
function IsLeapYear() {
return false;
}
// Compare two dates - helper function for sorting by date
static function Compare($d1, $d2) {
if ($d1->maxJD < $d2->minJD)
return -1;
if ($d2->minJD > $d1->maxJD)
return 1;
return 0;
}
// How long between an event and a given julian day
// Return result as either a number of years or
// a gedcom-style age string.
// bool $full: true=gedcom style, false=just years
// int $jd: date for calculation
// TODO: JewishDate needs to redefine this to cope with leap months
function GetAge($full, $jd, $warn_on_negative=true) {
if ($this->y==0 || $jd==0) {
return '';
}
if ($this->minJD < $jd && $this->maxJD > $jd) {
return '';
}
if ($this->minJD==$jd) {
return $full?'':'0';
}
if ($warn_on_negative && $jd<$this->minJD) {
return '<img alt="" src="images/warning.gif" />';
}
list($y,$m,$d)=$this->JDtoYMD($jd);
$dy=$y-$this->y;
$dm=$m-max($this->m,1);
$dd=$d-max($this->d,1);
if ($dd<0) {
$dd+=$this->DaysInMonth();
$dm--;
}
if ($dm<0) {
$dm+=$this->NUM_MONTHS();
$dy--;
}
// Not a full age? Then just the years
if (!$full)
return $dy;
// Age in years?
if ($dy>1)
return $dy.'y';
$dm+=$dy*$this->NUM_MONTHS();
// Age in months?
if ($dm>1)
return $dm.'m';
// Age in days?
return ($jd-$this->minJD)."d";
}
// Convert a date from one calendar to another.
function convert_to_cal($calendar) {
global $LANGUAGE;
switch ($calendar) {
case 'gregorian':
return new GregorianDate($this);
case 'julian':
return new JulianDate($this);
case 'jewish':
if ($LANGUAGE!='hebrew')
return new JewishDate($this);
// no break
case 'hebrew':
return new HebrewDate($this);
case 'french':
return new FrenchRDate($this);
case 'arabic':
if ($LANGUAGE!='arabic')
return new ArabicDate($this);
// no break
case 'hijri':
return new HijriDate($this);
default:
return $this;
}
}
// Is this date within the valid range of the calendar
function InValidRange() {
return $this->minJD>=$this->CAL_START_JD() && $this->maxJD<=$this->CAL_END_JD();
}
// How many days in the current month
function DaysInMonth() {
list($ny,$nm)=$this->NextMonth();
return $this->YMDtoJD($ny, $nm, 1) - $this->YMDtoJD($this->y, $this->m, 1);
}
// How many days in the current week
function DaysInWeek() {
return self::NUM_DAYS_OF_WEEK();
}
// Format a date
// $format - format string: the codes are specified in http://php.net/date
function Format($format) {
// Legacy formats (DMY) become jFY
if (preg_match('/^[DMY,. ;\/-]+$/', $format)) {
$format=strtr($format, 'DM', 'jF');
}
// Don't show exact details for inexact dates
if (!$this->d) {
$format=str_replace(array('d', 'j', 'l', 'D', 'N', 'S', 'w', 'z'), '', $format);
}
if (!$this->m) {
$format=str_replace(array('F', 'm', 'M', 'n', 't'), '', $format);
}
if (!$this->y) {
$format=str_replace(array('t', 'L', 'G', 'y', 'Y'), '', $format);
}
// If we've trimmed the format, also trim the punctuation
if (!$this->d || !$this->m || !$this->y) {
$format=trim($format, ',. ;/-');
}
// Build up the formated date, character at a time
$str='';
foreach (str_split($format) as $code)
switch ($code) {
case 'd': $str.=$this->FormatDayZeros(); break;
case 'j': $str.=$this->FormatDay(); break;
case 'l': $str.=$this->FormatLongWeekday(); break;
case 'D': $str.=$this->FormatShortWeekday(); break;
case 'N': $str.=$this->FormatISOWeekday(); break;
case 'S': $str.=$this->FormatOrdinalSuffix(); break;
case 'w': $str.=$this->FormatNumericWeekday(); break;
case 'z': $str.=$this->FormatDayOfYear(); break;
case 'F': $str.=$this->FormatLongMonth(); break;
case 'm': $str.=$this->FormatMonthZeros(); break;
case 'M': $str.=$this->FormatShortMonth(); break;
case 'n': $str.=$this->FormatMonth(); break;
case 't': $str.=$this->DaysInMonth(); break;
case 'L': $str.=(int)$this->IsLeapYear(); break;
case 'Y': $str.=$this->FormatLongYear(); break;
case 'y': $str.=$this->FormatShortYear(); break;
// The 4 extensions might be useful for re-formatting gedcom dates.
case '@': $str.=$this->CALENDAR_ESCAPE(); break;
case 'A': $str.=$this->FormatGedcomDay(); break;
case 'O': $str.=$this->FormatGedcomMonth(); break;
case 'E': $str.=$this->FormatGedcomYear(); break;
default: $str.=$code; break;
}
return $str;
}
// Functions to extract bits of the date in various formats. Individual calendars
// will want to redefine some of these.
function FormatDayZeros() {
if ($this->d<10)
return '0'.$this->d;
else
return $this->d;
}
function FormatDay() {
return $this->d;
}
function FormatLongWeekday() {
global $pgv_lang;
$day=$this->DAYS_OF_WEEK($this->minJD % self::NUM_DAYS_OF_WEEK());
if (isset($pgv_lang[$day]))
return $pgv_lang[$day];
return $day;
}
function FormatShortWeekday() {
global $pgv_lang;
$day=$this->DAYS_OF_WEEK($this->minJD % self::NUM_DAYS_OF_WEEK());
if (isset($pgv_lang[$day.'_1st']))
return $pgv_lang[$day.'_1st'];
if (isset($pgv_lang[$day]))
return $pgv_lang[$day];
return $day;
}
function FormatISOWeekday() {
return $this->minJD % 7 + 1;
}
function FormatOrdinalSuffix() {
global $lang_short_cut, $LANGUAGE;
$func="ordinal_suffix_{$lang_short_cut[$LANGUAGE]}";
if (function_exists($func))
return $func($this->d);
else
return '';
}
function FormatNumericWeekday() {
return ($this->minJD + 1) % self::NUM_DAYS_OF_WEEK();
}
function FormatDayOfYear() {
return $this->minJD - $this->YMDtoJD($this->y, 1, 1);
}
function FormatMonth() {
return $this->m;
}
function FormatMonthZeros() {
if ($this->m > 9)
return $this->m;
else
return '0'.$this->m;
}
function FormatLongMonth() {
global $pgv_lang;
$tmp=$this->NUM_TO_MONTH($this->m);
if (isset($pgv_lang[$tmp]))
return $pgv_lang[$tmp];
else
return $tmp;
}
function FormatShortMonth() {
global $pgv_lang;
$tmp=$this->NUM_TO_MONTH($this->m).'_1st';
if (isset($pgv_lang[$tmp]))
return $pgv_lang[$tmp];
else
return $this->FormatLongMonth();
}
// NOTE Short year is NOT a 2-digit year. It is for calendars such as hebrew
// which have a 3-digit form of 4-digit years.
function FormatShortYear() {
return $this->y;
}
function FormatGedcomDay() {
if ($this->d==0)
return '';
else
return sprintf('%02d', $this->d);
}
function FormatGedcomMonth() {
return strtoupper($this->NUM_TO_MONTH($this->m));
}
function FormatGedcomYear() {
if ($this->y==0)
return '';
else
return sprintf('%04d', $this->y);
}
function FormatLongYear() {
return $this->y;
}
// Calendars with leap-months should redefine this.
function NextMonth() {
return array(
$this->m==$this->NUM_MONTHS() ? $this->NextYear($this->y) : $this->y,
($this->m%$this->NUM_MONTHS())+1
);
}
// Convert a decimal number to roman numerals
static function NumToRoman($num) {
static $lookup=array(1000=>'M', '900'=>'CM', '500'=>'D', 400=>'CD', 100=>'C', 90=>'XC', 50=>'L', 40=>'XL', 10=>'X', 9=>'IX', 5=>'V', 4=>'IV', 1=>'I');
if ($num<1) return $num;
$roman='';
foreach ($lookup as $key=>$value)
while ($num>=$key) {
$roman.=$value;
$num-=$key;
}
return $roman;
}
// Convert a roman numeral to decimal
static function RomanToNum($roman) {
static $lookup=array(1000=>'M', '900'=>'CM', '500'=>'D', 400=>'CD', 100=>'C', 90=>'XC', 50=>'L', 40=>'XL', 10=>'X', 9=>'IX', 5=>'V', 4=>'IV', 1=>'I');
$num=0;
foreach ($lookup as $key=>$value)
if (strpos($roman, $value)===0) {
$num+=$key;
$roman=substr($roman, strlen($value));
}
return $num;
}
// Get today's date in the current calendar
function TodayYMD() {
return $this->JDtoYMD(GregorianDate::YMDtoJD(date('Y'), date('n'), date('j')));
}
function Today() {
$tmp=clone $this;
$ymd=$tmp->TodayYMD();
$tmp->y=$ymd[0];
$tmp->m=$ymd[1];
$tmp->d=$ymd[2];
$tmp->SetJDfromYMD();
return $tmp;
}
// Create a URL that links this date to the PGV calendar
function CalendarURL($date_fmt="") {
global $DATE_FORMAT;
if (empty($date_fmt))
$date_fmt=$DATE_FORMAT;
$URL='calendar.php?cal='.$this->CALENDAR_ESCAPE();
$action="year";
if (strpos($date_fmt, "Y")!==false
|| strpos($date_fmt, "y")!==false) {
$URL.='&year='.$this->FormatGedcomYear();
}
if (strpos($date_fmt, "F")!==false
|| strpos($date_fmt, "M")!==false
|| strpos($date_fmt, "m")!==false
|| strpos($date_fmt, "n")!==false) {
$URL.='&month='.$this->FormatGedcomMonth();
if ($this->m>0)
$action="calendar";
}
if (strpos($date_fmt, "d")!==false
|| strpos($date_fmt, "D")!==false
|| strpos($date_fmt, "j")!==false) {
$URL.='&day='.$this->FormatGedcomDay();
if ($this->d>0)
$action="today";
}
return encode_url($URL.'&action='.$action);
}
} // class CalendarDate
////////////////////////////////////////////////////////////////////////////////
// Definitions for the Gregorian calendar
////////////////////////////////////////////////////////////////////////////////
class GregorianDate extends CalendarDate {
static function CALENDAR_ESCAPE() {
return '@#DGREGORIAN@';
}
static function CAL_START_JD() {
return 2299161; // 15 OCT 1582
}
function IsLeapYear() {
return $this->y%4==0 && $this->y%100!=0 || $this->y%400==0;
}
static function YMDtoJD($y, $m, $d) {
if ($y<0) // 0=1BC, -1=2BC, etc.
++$y;
$a=floor((14-$m)/12);
$y=$y+4800-$a;
$m=$m+12*$a-3;
return $d+floor((153*$m+2)/5)+365*$y+floor($y/4)-floor($y/100)+floor($y/400)-32045;
}
static function JDtoYMD($j) {
$a=$j+32044;
$b=floor((4*$a+3)/146097);
$c=$a-floor($b*146097/4);
$d=floor((4*$c+3)/1461);
$e=$c-floor((1461*$d)/4);
$m=floor((5*$e+2)/153);
$day=$e-floor((153*$m+2)/5)+1;
$month=$m+3-12*floor($m/10);
$year=$b*100+$d-4800+floor($m/10);
if ($year<1) // 0=1BC, -1=2BC, etc.
--$year;
return array($year, $month, $day);
}
} // class GregorianDate
////////////////////////////////////////////////////////////////////////////////
// Definitions for the Julian Proleptic calendar
// (Proleptic means we extend it backwards, prior to its introduction in 46BC)
////////////////////////////////////////////////////////////////////////////////
class JulianDate extends CalendarDate {
var $new_old_style=false;
static function CALENDAR_ESCAPE() {
return '@#DJULIAN@';
}
static function NextYear($y) {
if ($y==-1)
return 1;
else
return $y+1;
}
function IsLeapYear() {
return $this->y%4==0;
}
static function YMDtoJD($y, $m, $d) {
if ($y<0) // 0=1BC, -1=2BC, etc.
++$y;
$a=floor((14-$m)/12);
$y=$y+4800-$a;
$m=$m+12*$a-3;
return $d+floor((153*$m+2)/5)+365*$y+floor($y/4)-32083;
}
static function JDtoYMD($j) {
$c=$j+32082;
$d=floor((4*$c+3)/1461);
$e=$c-floor(1461*$d/4);
$m=floor((5*$e+2)/153);
$day=$e-floor((153*$m+2)/5)+1;
$month=$m+3-12*floor($m/10);
$year=$d-4800+floor($m/10);
if ($year<1) // 0=1BC, -1=2BC, etc.
--$year;
return array($year, $month, $day);
}
// Process new-style/old-style years and years BC
function ExtractYear($year) {
if (preg_match('/^(\d\d\d\d) \/ \d{1,4}$/', $year, $match)) { // Assume the first year is correct
$this->new_old_style=true;
return $match[1]+1;
} else
if (preg_match('/^(\d+) b ?c$/', $year, $match))
return -$match[1];
else
return (int)$year;
}
function FormatLongYear() {
global $pgv_lang;
if ($this->y<0)
return (-$this->y).$pgv_lang['b.c.'];
else
if ($this->new_old_style) {
return sprintf('%d/%02d', $this->y-1, $this->y % 100);
} else
return $this->y;
}
function FormatGedcomYear() {
if ($this->y<0)
return sprintf('%04dB.C.', -$this->y);
else
if ($this->new_old_style) {
return sprintf('%04d/%02d', $this->y-1, $this->y % 100);
} else
return sprintf('%04d', $this->y);
}
} // class JulianDate
////////////////////////////////////////////////////////////////////////////////
// Definitions for the Jewish calendar
////////////////////////////////////////////////////////////////////////////////
class JewishDate extends CalendarDate {
static function CALENDAR_ESCAPE() {
return '@#DHEBREW@';
}
static function MONTH_TO_NUM($m) {
static $months=array(''=>0, 'tsh'=>1, 'csh'=>2, 'ksl'=>3, 'tvt'=>4, 'shv'=>5, 'adr'=>6, 'ads'=>7, 'nsn'=>8, 'iyr'=>9, 'svn'=>10, 'tmz'=>11, 'aav'=>12, 'ell'=>13);
if (isset($months[$m])) {
return $months[$m];
} else {
return null;
}
}
static function NUM_TO_MONTH($n) {
static $months=array(0=>'', 1=>'tsh', 2=>'csh', 3=>'ksl', 4=>'tvt', 5=>'shv', 6=>'adr', 7=>'ads', 8=>'nsn', 9=>'iyr', 10=>'svn', 11=>'tmz', 12=>'aav', 13=>'ell');
if (isset($months[$n])) {
return $months[$n];
} else {
return null;
}
}
static function NUM_MONTHS() {
return 13;
}
static function CAL_START_JD() {
return 347998; // 01 TSH 0001 = @#JULIAN@ 7 OCT 3761B.C.
}
function NextMonth() {
if ($this->m==6 && !$this->IsLeapYear())
return array($this->y, 8);
else
return array($this->y+($this->m==13?1:0), ($this->m%13)+1);
}
function IsLeapYear() {
return ((7*$this->y+1)%19)<7;
}
// TODO implement this function locally
static function YMDtoJD($y, $mh, $d) {
if (function_exists('JewishToJD'))
return JewishToJD($mh, $d, $y);
else
return 0;
}
// TODO implement this function locally
static function JDtoYMD($j) {
if (function_exists('JdToJewish'))
list($m, $d, $y)=explode('/', JDToJewish($j));
else
list($m, $d, $y)=array(0, 0, 0);
return array($y, $m, $d);
}
function FormatLongMonth() {
global $pgv_lang;
$mon=$this->NUM_TO_MONTH($this->m);
if ($mon=='adr' && $this->IsLeapYear())
$mon.='_leap_year';
return $pgv_lang[$mon];
}
function FormatShortMonth() {
return $this->FormatLongMonth();
}
} // class JewishDate
////////////////////////////////////////////////////////////////////////////////
// Definitions for the Hebrew calendar.
// NOTE - this is the same as the Jewish Calendar, but displays dates in hebrew
// rather than the local language.
////////////////////////////////////////////////////////////////////////////////
class HebrewDate extends JewishDate {
const GERSHAYIM="״";
const GERSH="׳";
const ALAFIM="אלפים";
static $HEBREW_MONTHS=array("", "תשרי", "חשוון", "כסלו", "טבת", "שבט", "אדר", "אדר ב׳", "ניסן", "אייר", "סיוון", "תמוז", "אב", "אלול");
static $HEBREW_DAYS=array("שני", "שלישי", "רביעי", "חמישי", "ששי", "שבת", "ראשון");
function FormatDayZeros() {
return self::NumToHebrew($this->d);
}
function FormatDay() {
return self::NumToHebrew($this->d);
}
function FormatLongMonth() {
$mon=$this->NUM_TO_MONTH($this->m);
if ($mon=='adr' && $this->IsLeapYear())
return "אדר א׳";
else
return self::$HEBREW_MONTHS[$this->m];
}
function FormatLongWeekday() {
return self::$HEBREW_DAYS[$this->minJD % self::NUM_DAYS_OF_WEEK()];
}
function FormatShortWeekday() {
return self::$HEBREW_DAYS[$this->minJD % self::NUM_DAYS_OF_WEEK()];
}
function FormatShortYear() {
return self::NumToHebrew($this->y%1000);
}
function FormatLongYear() {
return self::NumToHebrew($this->y);
}
// Convert a decimal number to hebrew - like roman numerals, but with extra punctuation
// and special rules.
static function NumToHebrew($num) {
global $DISPLAY_JEWISH_THOUSANDS;
static $jHundreds = array("", "ק", "ר", "ש", "ת", "תק", "תר","תש", "תת", "תתק");
static $jTens = array("", "י", "כ", "ל", "מ", "נ", "ס", "ע", "פ", "צ");
static $jTenEnds = array("", "י", "ך", "ל", "ם", "ן", "ס", "ע", "ף", "ץ");
static $tavTaz = array("ט״ו", "ט״ז");
static $jOnes = array("", "א", "ב", "ג", "ד", "ה", "ו", "ז", "ח", "ט");
$shortYear = $num %1000; //discard thousands
//next check for all possible single Hebrew digit years
$singleDigitYear=($shortYear < 11 || ($shortYear <100 && $shortYear % 10 == 0) || ($shortYear <= 400 && $shortYear % 100 ==0));
$thousands = $num / 1000; //get # thousands
$sb = "";
//append thousands to String
if($num % 1000 == 0) { // in year is 5000, 4000 etc
$sb .= $jOnes[$thousands];
$sb .= self::GERSH;
$sb .= " ";
$sb .= self::ALAFIM; //add # of thousands plus word thousand (overide alafim boolean)
} else if($DISPLAY_JEWISH_THOUSANDS) { // if alafim boolean display thousands
$sb .= $jOnes[$thousands];
$sb .= self::GERSH; //append thousands quote
$sb .= " ";
}
$num = $num % 1000; //remove 1000s
$hundreds = $num / 100; // # of hundreds
$sb .= $jHundreds[$hundreds]; //add hundreds to String
$num = $num % 100; //remove 100s
if($num == 15) { //special case 15
$sb .= $tavTaz[0];
} else if($num == 16) { //special case 16
$sb .= $tavTaz[1];
} else {
$tens = $num / 10;
if($num % 10 == 0) { // if evenly divisable by 10
if($singleDigitYear == false) {
$sb .= $jTenEnds[$tens]; // use end letters so that for example 5750 will end with an end nun
} else {
$sb .= $jTens[$tens]; // use standard letters so that for example 5050 will end with a regular nun
}
} else {
$sb .= $jTens[$tens];
$num = $num % 10;
$sb .= $jOnes[$num];
}
}
if ($singleDigitYear == true) {
$sb .= self::GERSH; //append single quote
} else { // append double quote before last digit
$pos1 = strlen($sb)-2;
$sb = substr($sb, 0, $pos1) . self::GERSHAYIM . substr($sb, $pos1);
$sb = str_replace(self::GERSHAYIM . self::GERSHAYIM, self::GERSHAYIM, $sb); //replace double gershayim with single instance
}
return $sb;
}
} // class HebrewDate
////////////////////////////////////////////////////////////////////////////////
// Definitions for the French Republican calendar
////////////////////////////////////////////////////////////////////////////////
class FrenchRDate extends CalendarDate {
static function CALENDAR_ESCAPE() {
return '@#DFRENCH R@';
}
static function MONTH_TO_NUM($m) {
static $months=array(''=>0, 'vend'=>1, 'brum'=>2, 'frim'=>3, 'nivo'=>4, 'pluv'=>5, 'vent'=>6, 'germ'=>7, 'flor'=>8, 'prai'=>9, 'mess'=>10, 'ther'=>11, 'fruc'=>12, 'comp'=>13);
if (isset($months[$m])) {
return $months[$m];
} else {
return null;
}
}
static function NUM_TO_MONTH($n) {
static $months=array(0=>'', 1=>'vend', 2=>'brum', 3=>'frim', 4=>'nivo', 5=>'pluv', 6=>'vent', 7=>'germ', 8=>'flor', 9=>'prai', 10=>'mess', 11=>'ther', 12=>'fruc', 13=>'comp');
if (isset($months[$n])) {
return $months[$n];
} else {
return null;
}
}
static function NUM_MONTHS() {
return 13;
}
static function DAYS_OF_WEEK($n) {
static $days=array('primidi', 'duodi', 'tridi', 'quartidi', 'quintidi', 'sextidi', 'septidi', 'octidi', 'nonidi', 'decidi');
return $days[$n];
}
static function NUM_DAYS_OF_WEEK() {
return 10; // A "metric" week of 10 unimaginatively named days.
}
static function CAL_START_JD() {
return 2375840; // 22 SEP 1792 = 01 VEND 0001
}
static function CAL_END_JD() {
return 2380687; // 31 DEC 1805 = 10 NIVO 0014
}
// Leap years were based on astronomical observations. Only years 3, 7 and 11
// were ever observed. Moves to a gregorian-like (fixed) system were proposed
// but never implemented. These functions are valid over the range years 1-14.
function IsLeapYear() {
return $this->y%4==3;
}
static function YMDtoJD($y, $m, $d) {
return 2375444+$d+$m*30+$y*365+floor($y/4);
}
static function JDtoYMD($j) {
$y=floor(($j-2375109)*4/1461)-1;
$m=floor(($j-2375475-$y*365-floor($y/4))/30)+1;
$d=$j-2375444-$m*30-$y*365-floor($y/4);
return array($y, $m, $d);
}
// Years were written using roman numerals
function FormatLongYear() {
return $this->NumToRoman($this->y);
}
} // class FrenchRDate
////////////////////////////////////////////////////////////////////////////////
// Definitions for the Hijri calendar. Note that these are "theoretical" dates.
// "True" dates are based on local lunar observations, and can be a +/- one day.
////////////////////////////////////////////////////////////////////////////////
class HijriDate extends CalendarDate {
static function CALENDAR_ESCAPE() {
return '@#DHIJRI@';
}
static function MONTH_TO_NUM($m) {
static $months=array(''=>0, 'muhar'=>1, 'safar'=>2, 'rabia'=>3, 'rabit'=>4, 'jumaa'=>5, 'jumat'=>6, 'rajab'=>7, 'shaab'=>8, 'ramad'=>9, 'shaww'=>10, 'dhuaq'=>11, 'dhuah'=>12);
if (isset($months[$m])) {
return $months[$m];
} else {
return null;
}
}
static function NUM_TO_MONTH($n) {
static $months=array(0=>'', 1=>'muhar', 2=>'safar', 3=>'rabia', 4=>'rabit', 5=>'jumaa', 6=>'jumat', 7=>'rajab', 8=>'shaab', 9=>'ramad', 10=>'shaww', 11=>'dhuaq', 12=>'dhuah');
if (isset($months[$n])) {
return $months[$n];
} else {
return null;
}
}
static function CAL_START_JD() {
return 1948440; // @#DHIJRI@ 1 MUHAR 0001 = @#JULIAN@ 16 JUL 0622
}
function IsLeapYear() {
return ((11*$this->y+14)%30)<11;
}
static function YMDtoJD($y, $m, $d) {
return $d+29*($m-1)+floor((6*$m-1)/11)+$y*354+floor((3+11*$y)/30)+1948085;
}
static function JDtoYMD($j) {
$y=floor((30*($j-1948440)+10646)/10631);
$m=floor((11*($j-$y*354-floor((3+11*$y)/30)-1948086)+330)/325);
$d=$j-29*($m-1)-floor((6*$m-1)/11)-$y*354-floor((3+11*$y)/30)-1948085;
return array($y, $m, $d);
}
} // class HijriDate
////////////////////////////////////////////////////////////////////////////////
// Definitions for the Arabic calendar.
// NOTE - this is the same as the Hijri Calendar, but displays dates in arabic
// rather than the local language.
////////////////////////////////////////////////////////////////////////////////
class ArabicDate extends HijriDate {
static $ARABIC_MONTHS=array("", "محرّم", "صفر", "ربيع الأول", "ربيع الثانى", "جمادى الأول", "جمادى الثاني", "رجب", "شعبان", "رمضان", "شوّال", "ذو القعدة", "ذو الحجة");
static $ARABIC_DAYS=array("الأثنين", "الثلاثاء", "الأربعاء", "الخميس", "الجمعه", "السبت", "الأحد");
function FormatLongMonth() {
return self::$ARABIC_MONTHS[$this->m];
}
function FormatShortMonth() {
return self::$ARABIC_MONTHS[$this->m];
}
function FormatLongWeekday() {
return self::$ARABIC_DAYS[$this->minJD % self::NUM_DAYS_OF_WEEK()];
}
function FormatShortWeekday() {
return self::$ARABIC_DAYS[$this->minJD % self::NUM_DAYS_OF_WEEK()];
}
} // class ArabicDate
////////////////////////////////////////////////////////////////////////////////
// Definitions for the Roman calendar
// TODO The 5.5.1 gedcom spec mentions this calendar, but gives no details of
// how it is to be represented.... This class is just a place holder so that
// PGV won't compain if it receives one.
////////////////////////////////////////////////////////////////////////////////
class RomanDate extends CalendarDate {
static function CALENDAR_ESCAPE() {
return '@#DROMAN@';
}
function FormatGedcomYear() {
return sprintf('%04dAUC',$this->y);
}
function FormatLongYear() {
return $this->y.'AUC';
}
} // class RomanDate
////////////////////////////////////////////////////////////////////////////////
//
// GedcomDate represents the date or date range from a gedcom DATE record.
//
////////////////////////////////////////////////////////////////////////////////
class GedcomDate {
var $qual1=null; // Optional qualifier, such as BEF, FROM, ABT
var $date1=null; // The first (or only) date
var $qual2=null; // Optional qualifier, such as TO, AND
var $date2=null; // Optional second date
var $text =null; // Optional text, as included with an INTerpreted date
function __construct($date) {
// Extract any explanatory text
if (preg_match('/^(.*)( ?\(.*)$/', $date, $match)) {
$date=$match[1];
$this->text=$match[2];
}
// Ignore punctuation and normalise whitespace
$date=preg_replace(
array('/(\d+|@#[^@]+@)/', '/[\s;:.,-]+/', '/^ /', '/ $/'),
array(' $1 ', ' ', '', ''),
strtolower($date)
);
if (preg_match('/^(from|bet) (.+) (and|to) (.+)/', $date, $match)) {
$this->qual1=$match[1];
$this->date1=$this->ParseDate($match[2]);
$this->qual2=$match[3];
$this->date2=$this->ParseDate($match[4]);