Fecha y Hora (std::datetime)
En esta página
std::datetime proporciona acceso al instante actual del sistema, conversión entre
timestamps Unix y representaciones de calendario, formateo y análisis de strings
de fecha, y aritmética simple de días y horas.
Todos los ejemplos siguientes requieren el host y no se pueden ejecutar en el sandbox WASM del navegador; ejecútalos con la CLI de Zolo.
Instante actual
datetime.now() devuelve un objeto con los campos year, month, day,
hour, minute, second, millisecond y timestamp (segundos Unix como
float). El módulo también ofrece predicados de calendario:
Lee los campos del instante actual y demuestra is_leap_year / days_in_month.
// 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
Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.
Formateo
datetime.to_iso produce una cadena ISO-8601 UTC; datetime.format acepta
códigos %Y %m %d %H %M %S para salidas personalizadas. Usa
datetime.from_timestamp(seconds) para crear un objeto desde un timestamp
estático y obtener resultados deterministas:
Convierte el epoch Unix en cadenas legibles con to_iso y format.
// 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
Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.
Análisis y aritmética
datetime.parse detecta el formato automáticamente; datetime.from_timestamp
convierte un número de segundos. add_days y add_hours operan sobre
timestamps (devuelven un nuevo timestamp Int) — vuelve a envolverlo con
from_timestamp para leer los campos de calendario:
Analiza "2024-06-15", avanza 3 días y 5 horas con aritmética de timestamp.
// 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
// 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
Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.