time
The time package reads the clock and answers calendar questions.
now = time::now() # the current local date and timeprintln now.to_string("yyyy-MM-dd HH:mm") >= "2024" # true
println time::timestamp() # e.g. 2026-10-19 22:54:13Z (UTC)println time::timestamp("yyyy-MM-dd") # e.g. 2026-10-19timestamp(format) formats the current UTC time with the date patterns; the default, "u", is the sortable yyyy-MM-dd HH:mm:ssZ form.
Measuring elapsed time
Section titled “Measuring elapsed time”ticks() reads a high-resolution clock. Subtract two readings and convert the difference to milliseconds with ticksms:
started = time::ticks()task::sleep(15)elapsed = time::ticksms(time::ticks() - started)println elapsed >= 15 # trueFor anything more than a quick measurement, stopwatch and bench are more convenient.
Calendar helpers
Section titled “Calendar helpers”println time::leapyear(2024) # trueprintln time::monthdays(2024, 2) # 29println time::valid_day(2023, 2, 29) # falseprintln time::first_day().day() # 1: the first day of the current monthlast_day() returns the last day of the current month.
Date parts and arithmetic
Section titled “Date parts and arithmetic”time::year(), month(), day(), hour(), minute(), second(), millisecond(), weekday(), yearday(), ampm() and isdst() read a part of a date, or of the current time when called without one. add_days, add_months and the other add_* functions do date arithmetic.
The same operations are available as methods on any date, which usually reads better:
release = d(2024, 6, 15)println time::add_days(release, 10).to_string("yyyy-MM-dd") # 2024-06-25println release.add_days(10).to_string("yyyy-MM-dd") # 2024-06-25See date and Types: Dates.
Functions
Section titled “Functions”time::add_days
Section titled “time::add_days”time::add_days(dt: date, n: integer): dateAdds a number of days to a date.
| Parameter | Type | Default | Description |
|---|---|---|---|
dt | date | The date. | |
n | integer | The number of days. |
Returns date — The modified date.
time::add_months
Section titled “time::add_months”time::add_months(dt: date, n: integer): dateAdds a number of months to a date.
| Parameter | Type | Default | Description |
|---|---|---|---|
dt | date | The date. | |
n | integer | The number of months. |
Returns date — The modified date.
time::add_years
Section titled “time::add_years”time::add_years(dt: date, n: integer): dateAdds a number of years to a date.
| Parameter | Type | Default | Description |
|---|---|---|---|
dt | date | The date. | |
n | integer | The number of years. |
Returns date — The modified date.
time::add_hours
Section titled “time::add_hours”time::add_hours(dt: date, n: integer): dateAdds a number of hours to a date.
| Parameter | Type | Default | Description |
|---|---|---|---|
dt | date | The date. | |
n | integer | The number of hours. |
Returns date — The modified date.
time::add_minutes
Section titled “time::add_minutes”time::add_minutes(dt: date, n: integer): dateAdds a number of minutes to a date.
| Parameter | Type | Default | Description |
|---|---|---|---|
dt | date | The date. | |
n | integer | The number of minutes. |
Returns date — The modified date.
time::add_seconds
Section titled “time::add_seconds”time::add_seconds(dt: date, n: integer): dateAdds a number of seconds to a date.
| Parameter | Type | Default | Description |
|---|---|---|---|
dt | date | ||
n | integer |
Returns date — The modified date.
time::add_milliseconds
Section titled “time::add_milliseconds”time::add_milliseconds(dt: date, n: integer): dateAdds a number of milliseconds to a date.
| Parameter | Type | Default | Description |
|---|---|---|---|
dt | date | ||
n | integer |
Returns date — The modified date.
time::now
Section titled “time::now”time::now(): dateReturns the current time as a date value.
Returns date — The current time as a date value.
time::first_day
Section titled “time::first_day”time::first_day(): dateReturns the first day of the year
Returns date — The first day of the year
time::last_day
Section titled “time::last_day”time::last_day(): dateReturns the end of the year
Returns date — The last day of the year
time::hour
Section titled “time::hour”time::hour(dt = null): integerGet hours since midnight (0 to 23).
| Parameter | Type | Default | Description |
|---|---|---|---|
dt | date | null | The date to extract from. Defaults to null. |
Returns integer — The hours since midnight.
time::minute
Section titled “time::minute”time::minute(dt = null): integerGet minutes after the hour (0 to 59).
| Parameter | Type | Default | Description |
|---|---|---|---|
dt | date | null | The date to extract from. Defaults to null. |
Returns integer — The minutes after the hour.
time::second
Section titled “time::second”time::second(dt = null): integerGet seconds after the minute (0 to 60).
| Parameter | Type | Default | Description |
|---|---|---|---|
dt | date | null | The date to extract from. Defaults to null. |
Returns integer — The seconds after the minute.
time::millisecond
Section titled “time::millisecond”time::millisecond(dt = null): integerGet milliseconds after the second (0 to 1000).
| Parameter | Type | Default | Description |
|---|---|---|---|
dt | date | null | The date to extract from. Defaults to null. |
Returns integer — The milliseconds after the second.
time::ampm
Section titled “time::ampm”time::ampm(dt = null): stringGet time of day designation for 12 hour clocks as "AM" or "PM".
| Parameter | Type | Default | Description |
|---|---|---|---|
dt | date | null | The date to extract from. Defaults to null. |
Returns string — "AM" or "PM".
time::day
Section titled “time::day”time::day(dt = null): integerGet the day of the month (1 to 31).
| Parameter | Type | Default | Description |
|---|---|---|---|
dt | date | null | The date to extract from. Defaults to null. |
Returns integer — The day of the month.
time::month
Section titled “time::month”time::month(dt = null): integerGet current calendar month (1 to 12).
| Parameter | Type | Default | Description |
|---|---|---|---|
dt | date | null | The date to extract from. Defaults to null. |
Returns integer — The current month, or the month of dt.
time::year
Section titled “time::year”time::year(dt = null): integerGet the current year.
| Parameter | Type | Default | Description |
|---|---|---|---|
dt | date | null | The date to extract from. Defaults to null. |
Returns integer — The current year.
time::leapyear
Section titled “time::leapyear”time::leapyear(_year: integer): booleanCheck if a given year is a leap year.
| Parameter | Type | Default | Description |
|---|---|---|---|
_year | integer | The year to check. |
Returns boolean — Returns true when _year is a leap year.
time::monthdays
Section titled “time::monthdays”time::monthdays(_year: integer, _month: integer): integerGet how many days are in a given month.
| Parameter | Type | Default | Description |
|---|---|---|---|
_year | integer | The year to check. | |
_month | integer | The month to check. |
Returns integer — The number of days in a given month.
time::weekday
Section titled “time::weekday”time::weekday(dt = null): integerGet the day of the week (0 to 6).
| Parameter | Type | Default | Description |
|---|---|---|---|
dt | date | null | The date to check. Defaults to null. |
Returns integer — The day of the week starting from Sunday = 0.
time::yearday
Section titled “time::yearday”time::yearday(dt = null): integerGet the day of the year (1 to 366).
| Parameter | Type | Default | Description |
|---|---|---|---|
dt | date | null | The date to check. Defaults to null. |
Returns integer — The day of the year starting from Sunday = 0.
time::isdst
Section titled “time::isdst”time::isdst(dt = null): booleanChecks if currently in Daylight Savings Time (DST).
| Parameter | Type | Default | Description |
|---|---|---|---|
dt | date | null | The date to check. Defaults to null. |
Returns boolean — Returns true when in DST.
time::valid_day
Section titled “time::valid_day”time::valid_day(_year: integer, _month: integer, _day: integer): booleanCheck if a day is valid within the given month.
| Parameter | Type | Default | Description |
|---|---|---|---|
_year | integer | The year to check. | |
_month | integer | The month to check. | |
_day | integer | The day to check. |
Returns boolean — The number of days in a given month.
time::timestamp
Section titled “time::timestamp”time::timestamp(format: string = 'u'): stringGet current timestamp in a specified format.
| Parameter | Type | Default | Description |
|---|---|---|---|
format | string | 'u' |
Returns string — The current timestamp as a string.
time::ticks
Section titled “time::ticks”time::ticks(): floatGets a high-resolution timestamp in nanoseconds, for measuring elapsed time. It counts from an arbitrary point, not a calendar date; subtract two readings and convert the difference with ticksms.
Returns float — The timestamp in nanoseconds.
time::ticksms
Section titled “time::ticksms”time::ticksms(_ticks: float): floatConverts a difference of ticks() readings (nanoseconds) to milliseconds.
| Parameter | Type | Default | Description |
|---|---|---|---|
_ticks | float | The ticks. |
Returns float — Ticks in milliseconds.