Struct core.time.MonoTimeImpl
Represents a timestamp of the system's monotonic clock.
A monotonic clock is one which always goes forward and never moves
backwards, unlike the system's wall clock time (as represented by
std
). The system's wall clock time can be adjusted
by the user or by the system itself via services such as NTP, so it is
unreliable to use the wall clock time for timing. Timers which use the wall
clock time could easily end up never going off due to changes made to the
wall clock time or otherwise waiting for a different period of time than
that specified by the programmer. However, because the monotonic clock
always increases at a fixed rate and is not affected by adjustments to the
wall clock time, it is ideal for use with timers or anything which requires
high precision timing.
So, MonoTime should be used for anything involving timers and timing,
whereas std
should be used when the wall clock time
is required.
The monotonic clock has no relation to wall clock time. Rather, it holds
its time as the number of ticks of the clock which have occurred since the
clock started (typically when the system booted up). So, to determine how
much time has passed between two points in time, one monotonic time is
subtracted from the other to determine the number of ticks which occurred
between the two points of time, and those ticks are divided by the number of
ticks that occur every second (as represented by MonoTime.ticksPerSecond)
to get a meaningful duration of time. Normally, MonoTime does these
calculations for the programmer, but the ticks
and ticksPerSecond
properties are provided for those who require direct access to the system
ticks. The normal way that MonoTime would be used is
MonoTime before = MonoTime .currTime;
// do stuff...
MonoTime after = MonoTime .currTime;
Duration timeElapsed = after - before;
MonoTime
is an alias to MonoTimeImpl!(ClockType
and is
what most programs should use for the monotonic clock, so that's what is
used in most of MonoTimeImpl
's documentation. But MonoTimeImpl
can be instantiated with other clock types for those rare programs that need
it.
Properties
Name | Type | Description |
---|---|---|
currTime [get]
|
MonoTimeImpl | The current time of the system's monotonic clock. This has no relation to the wall clock time, as the wall clock time can be adjusted (e.g. by NTP), whereas the monotonic clock always moves forward. The source of the monotonic time is system-specific. |
ticks [get]
|
long | The number of ticks in the monotonic time. |
ticksPerSecond [get]
|
long | The number of ticks that MonoTime has per second - i.e. the resolution or frequency of the system's monotonic clock. |
Methods
Name | Description |
---|---|
max
|
Largest MonoTime possible.
|
min
|
Most negative MonoTime possible.
|
opBinary
|
Subtracting two MonoTimes results in a Duration representing
the amount of time which elapsed between them.
|
opBinary
|
Adding or subtracting a Duration to/from a MonoTime results in
a MonoTime which is adjusted by that amount.
|
opCmp
|
Compares this MonoTime with the given MonoTime. |
opOpAssign
|
Adding or subtracting a Duration to/from a MonoTime results in
a MonoTime which is adjusted by that amount.
|
toString
|
|
zero
|
A MonoTime of 0 ticks. It's provided to be consistent with
Duration , and it's more explicit than MonoTime .
|
See Also
Authors
Jonathan M Davis and Kato Shoichi