Duration literals
A number followed by time units, such as 500ms, 2h30m, 1.5s, or 90m, is a duration literal of type Time.Duration, written as in Go. It needs no import.
Details
- The units are
h,m,s,ms,us, andns, hours through nanoseconds, somis minutes. Each unit appears at most once, in descending order, with no white space between components. - A number is a decimal integer or float, with
_between digits and no exponent. Only the last component may have a fraction; it spreads into the smaller units and must come out as a whole number of nanoseconds. - A literal keeps its fields as written:
90mis 90 minutes, not 1 hour and 30 minutes. - There are no literals for days, weeks, months, or years, and
-,+,*, and the other arithmetic operators do not apply to a duration (==compares fields). WriteTime.duration(...)for those spans, for a span computed from a number, andTime.negatefor a negative one.
Basic examples
A literal renders as an ISO 8601 duration:
500ms // PT0.5S
2h30m // PT2H30M
1.5s // PT1.5SPass a literal wherever a function takes a Time.Duration:
let ctx = Context.new(50ms)
Time.sleep(1s, ctx: ctx)
println(ctx.reason())timeoutScope
Units
Each unit, from hours to nanoseconds:
2h // PT2H
90m // PT90M
45s // PT45S
250ms // PT0.25S
10us // PT0.00001S
5ns // PT0.000000005SComponents combine in descending order:
1h30m // PT1H30M
1m30s500ms // PT1M30.5S
2h0m5s // PT2H5SZero and digit separators
0s is the zero span, and _ groups digits:
0s // PT0S
1_000ms // PT1SFractions
A fraction on the last component spreads into the smaller units, exactly:
1.5h // PT1H30M
1h30.5m // PT1H30M30S
0.5ms // PT0.0005S
1.25s // PT1.25SThe type in any context
A literal is a Time.Duration wherever it stands: a record field default, a named-argument default, a map key:
type Job = { name: string, timeout: Time.Duration = 10m }
Job{ name: "backup" }.timeout // PT10M
Map{ 1h: "hour", 60m: "sixty minutes" } // Map{ PT1H: "hour", PT60M: "sixty minutes" }func retry(attempts: int, wait: Time.Duration = 200ms): string {
"${attempts} attempts, ${wait} apart"
}
println(retry(3))
println(retry(5, wait: 2s))3 attempts, PT0.2S apart
5 attempts, PT2S apartFields as written
A literal keeps its fields as written, and == compares fields, so two literals of the same length are not always equal. Time.compareDuration compares lengths:
let a = 90m
let b = 1h30m
a == b // false
Time.compareDuration(a, b) // 0
1.5h == 1h30m // trueCalls through .
A . after the unit is member access, so a literal reaches the Time functions that take a duration first:
1h.negate() // -PT1H
90m.total(Time.Minute{}) // 90.0
90m.roundDuration(largestUnit: Time.Hour{}) // PT1H30MRendering
String.from and string interpolation give the ISO 8601 form:
String.from(500ms) // "PT0.5S"
"${2h30m}" // "PT2H30M"ant fmt prints a literal exactly as written: 90m stays 90m, and 1_000ms stays 1_000ms.
Applications
Time out a slow operation with select:
let result: Channel[string] = Channel.new(capacity: 1)
go {
Time.sleep(1s)
result.send("done")
}
select {
let r = Channel.receive(result) {
println(r ?? "closed")
}
Time.sleep(50ms) {
println("timed out")
}
}timed outShift a time by a constant span:
let start = Time.plainTime(9, 0)
start.add(1h30m) // 10:30:00
start.subtract(15m) // 08:45:00Properties and relations
A literal and the constructor with the same fields are equal:
assert(2h30m == Time.duration(hours: 2, minutes: 30))
assert(1.5h == Time.duration(hours: 1, minutes: 30))
assert(500ms == Time.duration(milliseconds: 500))Possible issues
Comparing two literals with ==
ant check rejects == or != between two literals of the same length and different fields:
let same = 90m == 1h30m
// error: 90m == 1h30m compares the fields as written, and == is false: compare the lengths with Time.compareDuration(90m, 1h30m)Compare the lengths:
Time.compareDuration(90m, 1h30m) == 0 // trueNo days, weeks, months, or years
Their length depends on the calendar, so they have no literal:
let day = 1d
// error: there are no duration literals for days, weeks, months or years: write Time.duration(days: 1) and the likeTime.duration(days: 1) // P1D24h is exactly 24 hours, not the next day across a daylight saving change.
No arithmetic on durations
let back = -5s // error: unary - needs a number, got Time.Durationlet total = 5s + 1s
// error: + applies to a single numeric kind, string, or a named primitive type over one, not to Time.Durationlet twice = 2 * 1s // error: * needs both operands of one type, got int and Time.DurationCall the Time functions instead:
Time.negate(5s) // -PT5S
5s.negate() // -PT5S
Time.addDuration(5s, 1s) // PT6SA computed span is not a literal
A literal holds constants only. Build a span from a number with Time.duration:
let n = 3
Time.duration(seconds: n * 2) // PT6SEvery number needs a unit
let wait = 1h30 // error: the number 30 in a duration literal needs a unitA fraction goes on the last component only
let wait = 1.5h30m // error: only the last component of a duration literal can have a fractionOther rejected spellings
The lexer reads every letter, digit, and point after the number as part of the literal, so a wrong unit is an error rather than a name:
minis not a unit; minutes arem:antlet wait = 1min // error: malformed number literal: min is not a duration unit, which are h, m, s, ms, us and nsMis not a unit:antlet wait = 1M // error: malformed number literal: M is not a duration unit, which are h, m, s, ms, us and nsMicroseconds are
us, notµs:antlet wait = 1µs // error: malformed duration literal: write microseconds as usA duration has no exponent:
antlet wait = 1e3s // error: malformed duration literal: a duration has no exponentThe number is decimal, with no prefix:
antlet wait = 0x1s // error: malformed duration literal: a duration is decimal, as 1sThe value is not a whole number of nanoseconds:
antlet wait = 0.1ns // error: duration literal 0.1ns is not a whole number of nanosecondsThe units are out of order:
antlet wait = 1m2h // error: the units of a duration literal come in descending order, each at most onceA unit appears twice:
antlet wait = 1h1h // error: the units of a duration literal come in descending order, each at most onceA point after a digit begins a fraction:
antlet wait = 1.s // error: a float literal needs a digit after the point
Time.parseDuration reads text in Go's looser notation, where "1µs" and "1m2h" are fine.
Out of range
The time part of a duration stays below 2^53 seconds:
let long = 3000000000000h
// error: duration literal out of range: the time part of a duration must be below 2^53 secondsSee also
Number literals · Time.Duration · Time.duration · Time.parseDuration · Time.compareDuration · Time.negate
