From f38dbef3a3fa3e4729453654df03e6388919e4cf Mon Sep 17 00:00:00 2001 From: Henry Widd Date: Wed, 22 Nov 2023 16:30:37 +0000 Subject: [PATCH] same tests passing with java.time --- dev/com/widdindustries/gen/gen/accessors.clj | 40 +-- dev/com/widdindustries/gen/gen_in/tempo.cljc | 4 +- dev/com/widdindustries/gen/graph.clj | 4 +- dev/com/widdindustries/gen/graph_gen.clj | 4 - .../no-deps/src/com/widdindustries/tempo.clj | 320 ++++++++++++------ .../widdindustries/tempo/duration_alpha.cljc | 26 ++ test/com/widdindustries/tempo_test.cljc | 39 +-- 7 files changed, 276 insertions(+), 161 deletions(-) delete mode 100644 dev/com/widdindustries/gen/graph_gen.clj create mode 100644 src/temporal/com/widdindustries/tempo/duration_alpha.cljc diff --git a/dev/com/widdindustries/gen/gen/accessors.clj b/dev/com/widdindustries/gen/gen/accessors.clj index 4fb76a9..dfb50e9 100644 --- a/dev/com/widdindustries/gen/gen/accessors.clj +++ b/dev/com/widdindustries/gen/gen/accessors.clj @@ -45,14 +45,14 @@ (def kw->class { - 'date LocalDate - 'timezone ZoneId - 'zdt ZonedDateTime - 'instant Instant - 'time LocalTime - 'monthday MonthDay - 'datetime LocalDateTime - 'yearmonth YearMonth + 'date 'LocalDate + 'timezone 'ZoneId + 'zdt 'ZonedDateTime + 'instant 'Instant + 'time 'LocalTime + 'monthday 'MonthDay + 'datetime 'LocalDateTime + 'yearmonth 'YearMonth }) (def kw->cljc-ns @@ -130,7 +130,7 @@ (if-let [x (special-accessor (get target feature))] x (if-let [target-class (get kw->class target-name)] - (str ".to" (.getSimpleName target-class)) + (str ".to" (str target-class)) (str ".get" (upper-first (csk/->camelCaseString (name target-name))))))) foo)))))) (defn accessor-forms [feature] @@ -160,21 +160,14 @@ (~(symbol parser) foo))) ))) +(comment + (parse-fn :cljay (ffirst full-paths))) + (defn parse-forms [feature] (->> (apply concat full-paths) distinct (keep (fn [thing] (parse-fn feature thing)))) - - -; (defn duration-parse [s] -; #?(:clj (Duration/parse s) -; :cljs (methods/from entities/duration s))) -; -; (defn period-parse [s] -; #?(:clj (Period/parse s) -; :cljs (duration-parse s))) -; ) (defn now-fn [feature subject] @@ -184,15 +177,6 @@ :cljay (str (get kw->class (:tempo subject)) "/" (or (-> subject feature :parse) "now")) :cljs (str "clock/"(:tempo subject) ) :cljc (str (get kw->cljc-ns (:tempo subject)) "/" (or (-> subject feature :parse) "now")))] - ;; construction from clocks -; (defn date-now -; ([] #?(:clj (LocalDate/now) -; :cljs (clock/plain-date-iso))) -; ([clock] -; #?(:clj (LocalDate/now ^Clock clock) -; :cljs (clock/plain-date-iso clock)))) -; - (backtick/template (defn ~(symbol fn-name) ([] (~(symbol nower))) diff --git a/dev/com/widdindustries/gen/gen_in/tempo.cljc b/dev/com/widdindustries/gen/gen_in/tempo.cljc index 38d9449..b2d9630 100644 --- a/dev/com/widdindustries/gen/gen_in/tempo.cljc +++ b/dev/com/widdindustries/gen/gen_in/tempo.cljc @@ -4,7 +4,9 @@ "" (:refer-clojure :exclude [min max > < >= <= >> <<]) #?(:cljay - (:import (java.time LocalDateTime ZonedDateTime)) + (:import + [java.time Clock MonthDay ZoneId ZoneOffset Instant Duration Period DayOfWeek Month ZonedDateTime LocalTime LocalDateTime LocalDate Year YearMonth ZoneId OffsetDateTime OffsetTime] + [java.time.temporal Temporal TemporalAmount]) :cljc (:require [cljc.java-time.local-date] [cljc.java-time.local-date-time] diff --git a/dev/com/widdindustries/gen/graph.clj b/dev/com/widdindustries/gen/graph.clj index 24079db..96b4f9a 100644 --- a/dev/com/widdindustries/gen/graph.clj +++ b/dev/com/widdindustries/gen/graph.clj @@ -1,9 +1,7 @@ (ns com.widdindustries.gen.graph - (:require [clojure.walk] - [sc.api]) + (:require [clojure.walk]) (:import (java.time ZonedDateTime MonthDay DayOfWeek YearMonth))) - (defn parts->paths [parent-key parts] (->> parts (mapcat (fn [[k v]] diff --git a/dev/com/widdindustries/gen/graph_gen.clj b/dev/com/widdindustries/gen/graph_gen.clj deleted file mode 100644 index 32ac3b8..0000000 --- a/dev/com/widdindustries/gen/graph_gen.clj +++ /dev/null @@ -1,4 +0,0 @@ -(ns com.widdindustries.gen.graph-gen - (:require [com.widdindustries.gen.graph :as graph])) - - diff --git a/gen-out/no-deps/src/com/widdindustries/tempo.clj b/gen-out/no-deps/src/com/widdindustries/tempo.clj index 15b1184..6c15409 100644 --- a/gen-out/no-deps/src/com/widdindustries/tempo.clj +++ b/gen-out/no-deps/src/com/widdindustries/tempo.clj @@ -1,182 +1,290 @@ (ns com.widdindustries.tempo "" - (:import (java.time LocalDateTime ZonedDateTime))) + (:refer-clojure :exclude [min max > < >= <= >> <<]) + (:import + [java.time + Clock + MonthDay + ZoneId + ZoneOffset + Instant + Duration + Period + DayOfWeek + Month + ZonedDateTime + LocalTime + LocalDateTime + LocalDate + Year + YearMonth + ZoneId + OffsetDateTime + OffsetTime] + [java.time.temporal Temporal TemporalAmount])) -(defn - datetime->month - [^java.time.LocalDateTime foo] - (.getMonthValue foo)) +(set! *warn-on-reflection* true) -(defn datetime->second [^java.time.LocalDateTime foo] (.getSecond foo)) +(defn extend-all-cljs-protocols []) -(defn zdt->date [^java.time.ZonedDateTime foo] (.toLocalDate foo)) +(defn period? [v] (instance? Period v)) -(defn datetime->year [^java.time.LocalDateTime foo] (.getYear foo)) +(defn duration? [v] (instance? Duration v)) -(defn date->month [^java.time.LocalDate foo] (.getMonthValue foo)) +(defn instant? [v] (instance? Instant v)) -(defn - datetime->month - [^java.time.LocalDateTime foo] - (.getMonthValue foo)) +(defn date? [v] (instance? LocalDate v)) -(defn - date->day-of-week - [^java.time.LocalDate foo] - (.{:wrap #object[com.widdindustries.gen.graph$fn__10237 0x717b163d "com.widdindustries.gen.graph$fn__10237@717b163d"]} - foo)) +(defn datetime? [v] (instance? LocalDateTime v)) -(defn zdt->year [^java.time.ZonedDateTime foo] (.getYear foo)) +(defn time? [v] (instance? LocalTime v)) -(defn yearmonth->year [^java.time.YearMonth foo] (.getYear foo)) +(defn monthday? [v] (instance? MonthDay v)) -(defn datetime->hour [^java.time.LocalDateTime foo] (.getHour foo)) +(defn yearmonth? [v] (instance? YearMonth v)) -(defn zdt->yearmonth [^java.time.ZonedDateTime foo] (.toYearMonth foo)) +(defn timezone? [v] (instance? ZoneId v)) + +(defn zdt? [v] (instance? ZonedDateTime v)) + +(defn + >> + [temporal temporal-amount] + (.plus ^Temporal temporal ^TemporalAmount temporal-amount)) (defn - monthday->day-of-month - [^java.time.MonthDay foo] - (.getDayOfMonth foo)) + << + [temporal temporal-amount] + (.minus ^Temporal temporal ^TemporalAmount temporal-amount)) -(defn date->month [^java.time.LocalDate foo] (.getMonthValue foo)) +(defn zone-system-default [] (ZoneId/systemDefault)) + +(defn duration->negated [d] (.negated ^Duration d)) + +(defn duration-negative? [d] (.isNegative ^Duration d)) (defn - datetime->day-of-week - [^java.time.LocalDateTime foo] - (.{:wrap #object[com.widdindustries.gen.graph$fn__10237 0x717b163d "com.widdindustries.gen.graph$fn__10237@717b163d"]} - foo)) + clock-fixed + [instant zone] + (Clock/fixed ^Instant instant ^ZoneId zone)) + +(defn clock-system-default-zone [] (Clock/systemDefaultZone)) -(defn zdt->hour [^java.time.ZonedDateTime foo] (.getHour foo)) +(defn greater [x y] (if (neg? (compare x y)) y x)) (defn - zdt->day-of-week - [^java.time.ZonedDateTime foo] - (.{:wrap #object[com.widdindustries.gen.graph$fn__10237 0x717b163d "com.widdindustries.gen.graph$fn__10237@717b163d"]} - foo)) + max + "Find the latest of the given arguments. Callers should ensure that no\n argument is nil." + [arg & args] + (assert (every? some? (cons arg args))) + (reduce + (fn* [p1__30382# p2__30383#] (greater p1__30382# p2__30383#)) + arg + args)) -(defn zdt->instant [^java.time.ZonedDateTime foo] (.toInstant foo)) +(defn lesser [x y] (if (neg? (compare x y)) x y)) -(defn zdt->nano [^java.time.ZonedDateTime foo] (.getNano foo)) +(defn + min + "Find the earliest of the given arguments. Callers should ensure that no\n argument is nil." + [arg & args] + (assert (every? some? (cons arg args))) + (reduce + (fn* [p1__30384# p2__30385#] (lesser p1__30384# p2__30385#)) + arg + args)) (defn - datetime->month - [^java.time.LocalDateTime foo] - (.getMonthValue foo)) + < + ([_x] true) + ([x y] (neg? (compare x y))) + ([x y & more] + (if + (< x y) + (if + (next more) + (recur y (first more) (next more)) + (< y (first more))) + false))) (defn - datetime->day-of-month - [^java.time.LocalDateTime foo] - (.getDayOfMonth foo)) + <= + ([_x] true) + ([x y] (not (pos? (compare x y)))) + ([x y & more] + (if + (<= x y) + (if + (next more) + (recur y (first more) (next more)) + (<= y (first more))) + false))) -(defn monthday->month [^java.time.MonthDay foo] (.getMonthValue foo)) +(defn + > + ([_x] true) + ([x y] (pos? (compare x y))) + ([x y & more] + (if + (> x y) + (if + (next more) + (recur y (first more) (next more)) + (> y (first more))) + false))) (defn - zdt->day-of-month - [^java.time.ZonedDateTime foo] - (.getDayOfMonth foo)) + >= + ([_x] true) + ([x y] (not (neg? (compare x y)))) + ([x y & more] + (if + (>= x y) + (if + (next more) + (recur y (first more) (next more)) + (>= y (first more))) + false))) -(defn zdt->minute [^java.time.ZonedDateTime foo] (.getMinute foo)) +^{:line 39, :column 9} (comment "accessors") -(defn zdt->year [^java.time.ZonedDateTime foo] (.getYear foo)) +(defn datetime->second [^LocalDateTime foo] (.getSecond foo)) -(defn datetime->date [^java.time.LocalDateTime foo] (.toLocalDate foo)) +(defn zdt->date [^ZonedDateTime foo] (.toLocalDate foo)) -(defn - date->day-of-month - [^java.time.LocalDate foo] - (.getDayOfMonth foo)) +(defn datetime->year [^LocalDateTime foo] (.getYear foo)) -(defn zdt->timezone [^java.time.ZonedDateTime foo] (.getZone foo)) +(defn datetime->month [^LocalDateTime foo] (.getMonthValue foo)) -(defn date->month [^java.time.LocalDate foo] (.getMonthValue foo)) +(defn zdt->year [^ZonedDateTime foo] (.getYear foo)) -(defn yearmonth->month [^java.time.YearMonth foo] (.getMonthValue foo)) +(defn datetime->day-of-month [^LocalDateTime foo] (.getDayOfMonth foo)) -(defn zdt->second [^java.time.ZonedDateTime foo] (.getSecond foo)) +(defn monthday->month [^MonthDay foo] (.getMonthValue foo)) -(defn - datetime->monthday - [^java.time.LocalDateTime foo] - (.toMonthDay foo)) +(defn datetime->hour [^LocalDateTime foo] (.getHour foo)) -(defn date->year [^java.time.LocalDate foo] (.getYear foo)) +(defn date->month [^LocalDate foo] (.getMonthValue foo)) -(defn instant->epochnano [^java.time.Instant foo] (.toEpochNano foo)) +(defn zdt->hour [^ZonedDateTime foo] (.getHour foo)) -(defn instant->epochmilli [^java.time.Instant foo] (.toEpochMilli foo)) +(defn zdt->instant [^ZonedDateTime foo] (.toInstant foo)) -(defn time->second [^java.time.LocalTime foo] (.getSecond foo)) +(defn zdt->nano [^ZonedDateTime foo] (.getNano foo)) -(defn - datetime->day-of-month - [^java.time.LocalDateTime foo] - (.getDayOfMonth foo)) +(defn zdt->timezone [^ZonedDateTime foo] (.getZone foo)) -(defn date->year [^java.time.LocalDate foo] (.getYear foo)) +(defn monthday->day-of-month [^MonthDay foo] (.getDayOfMonth foo)) -(defn - zdt->datetime - [^java.time.ZonedDateTime foo] - (.toLocalDateTime foo)) +(defn date->year [^LocalDate foo] (.getYear foo)) -(defn - date->day-of-month - [^java.time.LocalDate foo] - (.getDayOfMonth foo)) +(defn zdt->minute [^ZonedDateTime foo] (.getMinute foo)) -(defn zdt->time [^java.time.ZonedDateTime foo] (.toLocalTime foo)) +(defn datetime->date [^LocalDateTime foo] (.toLocalDate foo)) -(defn date->monthday [^java.time.LocalDate foo] (.toMonthDay foo)) +(defn date->day-of-month [^LocalDate foo] (.getDayOfMonth foo)) -(defn - datetime->yearmonth - [^java.time.LocalDateTime foo] - (.toYearMonth foo)) +(defn zdt->second [^ZonedDateTime foo] (.getSecond foo)) -(defn zdt->month [^java.time.ZonedDateTime foo] (.getMonthValue foo)) +(defn instant->epochmilli [^Instant foo] (.toEpochMilli foo)) -(defn - zdt->day-of-month - [^java.time.ZonedDateTime foo] - (.getDayOfMonth foo)) +(defn time->second [^LocalTime foo] (.getSecond foo)) + +(defn zdt->month [^ZonedDateTime foo] (.getMonthValue foo)) + +(defn zdt->datetime [^ZonedDateTime foo] (.toLocalDateTime foo)) + +(defn zdt->time [^ZonedDateTime foo] (.toLocalTime foo)) + +(defn yearmonth->year [^YearMonth foo] (.getYear foo)) -(defn zdt->month [^java.time.ZonedDateTime foo] (.getMonthValue foo)) +(defn zdt->day-of-month [^ZonedDateTime foo] (.getDayOfMonth foo)) -(defn datetime->minute [^java.time.LocalDateTime foo] (.getMinute foo)) +(defn yearmonth->month [^YearMonth foo] (.getMonthValue foo)) -(defn time->nano [^java.time.LocalTime foo] (.getNano foo)) +(defn datetime->minute [^LocalDateTime foo] (.getMinute foo)) -(defn zdt->monthday [^java.time.ZonedDateTime foo] (.toMonthDay foo)) +(defn time->nano [^LocalTime foo] (.getNano foo)) -(defn time->minute [^java.time.LocalTime foo] (.getMinute foo)) +(defn time->minute [^LocalTime foo] (.getMinute foo)) -(defn time->hour [^java.time.LocalTime foo] (.getHour foo)) +(defn time->hour [^LocalTime foo] (.getHour foo)) -(defn datetime->nano [^java.time.LocalDateTime foo] (.getNano foo)) +(defn datetime->nano [^LocalDateTime foo] (.getNano foo)) -(defn datetime->year [^java.time.LocalDateTime foo] (.getYear foo)) +(defn datetime->time [^LocalDateTime foo] (.toLocalTime foo)) -(defn date->yearmonth [^java.time.LocalDate foo] (.toYearMonth foo)) +^{:line 41, :column 9} (comment "parsers") -(defn zdt->month [^java.time.ZonedDateTime foo] (.getMonthValue foo)) +(defn date-parse [^java.lang.String foo] (LocalDate/parse foo)) + +(defn yearmonth-parse [^java.lang.String foo] (YearMonth/parse foo)) + +(defn datetime-parse [^java.lang.String foo] (LocalDateTime/parse foo)) + +(defn time-parse [^java.lang.String foo] (LocalTime/parse foo)) + +(defn zdt-parse [^java.lang.String foo] (ZonedDateTime/parse foo)) + +(defn instant-parse [^java.lang.String foo] (Instant/parse foo)) + +(defn monthday-parse [^java.lang.String foo] (MonthDay/parse foo)) + +(defn timezone-parse [^java.lang.String foo] (ZoneId/of foo)) + +^{:line 43, :column 9} (comment "nowers") + +(defn + date-now + ([] (LocalDate/now)) + ([^java.time.Clock clock] (LocalDate/now clock))) + +(defn + yearmonth-now + ([] (YearMonth/now)) + ([^java.time.Clock clock] (YearMonth/now clock))) + +(defn + datetime-now + ([] (LocalDateTime/now)) + ([^java.time.Clock clock] (LocalDateTime/now clock))) + +(defn + time-now + ([] (LocalTime/now)) + ([^java.time.Clock clock] (LocalTime/now clock))) + +(defn + zdt-now + ([] (ZonedDateTime/now)) + ([^java.time.Clock clock] (ZonedDateTime/now clock))) + +(defn + instant-now + ([] (Instant/now)) + ([^java.time.Clock clock] (Instant/now clock))) + +(defn + monthday-now + ([] (MonthDay/now)) + ([^java.time.Clock clock] (MonthDay/now clock))) -(defn datetime->time [^java.time.LocalDateTime foo] (.toLocalTime foo)) +^{:line 45, :column 9} (comment "constructors") (defn time-from [thing] (let - [^int hour + [hour (get thing :hour 0) - ^int minute + minute (get thing :minute 0) - ^int second + second (get thing :second 0) - ^int nano + nano (get thing :nano 0)] - (LocalTime/of hour minute second nano))) + (LocalTime/of ^int hour ^int minute ^int second ^int nano))) (defn date-from diff --git a/src/temporal/com/widdindustries/tempo/duration_alpha.cljc b/src/temporal/com/widdindustries/tempo/duration_alpha.cljc new file mode 100644 index 0000000..10224bb --- /dev/null +++ b/src/temporal/com/widdindustries/tempo/duration_alpha.cljc @@ -0,0 +1,26 @@ +(ns com.widdindustries.tempo.duration-alpha + "significant differences exist between java.time and Temporal wrt amounts of time. + + For now, have separate ns. + " + #?(:clj (:import [java.time Duration Period]))) + +(defn duration-parse [p] + #?(:cljs (js/Temporal.Duration.from p) + :clj (. Duration parse p))) + +(defn period-parse [p] + #?(:cljs (js/Temporal.Duration.from p) + :clj (. Period parse p))) + +(defn duration->negated [d] + #?(:cljs (.negated ^js d) + :clj (.negated d)) + ) + +;(defn duration->as-years ([d]) ([d ref])) +;(defn duration->as-months [d ref]) +;(defn duration->as-days [d ref]) +;(defn duration->as-hours [d]) +;(defn duration->as-minutes [d]) +;(defn duration->as-seconds [d]) \ No newline at end of file diff --git a/test/com/widdindustries/tempo_test.cljc b/test/com/widdindustries/tempo_test.cljc index d501059..63f14d4 100644 --- a/test/com/widdindustries/tempo_test.cljc +++ b/test/com/widdindustries/tempo_test.cljc @@ -1,17 +1,18 @@ (ns com.widdindustries.tempo-test (:require [clojure.test :refer [deftest is testing]] - [com.widdindustries.tempo :as t])) + [com.widdindustries.tempo :as t] + [com.widdindustries.tempo.duration-alpha :as d])) (t/extend-all-cljs-protocols) -#_(deftest parsing-duration - (is (t/duration? (t/duration-parse "PT1S")))) +(deftest parsing-duration + (is (t/duration? (d/duration-parse "PT1S")))) -#_(deftest equals-hash-compare-duration - (let [make-middle #(t/duration-parse "PT1S") +(deftest equals-hash-compare-duration + (let [make-middle #(d/duration-parse "PT1S") middle (make-middle) - smallest (t/duration->negated (t/duration-parse "PT1S")) - largest (t/duration-parse "PT2S")] + smallest (d/duration->negated (d/duration-parse "PT1S")) + largest (d/duration-parse "PT2S")] (is (not= middle smallest)) (is (= middle (make-middle))) (is (= (sort [largest smallest middle]) [smallest middle largest])) @@ -25,12 +26,12 @@ (is (= "2020-02-02" (str (t/date-now (t/clock-fixed (t/instant-parse "2020-02-02T09:19:42.128946Z") - "UTC")))))) + (t/timezone-parse "UTC"))))))) -#_(deftest equals-hash-compare-date +(deftest equals-hash-compare-date (let [middle (t/date-now) - earliest (t/<< middle (t/period-parse "P1D")) - latest (t/>> middle (t/period-parse "P1D"))] + earliest (t/<< middle (d/period-parse "P1D")) + latest (t/>> middle (d/period-parse "P1D"))] (is (not= middle earliest)) (is (= middle (t/date-now))) ;(compare earliest middle) @@ -42,17 +43,17 @@ (is (t/date? (t/date-now))) (is (not (t/period? (t/date-now))))) -#_(deftest parsing-test - (is (t/zone-id? (t/zone-parse "Europe/London")))) +(deftest parsing-test + (is (t/timezone? (t/timezone-parse "Europe/London")))) -#_(deftest equals-hash - (is (= (t/zone-parse "Europe/London") (t/zone-parse "Europe/London"))) - (is (not= (t/zone-parse "Europe/London") (t/zone-parse "Europe/Paris"))) - (is (= 1 (get {(t/zone-parse "Europe/London") 1} (t/zone-parse "Europe/London"))))) +(deftest equals-hash + (is (= (t/timezone-parse "Europe/London") (t/timezone-parse "Europe/London"))) + (is (not= (t/timezone-parse "Europe/London") (t/timezone-parse "Europe/Paris"))) + (is (= 1 (get {(t/timezone-parse "Europe/London") 1} (t/timezone-parse "Europe/London"))))) -#_(deftest shift +(deftest shift (let [a-date (t/date-now) - period (t/period-parse "P3D") + period (d/period-parse "P3D") plus3 (t/>> a-date period)] (is (= a-date (t/<< plus3 period)))))