Liking cljdoc? Tell your friends :D

tick

A Clojure library for dealing with time.

Install

Add to your lein or boot dependencies:

[tick "0.2.4"]

Dependencies

Tick is dependent on Clojure 1.9, since it uses clojure.spec.

Timelines

Tick adds the concept of immutable timelines to java.time. A timeline is a lazy but orderered sequence of maps, each containing a java.time.ZonedDateTime entry (under the key :tick/date.

Timelines are easy to generate. One generator is periodic-seq, which generates a uniform series of times separated by a fixed period.

(require '[tick.core :refer [minutes]]
          [tick.timeline :refer [timeline periodic-seq]]
          [tick.clock :refer [now]])

;; A timeline of 15 minute intervals
(take 10
  (timeline (periodic-seq (now) (minutes 15))))

Timelines are sequences of ticks, and ticks are Clojure maps. Here’s one:

{:tick/date #object[java.time.ZonedDateTime 0x5a56528d "2012-12-04T05:21Z[Europe/London]"]}

Timelines can be finite or, or of course, infinite so

(set! *print-length* 16)

to avoid eternal waits.

Interleaving timelines

Multiple timelines can be interleaved into a consolidated timeline.

interleave-timelines takes any number of timelines and returns a conslidated time-ordered timeline. amalgamation.

(require '[tick.cal])

(def holidays
  (interleave-timelines
    (timeline (tick.cal/bank-holiday-mondays))
    (timeline (tick.cal/easter-fridays))))

Schedules

Clojure’s map takes a function and applies it to every item in a sequence. Similarly, tick’s schedule takes a function and applies it to timelines.

Schedules can be run by starting them with a clock.

(def schedule (tick.schedule/schedule println timeline))
;; note that the scheduled function   ^^^^^^^   takes one argument

(tick.schedule/start schedule (tick.clock/clock-ticking-in-seconds))

You can stop, pause and resume schedules too.

Testing

A type of schdule, useful for testing, can be created with simulate.

(def simulator (tick.schedule/simulate println timeline))

(tick.schedule/start simulator (tick.clock/fixed-clock ...))

If you want to wait for a ticker to complete its journey over a timeline, deref the result of start.

At any time after starting a ticker you can find out its future timeline with remaining, while inspecting its clock with clock.

java.time

Most Clojure time libraries use clj-time, but tick uses java.time which means that it requires Java 8.

Java 8’s java.time API is both influenced by, and superior to, Joda Time. It is a modern immutable API that supports functional programming. Unless you’re stuck on Java 7 or ambivalent about library choice, you should be using java.time.

Discussion

The 'flow of time' complicates our systems and we can simplify them if we separate the concept of a timeline from the 'flow' of time we perceive.

For more information, see the author’s talk at ClojuTRE-2016 on The Universe As A Value where he argues that time is a perception.

Acknowledgements

Tick is based on the same original idea as Chime. The motivation is to be able to view timelines of remaining times while the schedule is running. Thanks to James Henderson for his work on Chime.

In particular, special credit to Eric Evans for discovering Allen’s interval algebra and pointing out its potential usefulness.

The MIT License (MIT)

Copyright © 2016 JUXT LTD.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

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

× close