/
luminary.clj
821 lines (727 loc) · 30.2 KB
/
luminary.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
(ns xyz.thoren.luminary
(:require [java-time :as t]
[xyz.thoren.equinox :refer [march-equinox]]
[xyz.thoren.luminary-feast-data :as fd])
(:import (org.shredzone.commons.suncalc SunTimes MoonPhase)))
(def jerusalem-lat 31.7781161)
(def jerusalem-lon 35.233804)
(def jerusalem-tz "Asia/Jerusalem")
(def calculated-feast-days
(apply merge [fd/feasts-2020
fd/feasts-2021
fd/feasts-2022
fd/feasts-2023
fd/feasts-2024
fd/feasts-2025
fd/feasts-2026
fd/feasts-2027
fd/feasts-2028
fd/feasts-2029
fd/feasts-2030
fd/feasts-2031
fd/feasts-2032
fd/feasts-2033
fd/feasts-2034
fd/feasts-2035
fd/feasts-2036
fd/feasts-2037
fd/feasts-2038
fd/feasts-2039]))
(defn- make-utc-date
[& args]
(t/with-zone (apply t/zoned-date-time args) "UTC"))
(defn- date-of-march-equinox
[year]
(->> [:year :month :day :hour :minute :second]
(map #(% (march-equinox year)))
(apply make-utc-date)))
(defn- go-back [adjustment date] (t/adjust date t/minus adjustment))
(defn- go-forward [adjustment date] (t/adjust date t/plus adjustment))
(defn- tz? [date] (t/zone-id date))
(defn- calculate-sun-events
[lat lon date]
(let [tz (tz? date)]
(-> (SunTimes/compute)
(.on date)
(.at lat lon)
(.oneDay)
(.timezone (str tz))
(.execute))))
(defn- next-sunset
[lat lon date & {:keys [adjusted], :or {adjusted false}}]
(let [sun (calculate-sun-events lat lon date)
always-up (.isAlwaysUp sun)
always-down (.isAlwaysDown sun)
sunset (.getSet sun)]
(cond
(or always-down always-up)
(cond (< lat -65.7) (next-sunset -65.7 lon date :adjusted true)
(> lat 65.7) (next-sunset 65.7 lon date :adjusted true)
:else (throw
(Exception.
(str "Sun either always up or always down but"
" latitude is: " lat))))
(nil? sunset) ;; This seems to happen when the sunset occurred
;; the minute before `date`.
(next-sunset lat
lon
(go-forward (t/minutes 1) date)
:adjusted
adjusted)
:else {:sunset (t/truncate-to sunset :minutes),
:adjusted-for-polar-region adjusted,
:always-down always-down,
:always-up always-up})))
(defn- next-start-of-day
[lat lon date]
(:sunset (next-sunset lat lon date)))
(defn- previous-start-of-day
[lat lon date]
(->> (next-start-of-day lat lon date)
(go-back (t/hours 25))
(next-start-of-day lat lon)))
(defn- noon
[lat lon date]
(let [tz (tz? date)
year (t/as date :year)
month (t/as date :month-of-year)
day (t/as date :day-of-month)
morning (t/with-zone (t/zoned-date-time year month day 4 0) tz)]
(.getNoon (calculate-sun-events lat lon morning))))
(defn- boundaries-of-day
[lat lon date]
(let [p (previous-start-of-day lat lon date)
n (next-start-of-day lat
lon
(noon lat lon (go-forward (t/hours 16) p)))]
[p (go-back (t/seconds 1) n)]))
(defn- calculate-new-moon
[date]
(let [tz (tz? date)]
(-> (MoonPhase/compute)
(.on date)
(.timezone (str tz))
(.execute))))
(defn- next-new-moon
[date]
(-> (calculate-new-moon date)
(.getTime)
(t/truncate-to :minutes)))
(defn- previous-new-moon
[date]
(->> (next-new-moon date)
(go-back (t/days 31))
(next-new-moon)))
(defn zone-it
"Given a string containing a valid timezone name `tz`, and a
java.time.ZonedDateTime object `date`, convert `date` to the same instant in
`tz`."
[tz date]
(t/with-zone-same-instant date tz))
(defn- next-start-of-month-in-israel
[date]
(let [zdate (zone-it jerusalem-tz date)
lat jerusalem-lat
lon jerusalem-lon
previous-moon (previous-new-moon zdate)
day-following-previous-moon (next-start-of-day lat lon previous-moon)
next-day (next-start-of-day lat lon zdate)]
(if (zero? (t/as (t/duration day-following-previous-moon next-day)
:seconds))
next-day
(next-start-of-day lat lon (next-new-moon zdate)))))
(defn- previous-start-of-month-in-israel
[date]
(->> (next-start-of-month-in-israel date)
(go-back (t/days 32))
(next-start-of-month-in-israel)))
(defn- year-month-day [date] (t/as date :year :month-of-year :day-of-month))
(defn- make-zoned-date
[tz & args]
(t/with-zone (apply t/zoned-date-time args) tz))
(defn- next-start-of-month
[lat lon date]
(let [tz (tz? date)
prev-month-israel (previous-start-of-month-in-israel date)
next-month-israel (next-start-of-month-in-israel date)
next-day (next-start-of-day lat lon date)]
(if (= (year-month-day prev-month-israel) (year-month-day next-day))
next-day
(as-> (year-month-day next-month-israel) <>
(make-zoned-date tz (first <>) (second <>) (last <>))
(noon lat lon <>)
(next-start-of-day lat lon <>)))))
(defn- previous-start-of-month
[lat lon date]
(->> (next-start-of-month lat lon date)
(go-back (t/days 32))
(next-start-of-month lat lon)))
(defn- boundaries-of-month
[lat lon date]
(let [p (previous-start-of-month lat lon date)
n (next-start-of-month lat lon (go-forward (t/days 2) p))]
[p (go-back (t/seconds 1) n)]))
(defn- next-march-equinox
[date]
(let [tz (tz? date)
year (Integer/parseInt (str (t/year date)))
same-year-march-equinox (date-of-march-equinox year)]
(if (t/before? date same-year-march-equinox)
(zone-it tz same-year-march-equinox)
(zone-it tz (date-of-march-equinox (inc year))))))
(defn- previous-march-equinox
[date]
(let [tz (tz? date)
year (Integer/parseInt (str (t/year date)))
same-year-march-equinox (date-of-march-equinox year)]
(if (t/before? same-year-march-equinox date)
(zone-it tz same-year-march-equinox)
(zone-it tz (date-of-march-equinox (dec year))))))
(defn- next-start-of-year-in-israel
[date]
(let [zdate (zone-it jerusalem-tz date)
this-year-march-equinox (date-of-march-equinox (t/as zdate :year))
previous-month (previous-start-of-month-in-israel zdate)
pme (previous-march-equinox zdate)
moon-following-previous-equinox (next-new-moon pme)
day-following-moon (next-start-of-day jerusalem-lat
jerusalem-lon
moon-following-previous-equinox)
next-day (next-start-of-day jerusalem-lat jerusalem-lon zdate)
potential-new-year (next-start-of-month-in-israel (next-march-equinox
zdate))]
(cond (zero? (t/as (t/duration day-following-moon next-day) :seconds))
next-day
(t/before? previous-month this-year-march-equinox zdate)
(next-start-of-month-in-israel zdate)
:else potential-new-year)))
(defn- previous-start-of-year-in-israel
[date]
(let [y (t/as date :year)
m (t/as date :month-of-year)]
(cond (> m 4) (next-start-of-year-in-israel
(make-zoned-date jerusalem-tz y 2 1))
(< m 3) (next-start-of-year-in-israel
(make-zoned-date jerusalem-tz (dec y) 2 1))
:else (->> (next-start-of-year-in-israel date)
(go-back (t/days 400))
(next-start-of-year-in-israel)))))
(defn- next-start-of-year
[lat lon date]
(let [tz (tz? date)
prev-year-israel (previous-start-of-year-in-israel date)
next-year-israel (next-start-of-year-in-israel date)
next-day (next-start-of-day lat lon date)]
(if (= (year-month-day prev-year-israel) (year-month-day next-day))
next-day
(as-> (year-month-day next-year-israel) <>
(make-zoned-date tz (first <>) (second <>) (last <>))
(noon lat lon <>)
(next-start-of-day lat lon <>)))))
(defn- previous-start-of-year
[lat lon date]
(let [y (t/as date :year)
m (t/as date :month-of-year)
tz (tz? date)]
(cond (> m 4) (next-start-of-year lat lon (make-zoned-date tz y 2 1))
(< m 3) (next-start-of-year lat lon (make-zoned-date tz (dec y) 2 1))
:else (->> (next-start-of-year lat lon date)
(go-back (t/days 400))
(next-start-of-year lat lon)))))
(defn- boundaries-of-year
[lat lon date]
(let [p (previous-start-of-year lat lon date)
n (next-start-of-year lat lon (go-forward (t/days 1) p))]
[p (go-back (t/seconds 1) n)]))
(defn- next-start-of-week
[lat lon date]
(->> (range 0 8)
(map #(->> (next-start-of-day lat lon date)
(go-forward (t/days %))
(go-back (t/hours 8))
(noon lat lon)
(next-start-of-day lat lon)))
(dedupe)
;; Since the day sometimes starts after local midnight, due to DST
(filter #(or (and (t/saturday? %) (>= (t/as % :hour-of-day) 12))
(and (t/sunday? %) (< (t/as % :hour-of-day) 12))))
(first)))
(defn- previous-start-of-week
[lat lon date]
(->> (next-start-of-week lat lon date)
(go-back (t/days 10))
(next-start-of-week lat lon)))
(defn- boundaries-of-week
[lat lon date]
(let [p (previous-start-of-week lat lon date)
n (next-start-of-week lat lon (go-forward (t/days 1) p))]
[p (go-back (t/seconds 1) n)]))
(defn- start-of-days-in-week
[lat lon date]
(let [w (boundaries-of-week lat lon date)]
(->> (range 1 8)
(map #(->> (first w)
(go-forward (t/days %))
(go-back (t/hours 8))
(noon lat lon)
(next-start-of-day lat lon)))
(dedupe)
(filter #(t/before? % (second w)))
(cons (first w)))))
(defn- day-of-week
[lat lon date]
(let [p (previous-start-of-day lat lon date)
pj (t/as p :julian-day)]
(->> (start-of-days-in-week lat lon date)
(map #(t/as % :julian-day))
(apply sorted-set)
(keep-indexed #(when (= %2 pj) %1))
(first)
(inc))))
(defn- new-moons-since-start-of-year
[lat lon date]
(let [y (boundaries-of-year lat lon date)
start-of-year (first y)
end-of-year (second y)]
(->> (range 0 14)
(map #(next-new-moon (go-forward (t/days (* % 29)) start-of-year)))
(dedupe)
(filter #(t/before? % end-of-year))
(drop-last))))
(defn- start-of-months-in-year
[lat lon date]
(->> (new-moons-since-start-of-year lat lon date)
(map #(next-start-of-month lat lon (go-back (t/hours 1) %)))
(cons (previous-start-of-year lat lon date))))
(defn- month
[lat lon date]
(let [p (t/as (previous-start-of-month lat lon date) :julian-day)]
(->> (start-of-months-in-year lat lon date)
(map #(t/as % :julian-day))
(apply sorted-set)
(keep-indexed #(when (= %2 p) %1))
(first)
(inc))))
(defn- start-of-days-in-month
[lat lon date]
(let [m (boundaries-of-month lat lon date)
start-of-month (first m)
end-of-month (second m)]
(->> (range 1 32)
(map #(->> start-of-month
(go-forward (t/days %))
(go-back (t/hours 2))
(noon lat lon)
(next-start-of-day lat lon)))
(dedupe)
(filter #(t/before? % end-of-month))
(cons start-of-month))))
(defn- day-of-month
[lat lon date]
(let [p (previous-start-of-day lat lon date)
pj (t/as p :julian-day)
m (start-of-days-in-month lat lon date)]
(->> (map #(t/as % :julian-day) m)
(apply sorted-set)
(keep-indexed #(when (= %2 pj) %1))
(first)
(inc))))
(def traditional-month-names
["Nisan" "Iyar" "Sivan" "Tammuz" "Av" "Elul" "Tishrei" "Marcheshvan" "Kislev"
"Tevet" "Shevat" "Adar" "Adar II"])
(def month-numbers
(flatten ["1st" "2nd" "3rd" (map #(str % "th") (range 4 14))]))
(def day-numbers
(flatten ["1st" "2nd" "3rd" (map #(str % "th") (range 4 21)) "21st" "22nd"
"23rd" (map #(str % "th") (range 24 31))]))
(def weekday-names
(flatten [(map #(str % " day of the week")
["1st" "2nd" "3rd" "4th" "5th" "6th"]) "Sabbath"]))
(defn- single-day-feast
[m]
(assoc m
:day-of-feast 1
:days-in-feast 1))
(defn- rosh-chodesh
[trad-name]
(single-day-feast {:name (str "First day of " trad-name,)
:hebrew-name (str "Rosh Chodesh " trad-name)}))
(def pesach (single-day-feast {:name "Passover", :hebrew-name "Pesach"}))
(defn- ha-matzot
[day-of-feast]
{:name "Feast of Unleavened Bread",
:hebrew-name "Chag Ha-Matzot",
:day-of-feast day-of-feast,
:days-in-feast 7})
(defn- yom-bikkurim
[day-of-month]
(single-day-feast {:name "Feast of First Fruits",
:hebrew-name "Yom Bikkurim"}))
(def shavuot
(single-day-feast {:name "Feast of Weeks",
:alternative-name "Feast of Pentecost",
:hebrew-name "Shavuot"}))
(defn- ha-sukkot
[day-of-feast]
{:name "Feast of Tabernacles",
:alternative-name "Feast of Booths",
:hebrew-name "Chag Ha-Sukkot",
:day-of-feast day-of-feast,
:days-in-feast 7})
(def yom-teruah
(single-day-feast {:name "Feast of Trumpets", :hebrew-name "Yom Teruah"}))
(def yom-ha-kippurim
(single-day-feast {:name "Day of Atonement",
:hebrew-name "Yom Ha-Kippurim",
:alternative-hebrew-name "Yom Kippur"}))
(def shemini-atzeret
(single-day-feast {:name "The Last Great Day",
:hebrew-name "Shemini Atzeret"}))
(defn- chanukah
[day-of-month day-of-feast]
{:name "Hanukkah",
:hebrew-name "Chanukah",
:day-of-feast day-of-feast,
:days-in-feast 8})
(def purim
{:name "Purim", :hebrew-name "Purim", :day-of-feast 1, :days-in-feast 2})
(def shushan-purim
{:name "Shushan Purim",
:hebrew-name "Shushan Purim",
:day-of-feast 2,
:days-in-feast 2})
(defn- days-between
"Given a `start` and an `end`, calculate the duration between the events in
days."
[start end]
(t/as (t/duration start end) :days))
(defn- minor-feast-day?
"Given `m` (hebrew month of year), `d` (hebrew day of month), return a map
with details of any minor feast day on that day, or
return false if there are none."
[m d]
(cond
(= d 1) (rosh-chodesh (nth traditional-month-names (dec m)))
:else false))
(defn- major-feast-day?
"Given `m` (hebrew month of year), `d` (hebrew day of month), and `dow`
(hebrew day of week), return a map with details of any major feast day on that
day, or return false if there are none."
[m d dow start-of-year start-of-month]
(let [days-in-first-month
(when (and (= m 3) (<= 5 d 12))
(as-> (go-forward (t/days 2) start-of-year) <>
(zone-it "Asia/Jerusalem" <>)
(boundaries-of-month jerusalem-lat jerusalem-lon <>)
(days-between (first <>) (second <>))))
days-in-prev-month
(when (or (and (= m 3) (<= 5 d 12)) (and (= m 10) (< 0 d 4)))
(as-> (go-back (t/days 2) start-of-month) <>
(zone-it "Asia/Jerusalem" <>)
(boundaries-of-month jerusalem-lat jerusalem-lon <>)
(days-between (first <>) (second <>))))
two-first-months (when (and days-in-first-month days-in-prev-month)
(+ days-in-first-month days-in-prev-month))]
(cond
(and (= m 1) (= d 14)) pesach
(and (= m 1) (<= 15 d 21) (= dow 1)) (yom-bikkurim d)
(and (= m 1) (= d 15)) (ha-matzot 1)
(and (= m 1) (= d 16)) (ha-matzot 2)
(and (= m 1) (= d 17)) (ha-matzot 3)
(and (= m 1) (= d 18)) (ha-matzot 4)
(and (= m 1) (= d 19)) (ha-matzot 5)
(and (= m 1) (= d 20)) (ha-matzot 6)
(and (= m 1) (= d 21)) (ha-matzot 7)
(and (= two-first-months 58) (= m 3) (<= 6 d 12) (= dow 1)) shavuot
(and (= two-first-months 59) (= m 3) (<= 5 d 11) (= dow 1)) shavuot
(and (= two-first-months 60) (= m 3) (<= 4 d 10) (= dow 1)) shavuot
(and (= m 7) (= d 1)) yom-teruah
(and (= m 7) (= d 10)) yom-ha-kippurim
(and (= m 7) (= d 15)) (ha-sukkot 1)
(and (= m 7) (= d 16)) (ha-sukkot 2)
(and (= m 7) (= d 17)) (ha-sukkot 3)
(and (= m 7) (= d 18)) (ha-sukkot 4)
(and (= m 7) (= d 19)) (ha-sukkot 5)
(and (= m 7) (= d 20)) (ha-sukkot 6)
(and (= m 7) (= d 21)) (ha-sukkot 7)
(and (= m 7) (= d 22)) shemini-atzeret
(and (= m 9) (= d 25)) (chanukah d 1)
(and (= m 9) (= d 26)) (chanukah d 2)
(and (= m 9) (= d 27)) (chanukah d 3)
(and (= m 9) (= d 28)) (chanukah d 4)
(and (= m 9) (= d 29)) (chanukah d 5)
(and (= m 9) (= d 30)) (chanukah d 6)
(and (= m 10) (= d 1) (= days-in-prev-month 29)) (chanukah d 6)
(and (= m 10) (= d 1) (= days-in-prev-month 30)) (chanukah d 7)
(and (= m 10) (= d 2) (= days-in-prev-month 29)) (chanukah d 7)
(and (= m 10) (= d 2) (= days-in-prev-month 30)) (chanukah d 8)
(and (= m 10) (= d 3) (= days-in-prev-month 29)) (chanukah d 8)
(and (= m 12) (= d 14)) purim
(and (= m 12) (= d 15)) shushan-purim
:else false)))
(defn- sabbath?
"Given the hebrew `month-of-year`, `day-of-month`, `day-of-week`, and
`feast-days`, return `true` if it's a Sabbath or `false` if it's not."
[month-of-year day-of-month day-of-week major-feast-day]
(or (= 7 day-of-week)
(and (= month-of-year 1) (= day-of-month 15)) ; "First day of the Feast of
; Unleavened Bread"
(and (= month-of-year 1) (= day-of-month 21)) ; "Last day of the Feast of
; Unleavened Bread"
(and (= month-of-year 7) (= day-of-month 1)) ; "Feast of Trumpets"
(and (= month-of-year 7) (= day-of-month 10)) ; "Day of Atonement"
(and (= month-of-year 7) (= day-of-month 15)) ; "First day of the Feast of
; Tabernacles"
(and (= month-of-year 7) (= day-of-month 22)) ; "The Last Great Day"
(and major-feast-day (= "Feast of First Fruits" (:name major-feast-day)))
(and major-feast-day (= "Feast of Weeks" (:name major-feast-day)))))
(defn- polar-adjusted?
[lat lon date]
(if (<= -65 lat 65)
false
(->> (go-back (t/hours 2) date)
(next-sunset lat lon)
(:adjusted-for-polar-region))))
(defn- assoc-polar-status
[lat lon m]
(assoc m
:start-adjusted-for-polar-region (polar-adjusted? lat lon (:start m))
:end-adjusted-for-polar-region (polar-adjusted? lat lon (:end m))))
(defn- with-polar-status
[lat lon m]
(into (empty m) (for [[k v] m] [k (assoc-polar-status lat lon v)])))
(defn now "Return the current time." [] (t/zoned-date-time))
(defn hebrew-date
"Return a map containing the details of a `hebrew-date` where:
`lat` is the latitude of the location,
`lon` is the longitude of the location, and
`date` is a java.time.ZonedDateTime object from which the beginning of the
hebrew year will be calculated. I.e, it will use the 'current' year of the
`date` as the base for calculating `m` and `d`.
If only `date` is provided `lat` and `lon` will default to the coordinates of
the Temple Mount in Jerusalem.
In addition, if only `lat`, and `lon` is provided `date` will default to the
current time in the \"Asia/Jerusalem\" timezone.
Example:
(hebrew-date 59.3325800 18.0649000 (zone-it \"Europe/Stockholm\" (now)))
The above will look for the current hebrew date. The coordinates are those of
Stockholm, Sweden. Using the `zone-it` function to make sure that `now` is
returned in the correct timezone is recommended, in this case
\"Europe/Stockholm\".
Caution: Make sure that `date` is using the actual timezone of the location at
the provided coordinates. Otherwise the results may not even be produced, or
they will be inaccurate. Calculating the timezone of a given location is out
of scope of this library.
See also `zone-it`, and `now`."
([lat lon date]
{:pre [(and (number? lat) (<= -90 lat 90))
(and (number? lon) (<= -180 lon 180))
(and (or (t/zoned-date-time? date)
(t/local-date-time? date)
(t/offset-date-time date))
(<= 1584 (t/as date :year) 2200))]}
(let [day-boundaries (boundaries-of-day lat lon date)
week-boundaries (boundaries-of-week lat lon date)
month-boundaries (boundaries-of-month lat lon date)
year-boundaries (boundaries-of-year lat lon date)
start-of-year (first year-boundaries)
end-of-year (last year-boundaries)
start-of-month (first month-boundaries)
end-of-month (last month-boundaries)
start-of-week (first week-boundaries)
end-of-week (last week-boundaries)
start-of-day (first day-boundaries)
end-of-day (last day-boundaries)
days-in-month (days-between start-of-month end-of-month),
days-in-year (days-between start-of-year end-of-year)
months-in-year (if (<= 383 days-in-year 384) 13 12)
month (month lat lon date)
dom (day-of-month lat lon date)
dow (day-of-week lat lon date)
names {:month-of-year (nth month-numbers (dec month)),
:traditional-month-of-year
(if (and (= month 12) (= months-in-year 13))
"Adar I"
(nth traditional-month-names (dec month))),
:day-of-month (nth day-numbers (dec dom)),
:day-of-week (nth weekday-names (dec dow))}
minor-feast-day (minor-feast-day? month dom)
major-feast-day (major-feast-day? month dom dow start-of-year
start-of-month)
sabbath (sabbath? month dom dow major-feast-day)]
{:hebrew {:month-of-year month,
:months-in-year months-in-year,
:day-of-month dom,
:days-in-month days-in-month,
:day-of-week dow,
:sabbath sabbath,
:minor-feast-day minor-feast-day,
:major-feast-day major-feast-day,
:names names},
:time (with-polar-status lat
lon
{:year {:start start-of-year, :end end-of-year},
:month {:start start-of-month,
:end end-of-month},
:week {:start start-of-week, :end end-of-week},
:day {:start start-of-day, :end end-of-day}})}))
([lat lon]
{:pre [(and (number? lat) (<= -90 lat 90))
(and (number? lon) (<= -180 lon 180))]}
(hebrew-date lat lon (now)))
([date]
{:pre [(and (or (t/zoned-date-time? date)
(t/local-date-time? date)
(t/offset-date-time date))
(<= 1584 (t/as date :year) 2100))]}
(hebrew-date jerusalem-lat jerusalem-lon date))
([] (hebrew-date (zone-it jerusalem-tz (now)))))
(defn find-date
"Return a map containing the details of a `hebrew-date` where:
`lat` is the latitude of the location,
`lon` is the longitude of the location,
`m` is the hebrew month of year,
`d` is the hebrew day of month that you are looking for, and
`date` is a java.time.ZonedDateTime object from which the beginning of the
hebrew year will be calculated. I.e, it will use the 'current' year of the
`date` as the base for calculating `m` and `d`.
If only `m`, `d`, and `date` are provided `lat` and `lon` will default to
the coordinates of the Temple Mount in Jerusalem.
In addition, if only `m`, and `d` is provided `date` will default to the
current time in the \"Asia/Jerusalem\" timezone.
Example:
(find-date 59.3325800 18.0649000 1 14 (zone-it \"Europe/Stockholm\" (now)))
The above will look for the 14th day of the 1st month in the hebrew year that
starts in the current gregorian year (based on the system time and timezone.
The coordinates are those of Stockholm, Sweden. Using the `zone-it` function
to make sure that `now` is returned in the correct timezone is recommended,
in this case \"Europe/Stockholm\".
Caution: Make sure that `date` is using the actual timezone of the location at
the provided coordinates. Otherwise the results may not even be produced, or
they will be inaccurate. Calculating the timezone of a given location is out
of scope of this library.
See also `hebrew-date`, `zone-it`, and `now`."
([lat lon m d date]
{:pre [(and (pos-int? m) (< 0 m 14)) (and (pos-int? d) (< 0 d 31))]}
(let [months (start-of-months-in-year lat lon date)
start-of-month (try (nth months (dec m))
(catch IndexOutOfBoundsException _e nil))
days (when start-of-month
(->> start-of-month
(go-forward (t/hours 1))
(start-of-days-in-month lat lon)))]
(when days
(try (->> (dec d)
(nth days)
(go-forward (t/hours 1))
(hebrew-date lat lon))
(catch IndexOutOfBoundsException _e nil)))))
([m d date] (find-date jerusalem-lat jerusalem-lon m d date))
([m d] (find-date m d (zone-it jerusalem-tz (now)))))
(defn find-date-in-year
"Return a map containing the details of a `hebrew-date` where:
`lat` is the latitude of the location
`lon` is the longitude of the location
`tz` is a string containing a valid TimeZone description
`y` is the gregorian year in which the hebrew year in question starts
`m` is the hebrew month of year, and
`d` is the hebrew day of month that you are looking for.
If only `tz`, `y`, `m`, and `d` are provided `lat` and `lon` will default to
the coordinates of the Temple Mount in Jerusalem.
In addition, if only `y`, `m`, and `d` is provided `tz` will default to
\"Asia/Jerusalem\".
Example:
(find-date-in-year 59.3325800 18.0649000 \"Europe/Stockholm\" 2025 1 14)
The above will look for the 14th day of the 1st month of the hebrew year that
starts in the gregorian year 2025. The timezone in use will be
\"Europe/Stockholm\" and the coordinates are those of Stockholm, Sweden.
Caution: Make sure that `tz` is the actual timezone of the location at the
provided coordinates. Otherwise the results may not even be produced, or they
will be inaccurate. Calculating the timezone of a given location is out of
scope of this library.
See also `find-date`."
([lat lon tz y m d]
{:pre [(string? tz) (and (pos-int? y) (<= 1584 y 2200))
(and (pos-int? m) (< 0 m 14)) (and (pos-int? d) (< 0 d 31))]}
(find-date lat lon m d (make-zoned-date tz y 6 1 12)))
([tz y m d] (find-date-in-year jerusalem-lat jerusalem-lon tz y m d))
([y m d] (find-date-in-year jerusalem-tz y m d)))
;; The following functions were used to calculate the feast days for
;; `calculated-feast-days`. They're terribly slow and shouldn't be used
;; for any other purpose. I'm leaving them here for future reference and if I
;; have to re-generate the dates due to a change in the calculation method or
;; future bug fix.
(def potential-feast-dates
(->> (concat (map #(vector 1 %) (range 14 22))
(map #(vector 3 %) (range 5 13))
[[7 1] [7 10]]
(map #(vector 7 %) (range 15 23))
(map #(vector 9 %) (range 25 31))
(map #(vector 10 %) (range 1 4))
[[12 14] [12 15]]
(map #(vector % 1) (range 1 14)))
(sort)
(dedupe)))
(defn- calculate-feast-days-in-gregorian-year
[year]
(->> (for [y [(dec year) year]
p potential-feast-dates]
(find-date-in-year y (first p) (last p)))
(filter #(or (get-in % [:hebrew :minor-feast-day])
(get-in % [:hebrew :major-feast-day])))
(pmap #(vector (t/as (get-in % [:time :day :start])
:year
:month-of-year
:day-of-month)
(get-in % [:hebrew :minor-feast-day])
(get-in % [:hebrew :major-feast-day])))
(map #(remove false? %))
(filter #(= (ffirst %) year))
(sort-by #(second (first %)))))
(defn- map-of-feast-days-in-gregorian-year
[year]
(let [dates (calculate-feast-days-in-gregorian-year year)]
(as-> (for [d dates]
{(second (first d)) {(last (first d)) (into [] (rest d))}}) <>
(apply merge-with into <>)
{year <>})))
(defn- feast-days-in-range-of-gregorian-years
[start end]
(->> (range start (inc end))
(map map-of-feast-days-in-gregorian-year)
(apply merge)
(into (sorted-map))))
(def feast-day-numbers
["First" "Second" "Third" "Fourth" "Fifth" "Sixth" "Last"])
(defn- long-feast-day-name
[y m d n day-of-feast days-in-feast]
(str y "-" (format "%02d" m) "-" (format "%02d" d) " "
(if (< days-in-feast 3)
n
(if (= days-in-feast 8)
(cond (= 7 day-of-feast) (str "Seventh day of " n)
(= 8 day-of-feast) (str "Last day of " n)
:else
(str (nth feast-day-numbers (dec day-of-feast))
" day of " n))
(str (nth feast-day-numbers (dec day-of-feast))
" day of the "
n)))))
(defn list-of-known-feast-days-in-gregorian-year
"Given a gregorian `year` between 1584 and 2100, return a list of strings
describing the feast days in that year. The dates represent the gregorian day
on which the sunset would begin the feast day in question. Some days will have
more than one feast day."
[year]
{:pre [(<= 1584 year 2100)]}
(let [c (if (get calculated-feast-days year)
calculated-feast-days
(map-of-feast-days-in-gregorian-year year))
y (get c year)]
(sort (for [m (keys y)
d (keys (get y m))
f (get-in y [m d])]
(let [n (:name f)
day-of-feast (:day-of-feast f)
days-in-feast (:days-in-feast f)]
(long-feast-day-name year m d n day-of-feast days-in-feast))))))