View source code
Display the source code in core/time.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Struct core.time.MonoTimeImpl

Represents a timestamp of the system's monotonic clock.

struct MonoTimeImpl(ClockType clockType) ;

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.

Properties

NameTypeDescription
currTime[get] MonoTimeImplThe 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] longThe number of ticks in the monotonic time.
ticksPerSecond[get] longThe number of ticks that MonoTime has per second - i.e. the resolution or frequency of the system's monotonic clock.

Methods

NameDescription
max () Largest MonoTime possible.
min () Most negative MonoTime possible.
opBinary (rhs) Subtracting two MonoTimes results in a Duration representing the amount of time which elapsed between them.
opBinary (rhs) Adding or subtracting a Duration to/from a MonoTime results in a MonoTime which is adjusted by that amount.
opCmp (rhs) Compares this MonoTime with the given MonoTime.
opOpAssign (rhs) 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.zero, and it's more explicit than MonoTime.init.

See Also

ClockType

Authors

Jonathan M Davis and Kato Shoichi

License

Boost License 1.0.