Time.now
func Time.now(): Time.InstantTime.now returns the current instant from the wall clock, which the host may set forward or back, so a later call can return an earlier instant; measure elapsed time with Time.monotonic.
Details
- The result has no zone and no calendar. Fields such as the year or the hour need a zone: use Time.nowIn, or Time.inZone on the result.
- To measure how long something takes, use Time.monotonic, which is meant for that.
Basic examples
let now = Time.now()
println(now)
println(now.inZone(Time.zone("Asia/Tokyo").unwrap()))It prints two lines such as 2026-11-11T21:27:07.050436Z and 2026-11-12T06:27:07.050436+09:00[Asia/Tokyo]: the instant, then the same instant seen in Tokyo.
The value changes on every run, but how it compares to a fixed instant does not:
Time.compare(Time.now(), Time.fromEpochMillis(0)) // 1Applications
Tell whether a token has expired, by comparing now with the time it was issued plus its lifetime:
type Token = { value: string, issued: Time.Instant }
func expired(t: Token, ttl: Time.Duration): bool {
Time.compare(Time.now(), t.issued.add(ttl)) > 0
}
let old = Token{ value: "a1", issued: Time.parseInstant("2020-01-01T00:00:00Z").unwrap() }
let fresh = Token{ value: "b2", issued: Time.now() }
println(expired(old, 1h))
println(expired(fresh, 1h))true
falseStamp each log line with the time, as milliseconds from the Unix epoch:
type LogLine = { at: int, message: string }
func log(message: string) {
println(Json.stringify(LogLine{ at: Time.now().epochMillis(), message }))
}
log("started")It prints a line such as {"at":1794432427050,"message":"started"}.
Keep trying until a deadline passes:
let deadline = Time.now().add(50ms)
var tries = 0
while Time.compare(Time.now(), deadline) < 0 {
tries += 1
Time.sleep(10ms)
}
println(tries > 0)trueProperties and relations
Time.inZone shows the same instant in a zone, and Time.toInstant gives it back:
let now = Time.now()
let tokyo = now.inZone(Time.zone("Asia/Tokyo").unwrap())
assert(tokyo.toInstant() == now)The current time lies within the range of Time.epochNanos, so it round-trips through nanoseconds:
let now = Time.now()
assert(Time.fromEpochNanos(now.epochNanos()) == now)Possible issues
The comparison operators do not apply to time values. Use Time.compare:
let deadline = Time.parseInstant("2030-01-01T00:00:00Z").unwrap()
let early = Time.now() < deadline
// error: < applies to a single numeric kind, string, or a named primitive type over one, not to Time.InstantAn instant has no year, month or hour until it is seen in a zone, so asking for one is a compile error:
let year = Time.year(Time.now()) // error: cannot use Time.Instant where Time.Dated is expectedAsk Time.nowIn for the current time in a zone instead:
let year = Time.nowIn(Time.utc).year()
println(year)Each call reads the clock again. When several values must agree on one moment, such as the start and the end of a window, read the clock once and use that value:
let now = Time.now()
let start = now.subtract(1h)
println(Time.until(start, now, largestUnit: Time.Hour{}))PT1HSee also
Time.nowIn · Time.monotonic · Time.inZone · Time.compare · Time.epochMillis · Time.since · Time.Instant
