forked from dm3/clojure.java-time
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamount.clj
More file actions
203 lines (164 loc) · 6.18 KB
/
amount.clj
File metadata and controls
203 lines (164 loc) · 6.18 KB
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
(ns java-time.amount
(:require [clojure.string :as string]
[java-time.core :as jt.c]
[java-time.local :as jt.l]
[java-time.util :as jt.u]
[java-time.properties :as jt.p]
[java-time.convert :as jt.convert]
[java-time.defconversion :refer (conversion! deffactory)])
(:import [java.time Duration Period]
[java.time.temporal ChronoUnit TemporalAmount Temporal TemporalUnit]))
(defn- ^Duration d-plus [^Duration cp, ^TemporalAmount o]
(.plus cp o))
(defn- ^Duration d-minus [^Duration cp, ^TemporalAmount o]
(.minus cp o))
(extend-type Duration
jt.c/Plusable
(seq-plus [d tas]
(reduce d-plus d tas))
jt.c/Minusable
(seq-minus [d tas]
(reduce d-minus d tas))
jt.c/Multipliable
(multiply-by [d v]
(.multipliedBy d v))
jt.c/Amount
(zero? [d]
(.isZero d))
(negative? [d]
(.isNegative d))
(negate [d]
(.negated d))
(abs [d]
(.abs d))
jt.c/As
(as* [o k]
(-> (.toNanos o)
(jt.convert/convert-amount :nanos k)
:whole)))
(deffactory duration
"Creates a duration - a temporal entity representing standard days, hours,
minutes, millis, micros and nanos. The duration itself contains only seconds
and nanos as properties.
Given one argument will
* interpret as millis if a number
* try to parse from the standard format if a string
* extract supported units from another `TemporalAmount`
* convert from a Joda Period/Duration
Given two arguments will
* get a duration between two `Temporal`s
* get a duration of a specified unit, e.g. `(duration 100 :seconds)`"
:returns Duration
:implicit-arities [1 2]
([] (Duration/ofMillis 0)))
(defn ^Duration micros
"Duration of a specified number of microseconds."
[micros]
(Duration/ofNanos (Math/multiplyExact (long micros) 1000)))
(doseq [[fn-name method-name] [['standard-days 'days]
['hours 'hours]
['minutes 'minutes]
['millis 'millis]
['seconds 'seconds]
['nanos 'nanos]]]
(let [fn-name (with-meta fn-name {:tag Duration})]
(eval
`(defn ~fn-name
~(str "Duration of a specified number of " method-name ".")
[v#]
(. Duration ~(symbol (str 'of (string/capitalize (str method-name)))) (long v#))))))
(doseq [t ['years 'months 'days 'weeks]]
(let [fn-name (with-meta t {:tag Period})]
(eval
`(defn ~fn-name [v#]
(. Period ~(symbol (str 'of (string/capitalize (str t)))) (int v#))))))
(deffactory period
"Creates a period - a temporal entity consisting of years, months and days.
Given one argument will
* interpret as years if a number
* try to parse from the standard format if a string
* extract supported units from another `TemporalAmount`
* convert from a Joda Period
Given two arguments will
* get a period of a specified unit, e.g. `(period 10 :months)`
* get a period between two temporals by converting them to local dates
* get a period of a specified number of years and months
Given three arguments will create a year/month/day period."
:returns Period
:implicit-arities [1 2 3]
([] (Period/of 0 0 0)))
(jt.u/when-joda-time-loaded
(defn ^Period joda-period->period [^org.joda.time.Period p]
(if-not (zero? (+ (.getMillis p) (.getSeconds p) (.getMinutes p) (.getHours p)))
(throw (ex-info "Cannot convert a Joda Period containing non year/month/days to a Java-Time Period!"
{:period p}))
(Period/of (.getYears p) (.getMonths p) (+ (* 7 (.getWeeks p)) (.getDays p)))))
(defn ^Duration joda-period->duration [^org.joda.time.Period p]
(if-not (zero? (+ (.getMonths p) (.getYears p)))
(throw (ex-info "Cannot convert a Joda Period containing months/years to a Java-Time Duration!"
{:period p}))
(jt.c/plus
(duration (.getMillis p) :millis)
(duration (.getSeconds p) :seconds)
(duration (.getMinutes p) :minutes)
(duration (.getHours p) :hours)
(duration (+ (* 7 (.getWeeks p)) (.getDays p)) :days))))
(conversion! org.joda.time.Duration Duration
(fn [^org.joda.time.Duration d]
(Duration/ofMillis (.getMillis d))))
(conversion! org.joda.time.Period Duration
joda-period->duration)
(conversion! org.joda.time.Duration Period
(fn [^org.joda.time.Duration d]
(Period/ofDays (.getStandardDays ^org.joda.time.Duration d))))
(conversion! org.joda.time.Period Period
joda-period->period))
(conversion! CharSequence Duration
(fn [^CharSequence s]
(Duration/parse s)))
(conversion! CharSequence Period
(fn [^CharSequence s]
(Period/parse s)))
(conversion! Number Duration
(fn [^Number millis]
(Duration/ofMillis millis)))
(conversion! Number Period
#(Period/of (int %1) 0 0))
(conversion! [Number Number] Period
#(Period/of (int %1) (int %2) 0))
(conversion! [Number Number Number] Period
#(Period/of (int %1) (int %2) (int %3)))
(conversion! [Temporal Temporal] Duration
(fn [^Temporal a, ^Temporal b]
(Duration/between a b)))
(conversion! [Temporal Temporal] Period
(fn [^Temporal a, ^Temporal b]
(Period/between a b)))
(conversion! [Number clojure.lang.Keyword] Period
(fn [value k]
(case k
:years (years value)
:months (months value)
:days (days value))))
(conversion! [Number TemporalUnit] Duration
(fn [value ^TemporalUnit unit]
(Duration/of (long value) unit)))
(conversion! clojure.lang.Keyword TemporalUnit
jt.p/get-unit-checked)
(conversion! clojure.lang.Keyword TemporalAmount
jt.p/get-unit-checked)
(extend-type Period
jt.c/As
(as* [o k]
(if (<= (.compareTo ^ChronoUnit (jt.p/unit k) ChronoUnit/WEEKS) 0)
(if (and (zero? (.getYears o))
(zero? (.getMonths o)))
(-> (.getDays o)
(jt.convert/convert-amount :days k)
:whole)
(throw (java.time.DateTimeException. "Period contains years or months")))
(if (zero? (.getDays o))
(-> (.toTotalMonths o)
(jt.convert/convert-amount :months k)
:whole)
(throw (java.time.DateTimeException. "Period contains days"))))))