blob: 7c63b7d31d2cf784ae0b9398122b955e7ad89499 (
plain)
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
|
(.module:
[lux #*
[abstract
[equivalence (#+ Equivalence)]
[order (#+ Order)]
[enum (#+ Enum)]
[codec (#+ Codec)]
[monad (#+ Monad do)]]
[control
[io (#+ IO io)]
["." try]
["." exception (#+ exception:)]
["<>" parser
["<t>" text (#+ Parser)]]]
[data
["." maybe]
[number
["i" int]]
["." text ("#\." monoid)]
[collection
["." row]]]
[type
abstract]]
["." // (#+ Time)
["." duration (#+ Duration)]
["." year (#+ Year)]
["." month (#+ Month)]
["." day (#+ Day)]
["." date (#+ Date)]])
(abstract: #export Instant
Int
{#.doc "Instant is defined as milliseconds since the epoch."}
(def: #export from-millis
(-> Int Instant)
(|>> :abstraction))
(def: #export to-millis
(-> Instant Int)
(|>> :representation))
(def: #export (span from to)
(-> Instant Instant Duration)
(duration.from-millis (i.- (:representation from) (:representation to))))
(def: #export (shift duration instant)
(-> Duration Instant Instant)
(:abstraction (i.+ (duration.to-millis duration) (:representation instant))))
(def: #export (relative instant)
(-> Instant Duration)
(|> instant :representation duration.from-millis))
(def: #export (absolute offset)
(-> Duration Instant)
(|> offset duration.to-millis :abstraction))
(structure: #export equivalence
(Equivalence Instant)
(def: (= param subject)
(\ i.equivalence = (:representation param) (:representation subject))))
(structure: #export order
(Order Instant)
(def: &equivalence ..equivalence)
(def: (< param subject)
(\ i.order < (:representation param) (:representation subject))))
(`` (structure: #export enum
(Enum Instant)
(def: &order ..order)
(~~ (template [<name>]
[(def: <name>
(|>> :representation (\ i.enum <name>) :abstraction))]
[succ] [pred]
))))
)
(def: #export epoch
{#.doc "The instant corresponding to 1970-01-01T00:00:00Z"}
Instant
(..from-millis +0))
(def: millis-per-day
(duration.query duration.milli-second duration.day))
(def: (date-time instant)
(-> Instant [Date Duration])
(let [offset (..to-millis instant)
bce? (i.< +0 offset)
[days day-time] (if bce?
(let [[days millis] (i./% ..millis-per-day offset)]
(case millis
+0 [days millis]
_ [(dec days) (i.+ ..millis-per-day millis)]))
(i./% ..millis-per-day offset))]
[(date.from-days days)
(duration.from-millis day-time)]))
(template [<value> <definition>]
[(def: <definition> Text <value>)]
["T" date-suffix]
["Z" time-suffix]
)
(def: (clock-time duration)
(-> Duration Time)
(let [time (if (\ duration.order < duration.empty duration)
(duration.merge duration.day duration)
duration)]
(|> time duration.to-millis .nat //.from-millis try.assume)))
(def: (encode instant)
(-> Instant Text)
(let [[date time] (..date-time instant)
time (..clock-time time)]
($_ text\compose
(\ date.codec encode date) ..date-suffix
(\ //.codec encode time) ..time-suffix)))
(def: parser
(Parser Instant)
(do {! <>.monad}
[days (\ ! map date.days date.parser)
_ (<t>.this ..date-suffix)
time (\ ! map //.to-millis //.parser)
_ (<t>.this ..time-suffix)]
(wrap (|> (if (i.< +0 days)
(|> duration.day
(duration.scale-up (.nat (i.* -1 days)))
duration.inverse)
(duration.scale-up (.nat days) duration.day))
(duration.merge (duration.scale-up time duration.milli-second))
..absolute))))
(structure: #export codec
{#.doc (doc "Based on ISO 8601."
"For example: 2017-01-15T21:14:51.827Z")}
(Codec Text Instant)
(def: encode ..encode)
(def: decode (<t>.run ..parser)))
(def: #export now
(IO Instant)
(io (..from-millis ("lux io current-time"))))
(template [<field> <type> <post-processing>]
[(def: #export (<field> instant)
(-> Instant <type>)
(let [[date time] (..date-time instant)]
(|> <field> <post-processing>)))]
[date Date (|>)]
[time Time ..clock-time]
)
(def: #export (day-of-week instant)
(-> Instant Day)
(let [offset (..relative instant)
days (duration.query duration.day offset)
day-time (duration.frame duration.day offset)
days (if (and (duration.negative? offset)
(not (duration.neutral? day-time)))
(dec days)
days)
## 1970/01/01 was a Thursday
y1970m0d0 +4]
(case (|> y1970m0d0
(i.+ days) (i.% +7)
## This is done to turn negative days into positive days.
(i.+ +7) (i.% +7))
+0 #day.Sunday
+1 #day.Monday
+2 #day.Tuesday
+3 #day.Wednesday
+4 #day.Thursday
+5 #day.Friday
+6 #day.Saturday
_ (undefined))))
|