Liking cljdoc? Tell your friends :D

Arithmetic

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.

Simple maths

Operating on an instant it will return another instant in time.

Addition:

(t/+ (t/now)
     (t/new-duration 15 :minutes))

Subtraction:

(t/- (t/now)
     (t/new-duration 10 :days))

An interval has a beginning and an end, operating on it will return a modified interval.

Addition:

(t/extend {:tick/beginning (t/instant "2018-01-01T00:00")
           :tick/end (t/instant "2018-01-10T00:00")}
  (t/new-period 10 :weeks))

Subtraction:

(t/extend {:tick/beginning (t/instant "2018-01-01T00:00")
           :tick/end (t/instant "2018-01-10T00:00")}
  (t/new-duration -1 :days))

This can be done with scale too:

(= (t/extend (t/today)
     (t/new-period 10 :weeks))
   (t/scale (t/today)
	    (t/new-period 10 :weeks)))

An interval can be divided into smaller intervals:

Divide the day by 24, to get hour long intervals:

(map #(apply t/new-interval %)
     (t/divide-by 24 {:tick/beginning (t/instant "2000-01-01T00:00")
                      :tick/end (t/instant "2000-01-02T00:00")}))

Or just divide the day by a duration of 1 hour to get the same result:

(= (t/divide-by (t/new-duration 1 :hours)
                {:tick/beginning (t/instant "2000-01-01T00:00")
                 :tick/end (t/instant "2000-01-02T00:00")})
   (t/divide-by 24
                {:tick/beginning (t/instant "2000-01-01T00:00")
                 :tick/end (t/instant "2000-01-02T00:00")}))

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/divide (t/new-duration 1 :hours)
          (t/new-duration 1 :minutes))

Can you improve this documentation? These fine people already did:
Johanna Antonelli & Malcolm Sparks
Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close