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.Durationlap s ms = Duration.seconds s + Duration.milliseconds msDuration
data DurationA duration: how many nanoseconds long it is, less than none for one back in time.
nanoseconds
nanoseconds : Int -> DurationA duration of n nanoseconds.
microseconds
microseconds : Int -> DurationA duration of n microseconds.
milliseconds
milliseconds : Int -> DurationA duration of n milliseconds.
seconds
seconds : Int -> DurationA duration of n seconds.
minutes
minutes : Int -> DurationA duration of n minutes.
hours
hours : Int -> DurationA duration of n hours.
days
days : Int -> DurationA duration of n days, each exactly 24 hours.
zero
zero : DurationThe duration of no time.
totalNanoseconds
totalNanoseconds : Duration -> IntHow many nanoseconds long it is.
>>> totalNanoseconds (hours 1) == totalNanoseconds (minutes 60)True>>> totalNanoseconds (days (-1))-86400000000000instanceEqualityDuration
instance instanceEqualityDuration : Equality DurationEqual: as long.
>>> days 1 == hours 24 && seconds 1 /= milliseconds 999TrueinstanceOrderDuration
instance instanceOrderDuration : Order DurationShorter first, one back in time shorter than none.
>>> minutes 1 < seconds 61 && seconds (-1) < zero && max (hours 1) (minutes 61) == minutes 61TrueinstanceAdditionDuration
instance instanceAdditionDuration : Addition DurationinstanceSubtractionDuration
instance instanceSubtractionDuration : Subtraction Duration>>> hours 1 - minutes 1 == minutes 59TrueinstanceNegationDuration
instance instanceNegationDuration : Negation Duration>>> -(seconds 2) == seconds (-2)TrueinstanceIsZeroDuration
instance instanceIsZeroDuration : IsZero Duration>>> instanceIsZeroDuration.isZero zero && not (instanceIsZeroDuration.isZero (nanoseconds 1))TrueinstanceDebugDuration
instance instanceDebugDuration : Debug DurationAs nanoseconds rebuilds it: nanoseconds 1500, a negative count
in parentheses.
>>> debug (microseconds 3) ^ " " ^ debug (seconds (-1))"nanoseconds 3000 nanoseconds (-1000000000)"instanceShowDuration
instance instanceShowDuration : Show DurationAs 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 -> DurationIt k times over: times d 0 is zero, and times d (-1) is
-d.
>>> times (minutes 20) 3 == hours 1True>>> debug (times (milliseconds 5) (-2))"nanoseconds (-10000000)"