// reference

Time Libraries

This page documents the date/time libraries shipped under lib/time. These are library classes, not core built-in classes. Load them with intern.

time/Duration

Duration is a time span stored as a whole number of milliseconds, shipped as a Nex library under lib/time/duration.nex. It is an immutable value: arithmetic returns a new Duration.

Loading

intern time/Duration

Support

TargetSupported
JVM REPL / interpreterYes
Generated JVM codeYes

Construction

let timeout: Duration := create Duration.seconds(30)
let sprint: Duration := create Duration.weeks(2)

Each constructor names its unit. Durations may be negative.

Methods

MethodArgumentsReturnsDescription
millisecondsms: IntegerDurationA span of ms milliseconds.
secondsn: IntegerDurationA span of n seconds.
minutesn: IntegerDurationA span of n minutes.
hoursn: IntegerDurationA span of n hours.
daysn: IntegerDurationA span of n 24-hour days.
weeksn: IntegerDurationA span of n 7-day weeks.
total_millisecondsnoneIntegerThe span in milliseconds.
total_secondsnoneRealThe span in seconds, including the fractional part.
plusother: DurationDurationThe sum of the two spans.
minusother: DurationDurationThis span minus other; may be negative.
to_stringnoneStringA descriptive string such as "Duration(300000 ms)".

Example

intern time/Duration

let a: Duration := create Duration.minutes(5)
let b: Duration := create Duration.seconds(30)

print(a.plus(b).total_seconds())        -- 330.0
print(a.minus(b).total_milliseconds())  -- 270000
print(create Duration.days(1).total_milliseconds())  -- 86400000
print(a)                                -- Duration(300000 ms)

Notes

  • Day and week durations are fixed multiples of 24 hours; they do not account for calendar effects.
  • There is no month or year duration, since those vary in length.

time/Date_Time

Date_Time is a UTC instant stored as milliseconds since the Unix epoch, shipped as a Nex library under lib/time/date_time.nex. It is an immutable value: add, subtract and the truncation methods return a new Date_Time.

Loading

intern time/Date_Time

Support

TargetSupported
JVM REPL / interpreterYes
Generated JVM codeYes

Construction

let now: Date_Time := create Date_Time.now()
let launch: Date_Time := create Date_Time.make(2026, 3, 13, 10, 30, 0)
let parsed: Date_Time := create Date_Time.parse_iso("2026-03-13T10:30:00Z")
let epoch: Date_Time := create Date_Time.from_epoch_millis(0)

intern time/Date_Time also loads time/Duration.

Parsing

parse_iso accepts:

  • A UTC instant with a Z suffix: "2026-03-13T10:30:00Z".
  • A date-time with an offset, converted to UTC: "2026-03-13T12:30:00+02:00".
  • A local date-time with no zone, interpreted as UTC: "2026-03-13T10:30:00".

Methods

MethodArgumentsReturnsDescription
nownoneDate_TimeThe current instant.
from_epoch_millisms: IntegerDate_TimeThe instant ms milliseconds after 1970-01-01T00:00:00Z.
parse_isotext: StringDate_TimeParse an ISO-8601 date-time (see Parsing). Raises on malformed input.
makeyear, month, day, hour, minute, second: IntegerDate_TimeThe given UTC calendar date and time. Raises when a field is out of range.
yearnoneIntegerThe calendar year.
monthnoneIntegerThe month, 1 (January) to 12.
month_namenoneStringThe English month name, e.g. "March".
daynoneIntegerThe day of the month, 1 to 31.
weekdaynoneIntegerThe ISO day of the week: 1 = Monday through 7 = Sunday.
weekday_namenoneStringThe English day name, e.g. "Friday".
day_of_yearnoneIntegerThe day of the year, 1 to 366.
hournoneIntegerThe hour, 0 to 23.
minutenoneIntegerThe minute, 0 to 59.
secondnoneIntegerThe second, 0 to 59.
epoch_millisnoneIntegerMilliseconds since the Unix epoch.
addduration: DurationDate_TimeThe instant duration later.
subtractduration: DurationDate_TimeThe instant duration earlier.
differenceother: Date_TimeDurationthis - other: positive when this instant is later.
truncate_to_daynoneDate_TimeMidnight UTC at the start of the same day.
truncate_to_hournoneDate_TimeThe start of the same hour.
is_beforeother: Date_TimeBooleanTrue when this instant is strictly earlier than other.
is_afterother: Date_TimeBooleanTrue when this instant is strictly later than other.
format_isononeStringISO-8601 UTC text such as "2026-03-13T10:30:00Z".
to_stringnoneStringSame as format_iso().

Example

intern time/Duration
intern time/Date_Time

let start: Date_Time := create Date_Time.make(2026, 3, 13, 10, 30, 0)
let later: Date_Time := start.add(create Duration.hours(2))

print(start.format_iso())                     -- "2026-03-13T10:30:00Z"
print(start.month_name())                     -- "March"
print(start.weekday())                        -- 5
print(start.weekday_name())                   -- "Friday"
print(start.day_of_year())                    -- 72
print(start.truncate_to_day().format_iso())   -- "2026-03-13T00:00:00Z"
print(start.truncate_to_hour().format_iso())  -- "2026-03-13T10:00:00Z"
print(later.hour())                           -- 12
print(later.difference(start).total_seconds())  -- 7200.0
print(start.is_before(later))                 -- true

let local: Date_Time := create Date_Time.parse_iso("2026-03-13T12:30:00+02:00")
print(local.format_iso())                     -- "2026-03-13T10:30:00Z"

Notes

  • All calendar fields are computed in UTC; there is no local time zone support.
  • format_iso() includes fractional seconds only when they are non-zero, e.g. "1970-01-01T00:00:01.500Z".
  • Comparison uses is_before / is_after or epoch_millis(); equal instants are neither before nor after each other.