veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

datetime

lib/datetime.vk

lib/datetime.vk Pure-Veko proleptic Gregorian calendar, UTC/offset DateTime arithmetic, RFC 3339 / ISO 8601 parsing & formatting, and HTTP IMF-fixdate (RFC 9110). Pure calendar functions require zero capabilities; now-utc requires .clocks.

DateTime struct
signature
DateTime = struct { year i64, month i64, day i64, hour i64 = ..., minute i64 = ..., second i64 = ..., nanosecond i64 = ..., offset_sec i64 = ... }

Structured UTC / fixed-offset calendar date and time.

signature
leap-year? = fn [year i64] bool

Returns true if year is a Gregorian leap year.

Executable Example
veko
use [dt = datetime]
if (not (dt/leap-year? 2024)) or (dt/leap-year? 1900) or (not (dt/leap-year? 2000)) [ err 'leap' ] else [ .ok ]
signature
days-in-month = fn [year i64, month i64] i64

Returns the number of days in month (1..12) of year.

signature
from-unix = fn [unix_sec i64, ns i64] DateTime

Converts a Unix epoch timestamp in seconds (with optional sub-second ns) to a UTC DateTime.

Executable Example
veko
use [dt = datetime]
t = dt/from-unix 0 0
if (dt/format-rfc3339 t) != '1970-01-01T00:00:00Z' [ err 'epoch' ] else [ .ok ]
signature
from-unix-ns = fn [unix_ns i64] DateTime

Converts a Unix epoch timestamp in nanoseconds (e.g. from time/unix) to a UTC DateTime.

now-utc fn caps: .clocks
signature
now-utc = fn [] DateTime

Returns the current UTC DateTime via time/unix (requires .clocks).

signature
to-unix = fn [t DateTime] i64

Converts t to Unix epoch seconds (accounting for t.offset_sec).

signature
to-unix-ns = fn [t DateTime] i64

Converts t to Unix epoch nanoseconds.

signature
weekday = fn [t DateTime] i64

Returns the ISO weekday (0 = Sunday, 1 = Monday, ..., 6 = Saturday).

Executable Example
veko
use [dt = datetime]
// 1970-01-01 was a Thursday (4)
if (dt/weekday (dt/from-unix 0 0)) != 4 [ err 'weekday' ] else [ .ok ]
signature
add-seconds = fn [t DateTime, delta_sec i64] DateTime

Returns a new DateTime shifted by delta_sec seconds, preserving t.offset_sec.

signature
add-days = fn [t DateTime, delta_days i64] DateTime

Returns a new DateTime shifted by delta_days days.

signature
with-offset = fn [t DateTime, new_offset_sec i64] DateTime

Converts t to the same instant expressed at new_offset_sec east of UTC.

signature
diff-seconds = fn [a DateTime, b DateTime] i64

Returns the difference (a - b) in seconds.

signature
format-rfc3339 = fn [t DateTime] str

Formats t as an RFC 3339 / ISO 8601 timestamp ('YYYY-MM-DDTHH:MM:SS[.fff][Z|+HH:MM]').

Executable Example
veko
use [dt = datetime]
t = dt/DateTime { year = 2026, month = 12, day = 2, hour = 13, minute = 45, second = 9 }
if (dt/format-rfc3339 t) != '2026-12-02T13:45:09Z' [ err 'rfc3339' ] else [ .ok ]
signature
format-http = fn [t DateTime] str

Formats t in UTC as an HTTP IMF-fixdate (RFC 9110, e.g. 'Thu, 01 Jan 1970 00:00:00 GMT').

Executable Example
veko
use [dt = datetime]
if (dt/format-http (dt/from-unix 0 0)) != 'Thu, 01 Jan 1970 00:00:00 GMT' [ err 'http date' ] else [ .ok ]
signature
parse-rfc3339 = fn [s str] DateTime

Parses an RFC 3339 / ISO 8601 timestamp string into a DateTime.

Executable Example
veko
use [dt = datetime]
t = dt/parse-rfc3339 '2026-12-02T08:30:15.25+02:00'
if t.year != 2026 or t.month != 12 or t.day != 2 or t.nanosecond != 250000000 or t.offset_sec != 7200 [
  err 'parse rfc3339'
] else [ .ok ]