Unit Casting
as extracts the clock component of a duration — the field at that
position, not the total in that unit. d as h gives the whole hours; d as min
gives the remaining minutes (0–59); d as s the remaining seconds (0–59);
d as ms the remaining milliseconds.
This behaviour is analogous to the fields of a clock: 25h1min1s displays
25:1:1, not 1501:61:61.
The h, min, s, and ms components of a 25-hour duration; total in a unit
via division; as ms for the remainder in milliseconds.
03-cast-to-units.zolo
// Feature: extract clock components with `as`
// Syntax: `dur as h | min | s | ms` — extracts the component of that
// "field" (not the total in the unit).
// When to use: formatting like `HH:MM:SS`. For total in the unit,
// just do arithmetic: `d / 60000` = total in minutes.
// 25h 1min 1s = 90061s = 90_061_000 ms.
let d = 90061000
// Clock components: each `as <unit>` extracts that "field".
print("{d as h}:{d as min}:{d as s}")
// expected: 25:1:1
// For total in the unit, divide.
let total_min = d / 60000
let total_sec = d / 1000
print("total min: {total_min}")
print("total sec: {total_sec}")
// expected:
// total min: 1501
// total sec: 90061
// `as ms` picks up the remainder in milliseconds.
let exact = 1500
print("ms: {exact as ms}")
// expected: ms: 500
// Combined with arithmetic.
let small = 300000 + 30000
print("min={small as min}, s={small as s}")
// expected: min=5, s=30
To get the total in a given unit, use arithmetic division:
d / 60000 gives the total minutes, d / 1000 the total seconds.
See also