-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.cljc
76 lines (74 loc) · 3.01 KB
/
core.cljc
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
(ns arco.core
(:require
[arco.utils :as utils]
[tick.alpha.api :as t]))
(defn time-since
[[t t-now] & [config]]
(let [vocabulary (merge-with #(or %2 %1)
utils/default-vocabulary
(:vocabulary config))
intervals (utils/generate-intervals utils/default-intervals
(:intervals config)
(:stop-at-interval config))
inst-now (when t-now
(t/instant t-now))
seconds-from-event (utils/diff-in-seconds (t/instant t)
(or inst-now (t/instant)))
interval (utils/find-interval intervals
seconds-from-event)
time-value (utils/time-value
seconds-from-event
interval)
interval-name (utils/interval-name
vocabulary
interval
time-value)
now? (= :now (first interval))
order (if now?
[:interval]
(:order config [:time :interval :ago]))
ago (when-not now?
{:ago (get vocabulary :ago)})]
(when (or (pos? seconds-from-event)
(zero? seconds-from-event))
(utils/format-output order
(:stringify? config true)
(merge ago
{:time time-value
:interval interval-name})))))
(defn time-to
[[t t-now] & [config]]
(let [vocabulary (merge-with #(or %2 %1)
utils/default-vocabulary
(:vocabulary config))
intervals (utils/generate-intervals utils/default-intervals
(:intervals config)
(:stop-at-interval config))
inst-now (when t-now
(t/instant t-now))
seconds-from-event (utils/diff-in-seconds
(or inst-now (t/instant))
(t/instant t))
interval (utils/find-interval
intervals
seconds-from-event)
time-value (utils/time-value
seconds-from-event
interval)
interval-name (utils/interval-name
vocabulary
interval
time-value)
now? (= :now (first interval))
order (if now?
[:interval]
(:order config [:in :time :interval]))
in (when-not now?
{:in (get vocabulary :in)})]
(when (or (pos? seconds-from-event)
(zero? seconds-from-event))
(utils/format-output order
(:stringify? config true)
(merge in
{:time time-value
:interval interval-name})))))