-
Notifications
You must be signed in to change notification settings - Fork 3
/
time.gleam
65 lines (54 loc) · 1.2 KB
/
time.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
import gleam/int
import birl/time
import birl/duration
pub fn in_interval(from: time.DateTime, to: time.DateTime) -> time.DateTime {
let unix_from = time.to_unix(from)
let unix_to = time.to_unix(to)
int.random(unix_from, unix_to)
|> time.from_unix()
}
pub fn symmetric(distance: duration.Duration) {
let now = time.now()
let unix_from =
now
|> time.subtract(distance)
|> time.to_unix
let unix_to =
now
|> time.add(distance)
|> time.to_unix
int.random(unix_from, unix_to)
|> time.from_unix()
}
pub fn past(
min_distance: duration.Duration,
max_distance: duration.Duration,
) -> time.DateTime {
let now = time.now()
let unix_from =
now
|> time.subtract(max_distance)
|> time.to_unix
let unix_to =
now
|> time.subtract(min_distance)
|> time.to_unix
int.random(unix_from, unix_to)
|> time.from_unix()
}
pub fn future(
min_distance: duration.Duration,
max_distance: duration.Duration,
) -> time.DateTime {
let now = time.now()
let unix_from =
now
|> time.add(min_distance)
|> time.to_unix
let unix_to =
now
|> time.add(max_distance)
|> time.to_unix
int.random(unix_from, unix_to)
|> time.from_unix()
}