time
The current time, durations, formatting and parsing dates, the monotonic clock, timers and tickers.
import time
There are two kinds of "time" in this package. An instant is a number of seconds since the start of 1970 in UTC, the Unix epoch, and is what you use for dates and clock times. A duration is a span with nanosecond precision, such as "1 hour 30 minutes", which you can add up and compare.
Functions such as Year and FormatDate work in the machine's local time, and their UTC counterparts work in UTC. To measure how long a piece of code took, use the monotonic clock (MonoNanos) rather than Now; the system clock can jump forwards or backwards.
For the Persian and Hijri calendars and for time zones, see the calendar package.
Types
DateTime
struct DateTime| Name | Type | Description |
|---|---|---|
epoch | i64 = 0 | The seconds since the Unix epoch. |
import time
import str
func main:
t := time.FromLocalParts(2024, 3, 20, 9, 30, 0)
println time.Year(t), time.Month(t), time.Day(t), time.Hour(t), time.Minute(t), time.Second(t)
println time.Weekday(t), time.YearDay(t)
println time.FormatDate(t), time.FormatTime(t)
println time.Format(t)
println time.FormatISO(t)
r := time.FormatRFC3339(t)
offset := time.LocalOffsetMinutesAt(t)
println str.StartsWith(r, "2024-03-20T09:30:00"), (offset == 0) == str.EndsWith(r, "Z")
dt := time.FromEpoch(t)
println time.Epoch(dt) == t, dt.epoch == t
println time.Epoch(time.NowDateTime()) >= t
endDuration
struct Duration| Name | Type | Description |
|---|---|---|
ns | i64 = 0 | The span in nanoseconds. |
import time
func main:
d := time.Hours(1).add(time.Minutes(30))
println d.to_str(), d.minutes(), d.secs()
println time.FormatDuration(time.Milliseconds(1500)), time.FormatDuration(time.Microseconds(250)), time.FormatDuration(time.Nanoseconds(42))
println time.Seconds(90).to_str(), time.Days(2).hours(), time.SecondsF(0.25).millis()
p := time.ParseDuration("1h15m30.5s")
println p.ok, p.value.to_str(), p.value.seconds()
println time.ParseDuration("ten minutes").ok, time.ParseDurationOr("?", time.Seconds(5)).to_str()
k := time.Minutes(7)
println k.sub(time.Minutes(10)).to_str(), k.neg().abs().to_str(), k.neg().is_negative()
println k.mul(3).to_str(), k.div(2).to_str(), time.Hours(1).ratio(k)
println k.cmp(time.Seconds(420)), k.less(time.Hours(1)), k.equals(time.Seconds(420))
t := time.Milliseconds(1750)
println t.truncate(time.Seconds(1)).to_str(), t.round(time.Seconds(1)).to_str()
println t.nanos(), t.micros(), t.millis(), time.ZeroDuration().is_zero(), t.days() < 0.001
endnanos
func nanos(): i64The example for this item is shown with the ones above: time-Duration.salam
micros
func micros(): i64The example for this item is shown with the ones above: time-Duration.salam
millis
func millis(): i64The example for this item is shown with the ones above: time-Duration.salam
secs
func secs(): i64The example for this item is shown with the ones above: time-Duration.salam
seconds
func seconds(): f64The example for this item is shown with the ones above: time-Duration.salam
minutes
func minutes(): f64The example for this item is shown with the ones above: time-Duration.salam
hours
func hours(): f64The example for this item is shown with the ones above: time-Duration.salam
days
func days(): f64The example for this item is shown with the ones above: time-Duration.salam
add
func add(o: Duration): DurationThe example for this item is shown with the ones above: time-Duration.salam
sub
func sub(o: Duration): DurationThe example for this item is shown with the ones above: time-Duration.salam
mul
func mul(k: i64): DurationThe example for this item is shown with the ones above: time-Duration.salam
div
func div(k: i64): DurationThe example for this item is shown with the ones above: time-Duration.salam
ratio
func ratio(o: Duration): i64The example for this item is shown with the ones above: time-Duration.salam
neg
func neg(): DurationThe example for this item is shown with the ones above: time-Duration.salam
abs
func abs(): DurationThe example for this item is shown with the ones above: time-Duration.salam
is_zero
func is_zero(): boolThe example for this item is shown with the ones above: time-Duration.salam
is_negative
func is_negative(): boolThe example for this item is shown with the ones above: time-Duration.salam
cmp
func cmp(o: Duration): intThe example for this item is shown with the ones above: time-Duration.salam
less
func less(o: Duration): boolThe example for this item is shown with the ones above: time-Duration.salam
equals
func equals(o: Duration): boolThe example for this item is shown with the ones above: time-Duration.salam
truncate
func truncate(m: Duration): Durationm.The example for this item is shown with the ones above: time-Duration.salam
round
func round(m: Duration): Durationm, with halves going away from zero.The example for this item is shown with the ones above: time-Duration.salam
to_str
func to_str(): str1h30m0s; the same as FormatDuration.The example for this item is shown with the ones above: time-Duration.salam
DurationResult
struct DurationResult| Name | Type | Description |
|---|---|---|
ok | bool = false | Whether the text was read successfully. |
value | Duration | The span that was read. |
The example for this item is shown with the ones above: time-Duration.salam
TimeResult
struct TimeResult| Name | Type | Description |
|---|---|---|
ok | bool = false | Whether the text was read successfully. |
epoch | i64 = 0 | The instant that was read, in seconds since the Unix epoch. |
nanos | int = 0 | The fraction of a second, in nanoseconds. |
import time
func main:
t := time.FromUTCParts(2024, 3, 20, 14, 5, 9)
println t, time.FormatISOUTC(t)
println time.YearUTC(t), time.MonthUTC(t), time.DayUTC(t), time.HourUTC(t), time.MinuteUTC(t), time.SecondUTC(t)
println time.FormatHTTPDate(t)
println time.ParseHTTPDate("Wed, 20 Mar 2024 14:05:09 GMT").epoch == t
p := time.ParseISO("2024-03-20T17:35:09+03:30")
println p.ok, p.epoch == t, p.nanos
println time.ParseISOIn("2024-03-20 17:35:09.25", 210).epoch == t, time.ParseISOIn("2024-03-20 17:35:09.25", 210).nanos
f := time.ParseFormat("%d/%m/%Y %H:%M %z", "20/03/2024 14:05 +0000")
println f.ok, time.FormatISOUTC(f.epoch)
println time.ParseISO("tomorrow").ok, time.ParseISOOr("tomorrow", 0)
later := time.AddDuration(t, time.Days(10))
println time.FormatISOUTC(later), time.DiffSeconds(later, t), time.DurationBetween(later, t).to_str()
println time.SecondsPerMinute, time.SecondsPerHour, time.SecondsPerDay
endTimer
struct Timer| Name | Type | Description |
|---|---|---|
deadline_ns | i64 = 0 | The end instant on the monotonic clock, in nanoseconds. |
active | bool = false | Whether the timer is running. |
import time
mut count := 0
func main:
mut timer := time.NewTimer(time.Milliseconds(100))
println timer.expired(), timer.remaining().millis() > 0
timer.wait()
println timer.expired(), timer.remaining().is_zero()
timer.reset(time.Seconds(10))
timer.stop()
println timer.expired(), timer.active
mut ticker := time.NewTicker(time.Milliseconds(50))
println ticker.fired()
ticker.wait()
time.Sleep(120)
println ticker.fired(), ticker.dropped >= 1
ticker.advance()
println ticker.remaining().millis() <= 100
ticker.reset(time.Milliseconds(5))
ticker.stop()
println ticker.fired()
time.AfterFunc(time.Milliseconds(10), (): println "after 10 milliseconds" end)
time.RepeatEvery(time.Milliseconds(5), 2, (): println "tick" end)
println time.Every(time.Milliseconds(5), (): count += 1 ret count < 3 end)
count = 0
println time.WaitUntil((): count += 1 ret count >= 3 end, time.Milliseconds(5), time.Seconds(5))
println time.WaitUntil((): ret false end, time.Milliseconds(5), time.Milliseconds(20))
endexpired
func expired(): boolThe example for this item is shown with the ones above: time-Timer.salam
remaining
func remaining(): DurationThe example for this item is shown with the ones above: time-Timer.salam
wait
func wait()The example for this item is shown with the ones above: time-Timer.salam
reset
func reset(d: Duration)The example for this item is shown with the ones above: time-Timer.salam
stop
func stop()The example for this item is shown with the ones above: time-Timer.salam
Ticker
struct Ticker| Name | Type | Description |
|---|---|---|
interval_ns | i64 = 0 | The interval between ticks, in nanoseconds. |
next_ns | i64 = 0 | The instant of the next tick. |
active | bool = false | Whether the ticker is running. |
dropped | i64 = 0 | How many ticks were missed because the program came back to the ticker late. |
The example for this item is shown with the ones above: time-Timer.salam
fired
func fired(): boolThe example for this item is shown with the ones above: time-Timer.salam
wait
func wait()The example for this item is shown with the ones above: time-Timer.salam
advance
func advance()The example for this item is shown with the ones above: time-Timer.salam
remaining
func remaining(): DurationThe example for this item is shown with the ones above: time-Timer.salam
reset
func reset(d: Duration)The example for this item is shown with the ones above: time-Timer.salam
stop
func stop()The example for this item is shown with the ones above: time-Timer.salam
Constants and variables
SecondsPerMinute
const SecondsPerMinute: i32 = 60The example for this item is shown with the ones above: time-UTC.salam
SecondsPerHour
const SecondsPerHour: i32 = 3600The example for this item is shown with the ones above: time-UTC.salam
SecondsPerDay
const SecondsPerDay: i32 = 86400The example for this item is shown with the ones above: time-UTC.salam
Functions
Now
func Now(): i64import time
func main:
start := time.Now()
a := time.MonoNanos()
time.Sleep(20)
time.SleepFor(time.Milliseconds(10))
b := time.MonoNanos()
println time.ElapsedMs(a, b) >= 30.0, time.ElapsedUs(a, b) >= 30000.0, time.ElapsedSec(a, b) < 5.0
println time.SinceNanos(a) >= 30000000, time.SinceMs(a) >= 30.0, time.SinceUs(a) >= 30000.0, time.SinceSec(a) < 5.0
println time.SinceDuration(a).millis() >= 30
println time.MonoMillis() > 0, time.MonoMicros() > 0
println time.Now() >= start, time.Since(start) >= 0, time.Until(start + 60) <= 60
println time.NowMillis() / 1000 >= start, time.NowMicros() / 1000000 >= start, time.NowNanos() / 1000000000 >= start
endNowDateTime
func NowDateTime(): DateTimeDateTime.The example for this item is shown with the ones above: time-Local.salam
FromEpoch
func FromEpoch(seconds: i64): DateTimeDateTime from a number of epoch seconds.The example for this item is shown with the ones above: time-Local.salam
Epoch
func Epoch(dt: DateTime): i64DateTime.The example for this item is shown with the ones above: time-Local.salam
Year
func Year(epoch: i64): intThe example for this item is shown with the ones above: time-Local.salam
Month
func Month(epoch: i64): intThe example for this item is shown with the ones above: time-Local.salam
Day
func Day(epoch: i64): intThe example for this item is shown with the ones above: time-Local.salam
Hour
func Hour(epoch: i64): intThe example for this item is shown with the ones above: time-Local.salam
Minute
func Minute(epoch: i64): intThe example for this item is shown with the ones above: time-Local.salam
Second
func Second(epoch: i64): intThe example for this item is shown with the ones above: time-Local.salam
YearUTC
func YearUTC(epoch: i64): intThe example for this item is shown with the ones above: time-UTC.salam
MonthUTC
func MonthUTC(epoch: i64): intThe example for this item is shown with the ones above: time-UTC.salam
DayUTC
func DayUTC(epoch: i64): intThe example for this item is shown with the ones above: time-UTC.salam
HourUTC
func HourUTC(epoch: i64): intThe example for this item is shown with the ones above: time-UTC.salam
MinuteUTC
func MinuteUTC(epoch: i64): intThe example for this item is shown with the ones above: time-UTC.salam
SecondUTC
func SecondUTC(epoch: i64): intThe example for this item is shown with the ones above: time-UTC.salam
Weekday
func Weekday(epoch: i64): intThe example for this item is shown with the ones above: time-Local.salam
YearDay
func YearDay(epoch: i64): intThe example for this item is shown with the ones above: time-Local.salam
FormatDate
func FormatDate(epoch: i64): str2024-03-20.The example for this item is shown with the ones above: time-Local.salam
FormatTime
func FormatTime(epoch: i64): str09:30:00.The example for this item is shown with the ones above: time-Local.salam
Format
func Format(epoch: i64): strThe example for this item is shown with the ones above: time-Local.salam
FormatISO
func FormatISO(epoch: i64): str2024-03-20T09:30:00.The example for this item is shown with the ones above: time-Local.salam
FormatHTTPDate
func FormatHTTPDate(epoch: i64): strWed, 20 Mar 2024 14:05:09 GMT.The example for this item is shown with the ones above: time-UTC.salam
Sleep
func Sleep(ms: int)The example for this item is shown with the ones above: time-Now.salam
DiffSeconds
func DiffSeconds(later: i64, earlier: i64): i64earlier to later.The example for this item is shown with the ones above: time-UTC.salam
Since
func Since(t: i64): i64The example for this item is shown with the ones above: time-Now.salam
Until
func Until(t: i64): i64The example for this item is shown with the ones above: time-Now.salam
Nanoseconds
func Nanoseconds(n: i64): Durationn nanoseconds.The example for this item is shown with the ones above: time-Duration.salam
Microseconds
func Microseconds(n: i64): Durationn microseconds.The example for this item is shown with the ones above: time-Duration.salam
Milliseconds
func Milliseconds(n: i64): Durationn milliseconds.The example for this item is shown with the ones above: time-Duration.salam
Seconds
func Seconds(n: i64): Durationn seconds.The example for this item is shown with the ones above: time-Duration.salam
Minutes
func Minutes(n: i64): Durationn minutes.The example for this item is shown with the ones above: time-Duration.salam
Hours
func Hours(n: i64): Durationn hours.The example for this item is shown with the ones above: time-Duration.salam
Days
func Days(n: i64): Durationn days.The example for this item is shown with the ones above: time-Duration.salam
SecondsF
func SecondsF(x: f64): DurationThe example for this item is shown with the ones above: time-Duration.salam
ZeroDuration
func ZeroDuration(): DurationThe example for this item is shown with the ones above: time-Duration.salam
FormatDuration
func FormatDuration(d: Duration): str42ns, 250us, 1.5s, 1m30s or 1h15m30.5s.The example for this item is shown with the ones above: time-Duration.salam
ParseDuration
func ParseDuration(s: str): DurationResult1h15m30.5s or -250ms. The units are ns, us, ms, s, m, h and d for days.The example for this item is shown with the ones above: time-Duration.salam
ParseDurationOr
func ParseDurationOr(s: str, fallback: Duration): DurationParseDuration, returning the default when the text is not valid.The example for this item is shown with the ones above: time-Duration.salam
SleepFor
func SleepFor(d: Duration)The example for this item is shown with the ones above: time-Now.salam
SinceDuration
func SinceDuration(start_ns: i64): DurationMonoNanos.The example for this item is shown with the ones above: time-Now.salam
DurationBetween
func DurationBetween(later: i64, earlier: i64): DurationThe example for this item is shown with the ones above: time-UTC.salam
AddDuration
func AddDuration(epoch: i64, d: Duration): i64The example for this item is shown with the ones above: time-UTC.salam
NowNanos
func NowNanos(): i64The example for this item is shown with the ones above: time-Now.salam
NowMicros
func NowMicros(): i64The example for this item is shown with the ones above: time-Now.salam
NowMillis
func NowMillis(): i64The example for this item is shown with the ones above: time-Now.salam
ElapsedMs
func ElapsedMs(start_ns: i64, end_ns: i64): f64The example for this item is shown with the ones above: time-Now.salam
ElapsedUs
func ElapsedUs(start_ns: i64, end_ns: i64): f64The example for this item is shown with the ones above: time-Now.salam
ElapsedSec
func ElapsedSec(start_ns: i64, end_ns: i64): f64The example for this item is shown with the ones above: time-Now.salam
MonoNanos
func MonoNanos(): i64The example for this item is shown with the ones above: time-Now.salam
MonoMicros
func MonoMicros(): i64The example for this item is shown with the ones above: time-Now.salam
MonoMillis
func MonoMillis(): i64The example for this item is shown with the ones above: time-Now.salam
SinceNanos
func SinceNanos(start_ns: i64): i64The example for this item is shown with the ones above: time-Now.salam
SinceMs
func SinceMs(start_ns: i64): f64The example for this item is shown with the ones above: time-Now.salam
SinceUs
func SinceUs(start_ns: i64): f64The example for this item is shown with the ones above: time-Now.salam
SinceSec
func SinceSec(start_ns: i64): f64The example for this item is shown with the ones above: time-Now.salam
ParseISO
func ParseISO(s: str): TimeResult2024-03-20T17:35:09+03:30. Without an offset it assumes local time.The example for this item is shown with the ones above: time-UTC.salam
ParseISOIn
func ParseISOIn(s: str, offsetMinutes: int): TimeResultParseISO, but without an offset it assumes offsetMinutes minutes.The example for this item is shown with the ones above: time-UTC.salam
ParseHTTPDate
func ParseHTTPDate(s: str): TimeResultThe example for this item is shown with the ones above: time-UTC.salam
ParseFormat
func ParseFormat(pattern: str, s: str): TimeResult%Y is a four digit year, %y a two digit one, %m the month, %d the day, %H the hour, %M the minute, %S the second, %f the fraction, %b the English month name, %a the day name, which is ignored, %z an offset such as +0330 and %Z a zone name such as UTC. One space in the pattern matches any number of spaces in the text.The example for this item is shown with the ones above: time-UTC.salam
ParseISOOr
func ParseISOOr(s: str, fallback: i64): i64ParseISO, returning the default when the text is not valid.The example for this item is shown with the ones above: time-UTC.salam
FormatISOUTC
func FormatISOUTC(epoch: i64): str2024-03-20T14:05:09Z.The example for this item is shown with the ones above: time-UTC.salam
FormatRFC3339
func FormatRFC3339(epoch: i64): str2024-03-20T17:35:09+03:30, or Z when the offset is zero.The example for this item is shown with the ones above: time-Local.salam
LocalOffsetMinutesAt
func LocalOffsetMinutesAt(epoch: i64): intThe example for this item is shown with the ones above: time-Local.salam
FromLocalParts
func FromLocalParts(year: int, month: int, day: int, hour: int, minute: int, second: int): i64The example for this item is shown with the ones above: time-Local.salam
FromUTCParts
func FromUTCParts(year: int, month: int, day: int, hour: int, minute: int, second: int): i64The example for this item is shown with the ones above: time-UTC.salam
NewTimer
func NewTimer(d: Duration): TimerThe example for this item is shown with the ones above: time-Timer.salam
NewTicker
func NewTicker(d: Duration): TickerThe example for this item is shown with the ones above: time-Timer.salam
AfterFunc
func AfterFunc(d: Duration, fn: func())The example for this item is shown with the ones above: time-Timer.salam
Every
func Every(d: Duration, fn: func(): bool): i64The example for this item is shown with the ones above: time-Timer.salam
RepeatEvery
func RepeatEvery(d: Duration, count: int, fn: func())count times at the given interval.The example for this item is shown with the ones above: time-Timer.salam
WaitUntil
func WaitUntil(fn: func(): bool, poll: Duration, limit: Duration): boolpoll until it holds or limit passes, and says whether it held.The example for this item is shown with the ones above: time-Timer.salam