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.

(t/clock)

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

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

Construction

CodeDescriptionReturn type
(t/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.

Shift a clock to run 2 hours slow.

(t/<< (t/clock) (t/new-duration 2 :hours))
CodeDescriptionReturn type
(t/<< (t/clock) (t/new-duration 2 :minutes))

Return a clock running 2 minutes slow

java.time.Clock
(t/>> (t/clock) (t/new-duration 2 :minutes))

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 (t/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.

(let [clk (t/atom (t/clock))]
  @clk)
CodeDescriptionReturn type
(t/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)'.

(t/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? These fine people already did:
Caro & Malcolm Sparks
Edit on GitHub

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

× close