core.time

Module containing core time functionality, such as Duration (which represents a duration of time) or MonoTime (which represents a timestamp of the system's monotonic clock).

Various functions take a string (or strings) to represent a unit of time (e.g. convert!("days", "hours")(numDays)). The valid strings to use with such functions are "years", "months", "weeks", "days", "hours", "minutes", "seconds", "msecs" (milliseconds), "usecs" (microseconds), "hnsecs" (hecto-nanoseconds - i.e. 100 ns) or some subset thereof. There are a few functions that also allow "nsecs", but very little actually has precision greater than hnsecs.

Types 14

structDuration

Represents a duration of time of weeks or less (kept internally as hnsecs). (e.g. 22 days or 700 seconds).

It is used when representing a duration of time - such as how long to sleep with Thread.sleep.

In std.datetime, it is also used as the result of various arithmetic operations on time points.

Use the dur function or one of its non-generic aliases to create

Durations.

It's not possible to create a Duration of months or years, because the variable number of days in a month or year makes it impossible to convert between months or years and smaller units without a specific date. So, nothing uses Durations when dealing with months or years. Rather, functions specific to months and years are defined. For instance,

Date has add!"years" and add!"months" for adding

years and months rather than creating a Duration of years or months and adding that to a Date. But Duration is used when dealing with weeks or smaller.

Examples

--------------------

import std.datetime;

assert(dur!"days"(12) == dur!"hnsecs"(10_368_000_000_000L)); assert(dur!"hnsecs"(27) == dur!"hnsecs"(27)); assert(std.datetime.Date(2010, 9, 7) + dur!"days"(5) == std.datetime.Date(2010, 9, 12));

assert(days(-12) == dur!"hnsecs"(-10_368_000_000_000L)); assert(hnsecs(-27) == dur!"hnsecs"(-27)); assert(std.datetime.Date(2010, 9, 7) - std.datetime.Date(2010, 10, 3) == days(-26)); --------------------

Fields
long _hnsecs
Methods
void toString(SinkT)(scope SinkT sink) const scopeConverts this `Duration` to a `string`.
Duration zero() @property nothrow @nogcA Duration of 0. It's shorter than doing something like dur!"seconds"(0) and more explicit than Duration.init.
Duration max() @property nothrow @nogcLargest Duration possible.
Duration min() @property nothrow @nogcMost negative Duration possible.
int opCmp(Duration rhs) const nothrow @nogcCompares this Duration with the given Duration.
Duration opBinary(string op)(const Duration rhs) if (op == "+" || op == "-" || op == "%") const nothrow @nogcAdds, subtracts or calculates the modulo of two durations.
Duration opBinary(string op)(const TickDuration rhs) if (op == "+" || op == "-") const nothrow @nogc
Duration opBinaryRight(string op, D)(D lhs) if ((op == "+" || op == "-") && is(immutable D == immutable TickDuration)) const nothrow @nogcTickDuration is Deprecated
Duration opOpAssign(string op)(const Duration rhs) if (op == "+" || op == "-" || op == "%") ref nothrow @nogcAdds, subtracts or calculates the modulo of two durations as well as assigning the result to this Duration.
Duration opOpAssign(string op)(const TickDuration rhs) if (op == "+" || op == "-") ref nothrow @nogc
Duration opBinary(string op)(long value) if (op == "*" || op == "/") const nothrow @nogcMultiplies or divides the duration by an integer value.
Duration opOpAssign(string op)(long value) if (op == "*" || op == "/") ref nothrow @nogcMultiplies/Divides the duration by an integer value as well as assigning the result to this Duration.
long opBinary(string op)(Duration rhs) if (op == "/") const nothrow @nogcDivides two durations.
Duration opBinaryRight(string op)(long value) if (op == "*") const nothrow @nogcMultiplies an integral value and a Duration.
Duration opUnary(string op)() if (op == "-") const nothrow @nogcReturns the negation of this Duration.
TickDuration opCast(T)() if (is(immutable T == immutable TickDuration)) const nothrow @nogcTickDuration is Deprecated
bool opCast(T : bool)() const nothrow @nogcAllow Duration to be used as a boolean. Returns: `true` if this duration is non-zero.
Duration opCast(T)() if (is(immutable T == immutable Duration)) const nothrow @nogc
long total(string units)() if (units == "weeks" || units == "days" || units == "hours" || units == "minutes" || units == "seconds" || units == "msecs" || units == "usecs" || units == "hnsecs" || units == "nsecs") @property const nothrow @nogcReturns the total number of the given units in this Duration. So, unlike split, it does not strip out the larger units.
string toString() const scope nothrowDitto
bool isNegative() @property const nothrow @nogcReturns whether this Duration is negative.
Constructors
this(long hnsecs)
Nested Templates
split(units...)Splits out the Duration into the given units.
aliasweeks = dur!"weeks"

Ditto

aliasdays = dur!"days"

Ditto

aliashours = dur!"hours"

Ditto

aliasminutes = dur!"minutes"

Ditto

aliasseconds = dur!"seconds"

Ditto

aliasmsecs = dur!"msecs"

Ditto

aliasusecs = dur!"usecs"

Ditto

aliashnsecs = dur!"hnsecs"

Ditto

aliasnsecs = dur!"nsecs"

Ditto

aliasMonoTime = MonoTimeImpl!(ClockType.normal)

alias for MonoTimeImpl instantiated with ClockType.normal. This is what most programs should use. It's also what much of MonoTimeImpl uses in its documentation (particularly in the examples), because that's what's going to be used in most code.

structMonoTimeImpl(ClockType clockType)

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

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 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.

See Also

Fields
private _clockTypeIdx(clockType) _clockIdx
private _clockTypeName(clockType) _clockName
long _ticks
Methods
MonoTimeImpl currTime() @property @trusted nothrow @nogcThe 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...
MonoTimeImpl zero()A MonoTime of 0 ticks. It's provided to be consistent with Duration.zero, and it's more explicit than MonoTime.init.
MonoTimeImpl max()Largest MonoTime possible.
MonoTimeImpl min()Most negative MonoTime possible.
int opCmp(MonoTimeImpl rhs) const pure nothrow @nogcCompares this MonoTime with the given MonoTime.
Duration opBinary(string op)(MonoTimeImpl rhs) if (op == "-") const pure nothrow @nogcSubtracting two MonoTimes results in a Duration representing the amount of time which elapsed between them.
MonoTimeImpl opBinary(string op)(Duration rhs) if (op == "+" || op == "-") const pure nothrow @nogcAdding or subtracting a Duration to/from a MonoTime results in a MonoTime which is adjusted by that amount.
MonoTimeImpl opOpAssign(string op)(Duration rhs) if (op == "+" || op == "-") ref pure nothrow @nogcDitto
long ticks() @property const pure nothrow @nogcThe number of ticks in the monotonic time.
long ticksPerSecond() @property pure nothrow @nogcThe number of ticks that MonoTime has per second - i.e. the resolution or frequency of the system's monotonic clock.
string toString() const pure nothrow
deprecated TickDuration has been deprecated, please use Duration or MonoTime instead
Warning: TickDuration is deprecated. Please use MonoTime for the cases where a monotonic timestamp is needed

and Duration when a duration is needed, rather than using TickDuration.

Represents a duration of time in system clock ticks.

The system clock ticks are the ticks of the system clock at the highest precision that the system provides.

Fields
long ticksPerSecThe number of ticks that the system clock has in one second.
TickDuration appOriginThe tick of the system clock (as a TickDuration) when the application started.
long lengthThe number of system ticks in this TickDuration.
Methods
TickDuration zero()It's the same as TickDuration(0), but it's provided to be consistent with Duration, which provides a zero property.
TickDuration max()Largest TickDuration possible.
TickDuration min()Most negative TickDuration possible.
long seconds() @property @safe const pure nothrow @nogcReturns the total number of seconds in this TickDuration.
long msecs() @property @safe const pure nothrow @nogcReturns the total number of milliseconds in this TickDuration.
long usecs() @property @safe const pure nothrow @nogcReturns the total number of microseconds in this TickDuration.
long hnsecs() @property @safe const pure nothrow @nogcReturns the total number of hecto-nanoseconds in this TickDuration.
long nsecs() @property @safe const pure nothrow @nogcReturns the total number of nanoseconds in this TickDuration.
TickDuration from(string units)(long length) if (units == "seconds" || units == "msecs" || units == "usecs" || units == "hnsecs" || units == "nsecs") @safe pure nothrow @nogcThis allows you to construct a TickDuration from the given time units with the given length.
Duration opCast(T)() if (is(immutable T == immutable Duration)) @safe const pure nothrow @nogcReturns a Duration with the same number of hnsecs as this TickDuration. Note that the conventional way to convert between TickDuration and Duration is using to, e.g.: tickDuration.to!Duration()
TickDuration opCast(T)() if (is(immutable T == immutable TickDuration)) @safe const pure nothrow @nogc
TickDuration opOpAssign(string op)(TickDuration rhs) if (op == "+" || op == "-") ref @safe pure nothrow @nogcAdds or subtracts two TickDurations as well as assigning the result to this TickDuration.
TickDuration opBinary(string op)(TickDuration rhs) if (op == "+" || op == "-") @safe const pure nothrow @nogcAdds or subtracts two TickDurations.
TickDuration opUnary(string op)() if (op == "-") @safe const pure nothrow @nogcReturns the negation of this TickDuration.
int opCmp(TickDuration rhs) @safe const pure nothrow @nogcoperator overloading "<, >, <=, >="
void opOpAssign(string op, T)(T value) if (op == "*" && (__traits(isIntegral, T) || __traits(isFloating, T))) @safe pure nothrow @nogcThe legal types of arithmetic for TickDuration using this operator overload are
void opOpAssign(string op, T)(T value) if (op == "/" && (__traits(isIntegral, T) || __traits(isFloating, T))) @safe pureThe legal types of arithmetic for TickDuration using this operator overload are
TickDuration opBinary(string op, T)(T value) if (op == "*" && (__traits(isIntegral, T) || __traits(isFloating, T))) @safe const pure nothrow @nogcThe legal types of arithmetic for TickDuration using this operator overload are
TickDuration opBinary(string op, T)(T value) if (op == "/" && (__traits(isIntegral, T) || __traits(isFloating, T))) @safe const pureThe legal types of arithmetic for TickDuration using this operator overload are
TickDuration currSystemTick() @property @trusted nothrow @nogcThe current system tick. The number of ticks per second varies from system to system. currSystemTick uses a monotonic clock, so it's intended for precision timing by comparing relative time values,...
Constructors
this(long ticks)Params: ticks = The number of ticks in the TickDuration.
classTimeException : Exception

Exception type used by core.time.

Constructors
this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null)Params: msg = The message for the exception. file = The file where the exception occurred. line = The line number where the exception occurred. next = The previous exception in the chain of except...
this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__)Params: msg = The message for the exception. next = The previous exception in the chain of exceptions. file = The file where the exception occurred. line = The line number where the exception occu...

