Time.part
func Time.part(d: Time.Duration, unit: Time.Unit): intTime.part returns the field of d for unit, as given and not balanced.
Details
Time.partreads one of the ten fields a duration keeps. It does not convert:PT90Mhas0hours and90minutes. UseTime.totalfor the length in a unit.- A field not given when the duration was made is
0. - On a negative duration every nonzero field is negative.
Basic examples
let d = 2h15m
Time.part(d, Time.Hour{}) // 2
Time.part(d, Time.Minute{}) // 15
Time.part(d, Time.Second{}) // 0Fields are not balanced, so 90 minutes stay 90 minutes:
Time.part(90m, Time.Hour{}) // 0
Time.part(90m, Time.Minute{}) // 90Scope
Every unit from years down to nanoseconds names a field:
let d = Time.duration(
years: 1,
months: 2,
weeks: 3,
days: 4,
hours: 5,
minutes: 6,
seconds: 7,
milliseconds: 8,
microseconds: 9,
nanoseconds: 10,
)
let units: Array[Time.Unit] = [
Time.Year{},
Time.Month{},
Time.Week{},
Time.Day{},
Time.Hour{},
Time.Minute{},
Time.Second{},
Time.Millisecond{},
Time.Microsecond{},
Time.Nanosecond{},
]
units.map({ d.part(x) }) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]A negative duration has negative fields:
let back = Time.duration(days: -2, hours: -3)
back.part(Time.Day{}) // -2
back.part(Time.Hour{}) // -3Call it with a dot on a duration that another function returns:
let shift = Time.until(Time.plainTime(9, 0, 0), Time.plainTime(17, 45, 0))
shift // PT8H45M
shift.part(Time.Hour{}) // 8
shift.part(Time.Minute{}) // 45Named arguments
Time.part(unit: Time.Minute{}, d: 90m) // 90Applications
Format an elapsed time as a clock reading. Time.roundDuration balances the seconds into hours, minutes and seconds first, and part reads them:
func clock(d: Time.Duration): string {
let b = Time.roundDuration(d, largestUnit: Time.Hour{})
let mm = "${b.part(Time.Minute{})}".padStart(2, "0")
let ss = "${b.part(Time.Second{})}".padStart(2, "0")
"${b.part(Time.Hour{})}:${mm}:${ss}"
}
clock(3725s) // "1:02:05"
clock(90m) // "1:30:00"Say how long ago something happened in the largest unit that is not zero. A duration between two instants is all seconds, so Time.roundDuration balances it into days, hours and minutes first:
type Step = { unit: Time.Unit, name: string }
let steps = [
Step{ unit: Time.Day{}, name: "day" },
Step{ unit: Time.Hour{}, name: "hour" },
Step{ unit: Time.Minute{}, name: "minute" },
]
func ago(d: Time.Duration): string {
let b = Time.roundDuration(d, largestUnit: Time.Day{})
let step = steps.find({ b.part(x.unit) != 0 })
if step == null { return "just now" }
let n = b.part(step.unit)
let plural = if n == 1 { "" } else { "s" }
"${n} ${step.name}${plural} ago"
}
let then = Time.parseInstant("2026-10-22T11:40:00Z") | Result.unwrap()
let now = Time.parseInstant("2026-10-22T15:00:00Z") | Result.unwrap()
ago(Time.since(now, then)) // "3 hours ago"
ago(Time.duration(days: 3, hours: 4)) // "3 days ago"
ago(1h) // "1 hour ago"
ago(20s) // "just now"Properties and relations
Time.negate negates every field, and Time.abs makes every field nonnegative:
let d = Time.duration(days: -1, hours: -6, minutes: -30)
let units: Array[Time.Unit] = [Time.Day{}, Time.Hour{}, Time.Minute{}, Time.Second{}]
for u in units {
assert(Time.negate(d).part(u) == -d.part(u))
assert(Time.abs(d).part(u) >= 0)
}Two durations are == exactly when every field is the same, so part tells apart durations of the same length:
let a = 90m
let b = 1h30m
Time.compareDuration(a, b) // 0
a == b // false
a.part(Time.Hour{}) == b.part(Time.Hour{}) // falsePossible issues
part is a field, not a length. A duration between two instants is in seconds, so its hours field is 0. Use Time.total for the length, or pass largestUnit: to get the field you want:
let start = Time.parseInstant("2026-10-22T14:00:00Z") | Result.unwrap()
let end = Time.parseInstant("2026-10-22T16:30:00Z") | Result.unwrap()
let d = Time.until(start, end)
d // PT9000S
d.part(Time.Hour{}) // 0
Time.total(d, Time.Hour{}) // 2.5
Time.until(start, end, largestUnit: Time.Hour{}).part(Time.Hour{}) // 2Two durations that print the same can hold different fields: 1500ms keeps 1500 milliseconds, while 1s500ms keeps 1 second and 500 milliseconds:
let a = 1500ms
let b = 1s500ms
a // PT1.5S
b // PT1.5S
a.part(Time.Second{}) // 0
b.part(Time.Second{}) // 1The unit is a value, written with braces. The bare name is a type:
let h = Time.part(1h, Time.Hour)
// error: Time.Hour is a type, not a value; a type appears only as a literal head, after is, or before a dotSee also
Time.total, Time.roundDuration, Time.duration, Time.parseDuration, Time.sign, Time.until, Time.Unit, Time.Duration
