dm3 / clojure.java-time

Java 8 Date-Time API for Clojure
MIT License
471 stars 47 forks source link

`jt/as` fn seems to be broken #110

Closed marksto closed 7 months ago

marksto commented 7 months ago

Hi @frenchy64 and thank you for this classy Java Time wrapper lib!

Alas, it looks like jt/as doesn't work (or, probably, my expectations about it are wrong):

(require '[java-time.api :as jt])

(def ldt (jt/local-date-time))
(clojure.java.data/from-java ldt)
=>
{:nano 410904000,
 :hour 17,
 :second 46,
 :monthValue 4,
 :month "APRIL",
 :dayOfMonth 16,
 :year 2024,
 :dayOfWeek "TUESDAY",
 :dayOfYear 107,
 :minute 22}

(jt/as ldt :year) ; this one mysteriously works
=> 2024

(jt/as ldt :month)
Execution error (DateTimeException) at java-time.temporal/eval31584$fn (temporal.clj:186).
Property :month doesn't exist in [2024-04-16T17:22:46.410904]!
(jt/as ldt :months)
Execution error (DateTimeException) at java-time.temporal/eval31584$fn (temporal.clj:186).
Property :months doesn't exist in [2024-04-16T17:22:46.410904]!

The same is true for a zoned date-time:

(require '[java-time.api :as jt])

(def zdt (jt/zoned-date-time ldt "UTC"))

(clojure.java.data/from-java zdt)
=>
{:nano 410904000,
 :hour 17,
 :second 46,
 :offset {:id "Z", :rules {:fixedOffset true, :transitionRules (), :transitions ()}, :totalSeconds 0},
 :zone {:id "UTC", :rules {:fixedOffset true, :transitionRules (), :transitions ()}},
 :monthValue 4,
 :month "APRIL",
 :dayOfMonth 16,
 :year 2024,
 :dayOfWeek "TUESDAY",
 :dayOfYear 107,
 :minute 22}

(jt/as zdt :year) ; this one mysteriously works
=> 2024

(jt/as zdt :second)
Execution error (DateTimeException) at java-time.temporal/eval31584$fn (temporal.clj:186).
Property :second doesn't exist in [2024-04-16T17:22:46.410904Z[UTC]]!

A documentation of the jt/as fn is scarce and doesn't give any examples with date-times. However, it states that the first argument is a "temporal entity", which dates-times definitely are.

Would be nice to hear your thoughts.

marksto commented 7 months ago

Ok, I've figured this out after more carefully reading the relevant README section and playing in REPL for a bit.

(jt/as (jt/local-date-time) :hour-of-day :minute-of-hour :second-of-minute)
=> [18 17 19]

Life is great again.