Functions 16

fnT to(string units, T, D)(D td) if (is(immutable D == immutable TickDuration) && (units == "seconds" || units == "msecs" || units == "usecs" || units == "hnsecs" || units == "nsecs")) @safe pure nothrow @nogc
deprecated TickDuration has been deprecated, please use Duration or MonoTime instead
TickDuration is DEPRECATED
fnDuration dur(string units)(long length) if (units == "weeks" || units == "days" || units == "hours" || units == "minutes" || units == "seconds" || units == "msecs" || units == "usecs" || units == "hnsecs" || units == "nsecs") @safe pure nothrow @nogcThese allow you to construct a Duration from the given time units with the given length.
private fnstring _clockTypeName(ClockType clockType)
private fnsize_t _clockTypeIdx(ClockType clockType)
fnvoid _d_initMonoTime() @nogc nothrow
fnlong convClockFreq(long ticks, long srcTicksPerSecond, long dstTicksPerSecond) @safe pure nothrow @nogcConverts the given time from one clock frequency/resolution to another.
fnlong ticksToNSecs(long ticks) @safe pure nothrow @nogcConvenience wrapper around convClockFreq which converts ticks at a clock frequency of MonoTime.ticksPerSecond to nanoseconds.
fnlong nsecsToTicks(long ticks) @safe pure nothrow @nogcThe reverse of ticksToNSecs.
fnlong convert(string from, string to)(long value) if (((from == "weeks" || from == "days" || from == "hours" || from == "minutes" || from == "seconds" || from == "msecs" || from == "usecs" || from == "hnsecs" || from == "nsecs") && (to == "weeks" || to == "days" || to == "hours" || to == "minutes" || to == "seconds" || to == "msecs" || to == "usecs" || to == "hnsecs" || to == "nsecs")) || ((from == "years" || from == "months") && (to == "years" || to == "months"))) @safe pure nothrow @nogcGeneric way of converting between two time units. Conversions to smaller units use truncating division. Years and months can be converted to each other, small units can be converted to each other, ...
fnDuration abs(Duration duration) @safe pure nothrow @nogcReturns the absolute value of a duration.
fnTickDuration abs(TickDuration duration) @safe pure nothrow @nogc
deprecated TickDuration has been deprecated, please use Duration or MonoTime instead
Ditto
fnlong splitUnitsFromHNSecs(string units)(ref long hnsecs) if (units == "weeks" || units == "days" || units == "hours" || units == "minutes" || units == "seconds" || units == "msecs" || units == "usecs" || units == "hnsecs") @safe pure nothrow @nogc
fnbool allAreAcceptedUnits(acceptedUnits...)(scope string[] units)
fnbool unitsAreInDescendingOrder(scope string[] units)
fnlong _abs(long val) @safe pure nothrow @nogc
fndouble _abs(double val) @safe pure nothrow @nogc

Variables 1

private varlong[__traits(allMembers, ClockType).length] _ticksPerSecond

Templates 1

tmplhnsecsPer(string units) if (units == "weeks" || units == "days" || units == "hours" || units == "minutes" || units == "seconds" || units == "msecs" || units == "usecs" || units == "hnsecs")