Liking cljdoc? Tell your friends :D

Intervals

In tick, an interval is a span of time defined by two points in time, the first being before the second.

Intervals are maps containing both a tick/beginning and a tick/end entry. This flexible design allows any Clojure map to be treated as an interval.

Intervals can be represented two local times as well as instants.

Construction

Obviously, the Clojure’s literal syntax for maps can be used to create intervals.

Here we use a literal map syntax to construct an interval representing the last 5 minutes of 2018 (in UTC).

{:tick/beginning "2018-12-31T23:55:00Z"
 :tick/end "2019-01-01T00:00:00Z")

If we have aliased tick.alpha.api to t, we can use this alternative shorthand syntax:

{::t/beginning "2018-12-31T23:55:00Z"
 ::t/end "2019-01-01T00:00:00Z")

Alternatively, we can use the t/make-interval function which takes the two boundaries of the interval as its arguments.

(t/make-interval
  (t/instant "2018-12-31T23:55:00Z")
  (t/instant "2019-01-01T00:00:00Z"))

Derivation

Dates, months and years can also be considered to be themselves ranges, and can be converted to intervals with the t/bounds function.

To return today as an interval:

(t/bounds (t/today))

The arguments to t/make-interval do not have to be instants, they can be any time supported by tick.

To return a 2-day interval spanning midnight this morning to midnight two days from today:

(t/make-interval (t/today) (t/tomorrow))

Comparison

Two intervals can be compared against each other with the t/relation function. Allen’s interval algebra tells us there are 13 possible relations between two intervals.

Example 1. Interval relations

Consider the time-span represented by the word 'yesterday' and compare it to the time-span represented by the word 'tomorrow'. Since yesterday is before tomorrow, with a gap between them, we say that yesterday precedes tomorrow:

(t/relation (t/yesterday) (t/tomorrow))

If the two intervals touch each other, in the case of 'today' and 'tomorrow', then we say the first interval (today) meets the second interval (tomorrow).

(t/relation (t/today) (t/tomorrow))

To see other possible relations, use the slider in the diagram below to move the top interval along:

abc

Collections

It is often useful to group intervals into collections and have functions operate on those collections.

For example, you may want to gather together:

  • all the time intervals when you were working last week

  • system outages over a given period

  • public holidays and weekends this year

Discuss ordered sequences of disjoint intervals.

Can you improve this documentation?Edit on GitHub

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

× close