Liking cljdoc? Tell your friends :D

Clocks

In tick, clocks are used for getting the current time, in a given time-zone. You should prefer using clocks to making direct calls to (System/currentTimeMillis), because this then allows you and others to plugin alternative clocks, perhaps for testing purposes.

You create a clock that tracks the current time.

(clock)

With an argument, you can fix a clock to always report a fixed time.

(clock "1999-12-31T23:59:59")

Construction

CodeDescriptionReturn type
(clock)

Return a clock that will always return the current time

java.time.Clock

Derivation

Just like times and dates, you can time-shift clocks forward and backward using the >> and << functions respectively.

(<< (clock) (hours 2))

You can also create a clock from a base clock which reports time with granualarity given by a duration.

(def minute-clk (tick (clock) (minutes 1)))
CodeDescriptionReturn type
(<< (clock) (minutes 2))

Return a clock running 2 minutes slow

java.time.Clock
(>> (clock) (minutes 2))

Return a clock running 2 minutes fast

java.time.Clock

Atomic clocks?

In Clojure, an atom is a holder of a value at a particular time. Similarly, a tick atom is a clock holding the clock’s time, which is constantly changing.

You create this atom with (atom). Naturally, you can get the instant of the atom’s clock by dereferencing, e.g. @(atom)

user> (def clk (atom))
user> (println @clk)
#object[java.time.Instant 0x2e014670 2018-02-28T07:52:52.302Z]
(some time later)
user> (println @clk)
#object[java.time.Instant 0x6e5b1dca 2018-02-28T08:01:50.622Z]

You can also create an atom with a clock.

(atom (clock))
CodeDescriptionReturn type
(atom)

Return a clock that tracks the current time

java.time.Clock

Substitution

A clock can be used to callibrate tick to a particular time and time-zone, if system defaults are not desired.

As I’m currently writing this in London, on my system I get the following when I use '(zone)'.

(zone)

=> #object[java.time.ZoneRegion 0x744a6545 "Europe/London"]

However, if we wanted to test in New York, we can set the clock to exist in that time-zone:

(t/with-clock (-> (t/clock) (t/in "America/New_York"))
  (t/zone))

=> #object[java.time.ZoneRegion 0x5a9d412 "America/New_York"]

Can you improve this documentation?Edit on GitHub

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

× close