diff options
| author | 2023-05-31 22:50:38 +0200 | |
|---|---|---|
| committer | 2023-05-31 22:50:38 +0200 | |
| commit | cbf8379eda54063902b004ced97b28cf8987fd51 (patch) | |
| tree | f987177954ea6d6175e68a8862ccf8957e785dab | |
| parent | add docs, callables for timegm (diff) | |
integrate timegm into the library, misc spelling fixes, tests
| -rw-r--r-- | date/init.janet | 25 | ||||
| -rw-r--r-- | test/01-native.janet | 22 |
2 files changed, 27 insertions, 20 deletions
diff --git a/date/init.janet b/date/init.janet index c2cfa37..cd47c4f 100644 --- a/date/init.janet +++ b/date/init.janet @@ -6,12 +6,12 @@ (native/time)) (defn time? - `Check if x is a datetime object.` + `Check if x is a date/time object.` [x] (= :date/time (type x))) (defn tm? - `Check if x is a datetm object.` + `Check if x is a date/tm object.` [x] (= :date/tm (type x))) @@ -36,23 +36,23 @@ (defn update-time! ``` - Update a given datetime object as a whole. + Update a given date/time object as a whole. If nil is passed as time, the function will call (date/time) for you. - This version will give you the corresponding localtime datetm object to mutate + This version will give you the corresponding timegm date/tm object to mutate as you see fit. This is dangerous and not perfectly portable. Use at your own peril. Don't forget to return the resulting object, as it will be normalized and - transformed back into a datetime object for you. + transformed back into a date/time object for you. ``` [time fun] (default time (native/time)) (assert (time? time)) (assert (callable? fun)) - (def newtm (fun (:localtime time))) - (:mktime newtm)) # mktime! is slightly more efficient and we're discarding it + (def newtm (fun (:gmtime time))) + (:timegm! newtm)) # timegm! is slightly more efficient and we're discarding it (defn update-time* `Variant of update-time that takes a dictionary.` @@ -64,7 +64,7 @@ (defn update-time ``` - Update a given datetime object. + Update a given date/time object. This takes a value for seconds, minutes, hours, day-of-month, month, and year. @@ -74,14 +74,13 @@ range, as they will be normalized. It will automatically re-determine if DST is applicable and give you back a - modified datetime object. - If you need additional control over the underlying localtime tm call, use + modified date/time object. + If you need additional control over the underlying gmtime tm call, use `update-time*` or `update-time!`. You can pass `nil` as `time`, in which case the current time will be used. - Combined with specifying all the fields allows you to construct datetime - objects in localtime. Constructing UTC datetime objects is non-trivial when - limited to what is provided by ISO C99. + Combined with specifying all the fields allows you to construct date/time + objects in UTC. ``` [time &keys {:sec second :min minute diff --git a/test/01-native.janet b/test/01-native.janet index 972e55e..3e6b0a2 100644 --- a/test/01-native.janet +++ b/test/01-native.janet @@ -15,22 +15,30 @@ # 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)) +(def gmt* (:gmtime now)) (update loc* :sec dec) +(update gmt* :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 # WARN: you can ONLY compare `tm` objects of the same type (localtime vs gmtime) (assert (< loc* loc)) +(assert (< gmt* gmt)) -# :mktime, and :mktime! "renormalize" the object -# :mktime returns a `time` object -# :mktime! does that, and normalizes the `tm` object in place by mutating it +# :mktime[!] and :timegm[!] "renormalize" the object +# :mktime and :timegm return a `time` object +# :mktime! and :timegm! do that, and normalizes the `tm` object in place by mutating it (assert (= (:mktime loc) now)) (assert (< (:mktime loc*) now)) +(assert (= (:timegm gmt) now)) +(assert (< (:timegm gmt*) now)) -# you must not call :mktime or :mktime! on the output of a :gmtime +# you must not call :mktime or :mktime! on the output of a :gmtime and vice versa # the data *will* be wrong, as mktime (as per the spec) presumes :localtime (assert (not= (:mktime gmt) now)) +(assert (not= (:timegm loc) now)) +(assert (= (:mktime loc) now)) +(assert (= (:timegm gmt) now)) # as such, the correct approach is to keep things in `time` format as much as # possible, only converting to :localtime or :gmtime for mutation and formatting @@ -49,15 +57,15 @@ :mon month :year year}] (assert (= (type time) :date/time)) - (let [l (:localtime time) - p (partial put-date l)] + (let [g (:gmtime time) + p (partial put-date g)] (p :sec seconds) (p :min minutes) (p :hour hours) (p :mday month-day) (p :mon month) (p :year year) - (:mktime! l))) # mktime! is slightly more efficient and we're going to GC l anyway + (:timegm! g))) # timegm! is slightly more efficient and we're going to GC l anyway # all supported keys can be given a function (assert (< now (update-time now :sec inc))) |
