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.
Copyright
Types 14
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)); --------------------
long _hnsecsDuration zero() @property nothrow @nogcA Duration of 0. It's shorter than doing something like dur!"seconds"(0) and more explicit than Duration.init.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 @nogcDuration opBinaryRight(string op, D)(D lhs) if ((op == "+" || op == "-") &&
is(immutable D == immutable TickDuration)) const nothrow @nogcTickDuration is DeprecatedDuration 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 @nogcDuration 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.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 Deprecatedbool opCast(T : bool)() const nothrow @nogcAllow Duration to be used as a boolean. Returns: `true` if this duration is non-zero.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.this(long hnsecs)split(units...)Splits out the Duration into the given units.Ditto
Ditto
Ditto
Ditto
Ditto
Ditto
Ditto
Ditto
Ditto
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.
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 toMonoTimeImpl!(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
private _clockTypeIdx(clockType) _clockIdxprivate _clockTypeName(clockType) _clockNamelong _ticksMonoTimeImpl 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.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 @nogcDittolong 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.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.
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.TickDuration TDRvalueOf(TickDuration td)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.void time_initializer()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 @nogcTickDuration 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.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 arevoid 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 areTickDuration 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 areTickDuration 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 areTickDuration 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,...this(long ticks)Params: ticks = The number of ticks in the TickDuration.Exception type used by core.time.
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
T 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 @nogcDuration 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.long convClockFreq(long ticks, long srcTicksPerSecond, long dstTicksPerSecond) @safe pure nothrow @nogcConverts the given time from one clock frequency/resolution to another.long ticksToNSecs(long ticks) @safe pure nothrow @nogcConvenience wrapper around convClockFreq which converts ticks at a clock frequency of MonoTime.ticksPerSecond to nanoseconds.long 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, ...TickDuration abs(TickDuration duration) @safe pure nothrow @nogclong 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 @nogcbool allAreAcceptedUnits(acceptedUnits...)(scope string[] units)bool unitsAreInDescendingOrder(scope string[] units)Variables 1
Templates 1
if (units == "weeks" ||
units == "days" ||
units == "hours" ||
units == "minutes" ||
units == "seconds" ||
units == "msecs" ||
units == "usecs" ||
units == "hnsecs")