-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcalendrica-3.0.cl
5999 lines (5387 loc) · 215 KB
/
calendrica-3.0.cl
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
;;;;
;;;; CALENDRICA 3.0 -- Common Lisp
;;;; E. M. Reingold and N. Dershowitz
;;;;
;;;; ================================================================
;;;;
;;;; The Functions (code, comments, and definitions) contained in this
;;;; file (the "Program") were written by Edward M. Reingold and Nachum
;;;; Dershowitz (the "Authors"), who retain all rights to them except as
;;;; granted in the License and subject to the warranty and liability
;;;; limitations below. These Functions are explained in the Authors'
;;;; book, "Calendrical Calculations", 3rd ed. (Cambridge University
;;;; Press, 2008), and are subject to an international copyright.
;;;;
;;;; The Authors' public service intent is more liberal than suggested
;;;; by the License below, as are their licensing policies for otherwise
;;;; nonallowed uses such as--without limitation--those in commercial,
;;;; web-site, and large-scale academic contexts. Please see the
;;;; web-site
;;;;
;;;; http://www.calendarists.com
;;;;
;;;; for all uses not authorized below; in case there is cause for doubt
;;;; about whether a use you contemplate is authorized, please contact
;;;; the Authors (e-mail: reingold@iit.edu). For commercial licensing
;;;; information, contact the first author at the Department of Computer
;;;; Science, Illinois Institute of Technology, Chicago, IL 60616-3729 USA.
;;;;
;;;; 1. LICENSE. The Authors grant you a license for personal use.
;;;; This means that for strictly personal use you may copy and use the
;;;; code, and keep a backup or archival copy also. The Authors grant you a
;;;; license for re-use within non-commercial, non-profit softeare provided prominent
;;;; credit is given and the Authors' rights are preserved. Any other uses,
;;;; including without limitation, allowing the code or its output to be
;;;; accessed, used, or available to others, is not permitted.
;;;;
;;;; 2. WARRANTY.
;;;;
;;;; (a) THE AUTHORS PROVIDE NO WARRANTIES OF ANY KIND, EITHER
;;;; EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITING THE
;;;; GENERALITY OF THE FOREGOING, ANY IMPLIED WARRANTY OF
;;;; MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
;;;;
;;;; (b) THE AUTHORS SHALL NOT BE LIABLE TO YOU OR ANY THIRD PARTIES
;;;; FOR DAMAGES OF ANY KIND, INCLUDING WITHOUT LIMITATION, ANY LOST
;;;; PROFITS, LOST SAVINGS, OR ANY OTHER INCIDENTAL OR CONSEQUENTIAL
;;;; DAMAGES ARISING OUT OF OR RELATED TO THE USE, INABILITY TO USE,
;;;; OR INACCURACY OF CALCULATIONS, OF THE CODE AND FUNCTIONS
;;;; CONTAINED HEREIN, OR THE BREACH OF ANY EXPRESS OR IMPLIED
;;;; WARRANTY, EVEN IF THE AUTHORS OR PUBLISHER HAVE BEEN ADVISED OF
;;;; THE POSSIBILITY OF THOSE DAMAGES.
;;;;
;;;; (c) THE FOREGOING WARRANTY MAY GIVE YOU SPECIFIC LEGAL
;;;; RIGHTS WHICH MAY VARY FROM STATE TO STATE IN THE U.S.A.
;;;;
;;;; 3. LIMITATION OF LICENSEE REMEDIES. You acknowledge and agree that
;;;; your exclusive remedy (in law or in equity), and Authors' entire
;;;; liability with respect to the material herein, for any breach of
;;;; representation or for any inaccuracy shall be a refund of the license
;;;; fee or service and handling charge which you paid the Authors, if any.
;;;;
;;;; SOME STATES IN THE U.S.A. DO NOT ALLOW THE EXCLUSION OR LIMITATION OF
;;;; LIABILITY FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO THE ABOVE
;;;; EXCLUSIONS OR LIMITATION MAY NOT APPLY TO YOU.
;;;;
;;;; 4. DISCLAIMER. Except as expressly set forth above, the Authors:
;;;;
;;;; (a) make no other warranties with respect to the material in the
;;;; Program and expressly disclaim any others;
;;;;
;;;; (b) do not warrant that the material contained in the Program will
;;;; meet your requirements or that their operation shall be
;;;; uninterrupted or error-free;
;;;;
;;;; (c) license this material on an "as is" basis, and the entire risk
;;;; as to the quality, accuracy, and performance of the Program is
;;;; yours, should the code prove defective (except as expressly
;;;; warranted herein). You alone assume the entire cost of all
;;;; necessary corrections.
;;;;
;;;; Sample values for the functions (useful for debugging) are given in
;;;; Appendix C of the book and on the accompanying CD.
;;;; These sample values are not available electronically.
;;;; Last modified 23 August 2007.
(if (not (find-package "CC3"))
(defpackage "CC3"))
(in-package "CC3")
(export '(true false bogus rd sunday monday tuesday wednesday
thursday friday saturday january february march april may
june july august september october november december kalends
nones ides ayyam-i-ha arya-solar-year arya-solar-month
arya-lunar-month arya-lunar-day arya-jovian-period mecca
spring summer autumn winter new first-quarter full
last-quarter haifa islamic-locale jerusalem tehran paris
ujjain hindu-locale quotient day-of-week-from-fixed
standard-month standard-day standard-year time-of-day
hour minute seconds
fixed-from-moment time-from-moment clock-from-moment
time-from-clock angle-from-degrees moment-from-jd jd-from-moment
fixed-from-jd jd-from-fixed fixed-from-mjd mjd-from-fixed
range start end in-range? list-range
egyptian-date fixed-from-egyptian egyptian-from-fixed armenian-date
fixed-from-armenian armenian-from-fixed gregorian-date
gregorian-leap-year? fixed-from-gregorian
gregorian-year-from-fixed gregorian-from-fixed
gregorian-date-difference day-number days-remaining
alt-fixed-from-gregorian alt-gregorian-from-fixed
alt-gregorian-year-from-fixed
gregorian-new-year gregorian-year-end
independence-day kday-on-or-before kday-on-or-after
kday-nearest kday-after kday-before nth-kday first-kday last-kday
labor-day memorial-day election-day daylight-saving-start
daylight-saving-end christmas advent epiphany
unlucky-fridays-in-range
iso-date iso-week iso-day iso-year fixed-from-iso iso-from-fixed
iso-long-year? julian-date bce ce julian-leap-year?
fixed-from-julian julian-from-fixed roman-date roman-year roman-month
roman-event roman-count roman-leap ides-of-month
nones-of-month fixed-from-roman roman-from-fixed year-rome-founded
julian-year-from-auc-year auc-year-from-julian-year
julian-in-gregorian eastern-orthodox-christmas coptic-date
coptic-leap-year? fixed-from-coptic coptic-from-fixed
ethiopic-date fixed-from-ethiopic ethiopic-from-fixed
coptic-in-gregorian coptic-christmas orthodox-easter
alt-orthodox-easter easter pentecost islamic-date
islamic-leap-year? fixed-from-islamic islamic-from-fixed
islamic-in-gregorian mawlid-an-nabi bahai-date bahai-major
bahai-cycle bahai-year bahai-month bahai-day
fixed-from-bahai bahai-from-fixed bahai-new-year
feast-of-ridvan hebrew-date hebrew-leap-year?
last-month-of-hebrew-year hebrew-sabbatical-year?
last-day-of-hebrew-month hebrew-new-year molad fixed-from-hebrew
hebrew-from-fixed yom-kippur passover omer purim ta-anit-esther
tishah-be-av birkath-ha-hama sh-ela yom-ha-zikaron
hebrew-birthday-in-gregorian yahrzeit-in-gregorian
possible-hebrew-days
mayan-long-count-date mayan-haab-date mayan-tzolkin-date
mayan-baktun mayan-katun mayan-tun mayan-uinal mayan-kin
mayan-haab-month mayan-haab-day mayan-tzolkin-number
mayan-tzolkin-name fixed-from-mayan-long-count
mayan-long-count-from-fixed mayan-haab-from-fixed
mayan-haab-on-or-before mayan-tzolkin-from-fixed
mayan-tzolkin-on-or-before mayan-year-bearer-from-fixed
mayan-calendar-round-on-or-before aztec-xihuitl-date
aztec-xihuitl-month aztec-xihuitl-day aztec-tonalpohualli-date
aztec-tonalpohualli-number aztec-tonalpohualli-name
aztec-xiuhmolpilli-designation aztec-xiuhmolpilli-number
aztec-xiuhmolpilli-name aztec-correlation aztec-xihuitl-ordinal
aztec-xihuitl-correlation aztec-xihuitl-from-fixed
aztec-xihuitl-on-or-before aztec-tonalpohualli-ordinal
aztec-tonalpohualli-correlation aztec-tonalpohualli-from-fixed
aztec-tonalpohualli-on-or-before
aztec-xihuitl-tonalpohualli-on-or-before
aztec-xiuhmolpilli-from-fixed old-hindu-lunar-date
old-hindu-lunar-month old-hindu-lunar-leap old-hindu-lunar-day
old-hindu-lunar-year hindu-solar-date hindu-day-count
old-hindu-solar-from-fixed fixed-from-old-hindu-solar
old-hindu-lunar-leap-year? old-hindu-lunar-from-fixed
fixed-from-old-hindu-lunar jovian-year balinese-date
bali-luang bali-dwiwara bali-triwara bali-caturwara
bali-pancawara bali-sadwara bali-saptawara bali-asatawara
bali-sangawara bali-dasawara bali-day-from-fixed
bali-luang-from-fixed bali-dwiwara-from-fixed
bali-triwara-from-fixed bali-caturwara-from-fixed
bali-pancawara-from-fixed bali-sadwara-from-fixed
bali-saptawara-from-fixed bali-asatawara-from-fixed
bali-sangawara-from-fixed bali-dasawara-from-fixed
bali-pawukon-from-fixed bali-week-from-fixed
bali-on-or-before positions-in-range
kajeng-keliwon tumpek hr sec deg mt
angle location latitude longitude elevation zone direction
standard-from-universal
universal-from-standard local-from-universal
universal-from-local standard-from-local local-from-standard
midday midnight local-from-apparent apparent-from-local
dawn dusk sunrise sunset islamic-sunrise
islamic-sunset jewish-dusk jewish-sabbath-ends
daytime-temporal-hour nighttime-temporal-hour
standard-from-sundial jewish-morning-end asr
universal-from-dynamical dynamical-from-universal
sidereal-from-moment equation-of-time solar-longitude
solar-longitude-after sidereal-solar-longitude
lunar-longitude nth-new-moon new-moon-before new-moon-at-or-after
lunar-phase lunar-phase-at-or-before lunar-phase-at-or-after
topocentric-lunar-altitude lunar-diameter
lunar-latitude lunar-altitude lunar-distance
fixed-from-observational-islamic
persian-date persian-new-year-on-or-before
fixed-from-persian persian-from-fixed
arithmetic-persian-leap-year? fixed-from-arithmetic-persian
arithmetic-persian-year-from-fixed
arithmetic-persian-from-fixed naw-ruz french-date
french-new-year-on-or-before fixed-from-french
french-from-fixed arithmetic-french-leap-year?
fixed-from-arithmetic-french arithmetic-french-from-fixed
chinese-date chinese-cycle chinese-year chinese-month
chinese-leap chinese-day chinese-location
chinese-solar-longitude-on-or-after current-major-solar-term
major-solar-term-on-or-after current-minor-solar-term
minor-solar-term-on-or-after chinese-new-year-on-or-before
chinese-new-year chinese-from-fixed fixed-from-chinese
chinese-name chinese-stem chinese-branch
chinese-sexagesimal-name chinese-name-difference
chinese-name-of-year chinese-name-of-month
chinese-name-of-day chinese-day-name-on-or-before dragon-festival
qing-ming chinese-age chinese-year-marriage-augury
japanese-location korean-location korean-year
vietnamese-location hindu-lunar-date hindu-lunar-month
hindu-lunar-leap-month hindu-lunar-day hindu-lunar-leap-day
hindu-lunar-year hindu-lunar-day-at-or-after hindu-solar-from-fixed
fixed-from-hindu-solar hindu-lunar-from-fixed
fixed-from-hindu-lunar hindu-sunrise alt-hindu-sunrise
ayanamsha astro-hindu-sunset hindu-sunset hindu-sundial-time
astro-hindu-solar-from-fixed fixed-from-astro-hindu-solar
astro-hindu-lunar-from-fixed fixed-from-astro-hindu-lunar
hindu-fullmoon-from-fixed fixed-from-hindu-fullmoon
hindu-lunar-station hindu-solar-longitude-at-or-after
mesha-samkranti hindu-lunar-holiday diwali shiva rama
hindu-lunar-new-year karana yoga sacred-wednesdays
tibetan-from-fixed fixed-from-tibetan losar tibetan-new-year
future-bahai-new-year-on-or-before fixed-from-future-bahai
future-bahai-from-fixed phasis-on-or-before phasis-on-or-after
observational-islamic-from-fixed astronomical-easter
observational-hebrew-from-fixed fixed-from-observational-hebrew
observational-hebrew-new-year classical-passover-eve
))
;;;; Section: Basic Code
(defconstant true
;; TYPE boolean
;; Constant representing true.
t)
(defconstant false
;; TYPE boolean
;; Constant representing false.
nil)
(defconstant bogus
;; TYPE string
;; Used to denote nonexistent dates.
"bogus")
(defun quotient (m n)
;; TYPE (real nonzero-real) -> integer
;; Whole part of $m$/$n$.
(floor m n))
(defun amod (x y)
;; TYPE (real real) -> real
;; The value of ($x$ mod $y$) with $y$ instead of 0.
(+ y (mod x (- y))))
(defmacro next (index initial condition)
;; TYPE (* integer (integer->boolean)) -> integer
;; First integer greater or equal to $initial$ such that
;; $condition$ holds.
`(do ((,index ,initial (1+ ,index)))
(,condition ,index)))
(defmacro final (index initial condition)
;; TYPE (* integer (integer->boolean)) -> integer
;; Last integer greater or equal to $initial$ such that
;; $condition$ holds.
`(do ((,index ,initial (1+ ,index)))
((not ,condition) (1- ,index))))
(defmacro sum (expression index initial condition)
;; TYPE ((integer->real) * integer (integer->boolean))
;; TYPE -> real
;; Sum $expression$ for $index$ = $initial$ and successive
;; integers, as long as $condition$ holds.
(let* ((temp (gensym)))
`(do ((,temp 0 (+ ,temp ,expression))
(,index ,initial (1+ ,index)))
((not ,condition) ,temp))))
(defmacro binary-search (l lo h hi x test end)
;; TYPE (* real * real * (real->boolean)
;; TYPE ((real real)->boolean)) -> real
;; Bisection search for $x$ in [$lo$,$hi$] such that
;; $end$ holds. $test$ determines when to go left.
(let* ((left (gensym)))
`(do* ((,x false (/ (+ ,h ,l) 2))
(,left false ,test)
(,l ,lo (if ,left ,l ,x))
(,h ,hi (if ,left ,x ,h)))
(,end (/ (+ ,h ,l) 2)))))
(defmacro invert-angular (f y a b)
;; TYPE (real->angle real real real) -> real
;; Use bisection to find inverse of angular function
;; $f$ at $y$ within interval [$a$,$b$].
(let* ((varepsilon 1/100000)); Desired accuracy
`(binary-search l ,a u ,b x
(< (mod (- (,f x) ,y) 360) (deg 180))
(< (- u l) ,varepsilon))))
(defmacro sigma (list body)
;; TYPE (list-of-pairs (list-of-reals->real))
;; TYPE -> real
;; $list$ is of the form ((i1 l1)..(in ln)).
;; Sum of $body$ for indices i1..in
;; running simultaneously thru lists l1..ln.
`(apply '+ (mapcar (function (lambda
,(mapcar 'car list)
,body))
,@(mapcar 'cadr list))))
(defun poly (x a)
;; TYPE (real list-of-reals) -> real
;; Sum powers of $x$ with coefficients (from order 0 up)
;; in list $a$.
(if (equal a nil)
0
(+ (first a) (* x (poly x (rest a))))))
(defun rd (tee)
;; TYPE moment -> moment
;; Identity function for fixed dates/moments. If internal
;; timekeeping is shifted, change $epoch$ to be RD date of
;; origin of internal count. $epoch$ should be an integer.
(let* ((epoch 0))
(- tee epoch)))
(defconstant sunday
;; TYPE day-of-week
;; Residue class for Sunday.
0)
(defconstant monday
;; TYPE day-of-week
;; Residue class for Monday.
(+ sunday 1))
(defconstant tuesday
;; TYPE day-of-week
;; Residue class for Tuesday.
(+ sunday 2))
(defconstant wednesday
;; TYPE day-of-week
;; Residue class for Wednesday.
(+ sunday 3))
(defconstant thursday
;; TYPE day-of-week
;; Residue class for Thursday.
(+ sunday 4))
(defconstant friday
;; TYPE day-of-week
;; Residue class for Friday.
(+ sunday 5))
(defconstant saturday
;; TYPE day-of-week
;; Residue class for Saturday.
(+ sunday 6))
(defun day-of-week-from-fixed (date)
;; TYPE fixed-date -> day-of-week
;; The residue class of the day of the week of $date$.
(mod (- date (rd 0) sunday) 7))
(defun standard-month (date)
;; TYPE standard-date -> standard-month
;; Month field of $date$ = (year month day).
(second date))
(defun standard-day (date)
;; TYPE standard-date -> standard-day
;; Day field of $date$ = (year month day).
(third date))
(defun standard-year (date)
;; TYPE standard-date -> standard-year
;; Year field of $date$ = (year month day).
(first date))
(defun time-of-day (hour minute second)
;; TYPE (hour minute second) -> clock-time
(list hour minute second))
(defun hour (clock)
;; TYPE clock-time -> hour
(first clock))
(defun minute (clock)
;; TYPE clock-time -> minute
(second clock))
(defun seconds (clock)
;; TYPE clock-time -> second
(third clock))
(defun fixed-from-moment (tee)
;; TYPE moment -> fixed-date
;; Fixed-date from moment $tee$.
(floor tee))
(defun time-from-moment (tee)
;; TYPE moment -> time
;; Time from moment $tee$.
(mod tee 1))
(defun clock-from-moment (tee)
;; TYPE moment -> clock-time
;; Clock time hour:minute:second from moment $tee$.
(let* ((time (time-from-moment tee))
(hour (floor (* time 24)))
(minute (floor (mod (* time 24 60) 60)))
(second (mod (* time 24 60 60) 60)))
(time-of-day hour minute second)))
(defun time-from-clock (hms)
;; TYPE clock-time -> time
;; Time of day from $hms$ = (hour minute second).
(let* ((h (hour hms))
(m (minute hms))
(s (seconds hms)))
(* 1/24 (+ h (/ (+ m (/ s 60)) 60)))))
(defun degrees-minutes-seconds (d m s)
;; TYPE (degree minute real) -> angle
(list d m s))
(defun angle-from-degrees (alpha)
;; TYPE angle -> list-of-reals
;; List of degrees-arcminutes-arcseconds from angle
;; $alpha$ in degrees.
(let* ((d (floor alpha))
(m (floor (* 60 (mod alpha 1))))
(s (mod (* alpha 60 60) 60)))
(degrees-minutes-seconds d m s)))
(defconstant jd-epoch
;; TYPE moment
;; Fixed time of start of the julian day number.
(rd -1721424.5L0))
(defun moment-from-jd (jd)
;; TYPE julian-day-number -> moment
;; Moment of julian day number $jd$.
(+ jd jd-epoch))
(defun jd-from-moment (tee)
;; TYPE moment -> julian-day-number
;; Julian day number of moment $tee$.
(- tee jd-epoch))
(defun fixed-from-jd (jd)
;; TYPE julian-day-number -> fixed-date
;; Fixed date of julian day number $jd$.
(floor (moment-from-jd jd)))
(defun jd-from-fixed (date)
;; TYPE fixed-date -> julian-day-number
;; Julian day number of fixed $date$.
(jd-from-moment date))
(defconstant mjd-epoch
;; TYPE fixed-date
;; Fixed time of start of the modified julian day number.
(rd 678576))
(defun fixed-from-mjd (mjd)
;; TYPE julian-day-number -> fixed-date
;; Fixed date of modified julian day number $mjd$.
(+ mjd mjd-epoch))
(defun mjd-from-fixed (date)
;; TYPE fixed-date -> julian-day-number
;; Modified julian day number of fixed $date$.
(- date mjd-epoch))
(defun interval (t0 t1)
;; TYPE (moment moment) -> range
;; Closed interval [$t0$,$t1$].
(list t0 t1))
(defun start (range)
;; TYPE range -> moment
;; Start $t0$ of $range$=[$t0$,$t1$].
(first range))
(defun end (range)
;; TYPE range -> moment
;; End $t1$ of $range$=[$t0$,$t1$].
(second range))
(defun in-range? (tee range)
;; TYPE (moment range) -> boolean
;; True if $tee$ is in $range$.
(<= (start range) tee (end range)))
(defun list-range (ell range)
;; TYPE (list-of-moments range) -> range
;; Those moments in list $ell$ that occur in $range$.
(if (equal ell nil)
nil
(let* ((r (list-range (rest ell) range)))
(if (in-range? (first ell) range)
(append (list (first ell)) r)
r))))
;;;; Section: Egyptian/Armenian Calendars
(defun egyptian-date (year month day)
;; TYPE (egyptian-year egyptian-month egyptian-day)
;; TYPE -> egyptian-date
(list year month day))
(defconstant egyptian-epoch
;; TYPE fixed-date
;; Fixed date of start of the Egyptian (Nabonasser)
;; calendar.
;; JD 1448638 = February 26, 747 BCE (Julian).
(fixed-from-jd 1448638))
(defun fixed-from-egyptian (e-date)
;; TYPE egyptian-date -> fixed-date
;; Fixed date of Egyptian date $e-date$.
(let* ((month (standard-month e-date))
(day (standard-day e-date))
(year (standard-year e-date)))
(+ egyptian-epoch ; Days before start of calendar
(* 365 (1- year)); Days in prior years
(* 30 (1- month)); Days in prior months this year
day -1))) ; Days so far this month
(defun egyptian-from-fixed (date)
;; TYPE fixed-date -> egyptian-date
;; Egyptian equivalent of fixed $date$.
(let* ((days ; Elapsed days since epoch.
(- date egyptian-epoch))
(year ; Year since epoch.
(1+ (quotient days 365)))
(month; Calculate the month by division.
(1+ (quotient (mod days 365)
30)))
(day ; Calculate the day by subtraction.
(- days
(* 365 (1- year))
(* 30 (1- month))
-1)))
(egyptian-date year month day)))
(defun armenian-date (year month day)
;; TYPE (armenian-year armenian-month armenian-day)
;; TYPE -> armenian-date
(list year month day))
(defconstant armenian-epoch
;; TYPE fixed-date
;; Fixed date of start of the Armenian calendar.
;; = July 11, 552 CE (Julian).
(rd 201443))
(defun fixed-from-armenian (a-date)
;; TYPE armenian-date -> fixed-date
;; Fixed date of Armenian date $a-date$.
(let* ((month (standard-month a-date))
(day (standard-day a-date))
(year (standard-year a-date)))
(+ armenian-epoch
(- (fixed-from-egyptian
(egyptian-date year month day))
egyptian-epoch))))
(defun armenian-from-fixed (date)
;; TYPE fixed-date -> armenian-date
;; Armenian equivalent of fixed $date$.
(egyptian-from-fixed
(+ date (- egyptian-epoch armenian-epoch))))
;;;; Section: Gregorian Calendar
(defun gregorian-date (year month day)
;; TYPE (gregorian-year gregorian-month gregorian-day)
;; TYPE -> gregorian-date
(list year month day))
(defconstant gregorian-epoch
;; TYPE fixed-date
;; Fixed date of start of the (proleptic) Gregorian
;; calendar.
(rd 1))
(defconstant january
;; TYPE standard-month
;; January on Julian/Gregorian calendar.
1)
(defconstant february
;; TYPE standard-month
;; February on Julian/Gregorian calendar.
2)
(defconstant march
;; TYPE standard-month
;; March on Julian/Gregorian calendar.
3)
(defconstant april
;; TYPE standard-month
;; April on Julian/Gregorian calendar.
4)
(defconstant may
;; TYPE standard-month
;; May on Julian/Gregorian calendar.
5)
(defconstant june
;; TYPE standard-month
;; June on Julian/Gregorian calendar.
6)
(defconstant july
;; TYPE standard-month
;; July on Julian/Gregorian calendar.
7)
(defconstant august
;; TYPE standard-month
;; August on Julian/Gregorian calendar.
8)
(defconstant september
;; TYPE standard-month
;; September on Julian/Gregorian calendar.
9)
(defconstant october
;; TYPE standard-month
;; October on Julian/Gregorian calendar.
10)
(defconstant november
;; TYPE standard-month
;; November on Julian/Gregorian calendar.
11)
(defconstant december
;; TYPE standard-month
;; December on Julian/Gregorian calendar.
12)
(defun gregorian-leap-year? (g-year)
;; TYPE gregorian-year -> boolean
;; True if $g-year$ is a leap year on the Gregorian
;; calendar.
(and (= (mod g-year 4) 0)
(not (member (mod g-year 400)
(list 100 200 300)))))
(defun fixed-from-gregorian (g-date)
;; TYPE gregorian-date -> fixed-date
;; Fixed date equivalent to the Gregorian date $g-date$.
(let* ((month (standard-month g-date))
(day (standard-day g-date))
(year (standard-year g-date)))
(+ (1- gregorian-epoch); Days before start of calendar
(* 365 (1- year)); Ordinary days since epoch
(quotient (1- year)
4); Julian leap days since epoch...
(- ; ...minus century years since epoch...
(quotient (1- year) 100))
(quotient ; ...plus years since epoch divisible...
(1- year) 400) ; ...by 400.
(quotient ; Days in prior months this year...
(- (* 367 month) 362); ...assuming 30-day Feb
12)
(if (<= month 2) ; Correct for 28- or 29-day Feb
0
(if (gregorian-leap-year? year)
-1
-2))
day))) ; Days so far this month.
(defun gregorian-year-from-fixed (date)
;; TYPE fixed-date -> gregorian-year
;; Gregorian year corresponding to the fixed $date$.
(let* ((d0 ; Prior days.
(- date gregorian-epoch))
(n400 ; Completed 400-year cycles.
(quotient d0 146097))
(d1 ; Prior days not in n400.
(mod d0 146097))
(n100 ; 100-year cycles not in n400.
(quotient d1 36524))
(d2 ; Prior days not in n400 or n100.
(mod d1 36524))
(n4 ; 4-year cycles not in n400 or n100.
(quotient d2 1461))
(d3 ; Prior days not in n400, n100, or n4.
(mod d2 1461))
(n1 ; Years not in n400, n100, or n4.
(quotient d3 365))
(year (+ (* 400 n400)
(* 100 n100)
(* 4 n4)
n1)))
(if (or (= n100 4) (= n1 4))
year ; Date is day 366 in a leap year.
(1+ year)))); Date is ordinal day (1+ (mod d3 365))
; in (1+ year).
(defun gregorian-new-year (g-year)
;; TYPE gregorian-year -> fixed-date
;; Fixed date of January 1 in $g-year$.
(fixed-from-gregorian
(gregorian-date g-year january 1)))
(defun gregorian-year-end (g-year)
;; TYPE gregorian-year -> fixed-date
;; Fixed date of December 31 in $g-year$.
(fixed-from-gregorian
(gregorian-date g-year december 31)))
(defun gregorian-year-range (g-year)
;; TYPE gregorian-year -> range
;; The range of moments in Gregorian year $g-year$.
(interval (gregorian-new-year g-year)
(gregorian-year-end g-year)))
(defun gregorian-from-fixed (date)
;; TYPE fixed-date -> gregorian-date
;; Gregorian (year month day) corresponding to fixed $date$.
(let* ((year (gregorian-year-from-fixed date))
(prior-days; This year
(- date (gregorian-new-year year)))
(correction; To simulate a 30-day Feb
(if (< date (fixed-from-gregorian
(gregorian-date year march 1)))
0
(if (gregorian-leap-year? year)
1
2)))
(month ; Assuming a 30-day Feb
(quotient
(+ (* 12 (+ prior-days correction)) 373)
367))
(day ; Calculate the day by subtraction.
(1+ (- date
(fixed-from-gregorian
(gregorian-date year month 1))))))
(gregorian-date year month day)))
(defun gregorian-date-difference (g-date1 g-date2)
;; TYPE (gregorian-date gregorian-date) -> integer
;; Number of days from Gregorian date $g-date1$ until
;; $g-date2$.
(- (fixed-from-gregorian g-date2)
(fixed-from-gregorian g-date1)))
(defun day-number (g-date)
;; TYPE gregorian-date -> positive-integer
;; Day number in year of Gregorian date $g-date$.
(gregorian-date-difference
(gregorian-year-end (1- (standard-year g-date)))
g-date))
(defun days-remaining (g-date)
;; TYPE gregorian-date -> nonnegative-integer
;; Days remaining in year after Gregorian date $g-date$.
(gregorian-date-difference
g-date
(gregorian-year-end (standard-year g-date))))
(defun alt-fixed-from-gregorian (g-date)
;; TYPE gregorian-date -> fixed-date
;; Alternative calculation of fixed date equivalent to the
;; Gregorian date $g-date$.
(let* ((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))))
(+ (1- gregorian-epoch)
-306 ; Days in March...December.
(* 365 (1- y)); Ordinary days since epoch.
(quotient (1- y)
4); Julian leap days since epoch...
(- ; ...minus century years since epoch...
(quotient (1- y) 100))
(quotient ; ...plus years since epoch divisible...
(1- y) 400); ...by 400.
(quotient ; Days in prior months this year.
(1- (* 3 m))
5)
(* 30 (1- m))
day))) ; Days so far this month.
(defun alt-gregorian-from-fixed (date)
;; TYPE fixed-date -> gregorian-date
;; Alternative calculation of Gregorian (year month day)
;; corresponding to fixed $date$.
(let* ((y (gregorian-year-from-fixed
(+ (1- gregorian-epoch)
date
306)))
(prior-days
(- date (fixed-from-gregorian
(gregorian-date (1- y) march 1))))
(month
(amod (+ (quotient
(+ (* 5 prior-days) 2)
153)
3)
12))
(year (- y (quotient (+ month 9) 12)))
(day
(1+ (- date
(fixed-from-gregorian
(gregorian-date year month 1))))))
(gregorian-date year month day)))
(defun alt-gregorian-year-from-fixed (date)
;; TYPE fixed-date -> gregorian-year
;; Gregorian year corresponding to the fixed $date$.
(let* ((approx ; approximate year
(quotient (- date gregorian-epoch -2)
146097/400))
(start ; start of next year
(+ gregorian-epoch
(* 365 approx)
(quotient approx 4)
(- (quotient approx 100))
(quotient approx 400))))
(if (< date start)
approx
(1+ approx))))
(defun independence-day (g-year)
;; TYPE gregorian-year -> fixed-date
;; Fixed date of United States Independence Day in
;; Gregorian year $g-yaer$.
(fixed-from-gregorian (gregorian-date g-year july 4)))
(defun kday-on-or-before (k date)
;; TYPE (day-of-week fixed-date) -> fixed-date
;; Fixed date of the $k$-day on or before fixed $date$.
;; $k$=0 means Sunday, $k$=1 means Monday, and so on.
(- date (day-of-week-from-fixed (- date k))))
(defun kday-on-or-after (k date)
;; TYPE (day-of-week fixed-date) -> fixed-date
;; Fixed date of the $k$-day on or after fixed $date$.
;; $k$=0 means Sunday, $k$=1 means Monday, and so on.
(kday-on-or-before k (+ date 6)))
(defun kday-nearest (k date)
;; TYPE (day-of-week fixed-date) -> fixed-date
;; Fixed date of the $k$-day nearest fixed $date$.
;; $k$=0 means Sunday, $k$=1 means Monday, and so on.
(kday-on-or-before k (+ date 3)))
(defun kday-after (k date)
;; TYPE (day-of-week fixed-date) -> fixed-date
;; Fixed date of the $k$-day after fixed $date$.
;; $k$=0 means Sunday, $k$=1 means Monday, and so on.
(kday-on-or-before k (+ date 7)))
(defun kday-before (k date)
;; TYPE (day-of-week fixed-date) -> fixed-date
;; Fixed date of the $k$-day before fixed $date$.
;; $k$=0 means Sunday, $k$=1 means Monday, and so on.
(kday-on-or-before k (- date 1)))
(defun nth-kday (n k g-date)
;; TYPE (integer day-of-week gregorian-date) -> fixed-date
;; 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$. A $k$-day of 0 means Sunday, 1
;; means Monday, and so on.
(if (> n 0)
(+ (* 7 n)
(kday-before k (fixed-from-gregorian g-date)))
(+ (* 7 n)
(kday-after k (fixed-from-gregorian g-date)))))
(defun first-kday (k g-date)
;; TYPE (day-of-week gregorian-date) -> fixed-date
;; 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.
(nth-kday 1 k g-date))
(defun last-kday (k g-date)
;; TYPE (day-of-week gregorian-date) -> fixed-date
;; 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.
(nth-kday -1 k g-date))
(defun labor-day (g-year)
;; TYPE gregorian-year -> fixed-date
;; Fixed date of United States Labor Day in Gregorian
;; year $g-year$ (the first Monday in September).
(first-kday monday (gregorian-date g-year september 1)))
(defun memorial-day (g-year)
;; TYPE gregorian-year -> fixed-date
;; Fixed date of United States Memorial Day in Gregorian
;; year $g-year$ (the last Monday in May).
(last-kday monday (gregorian-date g-year may 31)))
(defun election-day (g-year)
;; TYPE gregorian-year -> fixed-date
;; Fixed date of United States Election Day in Gregorian
;; year $g-year$ (the Tuesday after the first Monday in
;; November).
(first-kday tuesday (gregorian-date g-year november 2)))
(defun daylight-saving-start (g-year)
;; TYPE gregorian-year -> fixed-date
;; Fixed date of the start of United States daylight
;; saving time in Gregorian year $g-year$ (the second
;; Sunday in March).
(nth-kday 2 sunday (gregorian-date g-year march 1)))
(defun daylight-saving-end (g-year)
;; TYPE gregorian-year -> fixed-date
;; Fixed date of the end of United States daylight saving
;; time in Gregorian year $g-year$ (the first Sunday in
;; November).
(first-kday sunday (gregorian-date g-year november 1)))
(defun christmas (g-year)
;; TYPE gregorian-year -> fixed-date
;; Fixed date of Christmas in Gregorian year $g-year$.
(fixed-from-gregorian
(gregorian-date g-year december 25)))
(defun advent (g-year)
;; TYPE gregorian-year -> fixed-date
;; Fixed date of Advent in Gregorian year $g-year$
;; (the Sunday closest to November 30).
(kday-nearest sunday
(fixed-from-gregorian
(gregorian-date g-year november 30))))
(defun epiphany (g-year)
;; TYPE gregorian-year -> fixed-date
;; Fixed date of Epiphany in U.S. in Gregorian year
;; $g-year$ (the first Sunday after January 1).
(first-kday sunday (gregorian-date g-year january 2)))
(defun unlucky-fridays-in-range (range)
;; TYPE range -> list-of-fixed-dates
;; List of Fridays within $range$ of dates
;; that are day 13 of Gregorian months.
(let* ((a (start range))
(b (end range))
(fri (kday-on-or-after friday a))
(date (gregorian-from-fixed fri)))
(if (in-range? fri range)
(append
(if (= (standard-day date) 13)
(list fri)
nil)
(unlucky-fridays-in-range
(interval (1+ fri) b)))
nil)))
;;;; Section: ISO Calendar
(defun iso-date (year week day)
;; TYPE (iso-year iso-week iso-day) -> iso-date
(list year week day))
(defun iso-week (date)
;; TYPE iso-date -> iso-week
(second date))
(defun iso-day (date)
;; TYPE iso-date -> day-of-week
(third date))
(defun iso-year (date)
;; TYPE iso-date -> iso-year
(first date))
(defun fixed-from-iso (i-date)
;; TYPE iso-date -> fixed-date
;; Fixed date equivalent to ISO $i-date$.
(let* ((week (iso-week i-date))
(day (iso-day i-date))
(year (iso-year i-date)))