time util for Clojure(Script)
[time "0.2.6"]
(:require [time.core :as t])
;; current instant
(t/date)
(t/date millis-secs)
;; copy instant
(def d (t/date))
(t/date d)
;; copy and set time-zone
(d/date d :time-zone (t/time-zone "GMT+00:00"))
;; format use default time zone
(t/format "yyyyMMdd HH:mm:ss" (t/date))
(def zero-zone (t/time-zone "GMT+00:00"))
(def d (t/date (t/date) :time-zone zero-zone))
(t/format "yyyyMMdd HH:mm:ss" d) ; format with zero zone
;; yyyy-MM-dd => yyyy-MM-01
(t/begin-period [1 :month] (t/date))
;; two month as period
(t/begin-period [2 :month] (t/date))
;; one week as period & sunday is first day (default)
(t/begin-period [1 :week] (t/date))
;; one week as period & monday is first day
(t/begin-period
[1 :week]
(t/date current-millis :first-day-of-week :monday))
;; sunday as first day of week
(def week-seq
(t/period-seq :week (t/date)))
;; `[:1 week]` = `:week`
(def week-seq
(t/period-seq [1 :week] (t/date)))
;; use zero-zone, monday as first day, reverse traversal
;; last 15 week
(def week-seq
(->> (t/date current-millis
:first-day-of-week :monday
:time-zone zero-zone)
(t/period-seq [-1 :week])
(take 15)))
;; other periods: `:year`, `:month`, `:day`, `:hour`, `:minute`, `:second`
;; all periods support pair format exclude week, eg. `[1 :year]` = `:year`
(def quarter-seq
(t/period-seq
[15 :minute]
(t/date current-millss
:time-zone zero-zone)))
add-period is a low level api, period-seq implements vai `add-period` & `begin-period`, heare is an example of reverse traversal with `[-2 :week]` period:
(def double-week-seq
(iterate #(add-period % [-2 :week]) (begin-period :week (t/date))))
(:require [time.chime :as chime]
[clojure.core.async :as a :refer [<! go-loop]])
(let [ch (-> (t/date)
(t/period-seq :minute)
(chime/chime-ch))]
(go-loop [ch]
;; println at every begining of minutes
(when-let [p (<! ch)]
(println p (t/date))
(recur))))
(with-chime [ch (-> (t/date)
(t/period-seq :minute)
(chime/chime-ch))]
(println (t/date)))