Shifting forward:
(t/>> (t/now)
(t/new-duration 15 :minutes))
Shifting backward:
(t/<< (t/now)
(t/new-duration 10 :days))
The tick library lends itself to doing additions, subtractions and divisions of time chunks and durations. Below are some examples of how time can be treated as a quantity which can be operated on.
Note that the function to find the amount of time between two dates is t/between
. Using the legacy Date object it was common to get the epoch offset of two dates and subtract one from the other - so very much arithmetic. tick
however sticks to the java.time naming convention of between
. Also note that this epoch-offset arithmetic would only ever be applied to 2 dates, not 3, 4, 5 … For this reason, t/+
and t/-
only work for amounts of time, not dates, in that they can take more than 2 arguments, and so are analagous to their clojure.core equivalents.
Operating on an instant it will return another instant in time.
Shifting forward:
(t/>> (t/now)
(t/new-duration 15 :minutes))
Shifting backward:
(t/<< (t/now)
(t/new-duration 10 :days))
An interval has a beginning and an end, operating on it will return a modified interval.
Shifting interval end forward:
(t.i/extend {:tick/beginning (t/instant "2018-01-01T00:00:00.00Z")
:tick/end (t/instant "2018-01-10T00:00:00.00Z")}
(t/new-period 10 :weeks))
Shifting interval end backward:
(t.i/extend {:tick/beginning (t/instant "2018-01-01T00:00:00.00Z")
:tick/end (t/instant "2018-01-10T00:00:00.00Z")}
(t/new-duration -1 :days))
An interval can be divided into smaller intervals:
Divide the day by 24, to get hour long intervals:
(map #(apply t.i/new-interval %) (t.i/divide-by 24 {:tick/beginning (t/instant "2000-01-01T00:00:00.00Z") :tick/end (t/instant "2000-01-02T00:00:00.00Z")}))
Or just divide the day by a duration of 1 hour to get the same result:
(= (t.i/divide-by (t/new-duration 1 :hours)
{:tick/beginning (t/instant "2000-01-01T00:00:00.00Z")
:tick/end (t/instant "2000-01-02T00:00:00.00Z")})
(t.i/divide-by 24
{:tick/beginning (t/instant "2000-01-01T00:00:00.00Z")
:tick/end (t/instant "2000-01-02T00:00:00.00Z")}))
Durations can be treated like independent chunks of time. They can be extended, shrunk and divided.
Addition:
(t/+ (t/new-duration 1 :hours)
(t/new-duration 10 :minutes))
Subtraction:
(t/- (t/new-duration 1 :hours)
(t/new-duration 10 :minutes))
Division:
(t.i/divide (t/new-duration 1 :hours)
(t/new-duration 1 :minutes))
Can you improve this documentation? These fine people already did:
Henry Widd, Johanna Antonelli & Malcolm SparksEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close