-
Notifications
You must be signed in to change notification settings - Fork 12
/
util.clj
46 lines (40 loc) · 1.15 KB
/
util.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
(ns steffan-westcott.clj-otel.util
"General utility functions."
(:import (clojure.lang IPersistentVector Keyword)
(java.time Duration Instant)
(java.util.concurrent TimeUnit)))
(defprotocol AsDuration
(duration [d]
"Coerce to a `Duration` instance."))
(extend-protocol AsDuration
Duration
(duration [d]
d)
IPersistentVector
(duration [d]
(let [[amount unit] d]
(Duration/of amount unit))))
(defprotocol AsTimestamp
(timestamp [t]
"Coerce `Instant` to a vector `[amount ^TimeUnit unit]`."))
(extend-protocol AsTimestamp
Instant
(timestamp [t]
(let [seconds-part (.getEpochSecond t)
nanos-part (.getNano t)
nanos (+ (.toNanos TimeUnit/SECONDS seconds-part) nanos-part)]
[nanos TimeUnit/NANOSECONDS]))
IPersistentVector
(timestamp [t]
t))
(defprotocol AsQualifiedName
(qualified-name [x]
"Given a keyword, returns the name qualified with its namespace if it has
one. Given anything other than a keyword, returns argument."))
(extend-protocol AsQualifiedName
Keyword
(qualified-name [x]
(str (symbol x)))
Object
(qualified-name [x]
x))