Skip to content

27 · Standard Library

Date and Time (std::datetime)

Get the current instant, format and parse dates, and perform calendar arithmetic with std::datetime.

On this page

std::datetime provides access to the current system instant, conversion between Unix timestamps and calendar representations, formatting and parsing of date strings, and simple day and hour arithmetic.

All examples below require the host and cannot be run in the browser WASM sandbox; run them with the Zolo CLI.


Current instant

datetime.now() returns an object with the fields year, month, day, hour, minute, second, millisecond and timestamp (Unix seconds as a float). The module also provides calendar predicates:

Reads the fields of the current instant and demonstrates is_leap_year / days_in_month.

01-now.zolo
// Feature: datetime.now — current instant
// When to use: timestamps for logs, time measurement, metadata.

use std::datetime

let now = datetime.now()

// Expected fields:
//   year, month, day, hour, minute, second, millisecond, timestamp.
print(now.year >= 2024)  // expected: true
print(now.month >= 1 && now.month <= 12)  // expected: true
print(now.day >= 1 && now.day <= 31)  // expected: true
print(now.hour >= 0 && now.hour <= 23)  // expected: true
print(now.timestamp > 0.0)  // expected: true

// Useful: extract just the year for a log header.
print("current year: {now.year}")

// is_leap_year — predicate.
print(datetime.is_leap_year(2024))  // expected: true
print(datetime.is_leap_year(2023))  // expected: false

// days_in_month.
print(datetime.days_in_month(2024, 2))  // expected: 29
print(datetime.days_in_month(2023, 2))  // expected: 28
print(datetime.days_in_month(2024, 7))  // expected: 31

Requires the Zolo CLI/host — open in the playground or run locally.


Formatting

datetime.to_iso produces a UTC ISO-8601 string; datetime.format accepts %Y %m %d %H %M %S codes for custom output. Use datetime.from_timestamp(seconds) to create an object from a static timestamp and get deterministic results:

Converts the Unix epoch to human-readable strings with to_iso and format.

02-format.zolo
// Feature: datetime.format / datetime.to_iso — formatting dates
// When to use: produce human-readable or serializable strings (ISO-8601).

use std::datetime

// from_timestamp gives a stable, deterministic value.
// 0 = 1970-01-01T00:00:00Z (epoch).
let epoch = datetime.from_timestamp(0)

// to_iso — produces an ISO-8601 UTC string.
let iso = datetime.to_iso(epoch)
print(iso.starts_with("1970-01-01"))  // expected: true

// format — uses codes: %Y (year), %m (month), %d (day),
// %H (hour), %M (min), %S (sec).
print(datetime.format(epoch, "%Y-%m-%d"))  // expected: 1970-01-01
print(datetime.format(epoch, "%H:%M:%S"))  // expected: 00:00:00

// to_iso also accepts a numeric timestamp directly.
print(datetime.to_iso(0).starts_with("1970"))  // expected: true

Requires the Zolo CLI/host — open in the playground or run locally.


Parsing and arithmetic

datetime.parse detects the format automatically and returns (date, nil) on success or (nil, message) on invalid input on every host backend; datetime.from_timestamp converts a number of seconds. add_days and add_hours operate on timestamps (returning a new Int timestamp) — re-wrap with from_timestamp to read the calendar fields:

Parses "2024-06-15", advances 3 days and 5 hours with timestamp arithmetic.

03-parse.zolo
// Feature: datetime.parse / datetime.from_timestamp — input

// When to use: read dates coming from logs, configs, APIs.


use std::datetime

// from_timestamp — takes seconds since epoch.

let dt = datetime.from_timestamp(1700000000)
print(dt.year == 2023)  // expected: true


// parse — format is auto-detected by default.

let parsed = datetime.parse("2024-06-15")
print(parsed.year)  // expected: 2024

print(parsed.month)  // expected: 6

print(parsed.day)  // expected: 15


// Force the ISO grammar at an API boundary. This is a domain operation,

// never a cast from `str`.

let parsed_iso = datetime.parse("2024-06-15T12:30:00Z", "iso")
print(datetime.to_iso(parsed_iso))  // expected: 2024-06-15T12:30:00Z


// add_days — calendar arithmetic. NOTE: add_days/add_hours operate

// on timestamps (Int seconds since epoch) and return a timestamp;

// re-wrap with from_timestamp to read calendar fields.

let plus3_ts = datetime.add_days(parsed.timestamp, 3)
let plus3    = datetime.from_timestamp(plus3_ts)
print(plus3.day)  // expected: 18


// add_hours.

let plus5h_ts = datetime.add_hours(parsed.timestamp, 5)
let plus5h    = datetime.from_timestamp(plus5h_ts)
print(plus5h.hour)  // expected: 5

Requires the Zolo CLI/host — open in the playground or run locally.

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br