Three verbs, one discipline. Now reads the monotonic clock —
nanoseconds since an arbitrary epoch, never the time of day — so a reading
cannot jump when an administrator or a time service adjusts the wall
clock, and only the difference between two readings carries meaning.
Since is that difference, and Sleep suspends for at least the duration
you name. Everything else on this page is consequences.
The example is examples/Clock
in the tutorial repository.
Getting the file
make -C examples/Clock runClock
250 * Millisecond 250000000 nanoseconds
2 * Second 2000000000 nanoseconds
a later Now is never smaller 1
slept at least the asked 50ms 1A clock’s actual readings are the one thing an honest example must not print — they differ on every run. What is printed is what the unit promises: exact constant arithmetic, and verdicts the promises make true everywhere.
The example, walked
A duration is an integer, and the units are constants
Sleep(50 * Millisecond);A duration is a plain int64 count of nanoseconds: integer arithmetic
keeps every duration exact and bit-stable, with none of the drift a
floating-point seconds count would smuggle in. The constants —
Nanosecond, Microsecond, Millisecond, Second, Minute, Hour —
are int64 by declaration, so a product like 250 * Millisecond computes
in the duration’s own width and reads the way it is meant.
Measure with differences
start := Now();
...
slept := Since(start);Now’s epoch is arbitrary — on Linux, the boot — so a single reading is
not a time of day and should never be shown as one. The pattern is always
the pair: read, work, Since. This is the one guarantee measurement and
timeout code actually needs, and it is the guarantee wall-clock time
famously breaks twice a year.
Sleep keeps its floor
Sleep suspends for at least the requested duration. Under the
operating system an interrupting signal can end a wait early with the
unslept remainder — Sleep consumes that and sleeps the remainder again,
so the floor survives signals without the caller writing the retry loop
every systems programmer has written once. The verdict line in the output
is that promise, checked against the clock itself.
Try it
1. Break the floor’s spelling. Change the check to
slept >= 51 * Millisecond and rerun a few times. Sometimes true,
sometimes false — the promise is a floor, not a target: at least what you
asked, no statement about how much more.
2. Time something real. Wrap Since around the
reductions example’s work or a sort from
order and disorder, and print the duration
divided by Millisecond. Numbers vary per machine — which is exactly why
this page’s shipped output prints verdicts instead.
Next
Carriers, multicore and busy loops for where waiting meets scheduling, and the client road for the deadline that reaches a parked network wait. Every example lives in the tutorial repository.