Time.Duration
type Time.DurationTime.Duration is an amount of time in ten fields, years down to nanoseconds, all of one sign and kept as given. 90 minutes stays 90 minutes until Time.roundDuration balances it, and two durations are == only when all ten fields are, so PT90M and PT1H30M are not. It renders in ISO 8601 form, as P1Y2M3DT4H5M6.5S, -PT1H or PT0S.
Details
- A constant span of hours and smaller units is a duration literal, written as in Go:
500ms,2h30m,1.5s,90m. The units areh,m,s,ms,usandns, somis minutes; they come in descending order, each at most once, and only the last component may have a fraction. A literal has type Time.Duration and needs no import. There are no literals for days, weeks, months or years, which areTime.duration(days: 1)and the like, and a span computed from a number isTime.duration(seconds: n). A literal keeps its fields as written, so90mis PT90M. - Durations take no arithmetic or ordering operators, so
-5s,5s + 1sand90m < 2hare compile errors. Negate withTime.negate, add withTime.addDuration, and order withTime.compareDuration. - Years, months and weeks have no fixed length.
Time.sleep,Time.ticker,Time.addDurationandContext.newpanic on them, andTime.compareDuration,Time.totalandTime.roundDurationneed arelativeTodate for them. - Days count as 24 hours in a wait or a sum, but
Time.addon a zoned time adds calendar days, and aTime.Instanttakes no days at all.
Basic examples
Write a constant duration as a literal:
1h30m // PT1H30M
500ms // PT0.5SBuild a calendar or computed duration with Time.duration:
Time.duration(days: 3) // P3Dlet n = 90
Time.duration(seconds: n) // PT90SParse one from its ISO 8601 text or Go's notation with Time.parseDuration:
Time.parseDuration("PT1H30M") // Ok{ value: PT1H30M }
Time.parseDuration("1h30m") // Ok{ value: PT1H30M }Measure the time between two points with Time.until:
Time.until(Time.plainTime(9, 0), Time.plainTime(17, 45)) // PT8H45MScope
A duration renders as ISO 8601 text: the date part after P, the time part after T, the subsecond fields folded into the seconds, and PT0S when it is zero:
Time.duration(years: 1, months: 2, days: 3) // P1Y2M3D
Time.duration(days: 3, hours: 4, seconds: 6, milliseconds: 500) // P3DT4H6.5S
Time.duration(weeks: 2) // P2W
1500ms // PT1.5S
0s // PT0SA negative duration has a sign in front:
Time.duration(hours: -2) // -PT2H
1h.negate() // -PT1H
Time.negate(5s) // -PT5S
Time.negate(15m).sign() // -1Fields are not balanced. Ninety minutes stays ninety minutes, and two weeks has no days, until Time.roundDuration balances it:
let d = 90m
d // PT90M
d.part(Time.Hour{}) // 0
d.roundDuration(largestUnit: Time.Hour{}) // PT1H30M
Time.duration(weeks: 2).part(Time.Day{}) // 0String interpolation, String.from and Json.stringify all write the same text:
let d = 5m
"retry in ${d}" // "retry in PT5M"
String.from(d) // "PT5M"
Json.stringify([d]) // "[\"PT5M\"]"A duration is a value like any other: a record field, a field default, an optional parameter, a map key:
type Job = { name: string, timeout: Time.Duration | null }
Job{ name: "build", timeout: 10m } // Job{ name: "build", timeout: PT10M }
Job{ name: "lint", timeout: null } // Job{ name: "lint", timeout: null }
type Task = { name: string, timeout: Time.Duration = 10m }
Task{ name: "build" } // Task{ name: "build", timeout: PT10M }
let names = Map{ 1h: "hour", 60m: "sixty minutes" }
names.size() // 2func fetch(url: string, timeout: Time.Duration | null = null): string {
if timeout == null { "${url} without a timeout" } else { "${url} within ${timeout}" }
}
println(fetch("https://example.com"))
println(fetch("https://example.com", timeout: 2s))https://example.com without a timeout
https://example.com within PT2STime.total gives the whole length in one unit as a float64:
1h30m.total(Time.Minute{}) // 90.0
90m.total(Time.Hour{}) // 1.5Applications
Move a time by a duration with Time.add:
Time.plainDateTime(Time.plainDate(2026, 10, 22), Time.plainTime(9, 0)).add(1h30m) // 2026-10-22T10:30:00Show the time between two instants as hours, minutes and seconds. Balance it to hours and round to the second first, then read each field:
func label(d: Time.Duration): string {
let b = d.roundDuration(largestUnit: Time.Hour{}, smallestUnit: Time.Second{})
"${b.part(Time.Hour{})}h ${b.part(Time.Minute{})}m ${b.part(Time.Second{})}s"
}
let start = Time.parseInstant("2026-10-22T14:03:05Z").unwrap()
let end = Time.parseInstant("2026-10-22T16:20:00.75Z").unwrap()
println(label(start.until(end)))2h 16m 56sGive up waiting after a timeout. Every wait in the library takes a duration:
let ctx = Context.new(timeout: 20ms)
Time.sleep(5s, ctx: ctx)
println("gave up waiting")gave up waitingRead a timeout from configuration, in either notation, with a default for text that is not a duration:
func timeoutFrom(text: string): Time.Duration {
Time.parseDuration(text).unwrapOr(30s)
}
println(timeoutFrom("500ms"))
println(timeoutFrom("PT2M"))
println(timeoutFrom("soon"))PT0.5S
PT2M
PT30SSort durations by length with Time.compareDuration:
let laps = [90m, 1h, 1h15m]
laps.sortWith(Time.compareDuration) // [PT1H, PT1H15M, PT90M]Properties and relations
== compares fields, and Time.compareDuration compares lengths:
let a = 90m
let b = 1h30m
assert(a != b)
assert(Time.compareDuration(a, b) == 0)
assert(a.roundDuration(largestUnit: Time.Hour{}) == b)A fraction in a literal spreads into the smaller fields, so 1.5h has the fields of 1h30m:
assert(1.5h == 1h30m)
assert(String.from(1.5h) == "PT1H30M")The text of a duration parses back to an equal duration:
let d = Time.duration(days: 1, hours: 2, seconds: 3, milliseconds: 40)
assert(Time.parseDuration("${d}").unwrap() == d)Negating twice gives the duration back:
let d = 3h5m
assert(d.negate().negate() == d)
assert(d.negate().abs() == d)Possible issues
== is not a test of length. Two durations of one length are == only with the same fields, and ant check rejects == between two such literals:
let same = 90m == 1h30m
// error: 90m == 1h30m compares the fields as written, and == is false: compare the lengths with Time.compareDuration(90m, 1h30m)Time.compareDuration(90m, 1h30m) // 0< does not compare durations. Use Time.compareDuration:
let short = 90m < 2h
// error: < applies to a single numeric kind, string, or a named primitive type over one, not to Time.DurationDurations take no operators. Negate with Time.negate, as in Time.negate(5s):
let d = -5s // error: unary - needs a number, got Time.DurationAdd with Time.addDuration, as in Time.addDuration(5s, 1s):
let d = 5s + 1s
// error: + applies to a single numeric kind, string, or a named primitive type over one, not to Time.DurationThere are no literals for days, weeks, months or years:
let d = 1d
// error: there are no duration literals for days, weeks, months or years: write Time.duration(days: 1) and the likeA wait needs a fixed length, so a duration in months panics:
Time.sleep(Time.duration(months: 1))
// panic: Time.sleep needs a duration without years, months or weeks, which have no fixed lengthAll fields of a duration have one sign, so an hour less 30 minutes is not hours: 1, minutes: -30. Write 30m, or negate the duration to take away and pass it to Time.addDuration:
Time.duration(hours: 1, minutes: -30)
// panic: Time.duration: a duration's fields must all have one signTime.addDuration(1h, Time.negate(30m)) // PT30MText in neither ISO 8601 nor Go's notation is an Err, and so is a bare number other than 0, which has no unit:
Time.parseDuration("1 hour") // Err{ error: Error{ message: "not a duration: \"1 hour\"" } }
Time.parseDuration("5") // Err{ error: Error{ message: "not a duration: \"5\"" } }See also
Time.duration, Time.parseDuration, Time.until, Time.since, Time.add, Time.addDuration, Time.negate, Time.compareDuration, Time.roundDuration, Time.total, Time.part, Time.Unit, Duration literals
