Time.Point
type Time.Point = Time.Instant | Time.ZonedDateTime | Time.PlainDate | Time.PlainTime | Time.PlainDateTimeTime.Point is any of the five time types, the ones Time.add, Time.until and Time.compare serve.
Details
- Its main use is as a bound:
[T: Time.Point]lets one generic function take every time type. Each call fixesTto a single member, so all theTarguments share one type and aTresult has it too. - Inside such a function,
Time.add,Time.subtract,Time.until,Time.sinceandTime.comparetakeT. The comparison operators do not. - A value of type
Time.Pointholds any of the five types, but a function bounded byTime.Pointneeds one of them: narrow the value withisfirst.
Basic examples
A function bounded by Time.Point takes any time type and returns that type:
func later[T: Time.Point](a: T, b: T): T {
if Time.compare(a, b) >= 0 { a } else { b }
}
later(Time.plainDate(2026, 3, 1), Time.plainDate(2025, 12, 24)) // 2026-03-01
later(Time.plainTime(9), Time.plainTime(17, 30)) // 17:30:00As a value type, Time.Point holds time values of different types side by side:
let points: Array[Time.Point] = [Time.fromEpochMillis(0), Time.plainDate(2026, 5, 4), Time.plainTime(9)]
points // [1970-01-01T00:00:00Z, 2026-05-04, 09:00:00]Scope
All five members pass, and the result keeps the argument's type:
func later[T: Time.Point](a: T, b: T): T {
if Time.compare(a, b) >= 0 { a } else { b }
}
let ny = Time.zone("America/New_York").unwrap()
let morning = Time.plainDateTime(Time.plainDate(2026, 5, 4), Time.plainTime(9))
let lastNight = Time.plainDateTime(Time.plainDate(2026, 5, 3), Time.plainTime(23))
let first = Time.fromEpochMillis(0)
let second = Time.fromEpochMillis(1000)
later(Time.plainDate(2026, 1, 1), Time.plainDate(2026, 1, 2)) // 2026-01-02
later(Time.plainTime(23, 59), Time.plainTime(0, 1)) // 23:59:00
later(morning, lastNight) // 2026-05-04T09:00:00
later(first, second) // 1970-01-01T00:00:01Z
later(first.inZone(ny), second.inZone(ny)) // 1969-12-31T19:00:01-05:00[America/New_York]A generic record holds two values of one member type:
type Span[T: Time.Point] = { start: T, end: T }
func Span.length[T: Time.Point](s: Span[T]): Time.Duration {
Time.until(s.start, s.end)
}
let shift = Span{ start: Time.plainTime(9), end: Time.plainTime(17, 30) }
let trip = Span{ start: Time.plainDate(2026, 7, 1), end: Time.plainDate(2026, 7, 15) }
shift.length() // PT8H30M
trip.length() // P14DNarrow a Time.Point value with is to use what only one member has:
func describe(t: Time.Point): string {
if t is Time.Instant {
"instant ${t.epochMillis()}"
} else if t is Time.PlainDate {
"date in ${t.year()}"
} else if t is Time.PlainTime {
"time at hour ${t.hour()}"
} else {
"date and time ${t}"
}
}
let day = Time.plainDate(2026, 5, 4)
describe(Time.fromEpochMillis(0)) // "instant 0"
describe(day) // "date in 2026"
describe(Time.plainTime(9)) // "time at hour 9"
describe(Time.plainDateTime(day, Time.plainTime(9))) // "date and time 2026-05-04T09:00:00"== compiles on Time.Point values; values of different member types are never equal:
let a: Time.Point = Time.plainDate(2026, 5, 4)
let b: Time.Point = Time.plainDateTime(Time.plainDate(2026, 5, 4), Time.plainTime(0))
a == Time.plainDate(2026, 5, 4) // true
a == b // falseApplications
Test whether a value falls in a half-open range, for any time type:
func within[T: Time.Point](t: T, start: T, end: T): bool {
Time.compare(start, t) <= 0 && Time.compare(t, end) < 0
}
let july = Time.plainDate(2026, 7, 1)
let august = Time.plainDate(2026, 8, 1)
within(Time.plainTime(12, 30), Time.plainTime(9), Time.plainTime(17)) // true
within(Time.plainDate(2026, 7, 31), july, august) // true
within(august, july, august) // falseThe earliest of an array of any one time type, or null when it is empty:
func earliest[T: Time.Point](xs: Array[T]): T | null {
xs.sortWith(Time.compare).get(0)
}
let none: Array[Time.PlainTime] = []
earliest([Time.plainDate(2026, 3, 1), Time.plainDate(2025, 12, 24)]) // 2025-12-24
earliest(none) // nullA schedule of evenly spaced values. Time.add takes T inside the body, so the steps keep the start's type:
func steps[T: Time.Point](start: T, step: Time.Duration, count: int): Array[T] {
let out: Array[T] = []
var t = start
for _ in Iter.range(0, count) {
out.push(t)
t = t.add(step)
}
out
}
for t in steps(Time.plainTime(22), 2h, 3) {
println(t)
}
println(steps(Time.fromEpochMillis(0), 15m, 3))22:00:00
00:00:00
02:00:00
[1970-01-01T00:00:00Z, 1970-01-01T00:15:00Z, 1970-01-01T00:30:00Z]Properties and relations
Every member of Time.Wall, Time.Dated and Time.Timed is a member of Time.Point, so a value of those unions is a Time.Point too. Time.Wall is Time.Point without Time.Instant:
let d: Time.Dated = Time.plainDate(2026, 5, 4)
let t: Time.Timed = Time.plainTime(9)
let points: Array[Time.Point] = [d, t]
assert(points.length() == 2)Inside a body bounded by Time.Point, Time.since is Time.until negated, for every member:
func check[T: Time.Point](a: T, b: T) {
assert(Time.since(a, b) == Time.until(a, b).negate())
}
check(Time.plainDate(2026, 3, 1), Time.plainDate(2026, 4, 1))
check(Time.plainTime(8), Time.plainTime(17, 45))
check(Time.fromEpochMillis(0), Time.fromEpochMillis(90_000))Possible issues
A value typed Time.Point cannot be passed to Time.add and the other functions bounded by it: T would be the whole union, not one member:
func nextDay(t: Time.Point): Time.Point {
Time.add(t, Time.duration(days: 1))
// error: T would be Time.Point, which is not a single member of its bound Time.Point; narrow first
}Narrow it with is, one member per branch. An Instant has no days, so it takes 24 hours; a PlainTime ignores days, so it comes back unchanged:
func nextDay(t: Time.Point): Time.Point {
let day = Time.duration(days: 1)
if t is Time.Instant {
t.add(24h)
} else if t is Time.ZonedDateTime {
t.add(day)
} else if t is Time.PlainDate {
t.add(day)
} else if t is Time.PlainTime {
t
} else {
t.add(day)
}
}
nextDay(Time.plainDate(2026, 2, 28)) // 2026-03-01
nextDay(Time.fromEpochMillis(0)) // 1970-01-02T00:00:00ZAll the arguments typed T share one member type, so a date and a date-time do not mix:
let day = Time.plainDate(2026, 5, 4)
let gap = Time.until(day, Time.plainDateTime(day, Time.plainTime(9)))
// error: cannot use Time.PlainDateTime where Time.PlainDate is expectedConvert one of them, here the date to its midnight:
let day = Time.plainDate(2026, 5, 4)
let midnight = Time.plainDateTime(day, Time.plainTime(0))
Time.until(midnight, Time.plainDateTime(day, Time.plainTime(9))) // PT9H< and the other comparison operators do not apply to T, even with this bound. Use Time.compare:
func later[T: Time.Point](a: T, b: T): T {
if a > b { a } else { b }
// error: > applies to a single numeric kind, string, or a named primitive type over one, not to T
}is cannot test Time.Point itself, since an alias has no run-time identity:
func isTime(x: Time.Point | string): bool {
x is Time.Point // error: Time.Point is an alias and has no run-time identity; test its members instead
}A narrowed value is a plain member type, no longer a T, so it cannot be returned as a T:
func shift[T: Time.Point](t: T): T {
if t is Time.Instant { t.add(1h) } else { t }
// error: cannot use Time.Instant where T is expected
}A Time.Duration is not a member. Compare durations with Time.compareDuration:
let d = Time.compare(1h, 2h)
// error: T would be Time.Duration, which is not a member of its bound Time.PointTime.compareDuration(1h, 2h) // -1See also
- Time.Wall, Time.Dated, Time.Timed: the narrower time unions.
- Time.Instant, Time.ZonedDateTime, Time.PlainDate, Time.PlainTime, Time.PlainDateTime: the members.
- Time.add, Time.subtract, Time.until, Time.since, Time.compare
- is
