← index

Duration

inherits Object

A signed, fixed length of time, with nanosecond precision.

Built with the unit constructors (Duration.seconds:, Duration.minutes:, …), combined with +: / -: / *:, and read back as unit totals (asSeconds, asMilliseconds, …). Fixed means clock-agnostic: an hour is always 3600 seconds. Calendar-aware arithmetic (days, months, DST) lives on DateTime (plusDays: and friends). Durations are what Timestamp / DateTime / Instant subtraction yields, and what sleep: / timeout: consume.

(Duration.minutes:90).s                          "* -> 1h 30m
((Duration.minutes:1) + (Duration.seconds:30)).s "* -> 1m 30s
(Duration.milliseconds:1500).asSeconds           "* -> 1.5
extended at core/16-serialize.qn:44

The fixed duration, as an ISO 8601 duration ('PT1H30M'; Span.parse: reads it back — as a time-only Span, asDuration completing the round trip).

Class methods

hours:Integer

A Duration of exactly the given whole number of hours (may be negative).

native

microseconds:Integer

A Duration of exactly the given whole number of microseconds (may be negative).

native

milliseconds:Integer

A Duration of exactly the given whole number of milliseconds (may be negative).

(Duration.milliseconds:1500).s     "* -> 1s 500ms

native

minutes:Integer

A Duration of exactly the given whole number of minutes (may be negative).

(Duration.minutes:90).s     "* -> 1h 30m

native

nanoseconds:Integer

A Duration of exactly the given whole number of nanoseconds (may be negative).

native

seconds:Integer

A Duration of exactly the given whole number of seconds (may be negative).

(Duration.seconds:90).s     "* -> 1m 30s

native

zero

The zero-length Duration — the identity for +: and the natural seed for a summing fold.

Duration.zero.s     "* -> 0s

native

Instance methods

*:Integer

The Duration scaled by an Integer factor (the one arithmetic operator whose operand is not a Duration). Overflow throws an ArithmeticError.

((Duration.seconds:10) * 6).s     "* -> 1m

native

+:Duration

The sum of two Durations. Overflow (past roughly ±292 billion years) throws an ArithmeticError rather than wrapping.

((Duration.minutes:1) + (Duration.seconds:30)).s     "* -> 1m 30s

native

-:Duration

The difference of two Durations — negative when the argument is longer (a Duration is signed). Overflow throws an ArithmeticError.

((Duration.hours:2) - (Duration.minutes:30)).s     "* -> 1h 30m
((Duration.minutes:90) - (Duration.hours:2)).s     "* -> 30m ago

native

<:Duration

Whether the receiver is shorter than the argument (signed: any negative Duration is less than any positive one). Only <: is native; >: / <=: / >=: derive from it on Object.

native

==:

Whether the argument is a Duration of the same length. A non-Duration argument is simply unequal — never an error.

(Duration.seconds:60) == (Duration.minutes:1)     "* -> true

native

abs

The magnitude: a negative Duration made positive, a non-negative one unchanged.

native

asData

core/16-serialize.qn:45

asMicroseconds

The total number of whole microseconds, truncated toward zero (Integer); throws an ArithmeticError if the count does not fit a 64-bit Integer.

native

asMilliseconds

The total number of whole milliseconds, truncated toward zero (Integer).

(Duration.seconds:90).asMilliseconds     "* -> 90000

native

asNanoseconds

The total number of whole nanoseconds (Integer); throws an ArithmeticError if the count does not fit a 64-bit Integer (a span of ~292 years).

native

asSeconds

The total length in seconds, as a fractional Double (the one fractional unit total; the others are whole Integer counts).

(Duration.milliseconds:1500).asSeconds     "* -> 1.5

native

iso8601

The canonical ISO 8601 duration string, for serialization.

(Duration.minutes:90).iso8601     "* -> PT1H30M

native

negate

The same length with the sign flipped.

(Duration.seconds:30).negate.s     "* -> 30s ago

native

s

A human-readable rendering; negative Durations read as time "ago". For a machine-readable form use iso8601.

(Duration.minutes:90).s            "* -> 1h 30m
(Duration.seconds:30).negate.s     "* -> 30s ago

native