mirror of
https://github.com/penpot/penpot.git
synced 2025-02-01 11:59:17 -05:00
✨ Reorganize util.time ns (on backend).
This commit is contained in:
parent
146394f3ca
commit
e446f47e2c
2 changed files with 84 additions and 72 deletions
|
@ -10,11 +10,15 @@
|
||||||
(ns app.util.time
|
(ns app.util.time
|
||||||
(:require
|
(:require
|
||||||
[app.common.exceptions :as ex]
|
[app.common.exceptions :as ex]
|
||||||
[clojure.spec.alpha :as s])
|
[clojure.spec.alpha :as s]
|
||||||
|
[cuerdas.core :as str])
|
||||||
(:import
|
(:import
|
||||||
java.time.Instant
|
java.time.Instant
|
||||||
java.time.Duration
|
java.time.Duration
|
||||||
java.util.Date
|
java.util.Date
|
||||||
|
java.time.ZonedDateTime
|
||||||
|
java.time.ZoneId
|
||||||
|
java.time.format.DateTimeFormatter
|
||||||
java.time.temporal.TemporalAmount
|
java.time.temporal.TemporalAmount
|
||||||
org.apache.logging.log4j.core.util.CronExpression))
|
org.apache.logging.log4j.core.util.CronExpression))
|
||||||
|
|
||||||
|
@ -22,39 +26,16 @@
|
||||||
;; Instant & Duration
|
;; Instant & Duration
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
(defn from-string
|
;; --- DURATION
|
||||||
[s]
|
|
||||||
{:pre [(string? s)]}
|
|
||||||
(Instant/parse s))
|
|
||||||
|
|
||||||
(defn instant?
|
|
||||||
[v]
|
|
||||||
(instance? Instant v))
|
|
||||||
|
|
||||||
(defn is-after?
|
|
||||||
[da db]
|
|
||||||
(.isAfter ^Instant da ^Instant db))
|
|
||||||
|
|
||||||
(defn is-before?
|
|
||||||
[da db]
|
|
||||||
(.isBefore ^Instant da ^Instant db))
|
|
||||||
|
|
||||||
(defn plus
|
|
||||||
[d ta]
|
|
||||||
(.plus d ^TemporalAmount ta))
|
|
||||||
|
|
||||||
(defn minus
|
|
||||||
[d ta]
|
|
||||||
(.minus d ^TemporalAmount ta))
|
|
||||||
|
|
||||||
(defn- obj->duration
|
(defn- obj->duration
|
||||||
[{:keys [days minutes seconds hours nanos millis]}]
|
[{:keys [days minutes seconds hours nanos millis]}]
|
||||||
(cond-> (Duration/ofMillis (if (int? millis) ^long millis 0))
|
(cond-> (Duration/ofMillis (if (int? millis) ^long millis 0))
|
||||||
(int? days) (.plusDays ^long days)
|
(int? days) (.plusDays ^long days)
|
||||||
(int? hours) (.plusHours ^long hours)
|
(int? hours) (.plusHours ^long hours)
|
||||||
(int? minutes) (.plusMinutes ^long minutes)
|
(int? minutes) (.plusMinutes ^long minutes)
|
||||||
(int? seconds) (.plusSeconds ^long seconds)
|
(int? seconds) (.plusSeconds ^long seconds)
|
||||||
(int? nanos) (.plusNanos ^long nanos)))
|
(int? nanos) (.plusNanos ^long nanos)))
|
||||||
|
|
||||||
(defn duration?
|
(defn duration?
|
||||||
[v]
|
[v]
|
||||||
|
@ -75,40 +56,10 @@
|
||||||
:else
|
:else
|
||||||
(obj->duration ms-or-obj)))
|
(obj->duration ms-or-obj)))
|
||||||
|
|
||||||
(defn now
|
|
||||||
[]
|
|
||||||
(Instant/now))
|
|
||||||
|
|
||||||
(defn in-future
|
|
||||||
[v]
|
|
||||||
(plus (now) (duration v)))
|
|
||||||
|
|
||||||
(defn in-past
|
|
||||||
[v]
|
|
||||||
(minus (now) (duration v)))
|
|
||||||
|
|
||||||
(defn duration-between
|
(defn duration-between
|
||||||
[t1 t2]
|
[t1 t2]
|
||||||
(Duration/between t1 t2))
|
(Duration/between t1 t2))
|
||||||
|
|
||||||
(defn instant
|
|
||||||
[ms]
|
|
||||||
(Instant/ofEpochMilli ms))
|
|
||||||
|
|
||||||
(defn parse-duration
|
|
||||||
[s]
|
|
||||||
(Duration/parse s))
|
|
||||||
|
|
||||||
(extend-protocol clojure.core/Inst
|
|
||||||
java.time.Duration
|
|
||||||
(inst-ms* [v] (.toMillis ^Duration v)))
|
|
||||||
|
|
||||||
(defmethod print-method Duration
|
|
||||||
[mv ^java.io.Writer writer]
|
|
||||||
(.write writer (str "#app/duration \"" (subs (str mv) 2) "\"")))
|
|
||||||
|
|
||||||
(defmethod print-dup Duration [o w]
|
|
||||||
(print-method o w))
|
|
||||||
|
|
||||||
(letfn [(conformer [v]
|
(letfn [(conformer [v]
|
||||||
(cond
|
(cond
|
||||||
|
@ -126,6 +77,80 @@
|
||||||
(subs (str v) 2))]
|
(subs (str v) 2))]
|
||||||
(s/def ::duration (s/conformer conformer unformer)))
|
(s/def ::duration (s/conformer conformer unformer)))
|
||||||
|
|
||||||
|
(extend-protocol clojure.core/Inst
|
||||||
|
java.time.Duration
|
||||||
|
(inst-ms* [v] (.toMillis ^Duration v)))
|
||||||
|
|
||||||
|
(defmethod print-method Duration
|
||||||
|
[mv ^java.io.Writer writer]
|
||||||
|
(.write writer (str "#app/duration \"" (str/lower (subs (str mv) 2)) "\"")))
|
||||||
|
|
||||||
|
(defmethod print-dup Duration [o w]
|
||||||
|
(print-method o w))
|
||||||
|
|
||||||
|
;; --- INSTANT
|
||||||
|
|
||||||
|
(defn instant
|
||||||
|
([s]
|
||||||
|
(if (int? s)
|
||||||
|
(Instant/ofEpochMilli s)
|
||||||
|
(Instant/parse s)))
|
||||||
|
([s fmt]
|
||||||
|
(case fmt
|
||||||
|
:rfc1123 (Instant/from (.parse DateTimeFormatter/RFC_1123_DATE_TIME ^String s))
|
||||||
|
:iso (Instant/from (.parse DateTimeFormatter/ISO_INSTANT ^String s))
|
||||||
|
:iso8601 (Instant/from (.parse DateTimeFormatter/ISO_INSTANT ^String s)))))
|
||||||
|
|
||||||
|
(defn instant?
|
||||||
|
[v]
|
||||||
|
(instance? Instant v))
|
||||||
|
|
||||||
|
(defn is-after?
|
||||||
|
[da db]
|
||||||
|
(.isAfter ^Instant da ^Instant db))
|
||||||
|
|
||||||
|
(defn is-before?
|
||||||
|
[da db]
|
||||||
|
(.isBefore ^Instant da ^Instant db))
|
||||||
|
|
||||||
|
(defn plus
|
||||||
|
[d ta]
|
||||||
|
(.plus d ^TemporalAmount (duration ta)))
|
||||||
|
|
||||||
|
(defn minus
|
||||||
|
[d ta]
|
||||||
|
(.minus d ^TemporalAmount (duration ta)))
|
||||||
|
|
||||||
|
(defn now
|
||||||
|
[]
|
||||||
|
(Instant/now))
|
||||||
|
|
||||||
|
(defn in-future
|
||||||
|
[v]
|
||||||
|
(plus (now) (duration v)))
|
||||||
|
|
||||||
|
(defn in-past
|
||||||
|
[v]
|
||||||
|
(minus (now) (duration v)))
|
||||||
|
|
||||||
|
(defn instant->zoned-date-time
|
||||||
|
[v]
|
||||||
|
(ZonedDateTime/ofInstant v (ZoneId/of "UTC")))
|
||||||
|
|
||||||
|
(defn format-instant
|
||||||
|
([v] (.format DateTimeFormatter/ISO_INSTANT ^Instant v))
|
||||||
|
([v fmt]
|
||||||
|
(case fmt
|
||||||
|
:iso (.format DateTimeFormatter/ISO_INSTANT ^Instant v)
|
||||||
|
:rfc1123 (.format DateTimeFormatter/RFC_1123_DATE_TIME
|
||||||
|
^ZonedDateTime (instant->zoned-date-time v)))))
|
||||||
|
|
||||||
|
(defmethod print-method Instant
|
||||||
|
[mv ^java.io.Writer writer]
|
||||||
|
(.write writer (str "#app/instant \"" (format-instant mv) "\"")))
|
||||||
|
|
||||||
|
(defmethod print-dup Instant [o w]
|
||||||
|
(print-method o w))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; Cron Expression
|
;; Cron Expression
|
||||||
|
@ -244,16 +269,3 @@
|
||||||
(defmethod print-dup CronExpression
|
(defmethod print-dup CronExpression
|
||||||
[o w]
|
[o w]
|
||||||
(print-ctor o (fn [o w] (print-dup (.toString ^CronExpression o) w)) w))
|
(print-ctor o (fn [o w] (print-dup (.toString ^CronExpression o) w)) w))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
||||||
;; Serialization
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
||||||
|
|
||||||
(defmethod print-method Instant
|
|
||||||
[mv ^java.io.Writer writer]
|
|
||||||
(.write writer (str "#app/instant \"" (.toString ^Instant mv) "\"")))
|
|
||||||
|
|
||||||
(defmethod print-dup Instant [o w]
|
|
||||||
(print-method o w))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
{app/instant app.util.time/from-string
|
{app/instant app.util.time/instant
|
||||||
app/cron app.util.time/cron
|
app/cron app.util.time/cron
|
||||||
app/duration app.util.time/duration}
|
app/duration app.util.time/duration}
|
||||||
|
|
Loading…
Add table
Reference in a new issue