Time.Timed
type Time.Timed = Time.ZonedDateTime | Time.PlainTime | Time.PlainDateTimeTime.Timed is the time types with a time of day, the ones the time fields such as Time.hour read.
Details
- A function with a type parameter bounded by
Time.Timedor a wider union, such asTime.roundorTime.add, takes one member at a time. Narrow aTime.Timedvalue withisbefore you pass it.
Basic examples
Use Time.Timed as a parameter type to accept any value with a time of day:
func clock(t: Time.Timed): string {
let minute = "${t.minute()}".padStart(2, "0")
"${t.hour()}:${minute}"
}
let alarm = Time.plainTime(7, minute: 5)
let flight = Time.plainDateTime(Time.plainDate(2026, 6, 1), Time.plainTime(18, minute: 40))
let call = Time.parseZoned("2026-06-02T09:30:00+02:00[Europe/Stockholm]").unwrap()
println(clock(alarm))
println(clock(flight))
println(clock(call))7:05
18:40
9:30An array of Time.Timed holds the three kinds side by side:
let breakfast = Time.plainTime(7, minute: 30)
let meeting = Time.plainDateTime(Time.plainDate(2026, 1, 6), Time.plainTime(10, minute: 15))
let launch = Time.parseZoned("2026-01-07T21:00:45Z[UTC]").unwrap()
let times: Array[Time.Timed] = [breakfast, meeting, launch]
times // [07:30:00, 2026-01-06T10:15:00, 2026-01-07T21:00:45+00:00[UTC]]
times.map(Time.minute) // [30, 15, 0]
times.map(Time.second) // [0, 0, 45]Scope
Test which member a value holds with is:
func kind(t: Time.Timed): string {
if t is Time.PlainTime {
"a time"
} else if t is Time.PlainDateTime {
"a date and time"
} else {
"a time in ${t.zoneOf().zoneId()}"
}
}
let tokyo = Time.parseZoned("2026-03-01T12:00:00+09:00[Asia/Tokyo]").unwrap()
println(kind(Time.plainTime(12)))
println(kind(Time.plainDateTime(Time.plainDate(2026, 3, 1), Time.plainTime(12))))
println(kind(tokyo))a time
a date and time
a time in Asia/TokyoA Time.Timed field in a record, and ?. on a value that may be null:
type Reminder = { text: string, at: Time.Timed | null }
let soon = Reminder{ text: "stretch", at: Time.plainTime(15) }
let someday = Reminder{ text: "learn Ant", at: null }
soon.at?.hour() // 15
someday.at?.hour() // nullBound a type parameter by Time.Timed to keep the caller's type. A call through a type parameter is positional, so write the namespace form to pass a named argument:
func toQuarter[T: Time.Timed](t: T): T {
Time.round(t, Time.Minute{}, increment: 15)
}
let start = Time.plainDateTime(Time.plainDate(2026, 3, 1), Time.plainTime(23, minute: 53))
toQuarter(Time.plainTime(9, minute: 8)) // 09:15:00
toQuarter(start) // 2026-03-02T00:00:00The time fields go down to the nanosecond:
let t = Time.plainTime(1, millisecond: 2, microsecond: 3, nanosecond: 4)
[t.millisecond(), t.microsecond(), t.nanosecond()] // [2, 3, 4]To compare or sort by time of day across members, turn each value into its Time.PlainTime, a single type that Time.compare takes:
func clockOf(t: Time.Timed): Time.PlainTime {
if t is Time.PlainTime { t } else { t.toPlainTime() }
}
let a: Time.Timed = Time.plainTime(9, minute: 30)
let b = Time.parseZoned("2026-05-04T09:30:00+02:00[Europe/Paris]").unwrap()
Time.compare(clockOf(a), clockOf(b)) // 0Applications
Order the day's events by the clock, whatever kind of value each holds:
type Event = { label: string, at: Time.Timed }
func clockOf(t: Time.Timed): Time.PlainTime {
if t is Time.PlainTime { t } else { t.toPlainTime() }
}
let call = Time.parseZoned("2026-11-12T17:00:00-05:00[America/New_York]").unwrap()
let events = [
Event{ label: "standup", at: Time.plainTime(9, minute: 30) },
Event{
label: "dentist",
at: Time.plainDateTime(Time.plainDate(2026, 11, 12), Time.plainTime(8, minute: 15)),
},
Event{ label: "call", at: call },
]
let ordered = events.sortWith(func(a: Event, b: Event): int {
Time.compare(clockOf(a.at), clockOf(b.at))
})
for e in ordered {
println("${clockOf(e.at)} ${e.label}")
}08:15:00 dentist
09:30:00 standup
17:00:00 callRound each value to the hour, narrowing first so that each call of Time.round gets one type:
func toHour(t: Time.Timed): Time.Timed {
if t is Time.PlainTime {
t.round(Time.Hour{})
} else if t is Time.PlainDateTime {
t.round(Time.Hour{})
} else {
t.round(Time.Hour{})
}
}
let night = Time.plainDateTime(Time.plainDate(2026, 12, 31), Time.plainTime(23, minute: 40))
let times: Array[Time.Timed] = [Time.plainTime(10, minute: 29), night]
let rounded = times.map(toHour)
println(rounded)[10:00:00, 2027-01-01T00:00:00]Properties and relations
A Time.ZonedDateTime and a Time.PlainDateTime have the time fields of their Time.toPlainTime:
let zoned = Time.parseZoned("2026-10-22T23:30:15+02:00[Europe/Stockholm]").unwrap()
let local = zoned.toPlainDateTime()
let p = zoned.toPlainTime()
let q = local.toPlainTime()
assert(p.hour() == zoned.hour() && p.minute() == zoned.minute() && p.second() == zoned.second())
assert(q.hour() == local.hour() && q.minute() == local.minute() && q.second() == local.second())Values of different members are never ==, even with the same time of day:
let a: Time.Timed = Time.plainTime(9)
let b: Time.Timed = Time.plainDateTime(Time.plainDate(2026, 1, 1), Time.plainTime(9))
assert(a != b)
assert(a == Time.plainTime(9))Possible issues
A Time.Instant and a Time.PlainDate are not members:
let epoch: Time.Timed = Time.fromEpochMillis(0)
// error: cannot use Time.Instant where Time.Timed is expected
let date: Time.Timed = Time.plainDate(2026, 1, 1)
// error: cannot use Time.PlainDate where Time.Timed is expectedA Time.Timed value does not reach a generic function such as Time.round, Time.with or Time.compare until is narrows it:
let t: Time.Timed = Time.plainTime(9, minute: 20)
let rounded = t.round(Time.Hour{})
// error: T would be Time.Timed, which is not a single member of its bound Time.Instant | Time.Timed; narrow first
let moved = t.with(minute: 0)
// error: T would be Time.Timed, which is not a single member of its bound Time.Wall; narrow firstTime.Timed is not Time.Dated: a Time.PlainTime has no date, so the date fields do not take a Time.Timed:
let t: Time.Timed = Time.plainTime(9)
let year = Time.year(t) // error: cannot use Time.Timed where Time.Dated is expectedAn unwrap checked against Time.Timed takes its type from the Result, so a parse result goes straight into an annotation or an argument:
let t: Time.Timed = Time.parseZoned("2026-06-02T09:30:00Z[UTC]").unwrap()
t.hour() // 9Inside a function bounded by Time.Timed, a call through . takes no named arguments:
func toQuarter[T: Time.Timed](t: T): T {
t.round(Time.Minute{}, increment: 15)
// error: a call through T is positional, since each implementation names its parameters as it likes
}See also
Time.hour · Time.minute · Time.round · Time.Dated · Time.Wall · Time.Point · Time.PlainTime · Time.PlainDateTime · Time.ZonedDateTime
