Time
In the terminalela --spec time
| Built-in | Does |
|---|---|
time.now() | The time as seconds since 1970, with a fraction |
time.since(t) | The seconds since t, a time from time.now() |
time.sleep(seconds) | Waits that long; a fraction is fine |
time.format(t, layout) | The time as text in Go's layout, in the local zone: time.format(time.now(), "2006-01-02 15:04") |
time.parse(text, layout) | Text in Go's layout as seconds since 1970; text that does not fit is an error that says which part and what to write there |
deadline = time.now() + $wait_min * 60while time.now() < $deadline time.sleep(30)end while
- Text with no zone or offset is read in the local zone, so
time.parsereads back whattime.formatwrote; an offset (-07:00,Z) or a zone's name the text gives sets the instant instead. - A part the layout leaves out is its least: January, the 1st, midnight. A layout with no year reads 1970, so
time.parse("10:00", "15:04")is 10 o'clock on 1 January 1970, local time. - A time runs from 1677-09-21 to 2262-04-11;
time.formatwrites a fraction to the microsecond.
The virtual clock #
ELA_CLOCK=virtual in the environment makes the clock a value the caller supplies, for a test
that runs a script which sleeps. Every time.sleep returns at once and adds its length to the
clock, and time.now() and time.since read real time plus the sleeps skipped, so a script's
deadlines run their course without the wait. Unset, the clock is the system's; any other value
is an error at the first time. call: ELA_CLOCK is "fast": it is unset or virtual.
- A real wait still counts, as a program running, a
waiton a job orsleepthe program. A sleep under the virtual clock waits for nothing real, so a script that needs a program done waits for it withwait, never withtime.sleep. with time.blockreads the same clock, so awith time.block = "2m"around a loop that sleeps 30 s at a time ends at its fourthtime.sleep(30), with its usual message, and a block that waits on a hung program still ends at its limit.- Every sleep jumps, a job running or not; a job's process keeps its own count from the moment it started.
ela --doctorandela --lintsay when it is set.
# ci-status-test.elat0 = time.now()with env.ELA_CLOCK = "virtual" out, err = try $(ci-status --wait app)end withecho "${$err == null} after ${int(time.since($t0))} s"