Time
Time holds dates, times, zones, and durations, after JavaScript's Temporal: exact time and wall-clock time are different types, a zone is named and carried, and nothing assumes UTC or the host's zone. Dates are in the ISO 8601 calendar, the proleptic Gregorian one, with a year 0 and negative years. The time types are opaque, immutable, and compared by value. Unlike the other I/O namespaces, Time is computation and panics on an argument that cannot be.
Details
- An Instant is a moment with no zone. A ZonedDateTime is a moment seen in a zone. PlainDate, PlainTime, and PlainDateTime are what a calendar or a clock shows, with no zone.
- Constructors panic on a value that cannot exist, such as February 30. Text from outside goes through the
parsefunctions, which return aResult. <and the other comparison operators do not apply to time values. Order them with Time.compare.- Years, months, weeks, and days move the wall clock; hours and smaller units move the instant. An
Instanthas no calendar, so adding a day to it panics. - A Duration keeps its fields as given:
90mand1h30mare not==. - A constant span of hours and smaller units is a duration literal such as
500ms,2h30m, or1.5s, of typeTime.Duration, with no import and no arithmetic operators. Time.duration builds computed and calendar spans. - Every value renders as its RFC 9557 string in
String.from, interpolation, andJson.stringifyalike. Theparsefunctions read it back, andTime.zonereads back a zone.
Basic examples
Build a date and a time, read their fields, and do calendar arithmetic:
let day = Time.plainDate(2026, 10, 22)
day // 2026-10-22
Time.dayOfWeek(day) // 4
Time.add(day, Time.duration(months: 1)) // 2026-11-22
Time.plainTime(14, 30) // 14:30:00Write a constant span as a duration literal and move a clock time by it:
let d = 1h30m
d // PT1H30M
Time.plainTime(9).add(d) // 10:30:00Place a wall-clock time in a zone to get a moment, and see that moment elsewhere:
let stockholm = Result.unwrap(Time.zone("Europe/Stockholm"))
let meeting = Time.plainDateTime(Time.plainDate(2026, 10, 22), Time.plainTime(14, 30))
let zoned = Time.toZoned(meeting, stockholm)
zoned // 2026-10-22T14:30:00+02:00[Europe/Stockholm]
zoned.toInstant() // 2026-10-22T12:30:00Z
zoned.withZone(Result.unwrap(Time.zone("America/New_York"))) // 2026-10-22T08:30:00-04:00[America/New_York]Measure the span between two dates:
let start = Time.plainDate(2026, 1, 1)
let end = Time.plainDate(2026, 10, 22)
Time.until(start, end) // P294D
Time.until(start, end, largestUnit: Time.Month{}) // P9M21DScope
Every value parses back from the string it renders as. Text that is not a time, or a date that does not exist, is an Err:
Time.parseInstant("2026-10-22T14:03:05Z") // Ok{ value: 2026-10-22T14:03:05Z }
Time.parseDuration("PT1H30M") // Ok{ value: PT1H30M }
Time.parseDuration("2h30m") // Ok{ value: PT2H30M }
Time.parsePlainDate("2026-02-30") // Err{ error: Error{ message: "no such date: 2026-02-30" } }A zone is an IANA name, in any letter case and including aliases, or a fixed offset. Time.utc is the named zone UTC, and a named zone never equals an offset zone:
Time.zone("asia/calcutta") == Time.zone("Asia/Kolkata") // true
Time.zoneId(Result.unwrap(Time.zone("+0530"))) // "+05:30"
Time.zone("Mars/Olympus") // Err{ error: Error{ message: "unknown time zone \"Mars/Olympus\"" } }
Time.fromEpochMillis(0).inZone(Time.utc) // 1970-01-01T00:00:00+00:00[UTC]
Time.utc == Result.unwrap(Time.zone("+00:00")) // falseTime.with replaces fields and constrains the result to a real date:
let day = Time.plainDate(2026, 10, 22)
day.with(month: 13, day: 31) // 2026-12-31
Time.plainTime(9, 15).with(minute: 0) // 09:00:00A value typed by one of the unions, such as Dated, narrows with is:
let t: Time.Dated = Time.plainDate(2026, 10, 22)
t.year() // 2026
t is Time.PlainDate // trueCalls chain through a pipe:
Time.plainDate(2026, 10, 22) | Time.add(Time.duration(days: 10)) | Time.month() // 11Functions by job
Types
- Instant: a moment on the time line, with no zone or calendar
- ZonedDateTime: an instant seen in a zone
- PlainDate, PlainTime, PlainDateTime: a calendar date, a clock time, and both, with no zone
- Zone: an IANA zone or a fixed offset
- Duration: a span in ten fields, kept as given
- Point, Wall, Dated, Timed: the unions of time types that functions take
- Unit, Disambiguation, RoundingMode: the options of the functions
Zones
- Time.zone: a zone from its name or offset, or an
Err - Time.localZone: the host's zone
- Time.zoneId: the name or offset of a zone
Time.utc: the named zone UTC
Now, waiting, and the epoch
- Time.now: the current instant
- Time.nowIn: the current time in a zone
- Time.monotonic: milliseconds from an arbitrary origin, for measuring
- Time.sleep: block the goroutine for a duration
- Time.ticker: a channel that receives the time every interval
- Time.fromEpochMillis, Time.fromEpochNanos: an instant from a count since the Unix epoch
- Time.epochMillis, Time.epochNanos: the count since the epoch
Construction and text
- Time.plainDate, Time.plainTime, Time.plainDateTime: build a plain value; panics on one that cannot exist
- Time.duration: build a duration from named fields; a constant span is a literal such as
2h30m - Time.parseInstant, Time.parseZoned, Time.parsePlainDate, Time.parsePlainTime, Time.parsePlainDateTime, Time.parseDuration: read a value from text, or an
Err
Conversion
- Time.inZone: an instant seen in a zone
- Time.withZone: the same instant in another zone
- Time.toZoned: a wall-clock time placed in a zone
- Time.possibleInstants: every instant a wall-clock time has in a zone
- Time.toInstant, Time.toPlainDate, Time.toPlainTime, Time.toPlainDateTime: one part of a value
- Time.startOfDay: the first instant of the day in its zone
- Time.zoneOf, Time.offset, Time.offsetSeconds: the zone and its offset at that moment
- Time.hoursInDay: the length of the day in its zone
- Time.nextTransition, Time.previousTransition: the nearest change of offset
Fields
- Time.year, Time.month, Time.day: the date
- Time.dayOfWeek, Time.dayOfYear: the day's place in the week and the year
- Time.weekOfYear, Time.yearOfWeek: the ISO week and its year
- Time.daysInMonth, Time.daysInYear, Time.inLeapYear: the size of the month and the year
- Time.hour, Time.minute, Time.second, Time.millisecond, Time.microsecond, Time.nanosecond: the time
- Time.with: a copy with some fields replaced
Arithmetic
- Time.add, Time.subtract: move a value by a duration
- Time.until, Time.since: the duration between two values
- Time.round: a value rounded to a unit
- Time.compare: the order of two values
Durations
- Time.part: one field of a duration
- Time.sign, Time.negate, Time.abs: the sign of a duration
- Time.addDuration: the sum of two durations
- Time.compareDuration: the order of two durations by length
- Time.total: a duration as a number of one unit
- Time.roundDuration: a duration balanced and rounded
Applications
Time.now() is the current Instant, and Time.nowIn(zone) is the wall-clock time in that zone:
let now = Time.nowIn(Time.localZone())
println(now.zoneOf() == Time.localZone())trueCompute an age in whole years:
func age(birthday: Time.PlainDate, today: Time.PlainDate): int {
Time.until(birthday, today, largestUnit: Time.Year{}).part(Time.Year{})
}
age(Time.plainDate(1990, 10, 23), Time.plainDate(2026, 10, 22)) // 35Schedule a weekly 9:00 meeting in New York. Weeks move the wall clock, so the meeting stays at 9:00 when daylight saving ends and the instant moves an hour:
let ny = Result.unwrap(Time.zone("America/New_York"))
let first = Time.plainDateTime(Time.plainDate(2026, 10, 26), Time.plainTime(9)).toZoned(ny)
for week in [0, 1, 2] {
let at = first.add(Time.duration(weeks: week))
println("${at.toPlainDateTime()} ${at.toInstant()}")
}2026-10-26T09:00:00 2026-10-26T13:00:00Z
2026-11-02T09:00:00 2026-11-02T14:00:00Z
2026-11-09T09:00:00 2026-11-09T14:00:00ZSort timestamps read from text. Time.compare is the ordering:
let events = ["2026-10-22T14:03:05Z", "2026-03-01T08:00:00Z", "2026-07-15T12:00:00Z"]
| Array.map({ Result.unwrap(Time.parseInstant(x)) })
| Array.sortWith(Time.compare)
println(events)[2026-03-01T08:00:00Z, 2026-07-15T12:00:00Z, 2026-10-22T14:03:05Z]Measure how long a piece of work takes with Time.monotonic:
let start = Time.monotonic()
Time.sleep(20ms)
let elapsed = Time.monotonic() - start
println(elapsed >= 20)trueAct on a beat with a ticker that stops with its context:
let ctx = Context.new(timeout: 1s)
defer { ctx.cancel() }
let ticks = Time.ticker(10ms, ctx: ctx)
for i in [1, 2, 3] {
ticks.receive()
println("tick ${i}")
}tick 1
tick 2
tick 3Properties and relations
A value renders as text that parses back to an equal value:
let t = Time.plainDateTime(Time.plainDate(2026, 10, 22), Time.plainTime(14, 3, 5, 500))
assert(Result.unwrap(Time.parsePlainDateTime("${t}")) == t)Two zoned values at one moment in different zones compare as 0 but are not ==:
let moment = Time.fromEpochMillis(0)
let utc = moment.inZone(Time.utc)
let kolkata = moment.inZone(Result.unwrap(Time.zone("Asia/Kolkata")))
assert(Time.compare(utc, kolkata) == 0)
assert(utc != kolkata)
assert(utc.toInstant() == kolkata.toInstant())Durations compare by fields with == and by length with Time.compareDuration:
let a = 90m
let b = 1h30m
assert(a != b)
assert(Time.compareDuration(a, b) == 0)
assert(Time.roundDuration(a, largestUnit: Time.Hour{}) == b)Possible issues
A day added in a zone is not always 24 hours. On the day daylight saving starts, a day is 23 hours:
let ny = Result.unwrap(Time.zone("America/New_York"))
let sat = Time.plainDateTime(Time.plainDate(2026, 3, 7), Time.plainTime(12)).toZoned(ny)
sat.add(Time.duration(days: 1)) // 2026-03-08T12:00:00-04:00[America/New_York]
sat.add(24h) // 2026-03-08T13:00:00-04:00[America/New_York]A constructor panics on a date that does not exist:
Time.plainDate(2026, 2, 30) // panic: Time.plainDate: no such date: 2026-02-30An Instant has no calendar, so calendar units panic on it. Convert it to a ZonedDateTime first:
Time.fromEpochMillis(0).add(Time.duration(days: 1)) // panic: Time.add: an instant has no calendar, so it takes no days; convert it with Time.inZone first
Time.fromEpochMillis(0).inZone(Time.utc).add(Time.duration(days: 1)) // 1970-01-02T00:00:00+00:00[UTC]< does not compare time values:
let a = Time.plainDate(2026, 1, 1)
let b = Time.plainDate(2026, 2, 1)
let earlier = a < b
// error: < applies to a single numeric kind, string, or a named primitive type over one, not to Time.PlainDatelet a = Time.plainDate(2026, 1, 1)
let b = Time.plainDate(2026, 2, 1)
Time.compare(a, b) < 0 // trueA value typed by a union passes to Time.year, but not to Time.add, Time.until, Time.compare, or Time.with, which take a single member of the union:
let t: Time.Dated = Time.plainDate(2026, 10, 22)
let next = Time.add(t, Time.duration(days: 1))
// error: T would be Time.Dated, which is not a single member of its bound Time.Point; narrow firstNarrow it with is first:
let t: Time.Dated = Time.plainDate(2026, 10, 22)
if t is Time.PlainDate { Time.add(t, Time.duration(days: 1)) } // 2026-10-23Comparing two duration literals of the same length with == is an error, since == compares the fields as written:
let same = 90m == 1h30m
// error: 90m == 1h30m compares the fields as written, and == is false: compare the lengths with Time.compareDuration(90m, 1h30m)Time.compareDuration(90m, 1h30m) // 0See also
- Context: deadlines and cancellation, measured in a
Time.Duration - Channel: receive from a Time.ticker, or wait in a
select - Json: time values as JSON strings
