-
Notifications
You must be signed in to change notification settings - Fork 9
/
duration.gleam
384 lines (328 loc) · 9.93 KB
/
duration.gleam
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import gleam/int
import gleam/bool
import gleam/list
import gleam/order
import gleam/regex
import gleam/string
import gleam/option
import gleam/result
pub type Duration {
Duration(Int)
}
pub type Unit {
MicroSecond
MilliSecond
Second
Minute
Hour
Day
Week
Month
Year
}
pub fn add(a: Duration, b: Duration) -> Duration {
let Duration(a) = a
let Duration(b) = b
Duration(a + b)
}
pub fn subtract(a: Duration, b: Duration) -> Duration {
let Duration(a) = a
let Duration(b) = b
Duration(a - b)
}
pub fn micro_seconds(value: Int) -> Duration {
Duration(value)
}
pub fn milli_seconds(value: Int) -> Duration {
Duration(value * milli_second)
}
pub fn seconds(value: Int) -> Duration {
Duration(value * second)
}
pub fn minutes(value: Int) -> Duration {
Duration(value * minute)
}
pub fn hours(value: Int) -> Duration {
Duration(value * hour)
}
pub fn days(value: Int) -> Duration {
Duration(value * day)
}
pub fn weeks(value: Int) -> Duration {
Duration(value * week)
}
pub fn months(value: Int) -> Duration {
Duration(value * month)
}
pub fn years(value: Int) -> Duration {
Duration(value * year)
}
pub fn compare(a: Duration, b: Duration) -> order.Order {
let Duration(dta) = a
let Duration(dtb) = b
case #(dta == dtb, dta < dtb) {
#(True, _) -> order.Eq
#(_, True) -> order.Lt
#(_, False) -> order.Gt
}
}
/// use this if you need short durations where a year just means 365 days and a month just means 30 days
pub fn new(values: List(#(Int, Unit))) -> Duration {
values
|> list.fold(0, fn(total, current) {
case current {
#(amount, MicroSecond) -> total + amount
#(amount, MilliSecond) -> total + amount * milli_second
#(amount, Second) -> total + amount * second
#(amount, Minute) -> total + amount * minute
#(amount, Hour) -> total + amount * hour
#(amount, Day) -> total + amount * day
#(amount, Week) -> total + amount * week
#(amount, Month) -> total + amount * month
#(amount, Year) -> total + amount * year
}
})
|> Duration
}
/// use this if you need very long durations where small inaccuracies could lead to large errors
pub fn accurate_new(values: List(#(Int, Unit))) -> Duration {
values
|> list.fold(0, fn(total, current) {
case current {
#(amount, MicroSecond) -> total + amount
#(amount, MilliSecond) -> total + amount * milli_second
#(amount, Second) -> total + amount * second
#(amount, Minute) -> total + amount * minute
#(amount, Hour) -> total + amount * hour
#(amount, Day) -> total + amount * day
#(amount, Week) -> total + amount * week
#(amount, Month) -> total + amount * accurate_month
#(amount, Year) -> total + amount * accurate_year
}
})
|> Duration
}
/// use this if you need short durations where a year just means 365 days and a month just means 30 days
pub fn decompose(duration: Duration) -> List(#(Int, Unit)) {
let Duration(value) = duration
let absolute_value = int.absolute_value(value)
let #(years, remaining) = extract(absolute_value, year)
let #(months, remaining) = extract(remaining, month)
let #(weeks, remaining) = extract(remaining, week)
let #(days, remaining) = extract(remaining, day)
let #(hours, remaining) = extract(remaining, hour)
let #(minutes, remaining) = extract(remaining, minute)
let #(seconds, remaining) = extract(remaining, second)
let #(milli_seconds, remaining) = extract(remaining, milli_second)
[
#(years, Year),
#(months, Month),
#(weeks, Week),
#(days, Day),
#(hours, Hour),
#(minutes, Minute),
#(seconds, Second),
#(milli_seconds, MilliSecond),
#(remaining, MicroSecond),
]
|> list.filter(fn(item) { item.0 > 0 })
|> list.map(fn(item) {
case value < 0 {
True -> #(-1 * item.0, item.1)
False -> item
}
})
}
/// use this if you need very long durations where small inaccuracies could lead to large errors
pub fn accurate_decompose(duration: Duration) -> List(#(Int, Unit)) {
let Duration(value) = duration
let absolute_value = int.absolute_value(value)
let #(years, remaining) = extract(absolute_value, accurate_year)
let #(months, remaining) = extract(remaining, accurate_month)
let #(weeks, remaining) = extract(remaining, week)
let #(days, remaining) = extract(remaining, day)
let #(hours, remaining) = extract(remaining, hour)
let #(minutes, remaining) = extract(remaining, minute)
let #(seconds, remaining) = extract(remaining, second)
let #(milli_seconds, remaining) = extract(remaining, milli_second)
[
#(years, Year),
#(months, Month),
#(weeks, Week),
#(days, Day),
#(hours, Hour),
#(minutes, Minute),
#(seconds, Second),
#(milli_seconds, MilliSecond),
#(remaining, MicroSecond),
]
|> list.filter(fn(item) { item.0 > 0 })
|> list.map(fn(item) {
case value < 0 {
True -> #(-1 * item.0, item.1)
False -> item
}
})
}
/// approximates the duration by only the given unit
///
/// if the duration is not an integer multiple of the unit,
/// the remainder will be disgarded if it's less than two thirds of the unit,
/// otherwise a single unit will be added to the multiplier.
///
/// - `blur_to(days(16), Month)` -> `0`
/// - `blur_to(days(20), Month)` -> `1`
pub fn blur_to(duration: Duration, unit: Unit) -> Int {
let assert Ok(unit_value) = list.key_find(unit_values, unit)
let Duration(value) = duration
let #(unit_counts, remaining) = extract(value, unit_value)
case remaining >= unit_value * 2 / 3 {
True -> unit_counts + 1
False -> unit_counts
}
}
/// approximates the duration by a value in a single unit
pub fn blur(duration: Duration) -> #(Int, Unit) {
case decompose(duration) {
[] -> #(0, MicroSecond)
decomposed ->
decomposed
|> list.reverse
|> inner_blur
}
}
const milli_second = 1000
const second = 1_000_000
const minute = 60_000_000
const hour = 3_600_000_000
const day = 86_400_000_000
const week = 604_800_000_000
const month = 2_592_000_000_000
const year = 31_536_000_000_000
const accurate_month = 2_629_746_000_000
const accurate_year = 31_556_952_000_000
const unit_values = [
#(Year, year),
#(Month, month),
#(Week, week),
#(Day, day),
#(Hour, hour),
#(Minute, minute),
#(Second, second),
#(MilliSecond, milli_second),
#(MicroSecond, 1),
]
fn inner_blur(values: List(#(Int, Unit))) -> #(Int, Unit) {
let assert Ok(second) = list.at(values, 0)
let leading = list.at(values, 1)
use <- bool.guard(result.is_error(leading), second)
let assert [leading] = result.values([leading])
let assert Ok(leading_unit) = list.key_find(unit_values, leading.1)
let assert Ok(second_unit) = list.key_find(unit_values, second.1)
let leading = case second.0 * second_unit < { leading_unit * 2 / 3 } {
True -> leading
False -> #(leading.0 + 1, leading.1)
}
case list.drop(values, 2) {
[] -> leading
chopped -> inner_blur([leading, ..chopped])
}
}
const year_units = ["y", "year", "years"]
const month_units = ["mon", "month", "months"]
const week_units = ["w", "week", "weeks"]
const day_units = ["d", "day", "days"]
const hour_units = ["h", "hour", "hours"]
const minute_units = ["m", "min", "minute", "minutes"]
const second_units = ["s", "sec", "secs", "second", "seconds"]
const milli_second_units = [
"ms", "msec", "msecs", "millisecond", "milliseconds", "milli-second",
"milli-seconds", "milli_second", "milli_seconds",
]
const units = [
#(Year, year_units),
#(Month, month_units),
#(Week, week_units),
#(Day, day_units),
#(Hour, hour_units),
#(Minute, minute_units),
#(Second, second_units),
#(MilliSecond, milli_second_units),
]
/// you can use this function to create a new duration using expressions like:
///
/// "accurate: 1 Year - 2days + 152M -1h + 25 years + 25secs"
///
/// where the units are:
///
/// Year: y, Y, YEAR, years, Years, ...
///
/// Month: mon, Month, mONths, ...
///
/// Week: w, W, Week, weeks, ...
///
/// Day: d, D, day, Days, ...
///
/// Hour: h, H, Hour, Hours, ...
///
/// Minute: m, M, Min, minute, Minutes, ...
///
/// Second: s, S, sec, Secs, second, Seconds, ...
///
/// MilliSecond: ms, Msec, mSecs, milliSecond, MilliSecond, ...
///
/// numbers with no unit are considered as microseconds.
/// specifying `accurate:` is equivalent to using `accurate_new`.
pub fn parse(expression: String) -> Result(Duration, Nil) {
let assert Ok(re) = regex.from_string("([+|\\-])?\\s*(\\d+)\\s*(\\w+)?")
let #(constructor, expression) = case
string.starts_with(expression, "accurate:")
{
True -> {
let assert [_, expression] = string.split(expression, ":")
#(accurate_new, expression)
}
False -> #(new, expression)
}
case
expression
|> string.lowercase
|> regex.scan(re, _)
|> list.try_map(fn(item) {
case item {
regex.Match(_, [sign_option, option.Some(amount_string)]) -> {
use amount <- result.then(int.parse(amount_string))
case sign_option {
option.Some("-") -> Ok(#(-1 * amount, MicroSecond))
option.None | option.Some("+") -> Ok(#(amount, MicroSecond))
option.Some(_) -> Error(Nil)
}
}
regex.Match(
_,
[sign_option, option.Some(amount_string), option.Some(unit)],
) -> {
use amount <- result.then(int.parse(amount_string))
use #(unit, _) <- result.then(
list.find(units, fn(item) { list.contains(item.1, unit) }),
)
case sign_option {
option.Some("-") -> Ok(#(-1 * amount, unit))
option.None | option.Some("+") -> Ok(#(amount, unit))
option.Some(_) -> Error(Nil)
}
}
_ -> Error(Nil)
}
})
{
Ok(values) ->
values
|> constructor
|> Ok
Error(Nil) -> Error(Nil)
}
}
fn extract(duration: Int, unit_value: Int) -> #(Int, Int) {
#(duration / unit_value, duration % unit_value)
}