-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendar.js
More file actions
1524 lines (1201 loc) · 47.9 KB
/
Copy pathcalendar.js
File metadata and controls
1524 lines (1201 loc) · 47.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
/*
JavaScript functions for the Fourmilab Calendar Converter
by John Walker -- September, MIM
http://www.fourmilab.ch/documents/calendar/
This program is in the public domain.
*/
/* You may notice that a variety of array variables logically local
to functions are declared globally here. In JavaScript, construction
of an array variable from source code occurs as the code is
interpreted. Making these variables pseudo-globals permits us
to avoid overhead constructing and disposing of them in each
call on the function in which whey are used. */
var J0000 = 1721424.5; // Julian date of Gregorian epoch: 0000-01-01
var J1970 = 2440587.5; // Julian date at Unix epoch: 1970-01-01
var JMJD = 2400000.5; // Epoch of Modified Julian Date system
var J1900 = 2415020.5; // Epoch (day 1) of Excel 1900 date system (PC)
var J1904 = 2416480.5; // Epoch (day 0) of Excel 1904 date system (Mac)
var NormLeap = ["Normal year", "Leap year"];
/* WEEKDAY_BEFORE -- Return Julian date of given weekday (0 = Sunday)
in the seven days ending on jd. */
function weekday_before(weekday, jd) {
return jd - jwday(jd - weekday);
}
/* SEARCH_WEEKDAY -- Determine the Julian date for:
weekday Day of week desired, 0 = Sunday
jd Julian date to begin search
direction 1 = next weekday, -1 = last weekday
offset Offset from jd to begin search
*/
function search_weekday(weekday, jd, direction, offset) {
return weekday_before(weekday, jd + (direction * offset));
}
// Utility weekday functions, just wrappers for search_weekday
function nearest_weekday(weekday, jd) {
return search_weekday(weekday, jd, 1, 3);
}
function next_weekday(weekday, jd) {
return search_weekday(weekday, jd, 1, 7);
}
function next_or_current_weekday(weekday, jd) {
return search_weekday(weekday, jd, 1, 6);
}
function previous_weekday(weekday, jd) {
return search_weekday(weekday, jd, -1, 1);
}
function previous_or_current_weekday(weekday, jd) {
return search_weekday(weekday, jd, 1, 0);
}
function TestSomething() {}
// LEAP_GREGORIAN -- Is a given year in the Gregorian calendar a leap year ?
function leap_gregorian(year) {
return ((year % 4) == 0) &&
(!(((year % 100) == 0) && ((year % 400) != 0)));
}
// GREGORIAN_TO_JD -- Determine Julian day number from Gregorian calendar date
var GREGORIAN_EPOCH = 1721425.5;
function gregorian_to_jd(year, month, day) {
return (GREGORIAN_EPOCH - 1) +
(365 * (year - 1)) +
Math.floor((year - 1) / 4) +
(-Math.floor((year - 1) / 100)) +
Math.floor((year - 1) / 400) +
Math.floor((((367 * month) - 362) / 12) +
((month <= 2) ? 0 :
(leap_gregorian(year) ? -1 : -2)
) +
day);
}
// JD_TO_GREGORIAN -- Calculate Gregorian calendar date from Julian day
function jd_to_gregorian(jd) {
var wjd, depoch, quadricent, dqc, cent, dcent, quad, dquad,
yindex, dyindex, year, yearday, leapadj;
wjd = Math.floor(jd - 0.5) + 0.5;
depoch = wjd - GREGORIAN_EPOCH;
quadricent = Math.floor(depoch / 146097);
dqc = mod(depoch, 146097);
cent = Math.floor(dqc / 36524);
dcent = mod(dqc, 36524);
quad = Math.floor(dcent / 1461);
dquad = mod(dcent, 1461);
yindex = Math.floor(dquad / 365);
year = (quadricent * 400) + (cent * 100) + (quad * 4) + yindex;
if (!((cent == 4) || (yindex == 4))) {
year++;
}
yearday = wjd - gregorian_to_jd(year, 1, 1);
leapadj = ((wjd < gregorian_to_jd(year, 3, 1)) ? 0 :
(leap_gregorian(year) ? 1 : 2)
);
month = Math.floor((((yearday + leapadj) * 12) + 373) / 367);
day = (wjd - gregorian_to_jd(year, month, 1)) + 1;
return [year, month, day];
}
// ISO_TO_JULIAN -- Return Julian day of given ISO year, week, and day
function n_weeks(weekday, jd, nthweek) {
var j = 7 * nthweek;
if (nthweek > 0) {
j += previous_weekday(weekday, jd);
} else {
j += next_weekday(weekday, jd);
}
return j;
}
function iso_to_julian(year, week, day) {
return day + n_weeks(0, gregorian_to_jd(year - 1, 12, 28), week);
}
// JD_TO_ISO -- Return array of ISO (year, week, day) for Julian day
function jd_to_iso(jd) {
var year, week, day;
year = jd_to_gregorian(jd - 3)[0];
if (jd >= iso_to_julian(year + 1, 1, 1)) {
year++;
}
week = Math.floor((jd - iso_to_julian(year, 1, 1)) / 7) + 1;
day = jwday(jd);
if (day == 0) {
day = 7;
}
return [year, week, day];
}
// ISO_DAY_TO_JULIAN -- Return Julian day of given ISO year, and day of year
function iso_day_to_julian(year, day) {
return (day - 1) + gregorian_to_jd(year, 1, 1);
}
// JD_TO_ISO_DAY -- Return array of ISO (year, day_of_year) for Julian day
function jd_to_iso_day(jd) {
var year, day;
year = jd_to_gregorian(jd)[0];
day = Math.floor(jd - gregorian_to_jd(year, 1, 1)) + 1;
return [year, day];
}
/* PAD -- Pad a string to a given length with a given fill character. */
function pad(str, howlong, padwith) {
var s = str.toString();
while (s.length < howlong) {
s = padwith + s;
}
return s;
}
// JULIAN_TO_JD -- Determine Julian day number from Julian calendar date
var JULIAN_EPOCH = 1721423.5;
function leap_julian(year) {
return mod(year, 4) == ((year > 0) ? 0 : 3);
}
function julian_to_jd(year, month, day) {
/* Adjust negative common era years to the zero-based notation we use. */
if (year < 1) {
year++;
}
/* Algorithm as given in Meeus, Astronomical Algorithms, Chapter 7, page 61 */
if (month <= 2) {
year--;
month += 12;
}
return ((Math.floor((365.25 * (year + 4716))) +
Math.floor((30.6001 * (month + 1))) +
day) - 1524.5);
}
// JD_TO_JULIAN -- Calculate Julian calendar date from Julian day
function jd_to_julian(td) {
var z, a, alpha, b, c, d, e, year, month, day;
td += 0.5;
z = Math.floor(td);
a = z;
b = a + 1524;
c = Math.floor((b - 122.1) / 365.25);
d = Math.floor(365.25 * c);
e = Math.floor((b - d) / 30.6001);
month = Math.floor((e < 14) ? (e - 1) : (e - 13));
year = Math.floor((month > 2) ? (c - 4716) : (c - 4715));
day = b - d - Math.floor(30.6001 * e);
/* If year is less than 1, subtract one to convert from
a zero based date system to the common era system in
which the year -1 (1 B.C.E) is followed by year 1 (1 C.E.). */
if (year < 1) {
year--;
}
return [year, month, day];
}
// HEBREW_TO_JD -- Determine Julian day from Hebrew date
var HEBREW_EPOCH = 347995.5;
var HEBREW_MONTHS = ["\u05E0\u05B4\u05D9\u05E1\u05B8\u05DF",
"\u05D0\u05B4\u05D9\u05B8\u05BC\u05E8",
"\u05E1\u05B4\u05D9\u05D5\u05B8\u05DF",
"\u05EA\u05B7\u05BC\u05DE\u05BC\u05D5\u05BC\u05D6",
"\u05D0\u05B8\u05D1", "\u05D0\u05B1\u05DC\u05D5\u05BC\u05DC",
"\u05EA\u05B4\u05BC\u05E9\u05C1\u05E8\u05B4\u05D9",
"\u05DE\u05B7\u05E8\u05B0\u05D7\u05B6\u05E9\u05B0\u05C1\u05D5\u05B8\u05DF",
"\u05DB\u05B4\u05BC\u05E1\u05B0\u05DC\u05B5\u05D5",
"\u05D8\u05B5\u05D1\u05B5\u05EA",
"\u05E9\u05B0\u05C1\u05D1\u05B8\u05D8",
"\u05D0\u05B2\u05D3\u05B8\u05E8 \u05D0\u05F3",
"\u05D0\u05B2\u05D3\u05B8\u05E8 \u05D1\u05F3",
"\u05D0\u05B2\u05D3\u05B8\u05E8"
];
// Is a given Hebrew year a leap year ?
function hebrew_leap(year) {
return mod(((year * 7) + 1), 19) < 7;
}
// How many months are there in a Hebrew year (12 = normal, 13 = leap)
function hebrew_year_months(year) {
return hebrew_leap(year) ? 13 : 12;
}
// Test for delay of start of new year and to avoid
// Sunday, Wednesday, and Friday as start of the new year.
function hebrew_delay_1(year) {
var months, days, parts;
months = Math.floor(((235 * year) - 234) / 19);
parts = 12084 + (13753 * months);
day = (months * 29) + Math.floor(parts / 25920);
if (mod((3 * (day + 1)), 7) < 3) {
day++;
}
return day;
}
// Check for delay in start of new year due to length of adjacent years
function hebrew_delay_2(year) {
var last, present, next;
last = hebrew_delay_1(year - 1);
present = hebrew_delay_1(year);
next = hebrew_delay_1(year + 1);
return ((next - present) == 356) ? 2 :
(((present - last) == 382) ? 1 : 0);
}
// How many days are in a Hebrew year ?
function hebrew_year_days(year) {
return hebrew_to_jd(year + 1, 7, 1) - hebrew_to_jd(year, 7, 1);
}
// How many days are in a given month of a given year
function hebrew_month_days(year, month) {
// First of all, dispose of fixed-length 29 day months
if (month == 2 || month == 4 || month == 6 ||
month == 10 || month == 13) {
return 29;
}
// If it's not a leap year, Adar has 29 days
if (month == 12 && !hebrew_leap(year)) {
return 29;
}
// If it's Heshvan, days depend on length of year
if (month == 8 && !(mod(hebrew_year_days(year), 10) == 5)) {
return 29;
}
// Similarly, Kislev varies with the length of year
if (month == 9 && (mod(hebrew_year_days(year), 10) == 3)) {
return 29;
}
// Nope, it's a 30 day month
return 30;
}
// Finally, wrap it all up into...
function hebrew_to_jd(year, month, day) {
var jd, mon, months;
months = hebrew_year_months(year);
jd = HEBREW_EPOCH + hebrew_delay_1(year) +
hebrew_delay_2(year) + day + 1;
if (month < 7) {
for (mon = 7; mon <= months; mon++) {
jd += hebrew_month_days(year, mon);
}
for (mon = 1; mon < month; mon++) {
jd += hebrew_month_days(year, mon);
}
} else {
for (mon = 7; mon < month; mon++) {
jd += hebrew_month_days(year, mon);
}
}
return jd;
}
/* JD_TO_HEBREW -- Convert Julian date to Hebrew date
This works by making multiple calls to
the inverse function, and is this very
slow. */
function jd_to_hebrew(jd) {
var year, month, day, i, count, first;
jd = Math.floor(jd) + 0.5;
count = Math.floor(((jd - HEBREW_EPOCH) * 98496.0) / 35975351.0);
year = count - 1;
for (i = count; jd >= hebrew_to_jd(i, 7, 1); i++) {
year++;
}
first = (jd < hebrew_to_jd(year, 1, 1)) ? 7 : 1;
month = first;
for (i = first; jd > hebrew_to_jd(year, i, hebrew_month_days(year, i)); i++) {
month++;
}
day = (jd - hebrew_to_jd(year, month, 1)) + 1;
return [year, month, day];
}
/* EQUINOXE_A_PARIS -- Determine Julian day and fraction of the
September equinox at the Paris meridian in
a given Gregorian year. */
function equinoxe_a_paris(year) {
var equJED, equJD, equAPP, equParis, dtParis;
// September equinox in dynamical time
equJED = equinox(year, 2);
// Correct for delta T to obtain Universal time
equJD = equJED - (deltat(year) / (24 * 60 * 60));
// Apply the equation of time to yield the apparent time at Greenwich
equAPP = equJD + equationOfTime(equJED);
/* Finally, we must correct for the constant difference between
the Greenwich meridian and that of Paris, 2˚20'15" to the
East. */
dtParis = (2 + (20 / 60.0) + (15 / (60 * 60.0))) / 360;
equParis = equAPP + dtParis;
return equParis;
}
/* PARIS_EQUINOXE_JD -- Calculate Julian day during which the
September equinox, reckoned from the Paris
meridian, occurred for a given Gregorian
year. */
function paris_equinoxe_jd(year) {
var ep, epg;
ep = equinoxe_a_paris(year);
epg = Math.floor(ep - 0.5) + 0.5;
return epg;
}
/* ANNEE_DE_LA_REVOLUTION -- Determine the year in the French
revolutionary calendar in which a
given Julian day falls. Returns an
array of two elements:
[0] Année de la Révolution
[1] Julian day number containing
equinox for this year.
*/
var FRENCH_REVOLUTIONARY_EPOCH = 2375839.5;
function annee_da_la_revolution(jd) {
var guess = jd_to_gregorian(jd)[0] - 2,
lasteq, nexteq, adr;
lasteq = paris_equinoxe_jd(guess);
while (lasteq > jd) {
guess--;
lasteq = paris_equinoxe_jd(guess);
}
nexteq = lasteq - 1;
while (!((lasteq <= jd) && (jd < nexteq))) {
lasteq = nexteq;
guess++;
nexteq = paris_equinoxe_jd(guess);
}
adr = Math.round((lasteq - FRENCH_REVOLUTIONARY_EPOCH) / TropicalYear) + 1;
return [adr, lasteq];
}
/* JD_TO_FRENCH_REVOLUTIONARY -- Calculate date in the French Revolutionary
calendar from Julian day. The five or six
"sansculottides" are considered a thirteenth
month in the results of this function. */
function jd_to_french_revolutionary(jd) {
var an, mois, decade, jour,
adr, equinoxe;
jd = Math.floor(jd) + 0.5;
adr = annee_da_la_revolution(jd);
an = adr[0];
equinoxe = adr[1];
mois = Math.floor((jd - equinoxe) / 30) + 1;
jour = (jd - equinoxe) % 30;
decade = Math.floor(jour / 10) + 1;
jour = (jour % 10) + 1;
return [an, mois, decade, jour];
}
/* FRENCH_REVOLUTIONARY_TO_JD -- Obtain Julian day from a given French
Revolutionary calendar date. */
function french_revolutionary_to_jd(an, mois, decade, jour) {
var adr, equinoxe, guess, jd;
guess = FRENCH_REVOLUTIONARY_EPOCH + (TropicalYear * ((an - 1) - 1));
adr = [an - 1, 0];
while (adr[0] < an) {
adr = annee_da_la_revolution(guess);
guess = adr[1] + (TropicalYear + 2);
}
equinoxe = adr[1];
jd = equinoxe + (30 * (mois - 1)) + (10 * (decade - 1)) + (jour - 1);
return jd;
}
// LEAP_ISLAMIC -- Is a given year a leap year in the Islamic calendar ?
function leap_islamic(year) {
return (((year * 11) + 14) % 30) < 11;
}
// ISLAMIC_TO_JD -- Determine Julian day from Islamic date
var ISLAMIC_EPOCH = 1948439.5;
var ISLAMIC_WEEKDAYS = ["al-'ahad", "al-'ithnayn",
"ath-thalatha'", "al-'arb`a'",
"al-khamis", "al-jum`a", "as-sabt"
];
function islamic_to_jd(year, month, day) {
return (day +
Math.ceil(29.5 * (month - 1)) +
(year - 1) * 354 +
Math.floor((3 + (11 * year)) / 30) +
ISLAMIC_EPOCH) - 1;
}
// JD_TO_ISLAMIC -- Calculate Islamic date from Julian day
function jd_to_islamic(jd) {
var year, month, day;
jd = Math.floor(jd) + 0.5;
year = Math.floor(((30 * (jd - ISLAMIC_EPOCH)) + 10646) / 10631);
month = Math.min(12,
Math.ceil((jd - (29 + islamic_to_jd(year, 1, 1))) / 29.5) + 1);
day = (jd - islamic_to_jd(year, month, 1)) + 1;
return [year, month, day];
}
/* TEHRAN_EQUINOX -- Determine Julian day and fraction of the
March equinox at the Tehran meridian in
a given Gregorian year. */
function tehran_equinox(year) {
var equJED, equJD, equAPP, equTehran, dtTehran;
// March equinox in dynamical time
equJED = equinox(year, 0);
// Correct for delta T to obtain Universal time
equJD = equJED - (deltat(year) / (24 * 60 * 60));
// Apply the equation of time to yield the apparent time at Greenwich
equAPP = equJD + equationOfTime(equJED);
/* Finally, we must correct for the constant difference between
the Greenwich meridian andthe time zone standard for
Iran Standard time, 52˚30' to the East. */
dtTehran = (52 + (30 / 60.0) + (0 / (60.0 * 60.0))) / 360;
equTehran = equAPP + dtTehran;
return equTehran;
}
/* TEHRAN_EQUINOX_JD -- Calculate Julian day during which the
March equinox, reckoned from the Tehran
meridian, occurred for a given Gregorian
year. */
function tehran_equinox_jd(year) {
var ep, epg;
ep = tehran_equinox(year);
epg = Math.floor(ep);
return epg;
}
/* PERSIANA_YEAR -- Determine the year in the Persian
astronomical calendar in which a
given Julian day falls. Returns an
array of two elements:
[0] Persian year
[1] Julian day number containing
equinox for this year.
*/
var PERSIAN_EPOCH = 1948320.5;
var PERSIAN_WEEKDAYS = ["Yekshanbeh", "Doshanbeh",
"Seshhanbeh", "Chaharshanbeh",
"Panjshanbeh", "Jomeh", "Shanbeh"
];
function persianAstronomical_year(jd) {
var guess = jd_to_gregorian(jd)[0] - 2,
lasteq, nexteq, adr;
lasteq = tehran_equinox_jd(guess);
while (lasteq > jd) {
guess--;
lasteq = tehran_equinox_jd(guess);
}
nexteq = lasteq - 1;
while (!((lasteq <= jd) && (jd < nexteq))) {
lasteq = nexteq;
guess++;
nexteq = tehran_equinox_jd(guess);
}
adr = Math.round((lasteq - PERSIAN_EPOCH) / TropicalYear) + 1;
return [adr, lasteq];
}
/* JD_TO_PERSIANA -- Calculate date in the Persian astronomical
calendar from Julian day. */
function jd_to_persianAstronomical(jd) {
var year, month, day,
adr, equinox, yday;
jd = Math.floor(jd) + 0.5;
adr = persianAstronomical_year(jd);
year = adr[0];
equinox = adr[1];
day = Math.floor((jd - equinox) / 30) + 1;
yday = (Math.floor(jd) - persianAstronomical_to_jd(year, 1, 1)) + 1;
month = (yday <= 186) ? Math.ceil(yday / 31) : Math.ceil((yday - 6) / 30);
day = (Math.floor(jd) - persianAstronomical_to_jd(year, month, 1)) + 1;
return [year, month, day];
}
/* PERSIANA_TO_JD -- Obtain Julian day from a given Persian
astronomical calendar date. */
function persianAstronomical_to_jd(year, month, day) {
var adr, equinox, guess, jd;
guess = (PERSIAN_EPOCH - 1) + (TropicalYear * ((year - 1) - 1));
adr = [year - 1, 0];
while (adr[0] < year) {
adr = persianAstronomical_year(guess);
guess = adr[1] + (TropicalYear + 2);
}
equinox = adr[1];
jd = equinox +
((month <= 7) ?
((month - 1) * 31) :
(((month - 1) * 30) + 6)
) +
(day - 1);
return jd;
}
/* LEAP_PERSIANA -- Is a given year a leap year in the Persian
astronomical calendar ? */
function leap_persianAstronomical(year) {
return (persianAstronomical_to_jd(year + 1, 1, 1) -
persianAstronomical_to_jd(year, 1, 1)) > 365;
}
// LEAP_PERSIAN -- Is a given year a leap year in the Persian calendar ?
function leap_persian(year) {
return ((((((year - ((year > 0) ? 474 : 473)) % 2820) + 474) + 38) * 682) %
2816) < 682;
}
// PERSIAN_TO_JD -- Determine Julian day from Persian date
function persian_to_jd(year, month, day) {
var epbase, epyear;
epbase = year - ((year >= 0) ? 474 : 473);
epyear = 474 + mod(epbase, 2820);
return day +
((month <= 7) ?
((month - 1) * 31) :
(((month - 1) * 30) + 6)
) +
Math.floor(((epyear * 682) - 110) / 2816) +
(epyear - 1) * 365 +
Math.floor(epbase / 2820) * 1029983 +
(PERSIAN_EPOCH - 1);
}
// JD_TO_PERSIAN -- Calculate Persian date from Julian day
function jd_to_persian(jd) {
var year, month, day, depoch, cycle, cyear, ycycle,
aux1, aux2, yday;
jd = Math.floor(jd) + 0.5;
depoch = jd - persian_to_jd(475, 1, 1);
cycle = Math.floor(depoch / 1029983);
cyear = mod(depoch, 1029983);
if (cyear == 1029982) {
ycycle = 2820;
} else {
aux1 = Math.floor(cyear / 366);
aux2 = mod(cyear, 366);
ycycle = Math.floor(((2134 * aux1) + (2816 * aux2) + 2815) / 1028522) +
aux1 + 1;
}
year = ycycle + (2820 * cycle) + 474;
if (year <= 0) {
year--;
}
yday = (jd - persian_to_jd(year, 1, 1)) + 1;
month = (yday <= 186) ? Math.ceil(yday / 31) : Math.ceil((yday - 6) / 30);
day = (jd - persian_to_jd(year, month, 1)) + 1;
return [year, month, day];
}
// MAYAN_COUNT_TO_JD -- Determine Julian day from Mayan long count
var MAYAN_COUNT_EPOCH = 584282.5;
function mayan_count_to_jd(baktun, katun, tun, uinal, kin) {
return MAYAN_COUNT_EPOCH +
(baktun * 144000) +
(katun * 7200) +
(tun * 360) +
(uinal * 20) +
kin;
}
// JD_TO_MAYAN_COUNT -- Calculate Mayan long count from Julian day
function jd_to_mayan_count(jd) {
var d, baktun, katun, tun, uinal, kin;
jd = Math.floor(jd) + 0.5;
d = jd - MAYAN_COUNT_EPOCH;
baktun = Math.floor(d / 144000);
d = mod(d, 144000);
katun = Math.floor(d / 7200);
d = mod(d, 7200);
tun = Math.floor(d / 360);
d = mod(d, 360);
uinal = Math.floor(d / 20);
kin = mod(d, 20);
return [baktun, katun, tun, uinal, kin];
}
// JD_TO_MAYAN_HAAB -- Determine Mayan Haab "month" and day from Julian day
var MAYAN_HAAB_MONTHS = ["Pop", "Uo", "Zip", "Zotz", "Tzec", "Xul",
"Yaxkin", "Mol", "Chen", "Yax", "Zac", "Ceh",
"Mac", "Kankin", "Muan", "Pax", "Kayab", "Cumku", "Uayeb"
];
function jd_to_mayan_haab(jd) {
var lcount, day;
jd = Math.floor(jd) + 0.5;
lcount = jd - MAYAN_COUNT_EPOCH;
day = mod(lcount + 8 + ((18 - 1) * 20), 365);
return new Array(Math.floor(day / 20) + 1, mod(day, 20));
}
// JD_TO_MAYAN_TZOLKIN -- Determine Mayan Tzolkin "month" and day from Julian day
var MAYAN_TZOLKIN_MONTHS = ["Imix", "Ik", "Akbal", "Kan", "Chicchan",
"Cimi", "Manik", "Lamat", "Muluc", "Oc",
"Chuen", "Eb", "Ben", "Ix", "Men",
"Cib", "Caban", "Etznab", "Cauac", "Ahau"
];
function jd_to_mayan_tzolkin(jd) {
var lcount;
jd = Math.floor(jd) + 0.5;
lcount = jd - MAYAN_COUNT_EPOCH;
return new Array(amod(lcount + 20, 20), amod(lcount + 4, 13));
}
// INDIAN_CIVIL_TO_JD -- Obtain Julian day for Indian Civil date
var INDIAN_CIVIL_WEEKDAYS = [
"ravivara", "somavara", "mangalavara", "budhavara",
"brahaspativara", "sukravara", "sanivara"
];
function indian_civil_to_jd(year, month, day) {
var Caitra, gyear, leap, start, jd, m;
gyear = year + 78;
leap = leap_gregorian(gyear); // Is this a leap year ?
start = gregorian_to_jd(gyear, 3, leap ? 21 : 22);
Caitra = leap ? 31 : 30;
if (month == 1) {
jd = start + (day - 1);
} else {
jd = start + Caitra;
m = month - 2;
m = Math.min(m, 5);
jd += m * 31;
if (month >= 8) {
m = month - 7;
jd += m * 30;
}
jd += day - 1;
}
return jd;
}
// JD_TO_INDIAN_CIVIL -- Calculate Indian Civil date from Julian day
function jd_to_indian_civil(jd) {
var Caitra, Saka, greg, greg0, leap, start, year, yday, mday;
Saka = 79 - 1; // Offset in years from Saka era to Gregorian epoch
start = 80; // Day offset between Saka and Gregorian
jd = Math.floor(jd) + 0.5;
greg = jd_to_gregorian(jd); // Gregorian date for Julian day
leap = leap_gregorian(greg[0]); // Is this a leap year?
year = greg[0] - Saka; // Tentative year in Saka era
greg0 = gregorian_to_jd(greg[0], 1, 1); // JD at start of Gregorian year
yday = jd - greg0; // Day number (0 based) in Gregorian year
Caitra = leap ? 31 : 30; // Days in Caitra this year
if (yday < start) {
// Day is at the end of the preceding Saka year
year--;
yday += Caitra + (31 * 5) + (30 * 3) + 10 + start;
}
yday -= start;
if (yday < Caitra) {
month = 1;
day = yday + 1;
} else {
mday = yday - Caitra;
if (mday < (31 * 5)) {
month = Math.floor(mday / 31) + 2;
day = (mday % 31) + 1;
} else {
mday -= 31 * 5;
month = Math.floor(mday / 30) + 7;
day = (mday % 30) + 1;
}
}
return [year, month, day];
}
// BAHAI_TO_JD -- Determine Julian day from Bahai date
var BAHAI_EPOCH = 2394646.5;
var BAHAI_WEEKDAYS = ["Jam\xE1l", "Kam\xE1l", "Fid\xE1l",
"Id\xE1l", "Istijl\xE1l", "Istiql\xE1l", "Jal\xE1l"
];
function bahai_to_jd(major, cycle, year, month, day) {
var gy;
gy = (361 * (major - 1)) + (19 * (cycle - 1)) + (year - 1) +
jd_to_gregorian(BAHAI_EPOCH)[0];
return gregorian_to_jd(gy, 3, 20) + (19 * (month - 1)) +
((month != 20) ? 0 : (leap_gregorian(gy + 1) ? -14 : -15)) +
day;
}
// JD_TO_BAHAI -- Calculate Bahai date from Julian day
function jd_to_bahai(jd) {
var major, cycle, year, month, day,
gy, bstarty, bys, days, bld;
jd = Math.floor(jd) + 0.5;
gy = jd_to_gregorian(jd)[0];
bstarty = jd_to_gregorian(BAHAI_EPOCH)[0];
bys = gy - (bstarty + (((gregorian_to_jd(gy, 1, 1) <= jd) && (jd <=
gregorian_to_jd(gy, 3, 20))) ? 1 : 0));
major = Math.floor(bys / 361) + 1;
cycle = Math.floor(mod(bys, 361) / 19) + 1;
year = mod(bys, 19) + 1;
days = jd - bahai_to_jd(major, cycle, year, 1, 1);
bld = bahai_to_jd(major, cycle, year, 20, 1);
month = (jd >= bld) ? 20 : (Math.floor(days / 19) + 1);
day = (jd + 1) - bahai_to_jd(major, cycle, year, month, 1);
return [major, cycle, year, month, day];
}
/* updateFromGregorian -- Update all calendars from Gregorian.
"Why not Julian date?" you ask. Because
starting from Gregorian guarantees we're
already snapped to an integral second, so
we don't get roundoff errors in other
calendars. */
function updateFromGregorian() {
var j, year, mon, mday, hour, min, sec, weekday, persYear, bahaiYear,
julCal, hebCal, islCal, hmindex, utime, isoWeek, may_countCal,
mayhaabCal, maytzolkinCal, frrCal, bahaiCal, indCal, isoDay;
year = new Number(document.gregorianForm.year.value);
mon = document.gregorianForm.month.selectedIndex;
mday = new Number(document.gregorianForm.day.value);
hour = min = sec = 0;
hour = new Number(document.gregorianForm.hour.value);
min = new Number(document.gregorianForm.min.value);
sec = new Number(document.gregorianForm.sec.value);
// Update Julian day
j = gregorian_to_jd(year, mon + 1, mday) +
(Math.floor(sec + 60 * (min + 60 * hour) + 0.5) / 86400.0);
document.julianDayForm.day.value = j;
document.modifiedJulianDayForm.day.value = j - JMJD;
// Update day of week in Gregorian box
weekday = jwday(j);
document.gregorianForm.wday.value = Weekdays[weekday];
// Update leap year status in Gregorian box
document.gregorianForm.leap.value = NormLeap[leap_gregorian(year) ? 1 : 0];
// Update Julian Calendar
julCal = jd_to_julian(j);
document.julianForm.year.value = julCal[0];
document.julianForm.month.selectedIndex = julCal[1] - 1;
document.julianForm.day.value = julCal[2];
document.julianForm.leap.value = NormLeap[leap_julian(julCal[0]) ? 1 : 0];
weekday = jwday(j);
document.julianForm.wday.value = Weekdays[weekday];
// Update Hebrew Calendar
hebCal = jd_to_hebrew(j);
if (hebrew_leap(hebCal[0])) {
document.hebrewForm.month.options.length = 13;
document.hebrewForm.month.options[11] = new Option("Adar I");
document.hebrewForm.month.options[12] = new Option("Veadar");
} else {
document.hebrewForm.month.options.length = 12;
document.hebrewForm.month.options[11] = new Option("Adar");
}
document.hebrewForm.year.value = hebCal[0];
document.hebrewForm.month.selectedIndex = hebCal[1] - 1;
document.hebrewForm.day.value = hebCal[2];
hmindex = hebCal[1];
if (hmindex == 12 && !hebrew_leap(hebCal[0])) {
hmindex = 14;
}
document.hebrewForm.hebmonth.value = HEBREW_MONTHS[hmindex - 1];
switch (hebrew_year_days(hebCal[0])) {
case 353:
document.hebrewForm.leap.value = "Common deficient (353 days)";
break;
case 354:
document.hebrewForm.leap.value = "Common regular (354 days)";
break;
case 355:
document.hebrewForm.leap.value = "Common complete (355 days)";
break;
case 383:
document.hebrewForm.leap.value = "Embolismic deficient (383 days)";
break;
case 384:
document.hebrewForm.leap.value = "Embolismic regular (384 days)";
break;
case 385:
document.hebrewForm.leap.value = "Embolismic complete (385 days)";
break;
default:
document.hebrewForm.leap.value = "Invalid year length: " +
hebrew_year_days(hebCal[0]) + " days.";
break;
}
// Update Islamic Calendar
islCal = jd_to_islamic(j);
document.islamicForm.year.value = islCal[0];
document.islamicForm.month.selectedIndex = islCal[1] - 1;
document.islamicForm.day.value = islCal[2];
document.islamicForm.wday.value = "yawm " + ISLAMIC_WEEKDAYS[weekday];
document.islamicForm.leap.value = NormLeap[leap_islamic(islCal[0]) ? 1 : 0];