aboutsummaryrefslogtreecommitdiff
path: root/test/01-native.janet
blob: ac04364eafb9fa00894565e41d2c463cd15ec2a5 (plain) (blame)
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
(use date/native)

(defn platform [& ps] (some (partial = (os/which)) ps))

# you generally start with this
# `time` returns the time in UTC
(def now (time))

# you can convert a `time` object into a `tm` object which represents a date
# using :localtime or :gmtime, where gmtime interprets the time as UTC
(def loc (:localtime now))

# you can modify `tm` objects like an associative array
# this means that put, update, keys, values, etc all work
# let's make another `tm` object, but remove one second from it
(def loc* (:localtime now))
(update loc* :sec dec)

# if the second count was at 0, it would now be at -1, which is out of range
# that's not a problem though, you can actually already perform comparisons
(assert (< loc* loc))

# :localtime, :localtime!, :gmtime, :gmtime!, :mktime, and :mktime!
# all "renormalize" the object
# :mktime returns a `time` object
# :mktime! does that, and normalizes the `tm` object in place by mutating it
(assert (= (:mktime loc) now))
(assert (< (:mktime loc*) now))

# :gmtime(!) is equivalent to (:gmtime (:mktime(!) tm)), ditto for localtime
(def loc* (:localtime! loc*))
(assert (< loc* loc))

# macos has a bug in its libc that makes it misinterpret gmtime outputs
# as localtime under certain conditions, so the gmtime tests are skipped
# TODO: UTC tests
(unless (platform :macos))

# TODO: building a UTC datetime