diff --git a/README.md b/README.md index 5aa8e25..2dca39f 100644 --- a/README.md +++ b/README.md @@ -126,158 +126,170 @@ Depend on tempo via deps.edn: (t/extend-cljs-protocols-to-instant) ``` -### Construction and access/get +### Construction and access -#### Clocks +### Clocks + +a Clock is required to be able to get the current time/date/zone etc ```clojure -;fixed in time and place -(t/clock-fixed (t/instant-now) "Europe/Paris") -; ambient place, ticking clock +; ticking clock in ambient place (t/clock-system-default-zone) +; ticking clock in specified place +(t/clock-with-zone "Pacific/Honolulu") + +; fixed in time and place +(t/clock-fixed (t/instant-parse "2020-02-02T00:00:00Z") "Europe/Paris") + ; offset existing clock by specified millis (t/clock-offset clock -5) +``` +a clock is then passed as arg to all `now` functions, for example: + +```clojure +(t/date-now clock) ``` -#### Time zones +#### Time zones & Offsets ```clojure (t/timezone-parse "Europe/London") + +(t/timezone-now clock) ``` -Where a timezone is accessed from an object, or passed into an object, only the string representation is used, referred to as `timezone_id` +Where a timezone is accessed from an object, or passed into an object, only the string representation is used, referred to as `timezone_id`. Call `str` on a timezone to get its id. ```clojure (t/zdt->timezone_id zdt) -(t/zdt-from {:datetime datetime :timezone_id timezone}) +(t/zdt-from {:datetime datetime :timezone_id timezone_id}) ``` -#### Temporals +### Temporals ```clojure -; Tempo construction and access is based on mnemonics +; naming of construction and access functions is based on mnemonics ; the first word in the function is the entity name of the subject of the operation ; for example, if I want to construct a date or access its parts the function will start t/date-, ; similarly for a zone-date-time, it will be t/zdt-* -(t/date-now) (t/date-now clock) (t/date-parse "2020-02-02") ;iso strings only (t/date-from {:year 2020 :month 2 :day 2}) ; the -from functions accept a map of components which is sufficient to build the entity -(t/datetime-from {:date (t/date-parse "2020-02-02") :time (t/time-now)}) +(t/datetime-from {:date (t/date-parse "2020-02-02") :time (t/time-now clock)}) ; or equivalently -(t/datetime-from {:year 2020 :month 2 :day 2 :time (t/time-now)}) +(t/datetime-from {:year 2020 :month 2 :day 2 :time (t/time-now clock)}) ; with -from, you can use smaller or larger components. ; larger ones take precedence. below, the :year is ignored, because the :date took precedence (being larger) (t/datetime-from {:year 2021 :date (t/date-parse "2020-02-02") :time (t/time-now)}) ; to get parts of an entity, start with the subject and add -> -(t/date->yearmonth (t/date-now)) -(t/date->month (t/date-now)) -(t/zdt->nanos (t/zdt-now)) -(-> (t/instant-now) (t/instant->epochmillis)) +(t/date->yearmonth (t/date-now clock)) +(t/date->month (t/date-now clock)) +(t/zdt->nanos (t/zdt-now clock)) +(-> (t/instant-now clock) (t/instant->epochmillis)) ``` -#### Temporal-amounts - -A temporal-amount is a quantity of time, e.g. hour, month, second. - -Temporal-Amount entities are represented differently in java.time and Temporal, but with some overlap. - -An `alpha` ns (groan!) exists which contains a few functions for working with temporal-amounts. +#### Manipulation -If not sufficient, use reader conditionals in your code to construct/manipulate as appropriate. +aka construction a new temporal from one of the same type ```clojure -(require '[com.widdindustries.tempo.duration-alpha :as d]) - -(d/duration-parse "PT0.001S") +;; move date forward 3 days +(t/>> (t/date-now clock) 3 t/days-property) -### Manipulation +;; set a particular field +(-> (t/yearmonth-now clock) (t/with 3030 t/years-property)) -aka construction a new entity from one of the same type +; set fields smaller than days (ie hours, mins etc) to zero +(t/truncate x t/days-property) -#### Temporal-amounts +``` -```clojure +#### Guardrails -(require '[com.widdindustries.tempo.duration-alpha :as d]) -(t/+ (d/duration-parse "PT3H") (d/duration-parse "PT3S")) +Consider the following: +```clojure +(let [start (t/date-parse "2020-01-31")] + (-> start (t/>> 1 t/months-property) + (t/<< 1 t/months-property))) ``` -#### Temporals +If you shift a date forward by an amount, then back by that amount then one might think the output would be equal to the input. In some cases that would happen, but not in the case shown above. + +Here's a similar example: ```clojure +(let [start (t/date-parse "2020-02-29")] + (-> start + (t/with 2021 t/years-property) + (t/with 2020 t/years-property))) +``` -(require '[com.widdindustries.tempo.duration-alpha :as d]) +We increment the year, then decrement it, but the output is not the same as the input. -;; move date forward 3 days -(t/>> (t/date-now) (d/period-parse "P3D")) +Both java.time and Temporal work this way and in my experience it is a source of bugs. For this reason, shifting `>>/<<` and `with` do not work in Tempo if the property is years or months. -(-> (t/date-now) (t/with 3030 t/years-property)) +As a safer alternative, I suggest getting the year-month from a temporal first, doing whatever with/shift operations you like then setting the remaining fields. -``` +If you do not wish to have this guardrail, set `t/*block-non-commutative-operations*` to false ### Comparison -#### Temporal ```clojure ;only entities of the same type can be compared (t/>= a b) - (t/max a b c) -; you must specify unit +; you must specify property (t/until a b t/minutes-property) ``` -### Predicates +#### Predicates ```clojure (t/date? x) ``` -### Guardrails +#### Temporal-amounts -Consider the following: +A temporal-amount is an entity representing a quantity of time, e.g. 3 hours and 5 seconds. -```clojure -(let [start (t/date-parse "2020-01-31")] - (-> start (t/>> 1 t/months-property) - (t/<< 1 t/months-property))) -``` +Temporal-Amount entities are represented differently in java.time vs Temporal, but with some overlap. -If you shift a date forward by an amount, then back by that amount then you might think you'd end up with the date you started. Sometimes yes, in this case above, no. +An `alpha` ns (groan!) exists which contains a few functions for working with temporal-amounts. -Here's a similar example: +If not sufficient, use reader conditionals in your code to construct/manipulate as appropriate. ```clojure -(let [start (t/date-parse "2020-02-29")] - (-> start (t/with 2021 t/years-property) - (t/with 2020 t/years-property))) -``` -We increment the year, then decrement it, but the output is not the same as the input. +(require '[com.widdindustries.tempo.duration-alpha :as d]) -Both java.time and Temporal work this way and in my experience it is a source of bugs. For this reason, shifting `>>/<<` and `with` do not work in Tempo if the property is years or months. +(d/duration-parse "PT0.001S") -As a safer alternative, I suggest getting the year-month from a temporal first, doing whatever with/shift operations you like then setting the remaining fields. +``` -If you do not wish to have this guardrail, set `t/*block-non-commutative-operations*` to false +In Tempo, some functions accept temporal-amounts as argument, but they are never returned from any function + +It is preferred to use numbers and properties for example + +```clojure +(t/>> a-date 1 t/days-property) +``` ## Dev diff --git a/dev/com/widdindustries/gen/gen/accessors.clj b/dev/com/widdindustries/gen/gen/accessors.clj index cd82869..c342749 100644 --- a/dev/com/widdindustries/gen/gen/accessors.clj +++ b/dev/com/widdindustries/gen/gen/accessors.clj @@ -250,7 +250,7 @@ ~(when (and (and (not= 'monthday subject) (not= 'month target-name)) (not (contains? #{'day-of-week 'date 'day-of-month 'instant 'datetime 'time 'timezone_id 'legacydate - 'epochmilli} + 'epochmilli 'year 'month} target-name))) (list 'is (list '= 'now-now (list 't/with 'now-now (list (symbol (str "t/" fn-name)) 'now-now) prop)))) )))))) diff --git a/dev/com/widdindustries/gen/graph.clj b/dev/com/widdindustries/gen/graph.clj index a0b0d54..552cbf2 100644 --- a/dev/com/widdindustries/gen/graph.clj +++ b/dev/com/widdindustries/gen/graph.clj @@ -49,7 +49,8 @@ (def epochmilli {:tempo 'epochmilli :return 'int? - :cljay {:accessor 'toEpochMilli}}) + :cljay {:accessor 'toEpochMilli} + :cljs {:xform-fn '(-> (.-epochMilliseconds))}}) (def epochnano {:ignore-accessor true ; its not in java @@ -87,7 +88,7 @@ (def monthday {:tempo 'monthday :ignore-accessor true - :needed-to-go-up {'year {}} + :needed-to-go-up {'year {}} :cljay {:no-getter true :fn-args ['month 'day-of-month] :fn (fn [month day] @@ -114,7 +115,7 @@ {:tempo 'legacydate :cljay {:xform-fn '(-> (.toEpochMilli) (java.util.Date.))} :cljs { - :xform-fn '(-> (.-epochMilliseconds) (Date.)) + :xform-fn '(-> (.-epochMilliseconds) (js/Date.)) }} {}}} {:tempo 'zdt ;todo - https://tc39.es/proposal-temporal/docs/zoneddatetime.html#startOfDay @@ -136,10 +137,14 @@ :return 'int?} {} month {}}}}} {:parts - {monthday {:parts {month {} + {monthday {:parts {{:tempo 'month + :return 'int? + ;get month not gener + :cljay {:accessor 'getMonthValue} + :cljs {:xform-fn '(-> (.-monthCode) (subs 1 3) js/parseInt)}} {} {:tempo 'day-of-month :return 'int? - :cljs {:accessor '-day}} {}}}}} + :cljs {:accessor '-day}} {}}}}} {:parts {{:tempo 'year :return 'int?} {} @@ -151,20 +156,20 @@ {day-of-week {}}}] } {:tempo 'time} {:parts {{:tempo 'hour - :return 'int?} {} + :return 'int?} {} {:tempo 'minute - :return 'int?} {} + :return 'int?} {} {:tempo 'second - :return 'int?} {} + :return 'int?} {} {:tempo 'millisecond :return 'int? - :cljay {:xform-fn '(-> -millisecond)}} {} + :cljay {:xform-fn '(-> -millisecond)}} {} {:tempo 'microsecond :return 'int? - :cljay {:xform-fn '(-> -microsecond)}} {} + :cljay {:xform-fn '(-> -microsecond)}} {} {:tempo 'nanosecond :return 'int? - :cljay {:xform-fn '(-> -nanosecond)}} {}}}}}}}]}}) + :cljay {:xform-fn '(-> -nanosecond)}} {}}}}}}}]}}) (def with-paths (paths graph)) diff --git a/dev/dev.clj b/dev/dev.clj index 4524f12..e4100ca 100644 --- a/dev/dev.clj +++ b/dev/dev.clj @@ -57,7 +57,7 @@ (capt/capt '(do (require '[com.widdindustries.gen.gen.tempo] :reload) - (require '[com.widdindustries.gen.gen.accessors] :reload) + (require '[com.widdindustries.gen.gen.accessors] :reload-all) (gen/gen-after))) (capt/exec) diff --git a/gen_in/constructors.cljc b/gen_in/constructors.cljc index adea0fb..c24113a 100644 --- a/gen_in/constructors.cljc +++ b/gen_in/constructors.cljc @@ -60,7 +60,7 @@ (ZonedDateTime/ofInstant instant (timezone-parse zone)) (ZonedDateTime/of ^LocalDateTime ldt ^ZoneId (timezone-parse zone))) :cljs (if instant - (.toZonedDateTimeISO ^js instant (ZoneId/of zone)) + (.toZonedDateTimeISO ^js instant zone) (.toZonedDateTime ^js ldt zone))))) (defn instant-from [thing] diff --git a/gen_in/tempo.cljc b/gen_in/tempo.cljc index 08f5429..1a9b176 100644 --- a/gen_in/tempo.cljc +++ b/gen_in/tempo.cljc @@ -11,7 +11,8 @@ :cljs (:require [com.widdindustries.tempo.cljs-protocols :as cljs-protocols] [com.widdindustries.tempo.js-temporal-entities :as entities] [com.widdindustries.tempo.js-temporal-methods :as methods] - [com.widdindustries.tempo.clock :as clock])) + [com.widdindustries.tempo.clock :as clock] + [goog.object])) ;(:require [com.widdindustries.tempo.js-temporal-entities :as entities]) ) @@ -69,16 +70,16 @@ (defn clock-offset-millis [clock offset-millis] #?(:cljay (Clock/offset clock (Duration/ofMillis offset-millis)) :cljs (clock/clock - (fn [] (.add (.instant clock) (js-obj "milliseconds" offset-millis))) - (clock/timezone clock)))) + (fn [] (.add (.instant ^js clock) (js-obj "milliseconds" offset-millis))) + (clock/timezone_id clock)))) (defn timezone-now ([clock] #?(:cljay (.getZone ^Clock clock) - :cljs (clock/timezone clock)))) + :cljs (clock/timezone_id clock)))) (defn legacydate->instant [d] #?(:cljay (.toInstant ^java.util.Date d) - :cljs (.toTemporalInstant d))) + :cljs (.toTemporalInstant ^js d))) (defn greater [x y] (if (neg? (compare x y)) y x)) @@ -154,12 +155,12 @@ (getFractional [_]) (setFractional [_ _]))) -(defn -millisecond [f] - (-> (getFractional f) (Duration/ofNanos) (.toMillisPart))) -(defn -microsecond [f] - (-> (getFractional f) (/ 1000) long (mod 1000))) -(defn -nanosecond [f] - (-> (getFractional f) (mod 1000))) +#?(:cljay (defn -millisecond [f] + (-> (getFractional f) (Duration/ofNanos) (.toMillisPart)))) +#?(:cljay (defn -microsecond [f] + (-> (getFractional f) (/ 1000) long (mod 1000)))) +#?(:cljay (defn -nanosecond [f] + (-> (getFractional f) (mod 1000)))) #?(:cljay (extend-protocol HasTime @@ -177,7 +178,7 @@ (setFractional [x ^long t] (.with x ChronoField/NANO_OF_SECOND t)) )) -(def ^ValueRange sub-second-range (ValueRange/of 0 999)) +#?(:cljay (def ^ValueRange sub-second-range (ValueRange/of 0 999))) (def nanoseconds-property #?(:cljay (reify @@ -194,18 +195,18 @@ new-fractional (+ millis+micros value)] (-> temporal (setFractional new-fractional))))))) :cljs (reify - object (toString [_] "nanoseconds-property") + ;object (toString [_] "nanoseconds-property") Property (field [_] "nanosecond") (unit [_] (reify Unit (unit-amount [_] "nanoseconds") (unit-field [_] "nanosecond") - (unit-accessor [_ ^js x] (.-nanos x))))))) + (unit-accessor [_ ^js x] (.-nanoseconds x))))))) -(defmethod print-method (type nanoseconds-property) - [_, ^java.io.Writer w] - (print-simple "nanoseconds-property" w)) +#_(defmethod print-method (type nanoseconds-property) + [_, ^java.io.Writer w] + (print-simple "nanoseconds-property" w)) (def microseconds-property #?(:cljay (reify Object (toString [_] "microseconds-property") @@ -221,13 +222,14 @@ new-fractional (+ millis nanos (-> value (* 1000)))] (-> temporal (setFractional new-fractional))))))) :cljs (reify - object (toString [_] "microseconds-property") + ;object (toString [_] "microseconds-property") Property (field [_] "microsecond") - (unit [_] (reify Unit (unit-amount [_] "microseconds") (unit-field [_] "microsecond") (unit-accessor [_ ^js x] (.-micros x))))))) + (unit [_] (reify Unit (unit-amount [_] "microseconds") (unit-field [_] "microsecond") + (unit-accessor [_ ^js x] (.-microseconds x))))))) -(defmethod print-method (type microseconds-property) - [_, ^java.io.Writer w] - (print-simple "microseconds-property" w)) +#_(defmethod print-method (type microseconds-property) + [_, ^java.io.Writer w] + (print-simple "microseconds-property" w)) (def milliseconds-property #?(:cljay (reify Object (toString [_] "milliseconds-property") @@ -241,90 +243,95 @@ micros+nanos (-> fractional (mod 1000000))] (-> temporal (setFractional (+ micros+nanos (-> value (* 1000000)))))))))) :cljs (reify - object (toString [_] "milliseconds-property") - Property (field [_] "millisecond") (unit [_] (reify Unit (unit-amount [_] "milliseconds") (unit-field [_] "millisecond") (unit-accessor [_ ^js x] (.-millis x))))))) + ;object (toString [_] "milliseconds-property") + Property (field [_] "millisecond") (unit [_] (reify Unit (unit-amount [_] "milliseconds") (unit-field [_] "millisecond") + (unit-accessor [_ ^js x] (.-milliseconds x))))))) -(defmethod print-method (type milliseconds-property) - [_, ^java.io.Writer w] - (print-simple "milliseconds-property" w)) +#_(defmethod print-method (type milliseconds-property) + [_, ^java.io.Writer w] + (print-simple "milliseconds-property" w)) (def seconds-property #?(:cljay (reify Object (toString [_] "seconds-property") Property (unit [_] ChronoUnit/SECONDS) (field [_] ChronoField/SECOND_OF_MINUTE)) :cljs (reify - object (toString [_] "seconds-property") + ;object (toString [_] "seconds-property") Property (field [_] "second") (unit [_] (reify Unit (unit-amount [_] "seconds") (unit-field [_] "second") (unit-accessor [_ ^js x] (.-seconds x))))))) -(defmethod print-method (type seconds-property) - [_, ^java.io.Writer w] - (print-simple "seconds-property" w)) +#_(defmethod print-method (type seconds-property) + [_, ^java.io.Writer w] + (print-simple "seconds-property" w)) (def minutes-property #?(:cljay (reify Object (toString [_] "minutes-property") Property (unit [_] ChronoUnit/MINUTES) (field [_] ChronoField/MINUTE_OF_HOUR)) :cljs (reify - object (toString [_] "minutes-property") + ;object (toString [_] "minutes-property") Property (field [_] "minute") (unit [_] (reify Unit (unit-amount [_] "minutes") (unit-field [_] "minute") (unit-accessor [_ ^js x] (.-minutes x))))))) -(defmethod print-method (type minutes-property) - [_, ^java.io.Writer w] - (print-simple "minutes-property" w)) +#_(defmethod print-method (type minutes-property) + [_, ^java.io.Writer w] + (print-simple "minutes-property" w)) (def hours-property #?(:cljay (reify Object (toString [_] "hours-property") Property (unit [_] ChronoUnit/HOURS) (field [_] ChronoField/HOUR_OF_DAY)) :cljs (reify - object (toString [_] "hours-property") + ;object (toString [_] "hours-property") Property (field [_] "hour") (unit [_] (reify Unit (unit-amount [_] "hours") (unit-field [_] "hour") (unit-accessor [_ ^js x] (.-hours x))))))) -(defmethod print-method (type hours-property) - [_, ^java.io.Writer w] - (print-simple "hours-property" w)) +#_(defmethod print-method (type hours-property) + [_, ^java.io.Writer w] + (print-simple "hours-property" w)) (def days-property #?(:cljay (reify Object (toString [_] "days-property") Property (unit [_] ChronoUnit/DAYS) (field [_] ChronoField/DAY_OF_MONTH)) :cljs (reify - object (toString [_] "days-property") - Property (field [_] "day") (unit [_] (reify Unit (unit-amount [_] "days") (unit-field [_] "day") (unit-accessor [_ ^js x] (.-days x))))))) + ;object (toString [_] "days-property") + Property + (field [_] "day") + (unit [_] (reify Unit (unit-amount [_] "days") + (unit-field [_] "day") + (unit-accessor [_ ^js x] (.-days x))))))) -(defmethod print-method (type days-property) - [_, ^java.io.Writer w] - (print-simple "days-property" w)) +#_(defmethod print-method (type days-property) + [_, ^java.io.Writer w] + (print-simple "days-property" w)) (def months-property #?(:cljay (reify Object (toString [_] "months-property") Property (unit [_] ChronoUnit/MONTHS) (field [_] ChronoField/MONTH_OF_YEAR)) :cljs (reify - object (toString [_] "months-property") + ;object (toString [_] "months-property") Property (field [_] "month") (unit [_] (reify Unit (unit-amount [_] "months") (unit-field [_] "month") (unit-accessor [_ ^js x] (.-months x))))))) -(defmethod print-method (type months-property) - [_, ^java.io.Writer w] - (print-simple "months-property" w)) +#_(defmethod print-method (type months-property) + [_, ^java.io.Writer w] + (print-simple "months-property" w)) (def years-property #?(:cljay (reify Object (toString [_] "years-property") Property (unit [_] ChronoUnit/YEARS) (field [_] ChronoField/YEAR)) :cljs (reify - object (toString [_] "years-property") + ;object (toString [_] "years-property") Property (field [_] "year") (unit [_] (reify Unit (unit-amount [_] "years") (unit-field [_] "year") (unit-accessor [_ ^js x] (.-years x))))))) -(defmethod print-method (type years-property) - [_, ^java.io.Writer w] - (print-simple "years-property" w)) +#_(defmethod print-method (type years-property) + [_, ^java.io.Writer w] + (print-simple "years-property" w)) (def ^:dynamic *block-non-commutative-operations* true) (defn assert-set-months-or-years [temporal temporal-property] (when *block-non-commutative-operations* - (assert (not (and (contains? #{years-property months-property} temporal) + (assert (not (and (contains? #{years-property months-property} temporal-property) (not (or (monthday? temporal) (yearmonth? temporal))))) "shifting by years or months yields odd results depending on input. intead shift a year-month, then set non-yearmonth parts"))) @@ -336,9 +343,9 @@ (defn until [v1 v2 property] #?(:cljay (.until ^Temporal v1 v2 (unit property)) ;https://tc39.es/proposal-temporal/docs/instant.html#until - :cljs (-> (.until ^js v1 ^js v2 (js-obj "smallestUnit" (unit-field (unit property)) - "largestUnit" (unit-field (unit property)))) - (unit-accessor (unit property))))) + :cljs (unit-accessor (unit property) + (-> (.until ^js v1 ^js v2 (js-obj "smallestUnit" (unit-field (unit property)) + "largestUnit" (unit-field (unit property)))))))) (defn >> #_([temporal temporal-property] @@ -390,16 +397,18 @@ (defprotocol JavaTruncateable (-truncate [_ unit])) -(extend-protocol JavaTruncateable - ZonedDateTime (-truncate [zdt unit] (.truncatedTo zdt unit)) - LocalDateTime (-truncate [zdt unit] (.truncatedTo zdt unit)) - LocalTime (-truncate [zdt unit] (.truncatedTo zdt unit)) - Instant (-truncate [zdt unit] (.truncatedTo zdt unit))) +#?(:cljay + (extend-protocol JavaTruncateable + ZonedDateTime (-truncate [zdt unit] (.truncatedTo zdt unit)) + LocalDateTime (-truncate [zdt unit] (.truncatedTo zdt unit)) + LocalTime (-truncate [zdt unit] (.truncatedTo zdt unit)) + Instant (-truncate [zdt unit] (.truncatedTo zdt unit)))) (defn truncate [temporal property] #?(:cljay (-truncate temporal (unit property)) - :cljs (.roundTo temporal (unit-field (unit property))))) + :cljs (.round ^js temporal + (js-obj "smallestUnit" (unit-field (unit property)) "roundingMode" "trunc")))) (defn get-field [temporal property] #?(:cljay (.get ^TemporalAccessor temporal (field property)) - :cljs (unit-accessor property temporal))) + :cljs (goog.object/get temporal (field property)))) diff --git a/src/com/widdindustries/tempo.clj b/src/com/widdindustries/tempo.clj index 83c7d4b..a6ff065 100644 --- a/src/com/widdindustries/tempo.clj +++ b/src/com/widdindustries/tempo.clj @@ -93,7 +93,7 @@ [arg & args] (assert (every? some? (cons arg args))) (reduce - (fn* [p1__36899# p2__36900#] (greater p1__36899# p2__36900#)) + (fn* [p1__137956# p2__137957#] (greater p1__137956# p2__137957#)) arg args)) @@ -105,7 +105,7 @@ [arg & args] (assert (every? some? (cons arg args))) (reduce - (fn* [p1__36901# p2__36902#] (lesser p1__36901# p2__36902#)) + (fn* [p1__137958# p2__137959#] (lesser p1__137958# p2__137959#)) arg args)) @@ -227,12 +227,6 @@ (+ millis+micros value)] (-> temporal (setFractional new-fractional)))))))) -(defmethod - print-method - (type nanoseconds-property) - [_ ^java.io.Writer w] - (print-simple "nanoseconds-property" w)) - (def microseconds-property (reify @@ -260,12 +254,6 @@ (+ millis nanos (-> value (* 1000)))] (-> temporal (setFractional new-fractional)))))))) -(defmethod - print-method - (type microseconds-property) - [_ ^java.io.Writer w] - (print-simple "microseconds-property" w)) - (def milliseconds-property (reify @@ -291,12 +279,6 @@ temporal (setFractional (+ micros+nanos (-> value (* 1000000))))))))))) -(defmethod - print-method - (type milliseconds-property) - [_ ^java.io.Writer w] - (print-simple "milliseconds-property" w)) - (def seconds-property (reify @@ -306,12 +288,6 @@ (unit [_] ChronoUnit/SECONDS) (field [_] ChronoField/SECOND_OF_MINUTE))) -(defmethod - print-method - (type seconds-property) - [_ ^java.io.Writer w] - (print-simple "seconds-property" w)) - (def minutes-property (reify @@ -321,12 +297,6 @@ (unit [_] ChronoUnit/MINUTES) (field [_] ChronoField/MINUTE_OF_HOUR))) -(defmethod - print-method - (type minutes-property) - [_ ^java.io.Writer w] - (print-simple "minutes-property" w)) - (def hours-property (reify @@ -336,12 +306,6 @@ (unit [_] ChronoUnit/HOURS) (field [_] ChronoField/HOUR_OF_DAY))) -(defmethod - print-method - (type hours-property) - [_ ^java.io.Writer w] - (print-simple "hours-property" w)) - (def days-property (reify @@ -351,12 +315,6 @@ (unit [_] ChronoUnit/DAYS) (field [_] ChronoField/DAY_OF_MONTH))) -(defmethod - print-method - (type days-property) - [_ ^java.io.Writer w] - (print-simple "days-property" w)) - (def months-property (reify @@ -366,12 +324,6 @@ (unit [_] ChronoUnit/MONTHS) (field [_] ChronoField/MONTH_OF_YEAR))) -(defmethod - print-method - (type months-property) - [_ ^java.io.Writer w] - (print-simple "months-property" w)) - (def years-property (reify @@ -381,12 +333,6 @@ (unit [_] ChronoUnit/YEARS) (field [_] ChronoField/YEAR))) -(defmethod - print-method - (type years-property) - [_ ^java.io.Writer w] - (print-simple "years-property" w)) - (def ^{:dynamic true} *block-non-commutative-operations* true) (defn @@ -397,7 +343,7 @@ (assert (not (and - (contains? #{months-property years-property} temporal) + (contains? #{months-property years-property} temporal-property) (not (or (monthday? temporal) (yearmonth? temporal))))) "shifting by years or months yields odd results depending on input. intead shift a year-month, then set non-yearmonth parts"))) @@ -503,39 +449,39 @@ (defn zdt->date [^ZonedDateTime foo] (-> foo .toLocalDate)) -(defn date->month [^LocalDate foo] (-> foo .getMonthValue)) +(defn zdt->month [^ZonedDateTime foo] (-> foo .getMonthValue)) -(defn - datetime->day-of-month - [^LocalDateTime foo] - (-> foo .getDayOfMonth)) +(defn yearmonth->month [^YearMonth foo] (-> foo .getMonthValue)) -(defn monthday->month [^MonthDay foo] (-> foo .getMonthValue)) +(defn yearmonth->year [^YearMonth foo] (-> foo .getYear)) + +(defn zdt->day-of-month [^ZonedDateTime foo] (-> foo .getDayOfMonth)) (defn zdt->millisecond [^ZonedDateTime foo] (-> foo (-> -millisecond))) (defn zdt->nanosecond [^ZonedDateTime foo] (-> foo (-> -nanosecond))) +(defn monthday->month [^MonthDay foo] (-> foo .getMonthValue)) + (defn zdt->second [^ZonedDateTime foo] (-> foo .getSecond)) (defn datetime->minute [^LocalDateTime foo] (-> foo .getMinute)) +(defn monthday->day-of-month [^MonthDay foo] (-> foo .getDayOfMonth)) + (defn zdt->hour [^ZonedDateTime foo] (-> foo .getHour)) (defn zdt->instant [^ZonedDateTime foo] (-> foo .toInstant)) -(defn datetime->month [^LocalDateTime foo] (-> foo .getMonthValue)) - -(defn datetime->year [^LocalDateTime foo] (-> foo .getYear)) +(defn + datetime->day-of-month + [^LocalDateTime foo] + (-> foo .getDayOfMonth)) (defn datetime->date [^LocalDateTime foo] (-> foo .toLocalDate)) (defn zdt->microsecond [^ZonedDateTime foo] (-> foo (-> -microsecond))) -(defn zdt->month [^ZonedDateTime foo] (-> foo .getMonthValue)) - -(defn zdt->day-of-month [^ZonedDateTime foo] (-> foo .getDayOfMonth)) - (defn zdt->day-of-week [^ZonedDateTime foo] @@ -543,21 +489,32 @@ (defn time->second [^LocalTime foo] (-> foo .getSecond)) -(defn date->day-of-month [^LocalDate foo] (-> foo .getDayOfMonth)) +(defn + instant->legacydate + [^Instant foo] + (-> foo (-> (.toEpochMilli) (java.util.Date.)))) + +(defn date->month [^LocalDate foo] (-> foo .getMonthValue)) (defn datetime->nanosecond [^LocalDateTime foo] (-> foo (-> -nanosecond))) +(defn instant->epochmilli [^Instant foo] (-> foo .toEpochMilli)) + (defn datetime->hour [^LocalDateTime foo] (-> foo .getHour)) +(defn date->day-of-month [^LocalDate foo] (-> foo .getDayOfMonth)) + (defn zdt->datetime [^ZonedDateTime foo] (-> foo .toLocalDateTime)) (defn time->microsecond [^LocalTime foo] (-> foo (-> -microsecond))) (defn time->nanosecond [^LocalTime foo] (-> foo (-> -nanosecond))) +(defn datetime->year [^LocalDateTime foo] (-> foo .getYear)) + (defn time->minute [^LocalTime foo] (-> foo .getMinute)) (defn time->hour [^LocalTime foo] (-> foo .getHour)) @@ -578,23 +535,12 @@ (defn zdt->minute [^ZonedDateTime foo] (-> foo .getMinute)) -(defn monthday->day-of-month [^MonthDay foo] (-> foo .getDayOfMonth)) - (defn datetime->millisecond [^LocalDateTime foo] (-> foo (-> -millisecond))) -(defn - instant->legacydate - [^Instant foo] - (-> foo (-> (.toEpochMilli) (java.util.Date.)))) - -(defn instant->epochmilli [^Instant foo] (-> foo .toEpochMilli)) - -(defn yearmonth->year [^YearMonth foo] (-> foo .getYear)) - -(defn yearmonth->month [^YearMonth foo] (-> foo .getMonthValue)) +(defn datetime->month [^LocalDateTime foo] (-> foo .getMonthValue)) (defn time->millisecond [^LocalTime foo] (-> foo (-> -millisecond))) @@ -612,12 +558,12 @@ (defn date-parse [^java.lang.String foo] (LocalDate/parse foo)) -(defn time-parse [^java.lang.String foo] (LocalTime/parse foo)) - (defn monthday-parse [^java.lang.String foo] (MonthDay/parse foo)) (defn yearmonth-parse [^java.lang.String foo] (YearMonth/parse foo)) +(defn time-parse [^java.lang.String foo] (LocalTime/parse foo)) + ^{:line 35, :column 9} (comment "nowers") (defn zdt-now ([^java.time.Clock clock] (ZonedDateTime/now clock))) @@ -626,14 +572,14 @@ (defn date-now ([^java.time.Clock clock] (LocalDate/now clock))) -(defn time-now ([^java.time.Clock clock] (LocalTime/now clock))) - -(defn instant-now ([^java.time.Clock clock] (Instant/now clock))) - (defn monthday-now ([^java.time.Clock clock] (MonthDay/now clock))) (defn yearmonth-now ([^java.time.Clock clock] (YearMonth/now clock))) +(defn time-now ([^java.time.Clock clock] (LocalTime/now clock))) + +(defn instant-now ([^java.time.Clock clock] (Instant/now clock))) + ^{:line 37, :column 9} (comment "constructors") (defn diff --git a/src/com/widdindustries/tempo.cljs b/src/com/widdindustries/tempo.cljs index 7159bac..bc51706 100644 --- a/src/com/widdindustries/tempo.cljs +++ b/src/com/widdindustries/tempo.cljs @@ -6,7 +6,8 @@ [com.widdindustries.tempo.cljs-protocols :as cljs-protocols] [com.widdindustries.tempo.js-temporal-entities :as entities] [com.widdindustries.tempo.js-temporal-methods :as methods] - [com.widdindustries.tempo.clock :as clock])) + [com.widdindustries.tempo.clock :as clock] + [goog.object])) (defn extend-all-cljs-protocols [] (cljs-protocols/extend-all)) @@ -54,12 +55,14 @@ clock-offset-millis [clock offset-millis] (clock/clock - (fn [] (.add (.instant clock) (js-obj "milliseconds" offset-millis))) - (clock/timezone clock))) + (fn + [] + (.add (.instant ^js clock) (js-obj "milliseconds" offset-millis))) + (clock/timezone_id clock))) -(defn timezone-now ([clock] (clock/timezone clock))) +(defn timezone-now ([clock] (clock/timezone_id clock))) -(defn legacydate->instant [d] (.toTemporalInstant d)) +(defn legacydate->instant [d] (.toTemporalInstant ^js d)) (defn greater [x y] (if (neg? (compare x y)) y x)) @@ -69,7 +72,7 @@ [arg & args] (assert (every? some? (cons arg args))) (reduce - (fn* [p1__36903# p2__36904#] (greater p1__36903# p2__36904#)) + (fn* [p1__137960# p2__137961#] (greater p1__137960# p2__137961#)) arg args)) @@ -81,7 +84,7 @@ [arg & args] (assert (every? some? (cons arg args))) (reduce - (fn* [p1__36905# p2__36906#] (lesser p1__36905# p2__36906#)) + (fn* [p1__137962# p2__137963#] (lesser p1__137962# p2__137963#)) arg args)) @@ -150,22 +153,9 @@ (defprotocol Property (unit [_]) (field [_])) -(defn - -millisecond - [f] - (-> (getFractional f) (Duration/ofNanos) (.toMillisPart))) - -(defn -microsecond [f] (-> (getFractional f) (/ 1000) long (mod 1000))) - -(defn -nanosecond [f] (-> (getFractional f) (mod 1000))) - -(def ^ValueRange sub-second-range (ValueRange/of 0 999)) - (def nanoseconds-property (reify - object - (toString [_] "nanoseconds-property") Property (field [_] "nanosecond") (unit @@ -174,19 +164,11 @@ Unit (unit-amount [_] "nanoseconds") (unit-field [_] "nanosecond") - (unit-accessor [_ ^js x] (.-nanos x)))))) - -(defmethod - print-method - (type nanoseconds-property) - [_ ^java.io.Writer w] - (print-simple "nanoseconds-property" w)) + (unit-accessor [_ ^js x] (.-nanoseconds x)))))) (def microseconds-property (reify - object - (toString [_] "microseconds-property") Property (field [_] "microsecond") (unit @@ -195,19 +177,11 @@ Unit (unit-amount [_] "microseconds") (unit-field [_] "microsecond") - (unit-accessor [_ ^js x] (.-micros x)))))) - -(defmethod - print-method - (type microseconds-property) - [_ ^java.io.Writer w] - (print-simple "microseconds-property" w)) + (unit-accessor [_ ^js x] (.-microseconds x)))))) (def milliseconds-property (reify - object - (toString [_] "milliseconds-property") Property (field [_] "millisecond") (unit @@ -216,19 +190,11 @@ Unit (unit-amount [_] "milliseconds") (unit-field [_] "millisecond") - (unit-accessor [_ ^js x] (.-millis x)))))) - -(defmethod - print-method - (type milliseconds-property) - [_ ^java.io.Writer w] - (print-simple "milliseconds-property" w)) + (unit-accessor [_ ^js x] (.-milliseconds x)))))) (def seconds-property (reify - object - (toString [_] "seconds-property") Property (field [_] "second") (unit @@ -239,17 +205,9 @@ (unit-field [_] "second") (unit-accessor [_ ^js x] (.-seconds x)))))) -(defmethod - print-method - (type seconds-property) - [_ ^java.io.Writer w] - (print-simple "seconds-property" w)) - (def minutes-property (reify - object - (toString [_] "minutes-property") Property (field [_] "minute") (unit @@ -260,17 +218,9 @@ (unit-field [_] "minute") (unit-accessor [_ ^js x] (.-minutes x)))))) -(defmethod - print-method - (type minutes-property) - [_ ^java.io.Writer w] - (print-simple "minutes-property" w)) - (def hours-property (reify - object - (toString [_] "hours-property") Property (field [_] "hour") (unit @@ -281,17 +231,9 @@ (unit-field [_] "hour") (unit-accessor [_ ^js x] (.-hours x)))))) -(defmethod - print-method - (type hours-property) - [_ ^java.io.Writer w] - (print-simple "hours-property" w)) - (def days-property (reify - object - (toString [_] "days-property") Property (field [_] "day") (unit @@ -302,17 +244,9 @@ (unit-field [_] "day") (unit-accessor [_ ^js x] (.-days x)))))) -(defmethod - print-method - (type days-property) - [_ ^java.io.Writer w] - (print-simple "days-property" w)) - (def months-property (reify - object - (toString [_] "months-property") Property (field [_] "month") (unit @@ -323,17 +257,9 @@ (unit-field [_] "month") (unit-accessor [_ ^js x] (.-months x)))))) -(defmethod - print-method - (type months-property) - [_ ^java.io.Writer w] - (print-simple "months-property" w)) - (def years-property (reify - object - (toString [_] "years-property") Property (field [_] "year") (unit @@ -344,12 +270,6 @@ (unit-field [_] "year") (unit-accessor [_ ^js x] (.-years x)))))) -(defmethod - print-method - (type years-property) - [_ ^java.io.Writer w] - (print-simple "years-property" w)) - (def ^{:dynamic true} *block-non-commutative-operations* true) (defn @@ -360,7 +280,7 @@ (assert (not (and - (contains? #{months-property years-property} temporal) + (contains? #{months-property years-property} temporal-property) (not (or (monthday? temporal) (yearmonth? temporal))))) "shifting by years or months yields odd results depending on input. intead shift a year-month, then set non-yearmonth parts"))) @@ -376,16 +296,17 @@ (defn until [v1 v2 property] - (-> - (.until - ^js v1 - ^js v2 - (js-obj - "smallestUnit" - (unit-field (unit property)) - "largestUnit" - (unit-field (unit property)))) - (unit-accessor (unit property)))) + (unit-accessor + (unit property) + (-> + (.until + ^js v1 + ^js v2 + (js-obj + "smallestUnit" + (unit-field (unit property)) + "largestUnit" + (unit-field (unit property))))))) (defn >> @@ -445,23 +366,21 @@ (defprotocol JavaTruncateable (-truncate [_ unit])) -(extend-protocol - JavaTruncateable - ZonedDateTime - (-truncate [zdt unit] (.truncatedTo zdt unit)) - LocalDateTime - (-truncate [zdt unit] (.truncatedTo zdt unit)) - LocalTime - (-truncate [zdt unit] (.truncatedTo zdt unit)) - Instant - (-truncate [zdt unit] (.truncatedTo zdt unit))) - (defn truncate [temporal property] - (.roundTo temporal (unit-field (unit property)))) + (.round + ^js temporal + (js-obj + "smallestUnit" + (unit-field (unit property)) + "roundingMode" + "trunc"))) -(defn get-field [temporal property] (unit-accessor property temporal)) +(defn + get-field + [temporal property] + (goog.object/get temporal (field property))) ^{:line 31, :column 9} (comment "accessors") @@ -484,14 +403,19 @@ (defn zdt->date [^js/Temporal.ZonedDateTime foo] (-> foo .toPlainDate)) -(defn date->month [^js/Temporal.PlainDate foo] (-> foo .-month)) +(defn + zdt->month + [^js/Temporal.ZonedDateTime foo] + (-> foo (-> (.-monthCode) (subs 1 3) js/parseInt))) (defn - datetime->day-of-month - [^js/Temporal.PlainDateTime foo] - (-> foo .-day)) + yearmonth->month + [^js/Temporal.PlainYearMonth foo] + (-> foo .-month)) -(defn monthday->month [^js/Temporal.PlainMonthDay foo] (-> foo .-month)) +(defn yearmonth->year [^js/Temporal.PlainYearMonth foo] (-> foo .-year)) + +(defn zdt->day-of-month [^js/Temporal.ZonedDateTime foo] (-> foo .-day)) (defn zdt->millisecond @@ -503,6 +427,11 @@ [^js/Temporal.ZonedDateTime foo] (-> foo .-nanosecond)) +(defn + monthday->month + [^js/Temporal.PlainMonthDay foo] + (-> foo (-> (.-monthCode) (subs 1 3) js/parseInt))) + (defn zdt->second [^js/Temporal.ZonedDateTime foo] (-> foo .-second)) (defn @@ -510,13 +439,19 @@ [^js/Temporal.PlainDateTime foo] (-> foo .-minute)) +(defn + monthday->day-of-month + [^js/Temporal.PlainMonthDay foo] + (-> foo .-day)) + (defn zdt->hour [^js/Temporal.ZonedDateTime foo] (-> foo .-hour)) (defn zdt->instant [^js/Temporal.ZonedDateTime foo] (-> foo .toInstant)) -(defn datetime->month [^js/Temporal.PlainDateTime foo] (-> foo .-month)) - -(defn datetime->year [^js/Temporal.PlainDateTime foo] (-> foo .-year)) +(defn + datetime->day-of-month + [^js/Temporal.PlainDateTime foo] + (-> foo .-day)) (defn datetime->date @@ -528,10 +463,6 @@ [^js/Temporal.ZonedDateTime foo] (-> foo .-microsecond)) -(defn zdt->month [^js/Temporal.ZonedDateTime foo] (-> foo .-month)) - -(defn zdt->day-of-month [^js/Temporal.ZonedDateTime foo] (-> foo .-day)) - (defn zdt->day-of-week [^js/Temporal.ZonedDateTime foo] @@ -539,15 +470,27 @@ (defn time->second [^js/Temporal.PlainTime foo] (-> foo .-second)) -(defn date->day-of-month [^js/Temporal.PlainDate foo] (-> foo .-day)) +(defn + instant->legacydate + [^js/Temporal.Instant foo] + (-> foo (-> (.-epochMilliseconds) (js/Date.)))) + +(defn date->month [^js/Temporal.PlainDate foo] (-> foo .-month)) (defn datetime->nanosecond [^js/Temporal.PlainDateTime foo] (-> foo .-nanosecond)) +(defn + instant->epochmilli + [^js/Temporal.Instant foo] + (-> foo (-> (.-epochMilliseconds)))) + (defn datetime->hour [^js/Temporal.PlainDateTime foo] (-> foo .-hour)) +(defn date->day-of-month [^js/Temporal.PlainDate foo] (-> foo .-day)) + (defn zdt->datetime [^js/Temporal.ZonedDateTime foo] @@ -563,6 +506,8 @@ [^js/Temporal.PlainTime foo] (-> foo .-nanosecond)) +(defn datetime->year [^js/Temporal.PlainDateTime foo] (-> foo .-year)) + (defn time->minute [^js/Temporal.PlainTime foo] (-> foo .-minute)) (defn time->hour [^js/Temporal.PlainTime foo] (-> foo .-hour)) @@ -583,32 +528,12 @@ (defn zdt->minute [^js/Temporal.ZonedDateTime foo] (-> foo .-minute)) -(defn - monthday->day-of-month - [^js/Temporal.PlainMonthDay foo] - (-> foo .-day)) - (defn datetime->millisecond [^js/Temporal.PlainDateTime foo] (-> foo .-millisecond)) -(defn - instant->legacydate - [^js/Temporal.Instant foo] - (-> foo (-> (.-epochMilliseconds) (Date.)))) - -(defn - instant->epochmilli - [^js/Temporal.Instant foo] - (-> foo .-epochmilli)) - -(defn yearmonth->year [^js/Temporal.PlainYearMonth foo] (-> foo .-year)) - -(defn - yearmonth->month - [^js/Temporal.PlainYearMonth foo] - (-> foo .-month)) +(defn datetime->month [^js/Temporal.PlainDateTime foo] (-> foo .-month)) (defn time->millisecond @@ -647,11 +572,6 @@ [^java.lang.String foo] (js/Temporal.PlainDate.from foo)) -(defn - time-parse - [^java.lang.String foo] - (js/Temporal.PlainTime.from foo)) - (defn monthday-parse [^java.lang.String foo] @@ -662,6 +582,11 @@ [^java.lang.String foo] (js/Temporal.PlainYearMonth.from foo)) +(defn + time-parse + [^java.lang.String foo] + (js/Temporal.PlainTime.from foo)) + ^{:line 35, :column 9} (comment "nowers") (defn zdt-now ([^java.time.Clock clock] (clock/zdt clock))) @@ -670,14 +595,14 @@ (defn date-now ([^java.time.Clock clock] (clock/date clock))) -(defn time-now ([^java.time.Clock clock] (clock/time clock))) - -(defn instant-now ([^java.time.Clock clock] (clock/instant clock))) - (defn monthday-now ([^java.time.Clock clock] (clock/monthday clock))) (defn yearmonth-now ([^java.time.Clock clock] (clock/yearmonth clock))) +(defn time-now ([^java.time.Clock clock] (clock/time clock))) + +(defn instant-now ([^java.time.Clock clock] (clock/instant clock))) + ^{:line 37, :column 9} (comment "constructors") (defn @@ -751,7 +676,7 @@ (get thing :timezone_id)] (if instant - (.toZonedDateTimeISO ^js instant (ZoneId/of zone)) + (.toZonedDateTimeISO ^js instant zone) (.toZonedDateTime ^js ldt zone)))) (defn diff --git a/src/com/widdindustries/tempo/clock.cljs b/src/com/widdindustries/tempo/clock.cljs index c671d27..03113c5 100644 --- a/src/com/widdindustries/tempo/clock.cljs +++ b/src/com/widdindustries/tempo/clock.cljs @@ -14,7 +14,7 @@ (tm/instant->plain-date-iso (instant-fn) zone)) :plainTimeISO (fn [] (tm/instant->plain-time-iso (instant-fn) zone)) - :timeZone (fn [] (tm/tz-from zone)) + :timeZoneId (fn [] zone) :zonedDateTimeISO (fn [] (tm/->zdt-iso (instant-fn) zone))}) @@ -36,8 +36,8 @@ (defn time ([^js clock] (.plainTimeISO clock))) -(defn timezone - ([^js clock] (.timeZone clock))) +(defn timezone_id + ([^js clock] (.timeZoneId clock))) (defn zdt ([^js clock] (.zonedDateTimeISO clock))) \ No newline at end of file diff --git a/test/com/widdindustries/tempo_gen_test.cljc b/test/com/widdindustries/tempo_gen_test.cljc index 6811c79..61fb7f3 100644 --- a/test/com/widdindustries/tempo_gen_test.cljc +++ b/test/com/widdindustries/tempo_gen_test.cljc @@ -28,18 +28,6 @@ [now-now (t/date-now (t/clock-system-default-zone))] (is (= now-now (-> now-now str t/date-parse))))) -(deftest - time-parse-test - (let - [now-now (t/time-now (t/clock-system-default-zone))] - (is (= now-now (-> now-now str t/time-parse))))) - -(deftest - instant-parse-test - (let - [now-now (t/instant-now (t/clock-system-default-zone))] - (is (= now-now (-> now-now str t/instant-parse))))) - (deftest monthday-parse-test (let @@ -52,6 +40,18 @@ [now-now (t/yearmonth-now (t/clock-system-default-zone))] (is (= now-now (-> now-now str t/yearmonth-parse))))) +(deftest + time-parse-test + (let + [now-now (t/time-now (t/clock-system-default-zone))] + (is (= now-now (-> now-now str t/time-parse))))) + +(deftest + instant-parse-test + (let + [now-now (t/instant-now (t/clock-system-default-zone))] + (is (= now-now (-> now-now str t/instant-parse))))) + ^{:line 55, :column 9} (comment "nowers") (deftest @@ -118,10 +118,10 @@ (is (t/> now-clock-2 now-clock-1)))) (deftest - time-now-test + monthday-now-test (let - [now-now (t/time-now (t/clock-system-default-zone))] - (is (t/time? now-now))) + [now-now (t/monthday-now (t/clock-system-default-zone))] + (is (t/monthday? now-now))) (let [clock-1 (t/clock-fixed @@ -132,17 +132,17 @@ (t/instant-parse "1955-12-02T17:46:08.017143Z") (str (t/timezone-now (t/clock-system-default-zone)))) now-clock-1 - (t/time-now clock-1) + (t/monthday-now clock-1) now-clock-2 - (t/time-now clock-2)] - (is (t/time? now-clock-1)) + (t/monthday-now clock-2)] + (is (t/monthday? now-clock-1)) (is (t/> now-clock-2 now-clock-1)))) (deftest - instant-now-test + yearmonth-now-test (let - [now-now (t/instant-now (t/clock-system-default-zone))] - (is (t/instant? now-now))) + [now-now (t/yearmonth-now (t/clock-system-default-zone))] + (is (t/yearmonth? now-now))) (let [clock-1 (t/clock-fixed @@ -153,17 +153,17 @@ (t/instant-parse "1955-12-02T17:46:08.017143Z") (str (t/timezone-now (t/clock-system-default-zone)))) now-clock-1 - (t/instant-now clock-1) + (t/yearmonth-now clock-1) now-clock-2 - (t/instant-now clock-2)] - (is (t/instant? now-clock-1)) + (t/yearmonth-now clock-2)] + (is (t/yearmonth? now-clock-1)) (is (t/> now-clock-2 now-clock-1)))) (deftest - monthday-now-test + time-now-test (let - [now-now (t/monthday-now (t/clock-system-default-zone))] - (is (t/monthday? now-now))) + [now-now (t/time-now (t/clock-system-default-zone))] + (is (t/time? now-now))) (let [clock-1 (t/clock-fixed @@ -174,17 +174,17 @@ (t/instant-parse "1955-12-02T17:46:08.017143Z") (str (t/timezone-now (t/clock-system-default-zone)))) now-clock-1 - (t/monthday-now clock-1) + (t/time-now clock-1) now-clock-2 - (t/monthday-now clock-2)] - (is (t/monthday? now-clock-1)) + (t/time-now clock-2)] + (is (t/time? now-clock-1)) (is (t/> now-clock-2 now-clock-1)))) (deftest - yearmonth-now-test + instant-now-test (let - [now-now (t/yearmonth-now (t/clock-system-default-zone))] - (is (t/yearmonth? now-now))) + [now-now (t/instant-now (t/clock-system-default-zone))] + (is (t/instant? now-now))) (let [clock-1 (t/clock-fixed @@ -195,10 +195,10 @@ (t/instant-parse "1955-12-02T17:46:08.017143Z") (str (t/timezone-now (t/clock-system-default-zone)))) now-clock-1 - (t/yearmonth-now clock-1) + (t/instant-now clock-1) now-clock-2 - (t/yearmonth-now clock-2)] - (is (t/yearmonth? now-clock-1)) + (t/instant-now clock-2)] + (is (t/instant? now-clock-1)) (is (t/> now-clock-2 now-clock-1)))) ^{:line 57, :column 9} (comment "accessors") @@ -208,10 +208,7 @@ (let [now-now (t/zdt-now (t/clock-system-default-zone))] (is (int? (t/zdt->year now-now))) - (is - (= - now-now - (t/with now-now (t/zdt->year now-now) t/years-property))))) + nil)) (deftest datetime->day-of-week @@ -251,24 +248,31 @@ nil)) (deftest - date->month + zdt->month (let - [now-now (t/date-now (t/clock-system-default-zone))] - (is (int? (t/date->month now-now))) + [now-now (t/zdt-now (t/clock-system-default-zone))] + (is (int? (t/zdt->month now-now))) nil)) (deftest - datetime->day-of-month + yearmonth->month (let - [now-now (t/datetime-now (t/clock-system-default-zone))] - (is (int? (t/datetime->day-of-month now-now))) + [now-now (t/yearmonth-now (t/clock-system-default-zone))] + (is (int? (t/yearmonth->month now-now))) nil)) (deftest - monthday->month + yearmonth->year (let - [now-now (t/monthday-now (t/clock-system-default-zone))] - (is (int? (t/monthday->month now-now))) + [now-now (t/yearmonth-now (t/clock-system-default-zone))] + (is (int? (t/yearmonth->year now-now))) + nil)) + +(deftest + zdt->day-of-month + (let + [now-now (t/zdt-now (t/clock-system-default-zone))] + (is (int? (t/zdt->day-of-month now-now))) nil)) (deftest @@ -297,6 +301,13 @@ (t/zdt->nanosecond now-now) t/nanoseconds-property))))) +(deftest + monthday->month + (let + [now-now (t/monthday-now (t/clock-system-default-zone))] + (is (int? (t/monthday->month now-now))) + nil)) + (deftest zdt->second (let @@ -317,6 +328,13 @@ now-now (t/with now-now (t/datetime->minute now-now) t/minutes-property))))) +(deftest + monthday->day-of-month + (let + [now-now (t/monthday-now (t/clock-system-default-zone))] + (is (int? (t/monthday->day-of-month now-now))) + nil)) + (deftest zdt->hour (let @@ -335,22 +353,12 @@ nil)) (deftest - datetime->month + datetime->day-of-month (let [now-now (t/datetime-now (t/clock-system-default-zone))] - (is (int? (t/datetime->month now-now))) + (is (int? (t/datetime->day-of-month now-now))) nil)) -(deftest - datetime->year - (let - [now-now (t/datetime-now (t/clock-system-default-zone))] - (is (int? (t/datetime->year now-now))) - (is - (= - now-now - (t/with now-now (t/datetime->year now-now) t/years-property))))) - (deftest datetime->date (let @@ -371,20 +379,6 @@ (t/zdt->microsecond now-now) t/microseconds-property))))) -(deftest - zdt->month - (let - [now-now (t/zdt-now (t/clock-system-default-zone))] - (is (int? (t/zdt->month now-now))) - nil)) - -(deftest - zdt->day-of-month - (let - [now-now (t/zdt-now (t/clock-system-default-zone))] - (is (int? (t/zdt->day-of-month now-now))) - nil)) - (deftest zdt->day-of-week (let @@ -403,10 +397,17 @@ (t/with now-now (t/time->second now-now) t/seconds-property))))) (deftest - date->day-of-month + instant->legacydate + (let + [now-now (t/instant-now (t/clock-system-default-zone))] + (is (t/legacydate? (t/instant->legacydate now-now))) + nil)) + +(deftest + date->month (let [now-now (t/date-now (t/clock-system-default-zone))] - (is (int? (t/date->day-of-month now-now))) + (is (int? (t/date->month now-now))) nil)) (deftest @@ -422,6 +423,13 @@ (t/datetime->nanosecond now-now) t/nanoseconds-property))))) +(deftest + instant->epochmilli + (let + [now-now (t/instant-now (t/clock-system-default-zone))] + (is (int? (t/instant->epochmilli now-now))) + nil)) + (deftest datetime->hour (let @@ -432,6 +440,13 @@ now-now (t/with now-now (t/datetime->hour now-now) t/hours-property))))) +(deftest + date->day-of-month + (let + [now-now (t/date-now (t/clock-system-default-zone))] + (is (int? (t/date->day-of-month now-now))) + nil)) + (deftest zdt->datetime (let @@ -465,6 +480,13 @@ (t/time->nanosecond now-now) t/nanoseconds-property))))) +(deftest + datetime->year + (let + [now-now (t/datetime-now (t/clock-system-default-zone))] + (is (int? (t/datetime->year now-now))) + nil)) + (deftest time->minute (let @@ -504,10 +526,7 @@ (let [now-now (t/date-now (t/clock-system-default-zone))] (is (int? (t/date->year now-now))) - (is - (= - now-now - (t/with now-now (t/date->year now-now) t/years-property))))) + nil)) (deftest date->day-of-week @@ -526,13 +545,6 @@ now-now (t/with now-now (t/zdt->minute now-now) t/minutes-property))))) -(deftest - monthday->day-of-month - (let - [now-now (t/monthday-now (t/clock-system-default-zone))] - (is (int? (t/monthday->day-of-month now-now))) - nil)) - (deftest datetime->millisecond (let @@ -547,34 +559,10 @@ t/milliseconds-property))))) (deftest - instant->legacydate - (let - [now-now (t/instant-now (t/clock-system-default-zone))] - (is (t/legacydate? (t/instant->legacydate now-now))) - nil)) - -(deftest - instant->epochmilli - (let - [now-now (t/instant-now (t/clock-system-default-zone))] - (is (int? (t/instant->epochmilli now-now))) - nil)) - -(deftest - yearmonth->year - (let - [now-now (t/yearmonth-now (t/clock-system-default-zone))] - (is (int? (t/yearmonth->year now-now))) - (is - (= - now-now - (t/with now-now (t/yearmonth->year now-now) t/years-property))))) - -(deftest - yearmonth->month + datetime->month (let - [now-now (t/yearmonth-now (t/clock-system-default-zone))] - (is (int? (t/yearmonth->month now-now))) + [now-now (t/datetime-now (t/clock-system-default-zone))] + (is (int? (t/datetime->month now-now))) nil)) (deftest diff --git a/test/com/widdindustries/tempo_test.cljc b/test/com/widdindustries/tempo_test.cljc index 3a58a94..3270b03 100644 --- a/test/com/widdindustries/tempo_test.cljc +++ b/test/com/widdindustries/tempo_test.cljc @@ -130,7 +130,8 @@ (deftest prop-test (let [combos [[t/instant-now [t/nanoseconds-property t/microseconds-property t/milliseconds-property ; not days - mistakenly assumed to be 24hr in java.time - ] [t/seconds-property t/hours-property]] + ] ;[t/seconds-property t/hours-property] + ] [t/zdt-now [t/nanoseconds-property t/microseconds-property t/milliseconds-property t/seconds-property t/hours-property t/days-property ;t/months-property t/years-property @@ -149,18 +150,21 @@ (let [i-1 (now (t/clock-system-default-zone)) i-2 (-> i-1 (t/>> 1 shiftable-prop))] - (testing (str "shift until" now " by " shiftable-prop) + (testing (str "shift until" i-1 " by " #?(:clj shiftable-prop :cljs (t/unit-field (t/unit shiftable-prop)))) (is (= 1 (t/until i-1 i-2 shiftable-prop))) (is (= -1 (t/until i-2 i-1 shiftable-prop)))))) (doseq [[now props] combos withable-prop props] (let [i-1 (now (t/clock-system-default-zone)) current (t/get-field i-1 withable-prop)] - (testing (str "with " i-1 " prop " (str withable-prop)) - (is (not= i-1 (t/with i-1 (if (= 1 current) 2 1) withable-prop)) (str i-1 " " withable-prop))))))) + (when-not (t/instant? i-1) + (testing (str "with " i-1 " prop " (str withable-prop) " current " current + " " #?(:cljs (t/unit-field (t/unit withable-prop)))) + (is (not= i-1 (t/with i-1 (if (= 1 current) 2 1) withable-prop)) (str i-1 " " withable-prop)))))))) -; clock tests +;(t/get-field (t/zdt-now (t/clock-system-default-zone)) t/days-property) + (deftest clock-test (let [zone (str (t/timezone-now (t/clock-system-default-zone))) now (t/instant-now (t/clock-system-default-zone)) @@ -229,7 +233,7 @@ (deftest truncate-test - (doseq [[temporal props] [[(t/zdt-parse "2020-02-02T09:19:42.123456789Z") [t/days-property t/hours-property t/minutes-property + (doseq [[temporal props] [[(t/zdt-parse "2020-02-02T09:19:42.123456789Z[Europe/London]") [t/days-property t/hours-property t/minutes-property t/seconds-property t/milliseconds-property t/microseconds-property t/nanoseconds-property]] [(t/datetime-parse "2020-02-02T09:19:42.123456789") [t/days-property t/hours-property t/minutes-property @@ -246,12 +250,16 @@ t/seconds-property t/milliseconds-property t/microseconds-property t/nanoseconds-property]] - (-> (t/zdt-parse "2020-02-02T09:19:42.123456789Z") + #_(-> (t/zdt-parse "2020-02-02T09:19:42.123456789Z") (t/truncate t/microseconds-property) (t/get-field t/microseconds-property)) ) ;todo +(deftest guardrails-test + (is (thrown? #?(:clj Throwable :cljs js/Error) (t/>> (t/date-parse "2020-02-02") 1 t/years-property)))) + + t/monthday-from t/yearmonth-from t/max