-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpycalcal.py
5391 lines (4579 loc) · 201 KB
/
pycalcal.py
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
"""Python implementation of Dershowitz and Reingold 'Calendrica Calculations'.
Python implementation of calendrical algorithms as described in Common
Lisp in calendrical-3.0.cl (and errata as made available by the authors.)
The companion book is Dershowitz and Reingold 'Calendrica Calculations',
3rd Ed., 2008, Cambridge University Press.
License: MIT License for my work, but read the one
for calendrica-3.0.cl which inspired this work.
Author: Enrico Spinielli
"""
# Copyright (c) 2009 Enrico Spinielli
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# AUTOMATICALLY GENERATED FROM pycalcal.nw: ANY CHANGES WILL BE OVERWRITTEN.
# use true division
from __future__ import division
# Precision in bits, for places where CL postfixes numbers with L0, meaning
# at least 50 bits of precision
from mpmath import *
mp.prec = 50
################################
# basic calendrical algorithms #
################################
# see lines 244-247 in calendrica-3.0.cl
BOGUS = 'bogus'
# see lines 249-252 in calendrica-3.0.cl
# m // n
# The following
# from operator import floordiv as quotient
# is not ok, the corresponding CL code
# uses CL 'floor' which always returns an integer
# (the floating point equivalent is 'ffloor'), while
# 'quotient' from operator module (or corresponding //)
# can return a float if at least one of the operands
# is a float...so I redefine it (and 'floor' and 'round' as well: in CL
# they always return an integer.)
def quotient(m, n):
"""Return the whole part of m/n towards negative infinity."""
return ifloor(m / n)
# I (re)define floor: in CL it always returns an integer.
# I make it explicit the fact it returns an integer by
# naming it ifloor
def ifloor(n):
"""Return the whole part of m/n."""
from math import floor
return int(floor(n))
# I (re)define round: in CL it always returns an integer.
# I make it explicit the fact it returns an integer by
# naming it iround
def iround(n):
"""Return the whole part of m/n."""
from builtins import round
return int(round(n))
# m % n (this works as described in book for negative integres)
# It is interesting to note that
# mod(1.5, 1)
# returns the decimal part of 1.5, so 0.5; given a moment 'm'
# mod(m, 1)
# returns the time of the day
from operator import mod
# see lines 254-257 in calendrica-3.0.cl
def amod(x, y):
"""Return the same as a % b with b instead of 0."""
return y + (mod(x, -y))
# see lines 259-264 in calendrica-3.0.cl
def next(i, p):
"""Return first integer greater or equal to initial index, i,
such that condition, p, holds."""
return i if p(i) else next(i + 1, p)
# see lines 266-271 in calendrica-3.0.cl
def final(i, p):
"""Return last integer greater or equal to initial index, i,
such that condition, p, holds."""
return i - 1 if not p(i) else final(i + 1, p)
# see lines 273-281 in calendrica-3.0.cl
def summa(f, k, p):
"""Return the sum of f(i) from i=k, k+1, ... till p(i) holds true or 0.
This is a tail recursive implementation."""
return 0 if not p(k) else f(k) + summa(f, k + 1, p)
def altsumma(f, k, p):
"""Return the sum of f(i) from i=k, k+1, ... till p(i) holds true or 0.
This is an implementation of the Summation formula from Kahan,
see Theorem 8 in Goldberg, David 'What Every Computer Scientist
Should Know About Floating-Point Arithmetic', ACM Computer Survey,
Vol. 23, No. 1, March 1991."""
if not p(k):
return 0
else:
S = f(k)
C = 0
j = k + 1
while p(j):
Y = f(j) - C
T = S + Y
C = (T - S) - Y
S = T
j += 1
return S
# see lines 283-293 in calendrica-3.0.cl
def binary_search(lo, hi, p, e):
"""Bisection search for x in [lo, hi] such that condition 'e' holds.
p determines when to go left."""
x = (lo + hi) / 2
if p(lo, hi):
return x
elif e(x):
return binary_search(lo, x, p, e)
else:
return binary_search(x, hi, p, e)
# see lines 295-302 in calendrica-3.0.cl
def invert_angular(f, y, a, b, prec=10 ** -5):
"""Find inverse of angular function 'f' at 'y' within interval [a,b].
Default precision is 0.00001"""
return binary_search(a, b,
(lambda l, h: ((h - l) <= prec)),
(lambda x: mod((f(x) - y), 360) < 180))
#def invert_angular(f, y, a, b):
# from scipy.optimize import brentq
# return(brentq((lambda x: mod(f(x) - y), 360)), a, b, xtol=error)
# see lines 304-313 in calendrica-3.0.cl
def sigma(l, b):
"""Return the sum of body 'b' for indices i1..in
running simultaneously thru lists l1..ln.
List 'l' is of the form [[i1 l1]..[in ln]]"""
# 'l' is a list of 'n' lists of the same lenght 'L' [l1, l2, l3, ...]
# 'b' is a lambda with 'n' args
# 'sigma' sums all 'L' applications of 'b' to the relevant tuple of args
# >>> a = [ 1, 2, 3, 4]
# >>> b = [ 5, 6, 7, 8]
# >>> c = [ 9,10,11,12]
# >>> l = [a,b,c]
# >>> z = zip(*l)
# >>> z
# [(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]
# >>> b = lambda x, y, z: x * y * z
# >>> b(*z[0]) # apply b to first elem of i
# 45
# >>> temp = []
# >>> z = zip(*l)
# >>> for e in z: temp.append(b(*e))
# >>> temp
# [45, 120, 231, 384]
# >>> from operator import add
# >>> reduce(add, temp)
# 780
return sum(b(*e) for e in zip(*l))
# see lines 315-321 in calendrica-3.0.cl
from copy import copy
def poly(x, a):
"""Calculate polynomial with coefficients 'a' at point x.
The polynomial is a[0] + a[1] * x + a[2] * x^2 + ...a[n-1]x^(n-1)
the result is
a[0] + x(a[1] + x(a[2] +...+ x(a[n-1])...)"""
# This implementation is also known as Horner's Rule.
n = len(a) - 1
p = a[n]
for i in range(1, n+1):
p = p * x + a[n-i]
return p
# see lines 323-329 in calendrica-3.0.cl
# Epoch definition. I took it out explicitly from rd().
def epoch():
"""Epoch definition. For Rata Diem, R.D., it is 0 (but any other reference
would do.)"""
return 0
def rd(tee):
"""Return rata diem (number of days since epoch) of moment in time, tee."""
return tee - epoch()
# see lines 331-334 in calendrica-3.0.cl
SUNDAY = 0
# see lines 10-15 in calendrica-3.0.errata.cl
MONDAY = 1
# see lines 17-20 in calendrica-3.0.errata.cl
TUESDAY = 2
# see lines 22-25 in calendrica-3.0.errata.cl
WEDNESDAY = 3
# see lines 27-30 in calendrica-3.0.errata.cl
THURSDAY = 4
# see lines 32-35 in calendrica-3.0.errata.cl
FRIDAY = 5
# see lines 37-40 in calendrica-3.0.errata.cl
SATURDAY = SUNDAY + 6
DAYS_OF_WEEK_NAMES = {
SUNDAY : "Sunday",
MONDAY : "Monday",
TUESDAY : "Tuesday",
WEDNESDAY : "Wednesday",
THURSDAY : "Thursday",
FRIDAY : "Friday",
SATURDAY : "Saturday"}
# see lines 366-369 in calendrica-3.0.cl
def day_of_week_from_fixed(date):
"""Return day of the week from a fixed date 'date'."""
return mod(date - rd(0) - SUNDAY, 7)
# see lines 371-374 in calendrica-3.0.cl
def standard_month(date):
"""Return the month of date 'date'."""
return date[1]
# see lines 376-379 in calendrica-3.0.cl
def standard_day(date):
"""Return the day of date 'date'."""
return date[2]
# see lines 381-384 in calendrica-3.0.cl
def standard_year(date):
"""Return the year of date 'date'."""
return date[0]
# see lines 386-388 in calendrica-3.0.cl
def time_of_day(hour, minute, second):
"""Return the time of day data structure."""
return [hour, minute, second]
# see lines 390-392 in calendrica-3.0.cl
def hour(clock):
"""Return the hour of clock time 'clock'."""
return clock[0]
# see lines 394-396 in calendrica-3.0.cl
def minute(clock):
"""Return the minutes of clock time 'clock'."""
return clock[1]
# see lines 398-400 in calendrica-3.0.cl
def seconds(clock):
"""Return the seconds of clock time 'clock'."""
return clock[2]
# see lines 402-405 in calendrica-3.0.cl
def fixed_from_moment(tee):
"""Return fixed date from moment 'tee'."""
return ifloor(tee)
# see lines 407-410 in calendrica-3.0.cl
def time_from_moment(tee):
"""Return time from moment 'tee'."""
return mod(tee, 1)
# see lines 412-419 in calendrica-3.0.cl
def clock_from_moment(tee):
"""Return clock time hour:minute:second from moment 'tee'."""
time = time_from_moment(tee)
hour = ifloor(time * 24)
minute = ifloor(mod(time * 24 * 60, 60))
second = mod(time * 24 * 60 * 60, 60)
return time_of_day(hour, minute, second)
# see lines 421-427 in calendrica-3.0.cl
def time_from_clock(hms):
"""Return time of day from clock time 'hms'."""
h = hour(hms)
m = minute(hms)
s = seconds(hms)
return(1/24 * (h + ((m + (s / 60)) / 60)))
# see lines 429-431 in calendrica-3.0.cl
def degrees_minutes_seconds(d, m, s):
"""Return the angular data structure."""
return [d, m, s]
# see lines 433-440 in calendrica-3.0.cl
def angle_from_degrees(alpha):
"""Return an angle in degrees:minutes:seconds from angle,
'alpha' in degrees."""
d = ifloor(alpha)
m = ifloor(60 * mod(alpha, 1))
s = mod(alpha * 60 * 60, 60)
return degrees_minutes_seconds(d, m, s)
# see lines 502-510 in calendrica-3.0.cl
def list_range(ell, range):
"""Return those moments in list ell that occur in range 'range'."""
return list(filter(lambda x: is_in_range(x, range), ell))
# see lines 482-485 in calendrica-3.0.cl
def interval(t0, t1):
"""Return the range data structure."""
return [t0, t1]
# see lines 487-490 in calendrica-3.0.cl
def start(range):
"""Return the start of range 'range'."""
return range[0]
# see lines 492-495 in calendrica-3.0.cl
def end(range):
"""Return the end of range 'range'."""
return range[1]
# see lines 497-500 in calendrica-3.0.cl
def is_in_range(tee, range):
"""Return True if moment 'tee' falls within range 'range',
False otherwise."""
return start(range) <= tee <= end(range)
# see lines 442-445 in calendrica-3.0.cl
JD_EPOCH = rd(mpf(-1721424.5))
# see lines 447-450 in calendrica-3.0.cl
def moment_from_jd(jd):
"""Return the moment corresponding to the Julian day number 'jd'."""
return jd + JD_EPOCH
# see lines 452-455 in calendrica-3.0.cl
def jd_from_moment(tee):
"""Return the Julian day number corresponding to moment 'tee'."""
return tee - JD_EPOCH
# see lines 457-460 in calendrica-3.0.cl
def fixed_from_jd(jd):
"""Return the fixed date corresponding to Julian day number 'jd'."""
return ifloor(moment_from_jd(jd))
# see lines 462-465 in calendrica-3.0.cl
def jd_from_fixed(date):
"""Return the Julian day number corresponding to fixed date 'rd'."""
return jd_from_moment(date)
# see lines 467-470 in calendrica-3.0.cl
MJD_EPOCH = rd(678576)
# see lines 472-475 in calendrica-3.0.cl
def fixed_from_mjd(mjd):
"""Return the fixed date corresponding to modified Julian day 'mjd'."""
return mjd + MJD_EPOCH
# see lines 477-480 in calendrica-3.0.cl
def mjd_from_fixed(date):
"""Return the modified Julian day corresponding to fixed date 'rd'."""
return date - MJD_EPOCH
##############################################
# egyptian and armenian calendars algorithms #
##############################################
# see lines 515-518 in calendrica-3.0.cl
def egyptian_date(year, month, day):
"""Return the Egyptian date data structure."""
return [year, month, day]
# see lines 520-525 in calendrica-3.0.cl
EGYPTIAN_EPOCH = fixed_from_jd(1448638)
# see lines 527-536 in calendrica-3.0.cl
def fixed_from_egyptian(e_date):
"""Return the fixed date corresponding to Egyptian date 'e_date'."""
month = standard_month(e_date)
day = standard_day(e_date)
year = standard_year(e_date)
return EGYPTIAN_EPOCH + (365*(year - 1)) + (30*(month - 1)) + (day - 1)
# see lines 538-553 in calendrica-3.0.cl
def egyptian_from_fixed(date):
"""Return the Egyptian date corresponding to fixed date 'date'."""
days = date - EGYPTIAN_EPOCH
year = 1 + quotient(days, 365)
month = 1 + quotient(mod(days, 365), 30)
day = days - (365*(year - 1)) - (30*(month - 1)) + 1
return egyptian_date(year, month, day)
# see lines 555-558 in calendrica-3.0.cl
def armenian_date(year, month, day):
"""Return the Armenian date data structure."""
return [year, month, day]
# see lines 560-564 in calendrica-3.0.cl
ARMENIAN_EPOCH = rd(201443)
# see lines 566-575 in calendrica-3.0.cl
def fixed_from_armenian(a_date):
"""Return the fixed date corresponding to Armenian date 'a_date'."""
month = standard_month(a_date)
day = standard_day(a_date)
year = standard_year(a_date)
return (ARMENIAN_EPOCH +
fixed_from_egyptian(egyptian_date(year, month, day)) -
EGYPTIAN_EPOCH)
# see lines 577-581 in calendrica-3.0.cl
def armenian_from_fixed(date):
"""Return the Armenian date corresponding to fixed date 'date'."""
return egyptian_from_fixed(date + (EGYPTIAN_EPOCH - ARMENIAN_EPOCH))
#################################
# gregorian calendar algorithms #
#################################
# see lines 586-589 in calendrica-3.0.cl
def gregorian_date(year, month, day):
"""Return a Gregorian date data structure."""
return [year, month, day]
# see lines 591-595 in calendrica-3.0.cl
GREGORIAN_EPOCH = rd(1)
# see lines 597-600 in calendrica-3.0.cl
JANUARY = 1
# see lines 602-605 in calendrica-3.0.cl
FEBRUARY = 2
# see lines 607-610 in calendrica-3.0.cl
MARCH = 3
# see lines 612-615 in calendrica-3.0.cl
APRIL = 4
# see lines 617-620 in calendrica-3.0.cl
MAY = 5
# see lines 622-625 in calendrica-3.0.cl
JUNE = 6
# see lines 627-630 in calendrica-3.0.cl
JULY = 7
# see lines 632-635 in calendrica-3.0.cl
AUGUST = 8
# see lines 637-640 in calendrica-3.0.cl
SEPTEMBER = 9
# see lines 642-645 in calendrica-3.0.cl
OCTOBER = 10
# see lines 647-650 in calendrica-3.0.cl
NOVEMBER = 11
# see lines 652-655 in calendrica-3.0.cl
DECEMBER = 12
GREGORIAN_MONTHS_OF_YEAR_NAMES = {
0 : "BOGUS",
JANUARY : "January",
FEBRUARY : "February",
MARCH : "March",
APRIL : "April",
MAY : "May",
JUNE : "June",
JULY : "July",
AUGUST : "August",
SEPTEMBER: "September",
OCTOBER : "October",
NOVEMBER : "November",
DECEMBER : "December"
}
# see lines 657-663 in calendrica-3.0.cl
def is_gregorian_leap_year(g_year):
"""Return True if Gregorian year 'g_year' is leap."""
return (mod(g_year, 4) == 0) and (mod(g_year, 400) not in [100, 200, 300])
# see lines 665-687 in calendrica-3.0.cl
def fixed_from_gregorian(g_date):
"""Return the fixed date equivalent to the Gregorian date 'g_date'."""
month = standard_month(g_date)
day = standard_day(g_date)
year = standard_year(g_date)
return ((GREGORIAN_EPOCH - 1) +
(365 * (year -1)) +
quotient(year - 1, 4) -
quotient(year - 1, 100) +
quotient(year - 1, 400) +
quotient((367 * month) - 362, 12) +
(0 if month <= 2
else (-1 if is_gregorian_leap_year(year) else -2)) +
day)
# see lines 689-715 in calendrica-3.0.cl
def gregorian_year_from_fixed(date):
"""Return the Gregorian year corresponding to the fixed date 'date'."""
d0 = date - GREGORIAN_EPOCH
n400 = quotient(d0, 146097)
d1 = mod(d0, 146097)
n100 = quotient(d1, 36524)
d2 = mod(d1, 36524)
n4 = quotient(d2, 1461)
d3 = mod(d2, 1461)
n1 = quotient(d3, 365)
year = (400 * n400) + (100 * n100) + (4 * n4) + n1
return year if (n100 == 4) or (n1 == 4) else (year + 1)
# see lines 717-721 in calendrica-3.0.cl
def gregorian_new_year(g_year):
"""Return the fixed date of January 1 in Gregorian year 'g_year'."""
return fixed_from_gregorian(gregorian_date(g_year, JANUARY, 1))
# see lines 723-727 in calendrica-3.0.cl
def gregorian_year_end(g_year):
"""Return the fixed date of December 31 in Gregorian year 'g_year'."""
return fixed_from_gregorian(gregorian_date(g_year, DECEMBER, 31))
# see lines 729-733 in calendrica-3.0.cl
def gregorian_year_range(g_year):
"""Return the range of fixed dates in Gregorian year 'g_year'."""
return interval(gregorian_new_year(g_year), gregorian_year_end(g_year))
# see lines 735-756 in calendrica-3.0.cl
def gregorian_from_fixed(date):
"""Return the Gregorian date corresponding to fixed date 'date'."""
year = gregorian_year_from_fixed(date)
prior_days = date - gregorian_new_year(year)
correction = (0
if (date < fixed_from_gregorian(gregorian_date(year,
MARCH,
1)))
else (1 if is_gregorian_leap_year(year) else 2))
month = quotient((12 * (prior_days + correction)) + 373, 367)
day = 1 + (date - fixed_from_gregorian(gregorian_date(year, month, 1)))
return gregorian_date(year, month, day)
# see lines 758-763 in calendrica-3.0.cl
def gregorian_date_difference(g_date1, g_date2):
"""Return the number of days from Gregorian date 'g_date1'
till Gregorian date 'g_date2'."""
return fixed_from_gregorian(g_date2) - fixed_from_gregorian(g_date1)
# see lines 42-49 in calendrica-3.0.errata.cl
def day_number(g_date):
"""Return the day number in the year of Gregorian date 'g_date'."""
return gregorian_date_difference(
gregorian_date(standard_year(g_date) - 1, DECEMBER, 31),
g_date)
# see lines 53-58 in calendrica-3.0.cl
def days_remaining(g_date):
"""Return the days remaining in the year after Gregorian date 'g_date'."""
return gregorian_date_difference(
g_date,
gregorian_date(standard_year(g_date), DECEMBER, 31))
# see lines 779-801 in calendrica-3.0.cl
def alt_fixed_from_gregorian(g_date):
"""Return the fixed date equivalent to the Gregorian date 'g_date'.
Alternative calculation."""
month = standard_month(g_date)
day = standard_day(g_date)
year = standard_year(g_date)
m = amod(month - 2, 12)
y = year + quotient(month + 9, 12)
return ((GREGORIAN_EPOCH - 1) +
-306 +
365 * (y - 1) +
quotient(y - 1, 4) +
-quotient(y - 1, 100) +
quotient(y - 1, 400) +
quotient(3 * m - 1, 5) +
30 * (m - 1) +
day)
# see lines 803-825 in calendrica-3.0.cl
def alt_gregorian_from_fixed(date):
"""Return the Gregorian date corresponding to fixed date 'date'.
Alternative calculation."""
y = gregorian_year_from_fixed(GREGORIAN_EPOCH - 1 + date + 306)
prior_days = date - fixed_from_gregorian(gregorian_date(y - 1, MARCH, 1))
month = amod(quotient(5 * prior_days + 2, 153) + 3, 12)
year = y - quotient(month + 9, 12)
day = date - fixed_from_gregorian(gregorian_date(year, month, 1)) + 1
return gregorian_date(year, month, day)
# see lines 827-841 in calendrica-3.0.cl
def alt_gregorian_year_from_fixed(date):
"""Return the Gregorian year corresponding to the fixed date 'date'.
Alternative calculation."""
approx = quotient(date - GREGORIAN_EPOCH +2, 146097/400)
start = (GREGORIAN_EPOCH +
(365 * approx) +
quotient(approx, 4) +
-quotient(approx, 100) +
quotient(approx, 400))
return approx if (date < start) else (approx + 1)
# see lines 843-847 in calendrica-3.0.cl
def independence_day(g_year):
"""Return the fixed date of United States Independence Day in
Gregorian year 'g_year'."""
return fixed_from_gregorian(gregorian_date(g_year, JULY, 4))
# see lines 849-853 in calendrica-3.0.cl
def kday_on_or_before(k, date):
"""Return the fixed date of the k-day on or before fixed date 'date'.
k=0 means Sunday, k=1 means Monday, and so on."""
return date - day_of_week_from_fixed(date - k)
# see lines 855-859 in calendrica-3.0.cl
def kday_on_or_after(k, date):
"""Return the fixed date of the k-day on or after fixed date 'date'.
k=0 means Sunday, k=1 means Monday, and so on."""
return kday_on_or_before(k, date + 6)
# see lines 861-865 in calendrica-3.0.cl
def kday_nearest(k, date):
"""Return the fixed date of the k-day nearest fixed date 'date'.
k=0 means Sunday, k=1 means Monday, and so on."""
return kday_on_or_before(k, date + 3)
# see lines 867-871 in calendrica-3.0.cl
def kday_after(k, date):
"""Return the fixed date of the k-day after fixed date 'date'.
k=0 means Sunday, k=1 means Monday, and so on."""
return kday_on_or_before(k, date + 7)
# see lines 873-877 in calendrica-3.0.cl
def kday_before(k, date):
"""Return the fixed date of the k-day before fixed date 'date'.
k=0 means Sunday, k=1 means Monday, and so on."""
return kday_on_or_before(k, date - 1)
# see lines 62-74 in calendrica-3.0.errata.cl
def nth_kday(n, k, g_date):
"""Return the fixed date of n-th k-day after Gregorian date 'g_date'.
If n>0, return the n-th k-day on or after 'g_date'.
If n<0, return the n-th k-day on or before 'g_date'.
If n=0, return BOGUS.
A k-day of 0 means Sunday, 1 means Monday, and so on."""
if n > 0:
return 7*n + kday_before(k, fixed_from_gregorian(g_date))
elif n < 0:
return 7*n + kday_after(k, fixed_from_gregorian(g_date))
else:
return BOGUS
# see lines 892-897 in calendrica-3.0.cl
def first_kday(k, g_date):
"""Return the fixed date of first k-day on or after Gregorian date 'g_date'.
A k-day of 0 means Sunday, 1 means Monday, and so on."""
return nth_kday(1, k, g_date)
# see lines 899-904 in calendrica-3.0.cl
def last_kday(k, g_date):
"""Return the fixed date of last k-day on or before Gregorian date 'g_date'.
A k-day of 0 means Sunday, 1 means Monday, and so on."""
return nth_kday(-1, k, g_date)
# see lines 906-910 in calendrica-3.0.cl
def labor_day(g_year):
"""Return the fixed date of United States Labor Day in Gregorian
year 'g_year' (the first Monday in September)."""
return first_kday(MONDAY, gregorian_date(g_year, SEPTEMBER, 1))
# see lines 912-916 in calendrica-3.0.cl
def memorial_day(g_year):
"""Return the fixed date of United States' Memorial Day in Gregorian
year 'g_year' (the last Monday in May)."""
return last_kday(MONDAY, gregorian_date(g_year, MAY, 31))
# see lines 918-923 in calendrica-3.0.cl
def election_day(g_year):
"""Return the fixed date of United States' Election Day in Gregorian
year 'g_year' (the Tuesday after the first Monday in November)."""
return first_kday(TUESDAY, gregorian_date(g_year, NOVEMBER, 2))
# see lines 925-930 in calendrica-3.0.cl
def daylight_saving_start(g_year):
"""Return the fixed date of the start of United States daylight
saving time in Gregorian year 'g_year' (the second Sunday in March)."""
return nth_kday(2, SUNDAY, gregorian_date(g_year, MARCH, 1))
# see lines 932-937 in calendrica-3.0.cl
def daylight_saving_end(g_year):
"""Return the fixed date of the end of United States daylight saving
time in Gregorian year 'g_year' (the first Sunday in November)."""
return first_kday(SUNDAY, gregorian_date(g_year, NOVEMBER, 1))
# see lines 939-943 in calendrica-3.0.cl
def christmas(g_year):
"""Return the fixed date of Christmas in Gregorian year 'g_year'."""
return fixed_from_gregorian(gregorian_date(g_year, DECEMBER, 25))
# see lines 945-951 in calendrica-3.0.cl
def advent(g_year):
"""Return the fixed date of Advent in Gregorian year 'g_year'
(the Sunday closest to November 30)."""
return kday_nearest(SUNDAY,
fixed_from_gregorian(gregorian_date(g_year,
NOVEMBER,
30)))
# see lines 953-957 in calendrica-3.0.cl
def epiphany_us(g_year):
"""Return the fixed date of Epiphany in U.S. in Gregorian year 'g_year'
(the first Sunday after January 1)."""
return first_kday(SUNDAY, gregorian_date(g_year, JANUARY, 2))
def epiphany(g_year):
"""Return fixed date of Epiphany in the world (except USA) in Gregorian year 'g_year'."""
return fixed_from_gregorian(gregorian_date(g_year, JANUARY, 6))
# see lines 959-974 in calendrica-3.0.cl
def unlucky_fridays_in_range(range):
"""Return the list of Fridays within range 'range' of fixed dates that
are day 13 of the relevant Gregorian months."""
a = start(range)
b = end(range)
fri = kday_on_or_after(FRIDAY, a)
date = gregorian_from_fixed(fri)
ell = [fri] if (standard_day(date) == 13) else []
if is_in_range(fri, range):
ell[:0] = unlucky_fridays_in_range(interval(fri + 1, b))
return ell
else:
return []
GREGORIAN_MONTHS_LENGTHS = {
JANUARY : 31,
FEBRUARY : 28,
MARCH : 31,
APRIL : 30,
MAY : 31,
JUNE : 30,
JULY : 31,
AUGUST : 31,
SEPTEMBER : 30,
OCTOBER : 31,
NOVEMBER : 30,
DECEMBER : 31}
def gregorian_last_day_of_month(g_date):
"""Return the last day of the month for Gregorian date 'g_date'."""
days = gregorian_month_lenth(g_date)
return gregorian_date(g_date[0], g_date[1], days)
def gregorian_month_lenth(g_date):
days = GREGORIAN_MONTHS_LENGTHS[g_date[1]]
# February of a leap year has 28 days
if (g_date[1] == 2 and is_gregorian_leap_year(g_date[0])):
days = 29
return days
##############################
# julian calendar algorithms #
##############################
# see lines 1037-1040 in calendrica-3.0.cl
def julian_date(year, month, day):
"""Return the Julian date data structure."""
return [year, month, day]
# see lines 1042-1045 in calendrica-3.0.cl
JULIAN_EPOCH = fixed_from_gregorian(gregorian_date(0, DECEMBER, 30))
# see lines 1047-1050 in calendrica-3.0.cl
def bce(n):
"""Retrun a negative value to indicate a BCE Julian year."""
return -n
# see lines 1052-1055 in calendrica-3.0.cl
def ce(n):
"""Return a positive value to indicate a CE Julian year."""
return n
# see lines 1057-1060 in calendrica-3.0.cl
def is_julian_leap_year(j_year):
"""Return True if Julian year 'j_year' is a leap year in
the Julian calendar."""
return mod(j_year, 4) == (0 if j_year > 0 else 3)
# see lines 1062-1082 in calendrica-3.0.cl
def fixed_from_julian(j_date):
"""Return the fixed date equivalent to the Julian date 'j_date'."""
month = standard_month(j_date)
day = standard_day(j_date)
year = standard_year(j_date)
y = year + 1 if year < 0 else year
return (JULIAN_EPOCH - 1 +
(365 * (y - 1)) +
quotient(y - 1, 4) +
quotient(367*month - 362, 12) +
(0 if month <= 2 else (-1 if is_julian_leap_year(year) else -2)) +
day)
# see lines 1084-1111 in calendrica-3.0.cl
def julian_from_fixed(date):
"""Return the Julian date corresponding to fixed date 'date'."""
approx = quotient(((4 * (date - JULIAN_EPOCH))) + 1464, 1461)
year = approx - 1 if approx <= 0 else approx
prior_days = date - fixed_from_julian(julian_date(year, JANUARY, 1))
correction = (0 if date < fixed_from_julian(julian_date(year, MARCH, 1))
else (1 if is_julian_leap_year(year) else 2))
month = quotient(12*(prior_days + correction) + 373, 367)
day = 1 + (date - fixed_from_julian(julian_date(year, month, 1)))
return julian_date(year, month, day)
# see lines 1113-1116 in calendrica-3.0.cl
KALENDS = 1
# see lines 1118-1121 in calendrica-3.0.cl
NONES = 2
# see lines 1123-1126 in calendrica-3.0.cl
IDES = 3
# see lines 1128-1131 in calendrica-3.0.cl
def roman_date(year, month, event, count, leap):
"""Return the Roman date data structure."""
return [year, month, event, count, leap]
# see lines 1133-1135 in calendrica-3.0.cl
def roman_year(date):
"""Return the year of Roman date 'date'."""
return date[0]
# see lines 1137-1139 in calendrica-3.0.cl
def roman_month(date):
"""Return the month of Roman date 'date'."""
return date[1]
# see lines 1141-1143 in calendrica-3.0.cl
def roman_event(date):
"""Return the event of Roman date 'date'."""
return date[2]
# see lines 1145-1147 in calendrica-3.0.cl
def roman_count(date):
"""Return the count of Roman date 'date'."""
return date[3]
# see lines 1149-1151 in calendrica-3.0.cl
def roman_leap(date):
"""Return the leap indicator of Roman date 'date'."""
return date[4]
# see lines 1153-1158 in calendrica-3.0.cl
def ides_of_month(month):
"""Return the date of the Ides in Roman month 'month'."""
return 15 if month in [MARCH, MAY, JULY, OCTOBER] else 13
# see lines 1160-1163 in calendrica-3.0.cl
def nones_of_month(month):
"""Return the date of Nones in Roman month 'month'."""
return ides_of_month(month) - 8
# see lines 1165-1191 in calendrica-3.0.cl
def fixed_from_roman(r_date):
"""Return the fixed date corresponding to Roman date 'r_date'."""
leap = roman_leap(r_date)
count = roman_count(r_date)
event = roman_event(r_date)
month = roman_month(r_date)
year = roman_year(r_date)
return ({KALENDS: fixed_from_julian(julian_date(year, month, 1)),
NONES: fixed_from_julian(julian_date(year,
month,
nones_of_month(month))),
IDES: fixed_from_julian(julian_date(year,
month,
ides_of_month(month)))
}[event] -
count +
(0 if (is_julian_leap_year(year) and
(month == MARCH) and
(event == KALENDS) and
(16 >= count >= 6))
else 1) +
(1 if leap else 0))
# see lines 1193-1229 in calendrica-3.0.cl
def roman_from_fixed(date):
"""Return the Roman name corresponding to fixed date 'date'."""
j_date = julian_from_fixed(date)
month = standard_month(j_date)
day = standard_day(j_date)
year = standard_year(j_date)
month_prime = amod(1 + month, 12)
year_prime = (year if month_prime != 1
else (year + 1 if (year != -1) else 1))
kalends1 = fixed_from_roman(
roman_date(year_prime, month_prime, KALENDS, 1, False))
if day == 1:
res = roman_date(year, month, KALENDS, 1, False)
elif day <= nones_of_month(month):
res = roman_date(year,
month,
NONES,
nones_of_month(month) - day + 1,
False)
elif day <= ides_of_month(month):
res = roman_date(year,
month,
IDES,
ides_of_month(month) - day + 1,
False)
elif (month != FEBRUARY) or not is_julian_leap_year(year):
res = roman_date(year_prime,
month_prime,
KALENDS,
kalends1 - date + 1,
False)
elif day < 25:
res = roman_date(year, MARCH, KALENDS, 30 - day, False)
else:
res = roman_date(year, MARCH, KALENDS, 31 - day, day == 25)
return res
# see lines 1231-1234 in calendrica-3.0.cl
YEAR_ROME_FOUNDED = bce(753)
# see lines 1236-1241 in calendrica-3.0.cl
def julian_year_from_auc_year(year):
"""Return the Julian year equivalent to AUC year 'year'."""
return ((year + YEAR_ROME_FOUNDED - 1)
if (1 <= year <= (year - YEAR_ROME_FOUNDED))
else (year + YEAR_ROME_FOUNDED))
# see lines 1243-1248 in calendrica-3.0.cl
def auc_year_from_julian_year(year):
"""Return the AUC year equivalent to Julian year 'year'."""
return ((year - YEAR_ROME_FOUNDED - 1)
if (YEAR_ROME_FOUNDED <= year <= -1)
else (year - YEAR_ROME_FOUNDED))
# see lines 1250-1266 in calendrica-3.0.cl
def julian_in_gregorian(j_month, j_day, g_year):
"""Return the list of the fixed dates of Julian month 'j_month', day
'j_day' that occur in Gregorian year 'g_year'."""
jan1 = gregorian_new_year(g_year)
y = standard_year(julian_from_fixed(jan1))
y_prime = 1 if (y == -1) else (y + 1)
date1 = fixed_from_julian(julian_date(y, j_month, j_day))
date2 = fixed_from_julian(julian_date(y_prime, j_month, j_day))
return list_range([date1, date2], gregorian_year_range(g_year))
# see lines 1268-1272 in calendrica-3.0.cl
def eastern_orthodox_christmas(g_year):