-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal.clj
90 lines (80 loc) · 2.74 KB
/
internal.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
(ns jedi-time.internal
(:require [jedi-time.units :as units])
(:import (java.time.format DateTimeFormatter)
(java.time.temporal Temporal TemporalUnit TemporalAccessor JulianFields TemporalAmount)
(java.time LocalDateTime LocalDate LocalTime YearMonth Period Duration)))
(defmacro now-variant
[klass clock zone]
`(cond
(some? ~clock) (. ~klass now ~clock)
(some? ~zone) (. ~klass now ~zone)
:else (. ~klass now)) ;; e.g. YearMonth/now
)
(defmacro parser-variant
[klass]
`(fn* parse*
([^CharSequence ~'input]
(. ~klass parse ~'input))
([^CharSequence ~'input ^DateTimeFormatter ~'formatter]
(. ~klass parse ~'input ~'formatter))))
(defn- period-or-duration
"Returns the right concrete instance of TemporalAmount
(Period vs Duration), depending on <mode> (:time vs :date)."
^TemporalAmount [mode n unit tu]
(case mode
:time (Duration/of n tu)
:date (case unit
:half-days (Period/ofDays (int (/ n 2)))
:days (Period/ofDays n)
:weeks (Period/ofWeeks n)
:months (Period/ofMonths n)
:years (Period/ofYears n)
:decades (Period/ofYears (* n 10))
:centuries (Period/ofYears (* n 100))
:millenia (Period/ofYears (* n 1000))
(Duration/of n tu))))
(defn safe-plus
^Temporal [mode ^Temporal t unit ^long n]
(when-let [^TemporalUnit u (units/chrono-units unit)]
(when (.isSupported t u)
(.plus t (period-or-duration mode n unit u)))))
(defn safe-minus
^Temporal [mode ^Temporal t unit ^long n]
(when-let [^TemporalUnit u (units/chrono-units unit)]
(when (.isSupported t u)
(.minus t (period-or-duration mode n unit u)))))
(defn julian-field
[^TemporalAccessor ta x]
(case x
:day (.get ta JulianFields/JULIAN_DAY)
:modified-day (.get ta JulianFields/MODIFIED_JULIAN_DAY)
:rata-die (.get ta JulianFields/RATA_DIE)
nil))
(defn local-datetime-of
^LocalDateTime [x]
(LocalDateTime/of
^int (get-in x [:year :value])
^int (get-in x [:year :month :value])
^int (get-in x [:year :month :day])
^int (get-in x [:day :hour])
^int (get-in x [:hour :minute])
^int (get-in x [:minute :second])
^int (get-in x [:second :nano])))
(defn local-date-of
^LocalDate [x]
(LocalDate/of
^int (get-in x [:year :value])
^int (get-in x [:year :month :value])
^int (get-in x [:year :month :day])))
(defn local-time-of
^LocalTime [x]
(LocalTime/of
(get-in x [:day :hour])
(get-in x [:hour :minute])
(get-in x [:minute :second])
(get-in x [:second :nano])))
(defn year-month-of
^YearMonth [x]
(YearMonth/of
^int (get-in x [:year :value])
^int (get-in x [:year :month :value])))