Skip to content

Time.now ​

ant
func Time.now(): Time.Instant

Time.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 ​

ant
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:

ant
Time.compare(Time.now(), Time.fromEpochMillis(0)) // 1

Applications ​

Tell whether a token has expired, by comparing now with the time it was issued plus its lifetime:

ant
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))
text
true
false

Stamp each log line with the time, as milliseconds from the Unix epoch:

ant
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:

ant
let deadline = Time.now().add(50ms)
var tries = 0
while Time.compare(Time.now(), deadline) < 0 {
  tries += 1
  Time.sleep(10ms)
}
println(tries > 0)
text
true

Properties and relations ​

Time.inZone shows the same instant in a zone, and Time.toInstant gives it back:

ant
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:

ant
let now = Time.now()
assert(Time.fromEpochNanos(now.epochNanos()) == now)

Possible issues ​

The comparison operators do not apply to time values. Use Time.compare:

ant
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.Instant

An instant has no year, month or hour until it is seen in a zone, so asking for one is a compile error:

ant
let year = Time.year(Time.now()) // error: cannot use Time.Instant where Time.Dated is expected

Ask Time.nowIn for the current time in a zone instead:

ant
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:

ant
let now = Time.now()
let start = now.subtract(1h)
println(Time.until(start, now, largestUnit: Time.Hour{}))
text
PT1H

See also ​

Time.nowIn · Time.monotonic · Time.inZone · Time.compare · Time.epochMillis · Time.since · Time.Instant