Liking cljdoc? Tell your friends :D

Times & dates

Tick is flexible with the way in which times and dates are created; ergo, increasing efficiency. Times and dates can be easily stripped down to smaller modules of time, likewise they can be built up into complete instants.

Create time

A specific time can be produced in multiple ways with varying degrees of precision:

(t/time "12:34")
(t/time "12:34:56.789")
(t/new-time 12 34)
(t/new-time 12 34 56 789000000)

Get the time

To get the current time:

(t/time)
(t/new-time)

Or the time from an instant:

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

Get the current time in another time-zone:

(t/time (t/in (t/now) "Australia/Darwin"))

Get a specific unit of time:

(t/hour (t/instant "1999-12-31T23:59:59"))
(t/minute (t/instant "1999-12-31T23:59:59"))
(t/second (t/instant "1999-12-31T23:59:59"))

Create a date

Creating dates is done in much the same way as creating time.

(t/date "2000-01-01")
(t/new-date 2000 01 01)

Get the date

To get the current date:

(t/date)
(t/new-date)

Or the date from an instant:

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

Get the date in another time-zone:

(t/date (t/in (t/instant "1999-12-31T23:59:59") "Australia/Darwin"))

Get a specific part of the date:

(t/year (t/instant "1999-12-31T23:59:59"))
(t/month (t/instant "1999-12-31T23:59:59"))
(t/day-of-month (t/instant "1999-12-31T23:59:59"))

Build up times and dates

A unique feature of tick is that you can treat individual units of time as modular, making it easy to build up and break down time into components.

Break up an instant:

(defn instant-breakdown
  "Takes an instant of time and breaks it down into units."
  [t]
  {:day  (t/day-of-week t)
   :month  (t/month t)
   :dd (t/day-of-month t)
   :MM (t/int (t/month t))
   :yyyy (t/int (t/year t))
   :mm (t/minute t)
   :HH (t/hour t)
   :ss (t/second t)})

We can treat the individual units of time as building blocks:

Table 1. Tick Time Blocks
TimeDateZone

(t/time)

(t/date)

(t/zone)

(t/hour)

(t/minute)

(t/second)

(t/year)

(t/month)

(t/day-of-month)

-

-

-

(t/millisecond)

(t/microsecond)

(t/nanosecond)

-

-

-

-

Make up a time

If we want it to be half-past the current hour:

(t/new-time (t/hour (t/instant)) 30)

Or about lunch time:

(t/new-time 13 (t/minute (t/instant)))

Make up a date-time

(t/at (t/date "2018-01-01") (t/time "13:00"))
(t/on (t/time "13:00") (t/date "2018-01-01"))
(-> (t/parse "1pm")
    (t/on "2018-10-20"))
(-> (t/tomorrow)
    (t/at (t/midnight)))
(-> (t/noon)
    (t/on (t/yesterday)))

Make up a Zoned-Date-Time

(-> (t/tomorrow)
    (t/at (t/midnight))
    (t/in "Europe/Paris"))
(-> (t/tomorrow)
    (t/at (t/midnight))
    (t/in (t/zone)))

Time and Date manipulation

Give a date a set time in the future:

(t/>> (t/date "2000-01-01") (t/new-period 1 :months))
(t/>> (t/date "2000-01-01") (t/new-period 4 :weeks))
(t/>> (t/date "2000-01-01") (t/new-period 30 :days))
(t/>> (t/date "2000-01-01") (t/+ (t/new-period 5 :days)
                                (t/new-period 1 :weeks)
                                (t/new-period 10 :months)))

Or past:

(t/<< (t/date "2000-01-01") (t/new-period 1 :years))

Move around in time:

(t/>> (t/time "12:00") (t/new-duration 5 :minutes))
(t/<< (t/time "12:00") (t/new-duration 5 :hours))
(t/>> (t/time "12:00") (t/+ (t/new-duration 5 :seconds)
                           (t/new-duration 5 :millis)
                           (t/new-duration 5 :micros)
                           (t/new-duration 5 :nanos)))

Increasing a time by a duration of day magnitude will leave the time alone - 12:00 in 5 days is still 12:00 (ignoring daylight savings)

(t/>> (t/time "12:00") (t/new-duration 5 :days))

Truncate time to a desired precision:

(t/truncate (t/time "10:30:59.99") :minutes)

Give the am pm time:

(defn twelve-hour-time
  "Takes a time and gives the 12 hour display"
  [t]
  (let [minute (t/minute t)
        hour (t/hour t)]
    (cond
      (= (t/noon) t)
      "12:00 NOON"

      (>= hour 13)
      (format "%02d:%02d PM" (- hour 12) minute)

      (>= hour 12)
      (format "%02d:%02d PM" hour minute)

      (< hour 12)
      (format "%02d:%02d AM" hour minute))))
"12 noon is by definition neither ante meridiem (before noon) nor post meridiem (after noon), then 12 a.m. refers to midnight at the start of the specified day (00:00) and 12 p.m. to midnight at the end of that day (24:00)" - NPL

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

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

× close