Module std::time::datetime

Date and time operations.

Work with timestamps as millisecond-precision epoch values. Extract date/time components, format and parse date strings, and perform time arithmetic.

Examples

import std::time::datetime;

fn main() {
    let now = datetime.now_ms();
    let iso = datetime.to_iso8601(now);
    println(iso);
    let year = datetime.year(now);
    println(year);
}

Contents

Functions

Function now_ms

pub fn now_ms() -> i64

Return the current time as milliseconds since the Unix epoch.

Function now_secs

pub fn now_secs() -> i64

Return the current time as seconds since the Unix epoch.

Function format

pub fn format(epoch_ms: i64, fmt: string) -> string

Format an epoch-millisecond timestamp using a strftime-style format string.

Examples

let s = datetime.format(now, "%Y-%m-%d");

Function parse

pub fn parse(s: string, fmt: string) -> i64

Parse a datetime string using the given format and return epoch milliseconds.

Panics if the input does not match the format. Use try_parse for a structured Result<i64, string> failure surface.

Function try_parse

pub fn try_parse(s: string, fmt: string) -> Result<i64, string>

Parse a datetime string using the given format and return epoch milliseconds.

Returns Err(string) with the native parser message when parsing fails.

Function year

pub fn year(epoch_ms: i64) -> i64

Extract the year component from an epoch-millisecond timestamp.

Function month

pub fn month(epoch_ms: i64) -> i64

Extract the month (1–12) from an epoch-millisecond timestamp.

Function day

pub fn day(epoch_ms: i64) -> i64

Extract the day of the month (1–31) from an epoch-millisecond timestamp.

Function hour

pub fn hour(epoch_ms: i64) -> i64

Extract the hour (0–23) from an epoch-millisecond timestamp.

Function minute

pub fn minute(epoch_ms: i64) -> i64

Extract the minute (0–59) from an epoch-millisecond timestamp.

Function second

pub fn second(epoch_ms: i64) -> i64

Extract the second (0–59) from an epoch-millisecond timestamp.

Function weekday

pub fn weekday(epoch_ms: i64) -> i64

Return the day of the week (0 = Monday, 6 = Sunday).

Function add_days

pub fn add_days(epoch_ms: i64, days: i64) -> i64

Add a number of days to an epoch-millisecond timestamp.

Function add_hours

pub fn add_hours(epoch_ms: i64, hours: i64) -> i64

Add a number of hours to an epoch-millisecond timestamp.

Function diff_secs

pub fn diff_secs(a: i64, b: i64) -> i64

Return the difference in seconds between two epoch-millisecond timestamps.

Function to_iso8601

pub fn to_iso8601(epoch_ms: i64) -> string

Format an epoch-millisecond timestamp as an ISO 8601 string.

Examples

let iso = datetime.to_iso8601(datetime.now_ms());
// e.g. "2025-07-10T14:30:00Z"

Function now_nanos

pub fn now_nanos() -> i64

Return the current monotonic clock time in nanoseconds.

Uses a monotonic clock not affected by wall-clock adjustments, suitable for high-resolution benchmarking and timing measurements.