Import this module with import @time.
| Function |
Signature |
Description |
now |
() -> i64 |
Current Unix timestamp (seconds) |
now_ms |
() -> i64 |
Current Unix timestamp (milliseconds) |
now_ns |
() -> i64 |
Current Unix timestamp (nanoseconds) |
| Function |
Signature |
Description |
year |
(timestamp i64) -> i64 |
Get year |
month |
(timestamp i64) -> i64 |
Get month (1-12) |
day |
(timestamp i64) -> i64 |
Get day of month |
hour |
(timestamp i64) -> i64 |
Get hour |
minute |
(timestamp i64) -> i64 |
Get minute |
second |
(timestamp i64) -> i64 |
Get second |
weekday |
(timestamp i64) -> i64 |
Get day of week (0=Sunday) |
weekday_name |
(timestamp i64) -> string |
English weekday name ("Sunday".."Saturday"), matching weekday() numbering |
month_name |
(timestamp i64) -> string |
English month name ("January".."December") |
day_of_year |
(timestamp i64) -> i64 |
Day of the year, 1–366 |
days_in_month |
(year i64, month i64) -> i64 |
Days in month (1–12) of year, leap-year aware; panics if month is outside 1–12 |
is_leap_year |
(year i64) -> bool |
Check if year is a leap year |
| Function |
Signature |
Description |
format |
(format string, timestamp i64) -> string |
Format time |
to_iso |
(timestamp i64) -> string |
ISO 8601 string |
date |
(timestamp i64) -> string |
Date (YYYY-MM-DD) |
to_clock |
(timestamp i64) -> string |
Time (HH:MM:SS) |
humanize |
(seconds i64) -> string |
Relative phrase for a signed delta: positive is past ("2 days ago"), negative is future ("in 1 hour"), 0 is "just now"; largest whole unit only |
format_duration |
(seconds i64) -> string |
"1h 30m 15s"; capped at hours (no days), zero components omitted unless the whole value is zero ("0s"), negative gets a leading - |
| Function |
Signature |
Description |
parse |
(s string, layout string) -> (i64, Error) |
Parse a time string into a Unix timestamp using strftime-style layout directives |
from_parts |
(year i64, month i64, day i64, hour i64, minute i64, second i64) -> (i64, Error) |
Build a UTC Unix timestamp from calendar parts, the inverse of year..second; an impossible date or time yields a non-nil error and 0 |
parse_duration |
(s string) -> (i64, Error) |
Parse "1h30m", "90s", "2d", "1h30m15s" (units s m h d) into total seconds; bad input yields a non-nil error and 0 |
Behavior:
parse, parse_duration and from_parts are fallible functions. Destructure the result (mut ts, err = time.parse(...)).
mut ts, err = time.parse(...) — inspect err (non-nil on invalid input).
mut ts, _ = time.parse(...) — discard the error; on invalid input ts is 0.
| Function |
Signature |
Description |
diff |
(t1 i64, t2 i64) -> i64 |
Difference in seconds (t2 - t1); negative if t1 is after t2 |
since |
(t i64) -> i64 |
Seconds elapsed from t to now; equivalent to diff(t, now()) |
add_days |
(timestamp i64, n i64) -> i64 |
timestamp shifted by n days (n may be negative); pure Unix arithmetic, no calendar/DST logic |
add_hours |
(timestamp i64, n i64) -> i64 |
timestamp shifted by n hours |
add_minutes |
(timestamp i64, n i64) -> i64 |
timestamp shifted by n minutes |
add_months |
(timestamp i64, n i64) -> i64 |
timestamp shifted by n calendar months (UTC); day and time of day are kept, and the day clamps to the target month’s last day (Jan 31 + 1 month is Feb 28, or Feb 29 in a leap year); panics on overflow |
add_seconds |
(timestamp i64, n i64) -> i64 |
timestamp shifted by n seconds |
start_of_day |
(timestamp i64) -> i64 |
Unix timestamp of 00:00:00 UTC on the same day |
end_of_day |
(timestamp i64) -> i64 |
Unix timestamp of 23:59:59 UTC on the same day (start_of_day + 86399) |
start_of_month |
(timestamp i64) -> i64 |
Unix timestamp of 00:00:00 UTC on the first day of the same month |
end_of_month |
(timestamp i64) -> i64 |
Unix timestamp of 23:59:59 UTC on the last day of the same month; leap-year aware |
These functions panic (P0157) when an intermediate or final result leaves the i64 range: diff, since, add_days, add_hours, add_minutes, add_months, add_seconds, start_of_day and end_of_day. add_months, start_of_month and end_of_month panic with P0140 instead when the timestamp is outside the range of dates that can be broken into parts.
| Function |
Signature |
Description |
tick |
() -> i64 |
High-resolution timestamp in nanoseconds |
elapsed_ms |
(start_tick i64) -> i64 |
Milliseconds elapsed since a tick |