-
Notifications
You must be signed in to change notification settings - Fork 17
/
duration.clj
114 lines (80 loc) · 3.19 KB
/
duration.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
(ns finagle-clojure.duration
"Functions for creating com.twitter.util.Duration and com.twitter.util.Time.
Duration represents a length of time.
It can be use to express timeouts.
Functions such as [[finagle-clojure.futures/within*]] accept an explicit Duration object, while others
such as [[finagle-clojure.futures/within]] create one for you."
(:import [com.twitter.util Duration Time]
[java.util.concurrent TimeUnit]))
;; TODO is it confusing for Time & Duration to be in the same ns?
(def ^:no-doc ->Duration-units {:s TimeUnit/SECONDS
TimeUnit/SECONDS TimeUnit/SECONDS
:ms TimeUnit/MILLISECONDS
TimeUnit/MILLISECONDS TimeUnit/MILLISECONDS
:us TimeUnit/MICROSECONDS
TimeUnit/MICROSECONDS TimeUnit/MICROSECONDS
:ns TimeUnit/NANOSECONDS
TimeUnit/NANOSECONDS TimeUnit/NANOSECONDS})
(defn ^Duration ->Duration
"Create a new Duration.
*Arguments*:
* `value`: the value of this Duration.
* `unit`: seconds or milliseconds, represented as `:s`, `:ms`, `:us`, `:ns`, or the corresponding `java.util.concurrent.TimeUnit`.
*Returns*:
A `com.twitter.util.Duration`."
[value unit]
(if-let [time-unit (get ->Duration-units unit)]
(Duration/fromTimeUnit value time-unit)
(throw (IllegalArgumentException. (str "Unit " unit " not found in " (keys ->Duration-units))))))
(defn ^Time ns->Time
"Create a new Time from `value` nanoseconds.
*Arguments*:
* `value`: how many nanoseconds since epoch.
*Returns*:
A `com.twitter.util.Time`."
[nanoseconds]
(Time/fromNanoseconds nanoseconds))
(defn ^Time us->Time
"Create a new Time from `value` microseconds.
*Arguments*:
* `value`: how many microseconds since epoch.
*Returns*:
A Time."
[microseconds]
(Time/fromMicroseconds microseconds))
(defn ^Time ms->Time
"Create a new Time from `value` milliseconds.
*Arguments*:
* `value`: how many milliseconds since epoch.
*Returns*:
A Time."
[milliseconds]
(Time/fromMilliseconds milliseconds))
(defn ^Time s->Time
"Create a new Time from `value` seconds.
*Arguments*:
* `value`: how many seconds since epoch.
*Returns*:
A Time."
[seconds]
(Time/fromSeconds seconds))
(def ^:no-doc ->Time-units {:us us->Time
TimeUnit/MICROSECONDS us->Time
:ns ns->Time
TimeUnit/NANOSECONDS ns->Time
:ms ms->Time
TimeUnit/MILLISECONDS ms->Time
:s s->Time
TimeUnit/SECONDS s->Time})
(defn ^Time ->Time
"Create a new Time.
*Arguments*:
* `value`: the value of this Time.
* `unit`: seconds, milliseconds, or nanoseconds, represented as `:s`, `:ms`, `:us`, `:ns`,
or the corresponding `java.util.concurrent.TimeUnit`.
*Returns*:
A Time."
[value unit]
(if-let [f (get ->Time-units unit)]
(f value)
(throw (IllegalArgumentException. (str "Unit " unit " not found in " (keys ->Time-units))))))