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.datetime.SysTime). 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.datetime.SysTime 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.normal) 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.

Members

Functions

opBinary
Duration opBinary(MonoTimeImpl rhs)

Subtracting two MonoTimes results in a Duration representing the amount of time which elapsed between them.

opBinary
MonoTimeImpl opBinary(Duration rhs)

Adding or subtracting a Duration to/from a MonoTime results in a MonoTime which is adjusted by that amount.

opCmp
int opCmp(MonoTimeImpl rhs)

Compares this MonoTime with the given MonoTime.

opOpAssign
MonoTimeImpl opOpAssign(Duration rhs)

Adding or subtracting a Duration to/from a MonoTime results in a MonoTime which is adjusted by that amount.

toString
string toString()

Properties

currTime
MonoTimeImpl currTime [@property getter]

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.

max
MonoTimeImpl max [@property getter]

Largest MonoTime possible.

min
MonoTimeImpl min [@property getter]

Most negative MonoTime possible.

ticks
long ticks [@property getter]

The number of ticks in the monotonic time.

ticksPerSecond
long ticksPerSecond [@property getter]

The number of ticks that MonoTime has per second - i.e. the resolution or frequency of the system's monotonic clock.

zero
MonoTimeImpl zero [@property getter]

A MonoTime of 0 ticks. It's provided to be consistent with Duration.zero, and it's more explicit than MonoTime.init.

See Also

Meta