| Symbol | Description |
|---|---|
| Types | |
| Duration | Represents a duration of time of weeks or less (kept internally as hnsecs). (e.g. 22 days or 700 seconds). |
| TickDuration | DEPRECATED Represents a duration of time in system clock ticks, using the highest precision that the system provides. |
| MonoTime | Represents a monotonic timestamp in system clock ticks, using the highest precision that the system provides. |
| Functions | |
| convert | Generic way of converting between two time units. |
| dur | Allows constructing aDuration from the given time units with the given length. |
| weeks days hours minutes seconds msecs usecs hnsecs nsecs | Convenience aliases fordur. |
| abs | Returns the absolute value of a duration. |
| FromDuration | FromTickDuration | From units | |
|---|---|---|---|
| ToDuration | - | tickDuration.to!Duration() | dur!"msecs"(5) or5.msecs() |
| ToTickDuration | duration.to!TickDuration() | - | TickDuration.from!"msecs"(msecs) |
| To units | duration.total!"days" | tickDuration.msecs | convert!("days", "msecs")(msecs) |
Sourcecore/time.d
ClockType: int;normalbootTimecoarsepreciseprocessCPUTimerawsecondthreadCPUTimeuptimeuptimeCoarseuptimePreciseDuration;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));
import core.time;// using the dur templateauto numDays = dur!"days"(12);// using the days functionnumDays = days(12);// alternatively using UFCS syntaxnumDays = 12.days;auto myTime = 100.msecs + 20_000.usecs + 30_000.hnsecs;assert(myTime == 123.msecs);
toString(SinkT)(scope SinkTsink) const scope;SinkTsink | A sink object, expected to be a delegate or aggregate implementingopCall that accepts ascope const(char)[] as argument. |
zero();max();min();opCmp(Durationrhs) const;| this < rhs | < 0 |
| this == rhs | 0 |
| this > rhs | > 0 |
opBinary(string op)(const Durationrhs) const| Duration | + | Duration | --> | Duration |
| Duration | - | Duration | --> | Duration |
| Duration | % | Duration | --> | Duration |
Durationrhs | The duration to add to or subtract from thisDuration. |
opBinaryRight(string op, D)(Dlhs) const| TickDuration | + | Duration | --> | Duration |
| TickDuration | - | Duration | --> | Duration |
Dlhs | TheTickDuration to add to thisDuration or to subtract thisDuration from. |
opOpAssign(string op)(const Durationrhs)| Duration | + | Duration | --> | Duration |
| Duration | - | Duration | --> | Duration |
| Duration | % | Duration | --> | Duration |
Durationrhs | The duration to add to or subtract from thisDuration. |
opBinary(string op)(longvalue) const| Duration | * | long | --> | Duration |
| Duration | / | long | --> | Duration |
longvalue | The value to multiply thisDuration by. |
opOpAssign(string op)(longvalue)| Duration | * | long | --> | Duration |
| Duration | / | long | --> | Duration |
longvalue | The value to multiply/divide thisDuration by. |
opBinary(string op)(Durationrhs) const| Duration | / | Duration | --> | long |
Durationrhs | The duration to divide thisDuration by. |
opBinaryRight(string op)(longvalue) const| long | * | Duration | --> | Duration |
longvalue | The number of units to multiply thisDuration by. |
opUnary(string op)() constopCast(T)() constopCast(T : bool)() const;split(units...) if (allAreAcceptedUnits!("weeks", "days", "hours", "minutes", "seconds", "msecs", "usecs", "hnsecs", "nsecs")([units]) && unitsAreInDescendingOrder([units])){auto d = dur!"days"(12) + dur!"minutes"(7) + dur!"usecs"(501223);long days;int seconds;short msecs; d.split!("days","seconds","msecs")(days, seconds, msecs);assert(days == 12);assert(seconds == 7 * 60);assert(msecs == 501);auto splitStruct = d.split!("days","seconds","msecs")();assert(splitStruct.days == 12);assert(splitStruct.seconds == 7 * 60);assert(splitStruct.msecs == 501);auto fullSplitStruct = d.split();assert(fullSplitStruct.weeks == 1);assert(fullSplitStruct.days == 5);assert(fullSplitStruct.hours == 0);assert(fullSplitStruct.minutes == 7);assert(fullSplitStruct.seconds == 0);assert(fullSplitStruct.msecs == 501);assert(fullSplitStruct.usecs == 223);assert(fullSplitStruct.hnsecs == 0);assert(d.split!"minutes"().minutes == d.total!"minutes");}{auto d = dur!"days"(12);assert(d.split!"weeks"().weeks == 1);assert(d.split!"days"().days == 12);assert(d.split().weeks == 1);assert(d.split().days == 5);}{auto d = dur!"days"(7) + dur!"hnsecs"(42);assert(d.split!("seconds","nsecs")().nsecs == 4200);}{auto d = dur!"days"(-7) + dur!"hours"(-9);auto result = d.split!("days","hours")();assert(result.days == -7);assert(result.hours == -9);}split(Args...)(out Argsargs) constargs.length == units.length) && allAreMutableIntegralTypes!Args);split() const;total(string units)() consttoString() const;assert(dur!"weeks"(12).total!"weeks" == 12);assert(dur!"weeks"(12).total!"days" == 84);assert(dur!"days"(13).total!"weeks" == 1);assert(dur!"days"(13).total!"days" == 13);assert(dur!"hours"(49).total!"days" == 2);assert(dur!"hours"(49).total!"hours" == 49);assert(dur!"nsecs"(2007).total!"hnsecs" == 20);assert(dur!"nsecs"(2007).total!"nsecs" == 2000);
assert(Duration.zero.toString() =="0 hnsecs");assert(weeks(5).toString() =="5 weeks");assert(days(2).toString() =="2 days");assert(hours(1).toString() =="1 hour");assert(minutes(19).toString() =="19 minutes");assert(seconds(42).toString() =="42 secs");assert(msecs(42).toString() =="42 ms");assert(usecs(27).toString() =="27 μs");assert(hnsecs(5).toString() =="5 hnsecs");assert(seconds(121).toString() =="2 minutes and 1 sec");assert((minutes(5) + seconds(3) + usecs(4)).toString() =="5 minutes, 3 secs, and 4 μs");assert(seconds(-42).toString() =="-42 secs");assert(usecs(-5239492).toString() =="-5 secs, -239 ms, and -492 μs");
isNegative() const;to(string units, T, D)(Dtd)| units | The units to convert to. Accepts"seconds" and smaller only. |
| T | The type to convert to (either an integral type or a floating point type). |
Dtd | The TickDuration to convert |
auto t = TickDuration.from!"seconds"(1000);long tl =to!("seconds",long)(t);assert(tl == 1000);import core.stdc.math : fabs;doubletd =to!("seconds",double)(t);assert(fabs(td - 1000) < 0.001);
dur(string units)(longlength)weeks = dur!"weeks".dur;days = dur!"days".dur;hours = dur!"hours".dur;minutes = dur!"minutes".dur;seconds = dur!"seconds".dur;msecs = dur!"msecs".dur;usecs = dur!"usecs".dur;hnsecs = dur!"hnsecs".dur;nsecs = dur!"nsecs".dur;| units | The time units of theDuration (e.g."days"). |
longlength | The number of units in theDuration. |
// Genericassert(dur!"weeks"(142).total!"weeks" == 142);assert(dur!"days"(142).total!"days" == 142);assert(dur!"hours"(142).total!"hours" == 142);assert(dur!"minutes"(142).total!"minutes" == 142);assert(dur!"seconds"(142).total!"seconds" == 142);assert(dur!"msecs"(142).total!"msecs" == 142);assert(dur!"usecs"(142).total!"usecs" == 142);assert(dur!"hnsecs"(142).total!"hnsecs" == 142);assert(dur!"nsecs"(142).total!"nsecs" == 100);// Non-genericassert(weeks(142).total!"weeks" == 142);assert(days(142).total!"days" == 142);assert(hours(142).total!"hours" == 142);assert(minutes(142).total!"minutes" == 142);assert(seconds(142).total!"seconds" == 142);assert(msecs(142).total!"msecs" == 142);assert(usecs(142).total!"usecs" == 142);assert(hnsecs(142).total!"hnsecs" == 142);assert(nsecs(142).total!"nsecs" == 100);
MonoTime = MonoTimeImpl!(ClockType.normal).MonoTimeImpl;MonoTimeImpl(ClockType clockType); 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 ofMonoTimeImpl's documentation. ButMonoTimeImpl can be instantiated with other clock types for those rare programs that need it.currTime();zero();max();min();opCmp(MonoTimeImplrhs) const;| this < rhs | < 0 |
| this == rhs | 0 |
| this > rhs | > 0 |
opBinary(string op)(MonoTimeImplrhs) constMonoTime before = MonoTime.currTime;// do stuffMonoTime after = MonoTime.currTime;// How long it took.Duration timeElapsed = after - before;or to use a wrapper (such as a stop watch type) which does that.Warning: BecauseDuration is in hnsecs, whereas MonoTime is in system ticks, it's usually the case that this assertion will fail
auto before = MonoTime.currTime;// do stuffauto after = MonoTime.currTime;auto timeElapsed = after - before;assert(before + timeElapsed == after);This is generally fine, and by its very nature, converting from system ticks to any type of seconds (hnsecs, nsecs, etc.) will introduce rounding errors, but if code needs to avoid any of the small rounding errors introduced by conversion, then it needs to use MonoTime'sticks property and keep all calculations in ticks rather than usingDuration.
opBinary(string op)(Durationrhs) constopOpAssign(string op)(Durationrhs)ticks() const;ticksPerSecond();toString() const;convClockFreq(longticks, longsrcTicksPerSecond, longdstTicksPerSecond);// one tick is one second -> one tick is a hecto-nanosecondassert(convClockFreq(45, 1, 10_000_000) == 450_000_000);// one tick is one microsecond -> one tick is a millisecondassert(convClockFreq(9029, 1_000_000, 1_000) == 9);// one tick is 1/3_515_654 of a second -> 1/1_001_010 of a secondassert(convClockFreq(912_319, 3_515_654, 1_001_010) == 259_764);// one tick is 1/MonoTime.ticksPerSecond -> one tick is a nanosecond// Equivalent to ticksToNSecsauto nsecs =convClockFreq(1982, MonoTime.ticksPerSecond, 1_000_000_000);
ticksToNSecs(longticks);auto before = MonoTime.currTime;// do stuffauto after = MonoTime.currTime;auto diffInTicks = after.ticks - before.ticks;auto diffInNSecs =ticksToNSecs(diffInTicks);assert(diffInNSecs == convClockFreq(diffInTicks, MonoTime.ticksPerSecond, 1_000_000_000));
nsecsToTicks(longticks);TickDuration;ticksPerSec;appOrigin;zero();max();min();length;seconds() const;msecs() const;usecs() const;hnsecs() const;nsecs() const;from(string units)(longlength)| units | The time units of theTickDuration (e.g."msecs"). |
longlength | The number of units in theTickDuration. |
opCast(T)() constopOpAssign(string op)(TickDurationrhs)| TickDuration | += | TickDuration | --> | TickDuration |
| TickDuration | -= | TickDuration | --> | TickDuration |
TickDurationrhs | TheTickDuration to add to or subtract from thisTickDuration. |
opBinary(string op)(TickDurationrhs) const| TickDuration | + | TickDuration | --> | TickDuration |
| TickDuration | - | TickDuration | --> | TickDuration |
TickDurationrhs | TheTickDuration to add to or subtract from thisTickDuration. |
opUnary(string op)() constopCmp(TickDurationrhs) const;opOpAssign(string op, T)(Tvalue)| TickDuration | * | long | --> | TickDuration |
| TickDuration | * | floating point | --> | TickDuration |
Tvalue | The value to divide from this duration. |
opOpAssign(string op, T)(Tvalue)| TickDuration | / | long | --> | TickDuration |
| TickDuration | / | floating point | --> | TickDuration |
Tvalue | The value to divide from thisTickDuration. |
opBinary(string op, T)(Tvalue) const| TickDuration | * | long | --> | TickDuration |
| TickDuration | * | floating point | --> | TickDuration |
Tvalue | The value to divide from thisTickDuration. |
opBinary(string op, T)(Tvalue) const| TickDuration | / | long | --> | TickDuration |
| TickDuration | / | floating point | --> | TickDuration |
Tvalue | The value to divide from thisTickDuration. |
ticks);longticks | The number of ticks in the TickDuration. |
currSystemTick();convert(string from, string to)(longvalue)| from | The units of time to convert from. |
| to | The units of time to convert to. |
longvalue | The value to convert. |
assert(convert!("years","months")(1) == 12);assert(convert!("months","years")(12) == 1);assert(convert!("weeks","days")(1) == 7);assert(convert!("hours","seconds")(1) == 3600);assert(convert!("seconds","days")(1) == 0);assert(convert!("seconds","days")(86_400) == 1);assert(convert!("nsecs","nsecs")(1) == 1);assert(convert!("nsecs","hnsecs")(1) == 0);assert(convert!("hnsecs","nsecs")(1) == 100);assert(convert!("nsecs","seconds")(1) == 0);assert(convert!("seconds","nsecs")(1) == 1_000_000_000);
TimeException:object.Exception;msg, stringfile = __FILE__, size_tline = __LINE__, Throwablenext = null);stringmsg | The message for the exception. |
stringfile | The file where the exception occurred. |
size_tline | The line number where the exception occurred. |
Throwablenext | The previous exception in the chain of exceptions, if any. |
msg, Throwablenext, stringfile = __FILE__, size_tline = __LINE__);stringmsg | The message for the exception. |
Throwablenext | The previous exception in the chain of exceptions. |
stringfile | The file where the exception occurred. |
size_tline | The line number where the exception occurred. |
abs(Durationduration);abs(TickDurationduration);