Skip to content
heff

std.duration

Durations: fixed lengths of time, exact to the nanosecond, of any size, as an Int is. NodaTime’s Duration is the model: a duration is a length on the time line, the same wherever it is laid — a day is exactly 24 hours, as no calendar is asked — and what a calendar counts, a month or a year, is std.period’s. The distance between two instants of std.instant is one, and an instant moves by one. Its instances are the prelude’s operators at Duration — x + y, x - y, -x, x < y, max x y — and show writes it as Go’s Duration.String does. Import it by a name, import std.duration as Duration, as its names are short.

import std.duration as Duration
-- | a lap of a race, in seconds and milliseconds
-- | >>> show (lap 83 250)
-- | "1m23.25s"
lap : Int -> Int -> Duration.Duration
lap s ms = Duration.seconds s + Duration.milliseconds ms

Duration

data Duration

A duration: how many nanoseconds long it is, less than none for one back in time.

nanoseconds

nanoseconds : Int -> Duration

A duration of n nanoseconds.

microseconds

microseconds : Int -> Duration

A duration of n microseconds.

milliseconds

milliseconds : Int -> Duration

A duration of n milliseconds.

seconds

seconds : Int -> Duration

A duration of n seconds.

minutes

minutes : Int -> Duration

A duration of n minutes.

hours

hours : Int -> Duration

A duration of n hours.

days

days : Int -> Duration

A duration of n days, each exactly 24 hours.

zero

zero : Duration

The duration of no time.

totalNanoseconds

totalNanoseconds : Duration -> Int

How many nanoseconds long it is.

>>> totalNanoseconds (hours 1) == totalNanoseconds (minutes 60)
True
>>> totalNanoseconds (days (-1))
-86400000000000

instanceEqualityDuration

instance instanceEqualityDuration : Equality Duration

Equal: as long.

>>> days 1 == hours 24 && seconds 1 /= milliseconds 999
True

instanceOrderDuration

instance instanceOrderDuration : Order Duration

Shorter first, one back in time shorter than none.

>>> minutes 1 < seconds 61 && seconds (-1) < zero && max (hours 1) (minutes 61) == minutes 61
True

instanceAdditionDuration

instance instanceAdditionDuration : Addition Duration

instanceSubtractionDuration

instance instanceSubtractionDuration : Subtraction Duration
>>> hours 1 - minutes 1 == minutes 59
True

instanceNegationDuration

instance instanceNegationDuration : Negation Duration
>>> -(seconds 2) == seconds (-2)
True

instanceIsZeroDuration

instance instanceIsZeroDuration : IsZero Duration
>>> instanceIsZeroDuration.isZero zero && not (instanceIsZeroDuration.isZero (nanoseconds 1))
True

instanceDebugDuration

instance instanceDebugDuration : Debug Duration

As nanoseconds rebuilds it: nanoseconds 1500, a negative count in parentheses.

>>> debug (microseconds 3) ^ " " ^ debug (seconds (-1))
"nanoseconds 3000 nanoseconds (-1000000000)"

instanceShowDuration

instance instanceShowDuration : Show Duration

As Go’s Duration.String writes it: hours, minutes and seconds (1h2m3.5s, 2m0s), the seconds’ fraction without the 0s it ends with; under a second, in the unit it has a whole one of (1.5ms, 1.5\u{B5}s, 7ns); none 0s; and one back in time after a -.

>>> show (hours 1 + minutes 2 + milliseconds 3500)
"1h2m3.5s"
>>> List.map show [zero, nanoseconds 7, nanoseconds 1500, microseconds 1500, seconds 1, seconds 90, minutes (-2), hours 100 + nanoseconds 1, days 2]
["0s", "7ns", "1.5\u{B5}s", "1.5ms", "1s", "1m30s", "-2m0s", "100h0m0.000000001s", "48h0m0s"]

times

times : Duration -> Int -> Duration

It k times over: times d 0 is zero, and times d (-1) is -d.

>>> times (minutes 20) 3 == hours 1
True
>>> debug (times (milliseconds 5) (-2))
"nanoseconds (-10000000)"