Liking cljdoc? Tell your friends :D

Durations & periods

A Duration instance stores time as an amount of seconds, for example 5.999999999 seconds.

A Period instance stores amounts of years, months and days, for example -1 years, 20 months and 100 days

The javadocs refer to these entities as time-based and date-based, respectively.

The reason for having both representations is that the Period units are variable length (leap years, DST etc) but the time-based ones are not.

So for example a Duration of 48 hours will not the same span as a Period of 2 days in all contexts.

Note that threeten-extra has an additional PeriodDuration entity

Construction

CodeDescriptionReturn type
(t/new-duration 1 :seconds)

Duration of a second

java.time.Duration
(t/new-duration 100 :days)

Duration of 100 days

java.time.Duration
(t/new-period 100 :days)

Period of 100 days

java.time.Period
(t/new-period 2 :months)

Period of 2 months

java.time.Period

Days, Months, Years…

Instances of other java.time types are readily constructed with tick.

ExampleDescriptionReturn type
(day "mon")

Monday

java.time.DayOfWeek
(month "August")

August

java.time.Month
(month 12)

December

java.time.Month
(year-month "2012-12")

December 2012

java.time.YearMonth
(year 1999)

The year 1999

java.time.Year

Derivation

  • Add durations to durations

Misc

TODO Don’t forget you can create zone-offsets from durations!
TODO Don’t forget you can create instants from durations - this is often needed when you get Unix times (e.g. JWT OAuth2 tokens)

The problem with numeric times is that there are cases where the units are in seconds and cases where milliseconds are used. If tick were to convert numbers to times, it would be a source of confusion and bugs if the units were not clear. For this reason, you cannot convert numbers to times. However, you can first create the duration from the number, specifying the units explicitly, and then convert the duration to an instant (or inst).

(t/instant (t/new-duration 1531467976048 :millis))
(t/inst (t/new-duration 1531468976 :seconds))

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

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

× close