Skip to content

Time & Duration API

Time Struct

Internal representation: nanoseconds since Unix epoch (i64).

Constructors

FunctionDescription
now()Current wall-clock time
Time { ns: value }From raw nanoseconds

Calendar Accessors

MethodReturnDescription
.year(): i642024Year
.month(): MonthMonth.JuneMonth enum
.day(): i6415Day of month
.hour(): i6414Hour (0–23)
.minute(): i6430Minute (0–59)
.second(): i640Second (0–59)
.weekday(): WeekdayWeekday.SaturdayDay of week

Unix Representations

MethodDescription
.unix(): i64Seconds since epoch
.unixNano(): i64Nanoseconds since epoch

Arithmetic

rust
let t2 = t.add(duration_ns);    // Add nanoseconds → new Time
let diff = t1.sub(t2);          // Subtract → i64 nanoseconds

// Free function variants
let t3 = add(t, duration);      // Time + Duration → Time
let d = sub(t1, t2);            // Time - Time → Duration

Fluent API (Day.js Style)

Truncate or round time to boundaries:

rust
let t = now();

// Start of day (midnight)
let dayStart = t.startOf("day");

// End of current hour
let hourEnd = t.endOf("hour");

// Start of month
let monthStart = t.startOf("month");

Supported units: "year" / "y", "month" / "M", "day" / "d", "hour" / "h", "minute" / "m", "second" / "s"

Formatting

rust
println("{t}");  // ISO 8601: "2024-06-15T14:30:00Z"

Duration Struct

MethodDescription
Duration { ns: i64 }Create from nanoseconds
.nsRaw nanosecond value

Time Constants

rust
import { NANOSECOND, MICROSECOND, MILLISECOND, SECOND, MINUTE, HOUR } from "time";

sleep(500 * MILLISECOND);
let timeout = 30 * SECOND;
let interval = 1 * HOUR;

Parsing

rust
import { parse } from "time";

let t = parse("2024-06-15T14:30:00Z");     // ISO 8601
let t = parse("2024-06-15 14:30:00");       // Common format

Enums

rust
enum Month { January, February, ..., December }
enum Weekday { Sunday, Monday, ..., Saturday }

Released under the MIT License.