datetime
lib/datetime.vklib/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 { 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.
leap-year? = fn [year i64] bool
Returns true if year is a Gregorian leap year.
use [dt = datetime]
if (not (dt/leap-year? 2024)) or (dt/leap-year? 1900) or (not (dt/leap-year? 2000)) [ err 'leap' ] else [ .ok ]
days-in-month = fn [year i64, month i64] i64
Returns the number of days in month (1..12) of year.
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.
use [dt = datetime]
t = dt/from-unix 0 0
if (dt/format-rfc3339 t) != '1970-01-01T00:00:00Z' [ err 'epoch' ] else [ .ok ]
from-unix-ns = fn [unix_ns i64] DateTime
Converts a Unix epoch timestamp in nanoseconds (e.g. from time/unix) to a UTC DateTime.
to-unix = fn [t DateTime] i64
Converts t to Unix epoch seconds (accounting for t.offset_sec).
weekday = fn [t DateTime] i64
Returns the ISO weekday (0 = Sunday, 1 = Monday, ..., 6 = Saturday).
use [dt = datetime]
// 1970-01-01 was a Thursday (4)
if (dt/weekday (dt/from-unix 0 0)) != 4 [ err 'weekday' ] else [ .ok ]
add-seconds = fn [t DateTime, delta_sec i64] DateTime
Returns a new DateTime shifted by delta_sec seconds, preserving t.offset_sec.
add-days = fn [t DateTime, delta_days i64] DateTime
Returns a new DateTime shifted by delta_days days.
with-offset = fn [t DateTime, new_offset_sec i64] DateTime
Converts t to the same instant expressed at new_offset_sec east of UTC.
diff-seconds = fn [a DateTime, b DateTime] i64
Returns the difference (a - b) in seconds.
format-rfc3339 = fn [t DateTime] str
Formats t as an RFC 3339 / ISO 8601 timestamp ('YYYY-MM-DDTHH:MM:SS[.fff][Z|+HH:MM]').
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 ]
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').
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 ]
parse-rfc3339 = fn [s str] DateTime
Parses an RFC 3339 / ISO 8601 timestamp string into a DateTime.
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 ]