Skip to content

Commit

Permalink
add instant->zdt-in-UTC
Browse files Browse the repository at this point in the history
  • Loading branch information
henryw374 committed Mar 20, 2024
1 parent 28700f7 commit 11341ac
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,21 @@ namespace exists which contains a subset of the functions from `tick.core` which

[![Clojars Project](https://img.shields.io/clojars/v/com.widdindustries/time-literals-tempo.svg)](https://clojars.org/com.widdindustries/time-literals-tempo)

* As of March 2024, a polyfill (e.g. [this](https://github.com/fullcalendar/temporal-polyfill)) of Temporal will be required for all javascript environments.
* The new underlying Javascript platform time API [Temporal](https://github.com/tc39/proposal-temporal) has semi-stabilized at
As of March 2024

* [Temporal](https://github.com/tc39/proposal-temporal) has semi-stabilized at
`ecma stage 3`, meaning implementors
can still suggest changes - although at this point any changes will be superficial.
can still suggest changes - although at this point any changes will be superficial.
* a polyfill (e.g. [this](https://github.com/fullcalendar/temporal-polyfill)) of Temporal will be required for all javascript environments.

```html
<script>
if(!window.Temporal){
document.write('<script src="https://cdn.jsdelivr.net/npm/temporal-polyfill@0.2.3/global.min.js"><\/script>');
}
</script>
```


### Setup

Expand Down Expand Up @@ -195,11 +206,14 @@ to as `timezone_id`. Call `str` on a timezone to get its id.
(t/yearmonth+day-at-end-of-month a-yearmonth) ; => a date
(t/datetime+timezone a-datetime "Pacific/Honolulu") ; => a zdt

; to get parts of an entity, start with the subject and add ->
; to get parts of an entity, the function name will start with the type of the entity, then add -> then put the target type. For example:
(t/date->yearmonth a-date)
(t/date->month a-date)
(t/zdt->nanosecond a-zdt)
(t/instant->epochmillisecond an-instant)
(t/epochmilli->instant 123)
(t/legacydate->instant d)
; & etc

```

Expand Down
4 changes: 4 additions & 0 deletions gen_in/tempo.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@
#?(:cljay (.toInstant ^java.util.Date d)
:cljs (.toTemporalInstant ^js d)))

(defn instant->zdt-in-UTC [instant]
#?(:cljay (ZonedDateTime/ofInstant instant (ZoneId/of "UTC"))
:cljs (.toZonedDateTimeISO ^js instant "UTC")))

(defn greater [x y]
(if (neg? (compare x y)) y x))

Expand Down
9 changes: 6 additions & 3 deletions test/com/widdindustries/tempo_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@
(is (= i (t/instant-from {:epochmilli (t/instant->epochmilli i)}))))
(let [d #?(:clj (Date.) :cljs (js/Date.))
i (t/instant-from {:legacydate d})]
(= (.getTime d) (t/instant->epochmilli i))
(is (= (.getTime d) (-> (.getTime d) (t/epochmilli->instant) (t/instant->epochmilli))))
))
(is (= (.getTime d) (t/instant->epochmilli i)))
(is (= i (-> i (t/instant->legacydate) (t/legacydate->instant))))
(is (= (.getTime d) (-> (.getTime d) (t/epochmilli->instant) (t/instant->epochmilli))))))
(testing "zdt-instant"
(let [i (t/instant-parse "2024-01-16T12:43:44.196000Z")]
(is (= i (-> i (t/instant->zdt-in-UTC) (t/zdt->instant))))))
(testing "zdt with offset"
(is (= "+05:50"
(->
Expand Down

0 comments on commit 11341ac

Please sign in to comment.