Time Module
The std/time module provides time manipulation utilities including timestamps, formatting, sleeping, and date components.
Import
Section titled “Import”import { time } from "std/time"Current Time
Section titled “Current Time”time.now(): int
Section titled “time.now(): int”Returns the current Unix timestamp in seconds.
const ts = time.now()println(ts) // e.g. 1719504000time.unix(): int
Section titled “time.unix(): int”Alias for time.now(). Returns the current Unix timestamp.
const ts = time.unix()time.sleep(ms: int): Task<void>
Section titled “time.sleep(ms: int): Task<void>”Pauses execution asynchronously for the given number of milliseconds.
println("Wait...")await time.sleep(1000) // Sleep for 1 secondprintln("Done!")Formatting & Parsing
Section titled “Formatting & Parsing”time.format(timestamp: int, layout: string): string
Section titled “time.format(timestamp: int, layout: string): string”Formats a timestamp to a string using the given layout.
const formatted = time.format(time.now(), "2006-01-02 15:04:05")println(formatted) // e.g. "2025-06-27 14:30:00"time.parse(str: string, layout: string): int
Section titled “time.parse(str: string, layout: string): int”Parses a date string into a Unix timestamp using the given layout.
const ts = time.parse("2025-06-27", "2006-01-02")println(ts)time.toISO(timestamp: int): string
Section titled “time.toISO(timestamp: int): string”Formats a timestamp as an ISO 8601 string.
const iso = time.toISO(time.now())println(iso) // e.g. "2025-06-27T14:30:00Z"Elapsed Time
Section titled “Elapsed Time”time.since(timestamp: int): int
Section titled “time.since(timestamp: int): int”Returns the number of seconds elapsed since the given timestamp.
const start = time.now()// ... do some work ...const elapsed = time.since(start)println("Took " + elapsed + " seconds")Date Components
Section titled “Date Components”time.date(): any
Section titled “time.date(): any”Returns the current date as an object with year, month, day, hour, minute, and second fields.
const d = time.date()println(d["year"]) // e.g. 2025println(d["month"]) // e.g. 6println(d["day"]) // e.g. 27println(d["hour"]) // e.g. 14println(d["minute"]) // e.g. 30println(d["second"]) // e.g. 0time.year(): int
Section titled “time.year(): int”Returns the current year.
time.month(): int
Section titled “time.month(): int”Returns the current month (1–12).
time.day(): int
Section titled “time.day(): int”Returns the current day of the month.
println(time.year()) // 2025println(time.month()) // 6println(time.day()) // 27Function Reference
Section titled “Function Reference”| Function | Description |
|---|---|
time.now() | Current Unix timestamp (seconds) |
time.unix() | Alias for now() |
time.sleep(ms) | Async sleep for milliseconds |
time.format(ts, layout) | Format timestamp to string |
time.parse(str, layout) | Parse string to timestamp |
time.toISO(ts) | Format as ISO 8601 |
time.since(ts) | Seconds elapsed since timestamp |
time.date() | Current date as object |
time.year() | Current year |
time.month() | Current month (1–12) |
time.day() | Current day of month |
Example: Simple Timer
Section titled “Example: Simple Timer”import { time } from "std/time"
const start = time.now()
// Simulate workawait time.sleep(2000)
const elapsed = time.since(start)println("Operation took " + elapsed + " seconds")