Skip to content

Std.Duration

opaque type Duration

Made with constructors like Duration.seconds(5).

fn milliseconds(n: Int64) -> Duration

n milliseconds.

fn seconds(n: Int64) -> Duration

n seconds.

fn minutes(n: Int64) -> Duration

n minutes.

fn hours(n: Int64) -> Duration

n hours.

fn to_milliseconds(d: Duration) -> Int64

The number of milliseconds.

fn to_seconds(d: Duration) -> Int64

Truncates toward zero.

fn add(d: Duration, other: Duration) -> Duration

The sum.

fn subtract(d: Duration, other: Duration) -> Duration

May be negative.

fn to_string(d: Duration) -> String

Milliseconds with an ms suffix (1500ms, -1500ms). parse reads it back. The format is in spec [api.std.duration].

fn to_iso8601(d: Duration) -> String

The time part of an ISO 8601 duration (PT1H30M, PT1.5S, -PT0.5S, PT0S): a sign for a negative length, then the hours, the minutes and the seconds, each left out when it is 0. The hours are not carried into days (PT48H). parse reads it back. The format is in spec [api.std.duration].

fn parse(s: String) -> Duration fails ParseError

The length of time written in s: numbers with units (1h30m, 1.5s, 2h 30min, 30 seconds, and 1500ms as to_string writes it), or the time part of an ISO 8601 duration (PT1H30M, -PT1.5S, as to_iso8601 writes it). Days and longer units are not read. The digits below the millisecond are dropped, which truncates toward 0. Fails with ParseError(input: s) otherwise, and for a length outside the range of Duration. The grammar is in spec [api.std.duration].