Alpha. Vary is under active development and not ready for production use. Syntax, APIs, performance, and behaviour may change between releases.

Time

import time

let start = time.now()
time.sleep_ms(100)
let elapsed = time.now().diff(start)
print(elapsed.to_millis())  # milliseconds elapsed

Instant

An Instant represents a point in time (milliseconds since epoch).

FunctionReturnsDescription
time.now()InstantCurrent wall-clock time
time.now_epoch_millis()IntCurrent time as epoch milliseconds
time.now_epoch_seconds()FloatCurrent time as epoch seconds
instant_from_epoch_millis(ms)InstantCreate Instant from epoch ms
instant_from_epoch_seconds(s)InstantCreate Instant from epoch seconds

Instant Methods

MethodReturnsDescription
.epoch_millis()IntEpoch milliseconds
.epoch_seconds()FloatEpoch seconds
.add(duration)InstantAdd a Duration
.sub(duration)InstantSubtract a Duration
.diff(other)DurationDuration between two instants
.format_iso()StrISO 8601 format

Duration

A Duration represents a time span in milliseconds.

FunctionReturnsDescription
duration_ms(ms)DurationFrom milliseconds
duration_seconds(s)DurationFrom seconds (Float)
duration_minutes(m)DurationFrom minutes
duration_hours(h)DurationFrom hours

Duration Methods

MethodReturnsDescription
.to_millis()IntMilliseconds
.to_seconds()FloatSeconds
.is_zero()BoolTrue if zero
.is_negative()BoolTrue if negative
.abs()DurationAbsolute value

Sleep

FunctionReturnsDescription
time.sleep(duration)NoneSleep for a Duration
time.sleep_ms(ms)NoneSleep for milliseconds
time.sleep_seconds(s)NoneSleep for seconds (Float)

Monotonic Time

FunctionReturnsDescription
monotonic_now_nanos()IntMonotonic nanoseconds
monotonic_now_millis()IntMonotonic milliseconds
measure(block)TimedResultTime a block's execution

Calendar

FunctionReturnsDescription
time.current_year()IntCurrent calendar year
time.now_local()ZonedDateTimeCurrent time in system zone

Clock Abstraction

FunctionReturnsDescription
system_clock()ClockReal system clock
mock_clock(start)ClockDeterministic mock clock
set_clock(clock)NoneReplace global clock
reset_clock()NoneRestore system clock
with_clock(clock, block)NoneScoped clock override

Duration Arithmetic

FunctionReturnsDescription
add_duration(a, b)DurationSum of two durations
sub_duration(a, b)DurationDifference of two durations
min_duration(a, b)DurationSmaller of two durations
max_duration(a, b)DurationLarger of two durations

Instant Comparison

FunctionReturnsDescription
before(a, b)BoolTrue if instant a is before b
after(a, b)BoolTrue if instant a is after b
min_instant(a, b)InstantEarlier of two instants
max_instant(a, b)InstantLater of two instants

ISO Formatting and Parsing

All parsing functions return Result[T, TimeError]. Use import time then time.function_name().

FunctionReturnsDescription
format_iso_instant(instant)StrFormat an Instant as ISO 8601 UTC string
parse_instant_iso(text)Result[Instant, TimeError]Parse an ISO 8601 UTC string to Instant
format_iso_date(d)StrFormat a LocalDate as ISO 8601 (YYYY-MM-DD)
format_iso_time(t)StrFormat a LocalTime as ISO 8601 (HH:MM:SS.mmm)
format_iso_datetime(dt)StrFormat a LocalDateTime as ISO 8601
parse_date_iso(text)Result[LocalDate, TimeError]Parse an ISO 8601 date string
parse_time_iso(text)Result[LocalTime, TimeError]Parse an ISO 8601 time string
parse_datetime_iso(text)Result[LocalDateTime, TimeError]Parse an ISO 8601 datetime string

Calendar Types

FunctionReturnsDescription
date(year, month, day)Result[LocalDate, TimeError]Construct a local date
local_time(hour, minute, second, millisecond)Result[LocalTime, TimeError]Construct a local time
datetime(d, t)LocalDateTimeCombine a LocalDate and LocalTime

LocalDate Methods

MethodReturnsDescription
.year()IntCalendar year
.month()IntMonth (1-12)
.day()IntDay of month (1-31)

LocalTime Methods

MethodReturnsDescription
.hour()IntHour (0-23)
.minute()IntMinute (0-59)
.second()IntSecond (0-59)
.millisecond()IntMillisecond (0-999)

LocalDateTime Methods

MethodReturnsDescription
.date()LocalDateThe date component
.time()LocalTimeThe time component

Timezone

FunctionReturnsDescription
system_zone()StrIANA name of the system timezone
available_zones()List[Str]All available IANA timezone names
zone(name)Result[Str, TimeError]Validate and return a timezone name
now_in(tz)Result[ZonedDateTime, TimeError]Current time in the given timezone
convert_zone(dt, tz)Result[ZonedDateTime, TimeError]Convert a ZonedDateTime to another timezone
instant_in_zone(instant, tz)Result[ZonedDateTime, TimeError]Convert an Instant to a ZonedDateTime in a timezone

ZonedDateTime Methods

MethodReturnsDescription
.instant()InstantThe underlying UTC instant
.zone()StrIANA timezone name
.year()IntLocal calendar year
.month()IntLocal month (1-12)
.day()IntLocal day of month (1-31)
.hour()IntLocal hour (0-23)
.minute()IntLocal minute (0-59)
.second()IntLocal second (0-59)

Pattern Formatting

Patterns follow Java DateTimeFormatter conventions (e.g. "yyyy-MM-dd HH:mm:ss").

FunctionReturnsDescription
format(instant, pattern, zone)Result[Str, TimeError]Format an Instant using a pattern in a timezone
format_local(dt, pattern)Result[Str, TimeError]Format a LocalDateTime using a pattern
parse_instant(text, pattern, zone)Result[Instant, TimeError]Parse an Instant from text using a pattern and timezone
parse_local_datetime(text, pattern)Result[LocalDateTime, TimeError]Parse a LocalDateTime from text using a pattern

TimeError

A structured error returned by parsing, validation, and timezone functions.

MethodReturnsDescription
.kind()StrError category (see below)
.message()StrHuman-readable description

Valid .kind() values:

KindWhen raised
"invalid_date"Year/month/day out of range or non-existent
"invalid_time"Hour/minute/second/millisecond out of range
"invalid_datetime"Combined date-time is invalid
"invalid_zone"Timezone name not recognised
"parse_error"Input text did not match the expected format
"format_error"Pattern string is malformed
"out_of_range"Value exceeds representable range

Construct a TimeError directly with time_error(kind, message)TimeError.