TimeService

The time now, waits, timers that fire once or keep repeating, and times written out as text.

A time is seconds since 1970, with milliseconds as the fraction. WaitAsync is how a program waits, since it has no wait. After and Every give a Timer, a child of TimeService, and never wait. Format writes local time by strftime codes and errors on a code it doesn't know; Iso writes UTC.

ShotInstanceservicenot creatableData and time

A service: write use TimeService.

Example

Times, waits and timersShotScript
-- Times as text, a timer that fires once, and a wait that says how long it took.
use TimeService
print("time 0 in UTC: " .. TimeService:Iso(0))
print("a billion seconds, local time: " .. TimeService:Format(1e9, "%B %Y"))
print("Now counts seconds since 1970: " .. tostring(TimeService:Now() > 1e9))

local started = TimeService:Clock()
TimeService:After(0.2, function()
	print("After: fired once")
end)
local waited = TimeService:WaitAsync(0.5)
print("WaitAsync waited at least 0.5 s: " .. tostring(waited >= 0.5))
print("Clock moved on as much: " .. tostring(TimeService:Clock() - started >= 0.5))
What it printedOutput
time 0 in UTC: 1970-01-01T00:00:00Z
a billion seconds, local time: September 2001
Now counts seconds since 1970: true
After: fired once
WaitAsync waited at least 0.5 s: true
Clock moved on as much: true

Methods

NameArgumentsReturnsDescription
After(seconds: number, callback: () -> ())TimerCalls callback once, seconds from now, without waiting; its Timer is destroyed once it fires, and destroying it first cancels the call.
Clock()numberSeconds since an arbitrary start, for timing how long something took, not for dates.
Every(seconds: number, callback: () -> ())TimerCalls callback every seconds until its Timer is destroyed, the first call one period from now; it doesn't wait.
Format(time: number?, format: string?)stringA time as local text by strftime codes, default "%Y-%m-%d %H:%M:%S", now when time is nil; an unknown code errors.
Iso(time: number?)stringA time as UTC text to the second, such as 1970-01-01T00:00:00Z, now when time is nil; a fraction is dropped.
Now()numberThe time now in seconds since 1970, with milliseconds as the fraction.
WaitAsync
yields
(seconds: number)numberWaits the seconds, at least one frame, and gives how long it really waited.
From ShotInstance 33