forked from EdReingold/calendar-code2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.clj
6995 lines (6238 loc) · 237 KB
/
core.clj
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
(ns clj-calcalc.core
(:refer-clojure :exclude [next]) ;; next is used to compute the next occurrence of a phenomenon
(:require [clojure.math.numeric-tower :as math :refer [expt floor round abs]]))
(declare summer)
(declare spring)
(declare winter)
(declare autumn)
(declare deg)
(declare hr)
(declare begin)
(declare end)
;;;; Section: Basic Code
(defn third [coll]
(nth coll 2))
(defn fourth [coll]
(nth coll 3))
(defn fifth [coll]
(nth coll 4))
(defn sixth [coll]
(nth coll 5))
(defn seventh [coll]
(nth coll 6))
(defn eighth [coll]
(nth coll 7))
(defn ninth [coll]
(nth coll 8))
(defn tenth [coll]
(nth coll 9))
(def length count)
(defn append
([coll x]
(concat coll [x]))
([coll1 coll2 & more]
(apply append (concat coll1 coll2) more)))
(defn member [x coll]
(boolean (some #{x} coll)))
(defn ceiling [x]
(Math/ceil x))
(def evenp even?)
(def integerp integer?)
(defn cos [x]
(Math/cos x))
(defn sin [x]
(Math/sin x))
(defn tan [x]
(Math/tan x))
(defn asin [x]
(Math/asin x))
(defn acos [x]
(Math/acos x))
(defn atan [x]
(Math/atan x))
(def PI Math/PI)
(defn sqrt [x]
(Math/sqrt x))
;; (def true
;; ;; TYPE boolean
;; ;; Constant representing true.
;; t)
;; (def false
;; ;; TYPE boolean
;; ;; Constant representing false.
;; nil)
(def bogus
;; TYPE string
;; Used to denote nonexistent dates.
"bogus")
;; copying the ICU4X logic from icu_calendar::helpers::div_rem_euclid,
;; which is based on i32 (ints)
(defn div-rem-euclid [n d]
(let [a (quot n d)
b (rem n d)]
(if (or (>= n 0) (zero? b))
[a b]
[(dec a) (+ d b)])))
(defn rem-euclid [n d]
(-> (div-rem-euclid n d)
second))
(defn quotient [m n]
;; TYPE (real nonzero-real) -> integer
;; Whole part of $m$/$n$.
;;(int (/ m n))
;; copying the ICU4X logic from icu_calendar::helpers::quotient,
;; which is based on i32 (ints)
;; (let [a (quot m n)
;; b (rem m n)]
;; (if (or (>= m 0) (zero? b))
;; a
;; (dec a)))
(-> (div-rem-euclid m n)
first))
(defn amod [x y]
;; TYPE (integer nonzero-integer) -> integer
;; The value of ($x$ mod $y$) with $y$ instead of 0.
(+ y (mod x (- y))))
(defn mod3 [x a b]
;; TYPE (real real real) -> real
;; The value of $x$ shifted into the range
;; [$a$..$b$). Returns $x$ if $a=b$.
(if (= a b)
x
(+ a (mod (- x a) (- b a)))))
(defmacro next [index initial condition]
;; TYPE (* integer (integer->boolean)) -> integer
;; First integer greater or equal to $initial$ such that
;; $condition$ holds.
`(loop [~index ~initial]
(if ~condition
~index
(recur (inc ~index)))))
(defmacro final [index initial condition]
;; TYPE (* integer (integer->boolean)) -> integer
;; Last integer greater or equal to $initial$ such that
;; $condition$ holds.
`(loop [~index ~initial]
(if-not ~condition
(dec ~index)
(recur (inc ~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.
`(loop [~index ~initial
sum# 0]
(if-not ~condition
sum#
(recur (inc ~index)
(+ sum# ~expression)))))
(defmacro prod [expression index initial condition]
;; TYPE ((integer->real) * integer (integer->boolean))
;; TYPE -> real
;; Product of $expression$ for $index$ = $initial$ and successive
;; integers, as long as $condition$ holds.
`(apply *
(loop [~index ~initial
exprs# []]
(if-not ~condition
exprs#
(recur (inc ~index)
(conj exprs# ~expression))))))
(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.
`(loop [~l ~lo
~h ~hi
~x (/ (+ ~h ~l) 2)]
(let [left# ~test]
(if ~end
~x
(let [new-l# (if left# ~l ~x)
new-h# (if left# ~x ~h)
new-x# (/ (+ new-h# new-l#) 2)]
(recur new-l#
new-h#
new-x#))))))
(defmacro invert-angular [f y r]
;; TYPE (real->angle real interval) -> real
;; Use bisection to find inverse of angular function
;; $f$ at $y$ within interval $r$.
(let [varepsilon# 1/100000] ; Desired accuracy
`(binary-search l# (begin ~r) u# (end ~r) 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.
;;
;; list is now a typical Clojure binding vector
`(apply + (map (fn ~(vec (map first (partition 2 list)))
~body)
~@(map second (partition 2 list)))))
(defn poly [x a]
;; TYPE (real list-of-reals) -> real
;; Sum powers of $x$ with coefficients (from order 0 up)
;; in list $a$.
(if (empty? a)
0
(+ (first a) (* x (poly x (rest a))))))
(defn 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)))
(def sunday
;; TYPE day-of-week
;; Residue class for Sunday.
0)
(def monday
;; TYPE day-of-week
;; Residue class for Monday.
1)
(def tuesday
;; TYPE day-of-week
;; Residue class for Tuesday.
2)
(def wednesday
;; TYPE day-of-week
;; Residue class for Wednesday.
3)
(def thursday
;; TYPE day-of-week
;; Residue class for Thursday.
4)
(def friday
;; TYPE day-of-week
;; Residue class for Friday.
5)
(def saturday
;; TYPE day-of-week
;; Residue class for Saturday.
6)
(defn 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))
(defn standard-month [date]
;; TYPE standard-date -> standard-month
;; Month field of $date$ = (year month day).
(second date))
(defn standard-day [date]
;; TYPE standard-date -> standard-day
;; Day field of $date$ = (year month day).
(third date))
(defn standard-year [date]
;; TYPE standard-date -> standard-year
;; Year field of $date$ = (year month day).
(first date))
(defn time-of-day [hour minute second]
;; TYPE (hour minute second) -> clock-time
(list hour minute second))
(defn hour [clock]
;; TYPE clock-time -> hour
(first clock))
(defn minute [clock]
;; TYPE clock-time -> minute
(second clock))
(defn seconds [clock]
;; TYPE clock-time -> second
(third clock))
(defn fixed-from-moment [tee]
;; TYPE moment -> fixed-date
;; Fixed-date from moment $tee$.
(floor tee))
(defn time-from-moment [tee]
;; TYPE moment -> time
;; Time from moment $tee$.
(mod tee 1))
(defn from-radix [a b &optional c]
;; TYPE (list-of-reals list-of-rationals list-of-rationals)
;; TYPE -> real
;; The number corresponding to $a$ in radix notation
;; with base $b$ for whole part and $c$ for fraction.
(/ (sum (* (nth i a)
(prod (nth j (append b c))
j i (< j (+ (length b) (length c)))))
i 0 (< i (length a)))
(apply * c)))
(defn to-radix [x b &optional c]
;; TYPE (real list-of-rationals list-of-rationals)
;; TYPE -> list-of-reals
;; The radix notation corresponding to $x$
;; with base $b$ for whole part and $c$ for fraction.
(if (empty? c)
(if (empty? b)
(list x)
(append (to-radix (quotient x (nth (dec (length b)) b))
(butlast b) nil)
(list (mod x (nth (dec (length b)) b)))))
(to-radix (* x (apply * c)) (append b c))))
(defn clock-from-moment [tee]
;; TYPE moment -> clock-time
;; Clock time hour:minute:second from moment $tee$.
(rest (to-radix tee nil (list 24 60 60))))
(defn time-from-clock [hms]
;; TYPE clock-time -> time
;; Time of day from $hms$ = hour:minute:second.
(/ (from-radix hms nil (list 24 60 60)) 24))
(defn degrees-minutes-seconds [d m s]
;; TYPE (degree minute real) -> angle
(list d m s))
(defn angle-from-degrees [alpha]
;; TYPE angle -> list-of-reals
;; List of degrees-arcminutes-arcseconds from angle $alpha$
;; in degrees.
(let [dms (to-radix (abs alpha) nil (list 60 60))]
(if (>= alpha 0)
dms
(list ; degrees-minutes-seconds
(- (first dms)) (- (second dms)) (- (third dms))))))
(def jd-epoch
;; TYPE moment
;; Fixed time of start of the julian day number.
(rd -1721424.5))
(defn moment-from-jd [jd]
;; TYPE julian-day-number -> moment
;; Moment of julian day number $jd$.
(+ jd jd-epoch))
(defn jd-from-moment [tee]
;; TYPE moment -> julian-day-number
;; Julian day number of moment $tee$.
(- tee jd-epoch))
(defn fixed-from-jd [jd]
;; TYPE julian-day-number -> fixed-date
;; Fixed date of julian day number $jd$.
(floor (moment-from-jd jd)))
(defn jd-from-fixed [date]
;; TYPE fixed-date -> julian-day-number
;; Julian day number of fixed $date$.
(jd-from-moment date))
(def mjd-epoch
;; TYPE fixed-date
;; Fixed time of start of the modified julian day number.
(rd 678576))
(defn fixed-from-mjd [mjd]
;; TYPE julian-day-number -> fixed-date
;; Fixed date of modified julian day number $mjd$.
(+ mjd mjd-epoch))
(defn mjd-from-fixed [date]
;; TYPE fixed-date -> julian-day-number
;; Modified julian day number of fixed $date$.
(- date mjd-epoch))
(def unix-epoch
;; TYPE fixed-date
;; Fixed date of the start of the Unix second count.
(rd 719163))
(defn moment-from-unix [s]
;; TYPE second -> moment
;; Fixed date from Unix second count $s$
(+ unix-epoch (/ s 24 60 60)))
(defn unix-from-moment [tee]
;; TYPE moment -> second
;; Unix second count from moment $tee$
(* 24 60 60 (- tee unix-epoch)))
(defn sign [y]
;; TYPE real -> {-1,0,+1}
;; Sign of $y$.
(cond
(< y 0) -1
(> y 0) +1
:true 0))
(defn list-of-fixed-from-moments [ell]
;; TYPE list-of-moments -> list-of-fixed-dates
;; List of fixed dates corresponding to list $ell$
;; of moments.
(if (empty? ell)
nil
(append (list (fixed-from-moment (first ell)))
(list-of-fixed-from-moments (rest ell)))))
(defn interval [t0 t1]
;; TYPE (moment moment) -> interval
;; Half-open interval [$t0$..$t1$).
(list t0 t1))
(defn interval-closed [t0 t1]
;; TYPE (moment moment) -> interval
;; Closed interval [$t0$..$t1$].
(list t0 t1))
(defn begin [range]
;; TYPE interval -> moment
;; Start $t0$ of $range$ [$t0$..$t1$) or [$t0$..$t1$].
(first range))
(defn end [range]
;; TYPE interval -> moment
;; End $t1$ of $range$ [$t0$..$t1$) or [$t0$..$t1$].
(second range))
(defn in-range? [tee range]
;; TYPE (moment interval) -> boolean
;; True if $tee$ is in half-open $range$.
(and (<= (begin range) tee) (< tee (end range))))
(defn list-range [ell range]
;; TYPE (list-of-moments interval) -> list-of-moments
;; Those moments in list $ell$ that occur in $range$.
(if (empty? ell)
nil
(let [r (list-range (rest ell) range)]
(if (in-range? (first ell) range)
(append (list (first ell)) r)
r))))
(defn positions-in-range [p c cap-Delta range]
;; TYPE (nonegative-real positive-real
;; TYPE nonegative-real interval) -> list-of-moments
;; List of occurrences of moment $p$ of $c$-day cycle
;; within $range$.
;; $cap-Delta$ is position in cycle of RD moment 0.
(let [a (begin range)
b (end range)
date (mod3 (- p cap-Delta) a (+ a c))]
(if (>= date b)
nil
(append (list date)
(positions-in-range p c cap-Delta
(interval (+ a c) b))))))
;;;; Section: Egyptian/Armenian Calendars
(defn egyptian-date [year month day]
;; TYPE (egyptian-year egyptian-month egyptian-day)
;; TYPE -> egyptian-date
(list year month day))
(def 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))
(defn 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 (dec year)); Days in prior years
(* 30 (dec month)); Days in prior months this year
day -1))) ; Days so far this month
(defn alt-fixed-from-egyptian [e-date]
;; TYPE egyptian-date -> fixed-date
;; Fixed date of Egyptian date $e-date$.
(+ egyptian-epoch
(sigma [a (list 365 30 1)
e-date e-date]
(* a (dec e-date)))))
(defn 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.
(inc (quotient days 365))
month ; Calculate the month by division.
(inc (quotient (mod days 365)
30))
day ; Calculate the day by subtraction.
(- days
(* 365 (dec year))
(* 30 (dec month))
-1)]
(egyptian-date year month day)))
(defn armenian-date [year month day]
;; TYPE (armenian-year armenian-month armenian-day)
;; TYPE -> armenian-date
(list year month day))
(def armenian-epoch
;; TYPE fixed-date
;; Fixed date of start of the Armenian calendar.
;; = July 11, 552 CE (Julian).
(rd 201443))
(defn 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))))
(defn armenian-from-fixed [date]
;; TYPE fixed-date -> armenian-date
;; Armenian equivalent of fixed $date$.
(egyptian-from-fixed
(+ date (- egyptian-epoch armenian-epoch))))
;;;; Section: Akan Calendar
(defn akan-name [prefix stem]
;; TYPE (akan-prefix akan-stem) -> akan-name
(list prefix stem))
(defn akan-prefix [name]
;; TYPE akan-name -> akan-prefix
(first name))
(defn akan-stem [name]
;; TYPE akan-name -> akan-stem
(second name))
(defn akan-day-name [n]
;; TYPE integer -> akan-name
;; The $n$-th name of the Akan cycle.
(akan-name (amod n 6)
(amod n 7)))
(defn akan-name-difference [a-name1 a-name2]
;; TYPE (akan-name akan-name) -> nonnegative-integer
;; Number of names from Akan name $a-name1$ to the
;; next occurrence of Akan name $a-name2$.
(let [prefix1 (akan-prefix a-name1)
prefix2 (akan-prefix a-name2)
stem1 (akan-stem a-name1)
stem2 (akan-stem a-name2)
prefix-difference (- prefix2 prefix1)
stem-difference (- stem2 stem1)]
(amod (+ prefix-difference
(* 36 (- stem-difference
prefix-difference)))
42)))
(def akan-day-name-epoch
;; TYPE fixed-date
;; RD date of an epoch (day 0) of Akan day cycle.
(rd 37))
(defn akan-name-from-fixed [date]
;; TYPE fixed-date -> akan-name
;; Akan name for $date$.
(akan-day-name (- date akan-day-name-epoch)))
(defn akan-day-name-on-or-before [name date]
;; TYPE (akan-name fixed-date) -> fixed-date
;; Fixed date of latest date on or before fixed $date$
;; that has Akan $name$.
(mod3
(akan-name-difference (akan-name-from-fixed 0) name)
date (- date 42)))
;;;; Section: Gregorian Calendar
(defn gregorian-date [year month day]
;; TYPE (gregorian-year gregorian-month gregorian-day)
;; TYPE -> gregorian-date
(list year month day))
(def gregorian-epoch
;; TYPE fixed-date
;; Fixed date of start of the (proleptic) Gregorian
;; calendar.
(rd 1))
(def january
;; TYPE standard-month
;; January on Julian/Gregorian calendar.
1)
(def february
;; TYPE standard-month
;; February on Julian/Gregorian calendar.
2)
(def march
;; TYPE standard-month
;; March on Julian/Gregorian calendar.
3)
(def april
;; TYPE standard-month
;; April on Julian/Gregorian calendar.
4)
(def may
;; TYPE standard-month
;; May on Julian/Gregorian calendar.
5)
(def june
;; TYPE standard-month
;; June on Julian/Gregorian calendar.
6)
(def july
;; TYPE standard-month
;; July on Julian/Gregorian calendar.
7)
(def august
;; TYPE standard-month
;; August on Julian/Gregorian calendar.
8)
(def september
;; TYPE standard-month
;; September on Julian/Gregorian calendar.
9)
(def october
;; TYPE standard-month
;; October on Julian/Gregorian calendar.
10)
(def november
;; TYPE standard-month
;; November on Julian/Gregorian calendar.
11)
(def december
;; TYPE standard-month
;; December on Julian/Gregorian calendar.
12)
(defn 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)))))
(defn 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)]
(+ (dec gregorian-epoch); Days before start of calendar
(* 365 (dec year)); Ordinary days since epoch
(quotient (dec year)
4); Julian leap days since epoch...
(- ; ...minus century years since epoch...
(quotient (dec year) 100))
(quotient ; ...plus years since epoch divisible...
(dec 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.
(defn 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.
(inc year)))); Date is ordinal day (inc (mod d3 365))
; in (inc year).
(defn 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)))
(defn 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)))
(defn 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-new-year (inc g-year))))
(defn 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.
(inc (- date
(fixed-from-gregorian
(gregorian-date year month 1))))]
(gregorian-date year month day)))
(defn 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)))
(defn day-number [g-date]
;; TYPE gregorian-date -> positive-integer
;; Day number in year of Gregorian date $g-date$.
(gregorian-date-difference
(gregorian-date (dec (standard-year g-date)) december 31)
g-date))
(defn days-remaining [g-date]
;; TYPE gregorian-date -> nonnegative-integer
;; Days remaining in year after Gregorian date $g-date$.
(gregorian-date-difference
g-date
(gregorian-date (standard-year g-date) december 31)))
(defn last-day-of-gregorian-month [g-year g-month]
;; TYPE (gregorian-year gregorian-month) -> gregorian-day
;; Last day of month $g-month$ in Gregorian year $g-year$.
(gregorian-date-difference
(gregorian-date g-year g-month 1)
(gregorian-date (if (= g-month 12)
(inc g-year)
g-year)
(amod (inc g-month) 12)
1)))
(defn 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-prime (mod (- month 3) 12)
y-prime (- year (quotient m-prime 10))]
(+ (dec gregorian-epoch)
-306 ; Days in March...December.
(* 365 y-prime); Ordinary days.
(sigma [y (to-radix y-prime (list 4 25 4))
a (list 97 24 1 0)]
(* y a))
(quotient ; Days in prior months.
(+ (* 3 m-prime) 2)
5)
(* 30 m-prime)
day))) ; Days so far this month.
(defn 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
(+ (dec gregorian-epoch)
date
306))
prior-days
(- date (fixed-from-gregorian
(gregorian-date (dec y) march 1)))
month
(amod (+ (quotient
(+ (* 5 prior-days) 2)
153)
3)
12)
year (- y (quotient (+ month 9) 12))
day
(inc (- date
(fixed-from-gregorian
(gregorian-date year month 1))))]
(gregorian-date year month day)))
(defn 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)
(sigma [y (to-radix approx (list 4 25 4))
a (list 97 24 1 0)]
(* y a)))]
(if (< date start)
approx
(inc approx))))
(defn 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)))
(defn 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))))
(defn 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)))
(defn 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)))
(defn 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)))
(defn 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)))
(defn nth-kday [n k g-date]
;; TYPE (integer day-of-week gregorian-date) -> fixed-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.
(cond (> n 0)
(+ (* 7 n)
(kday-before k (fixed-from-gregorian g-date)))
(< n 0)
(+ (* 7 n)
(kday-after k (fixed-from-gregorian g-date)))
:true bogus))
(defn 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))
(defn 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))
(defn 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)))
(defn 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)))
(defn 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)))
(defn 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)))
(defn 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)))
(defn 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)))
(defn 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))))
(defn epiphany [g-year]