Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.112.0

overview

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 usinga local clone.

core.time

Module containing core time functionality, such asDuration (which represents a duration of time) orMonoTime (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.
Cheat Sheet
SymbolDescription
    Types
DurationRepresents a duration of time of weeks or less (kept internally as hnsecs). (e.g. 22 days or 700 seconds).
TickDurationDEPRECATED Represents a duration of time in system clock ticks, using the highest precision that the system provides.
MonoTimeRepresents a monotonic timestamp in system clock ticks, using the highest precision that the system provides.
    Functions
convertGeneric way of converting between two time units.
durAllows constructing aDuration from the given time units with the given length.
weeks days hours
minutes seconds msecs
usecs hnsecs nsecs
Convenience aliases fordur.
absReturns the absolute value of a duration.
Conversions
FromDurationFromTickDurationFrom units
ToDuration-tickDuration.to!Duration()dur!"msecs"(5) or5.msecs()
ToTickDurationduration.to!TickDuration()-TickDuration.from!"msecs"(msecs)
To unitsduration.total!"days"tickDuration.msecsconvert!("days", "msecs")(msecs)
License:
Boost License 1.0.
Authors:
Jonathan M Davis and Kato Shoichi

Sourcecore/time.d

enumClockType: int;
What type of clock to use withMonoTime /MonoTimeImpl orstd.datetime.Clock.currTime. They default toClockType.normal, and most programs do not need to ever deal with the others.
The otherClockTypes are provided so that other clocks provided by the underlying C, system calls can be used withMonoTimeImpl orstd.datetime.Clock.currTime without having to use the C API directly.
In the case of the monotonic time,MonoTimeImpl is templatized onClockType, whereas withstd.datetime.Clock.currTime, its a runtime argument, since in the case of the monotonic time, the type of the clock affects the resolution of aMonoTimeImpl object, whereas withstd.datetime.SysTime, its resolution is always hecto-nanoseconds regardless of the source of the time.
ClockType.normal,ClockType.coarse, andClockType.precise work with bothClock.currTime andMonoTimeImpl.ClockType.second only works withClock.currTime. The others only work withMonoTimeImpl.
normal
Use the normal clock.
bootTime
Linux,OpenBSD-Only
UsesCLOCK_BOOTTIME.
coarse
Use the coarse clock, not the normal one (e.g. on Linux, that would beCLOCK_REALTIME_COARSE instead ofCLOCK_REALTIME forclock_gettime if a function is using the realtime clock). It's generally faster to get the time with the coarse clock than the normal clock, but it's less precise (e.g. 1 msec instead of 1 usec or 1 nsec). Howeover, itis guaranteed to still have sub-second precision (just not as high as withClockType.normal).
On systems which do not support a coarser clock,MonoTimeImpl!(ClockType.coarse) will internally use the same clock asMonoTime does, andClock.currTime!(ClockType.coarse) will use the same clock asClock.currTime. This is because the coarse clock is doing the same thing as the normal clock (just at lower precision), whereas some of the other clock types (e.g.ClockType.processCPUTime) mean something fundamentally different. So, treating those asClockType.normal on systems where they weren't natively supported would give misleading results.
Most programs should not use the coarse clock, exactly because it's less precise, and most programs don't need to get the time often enough to care, but for those rare programs that need to get the time extremely frequently (e.g. hundreds of thousands of times a second) but don't care about high precision, the coarse clock might be appropriate.
Currently, only Linux and FreeBSD/DragonFlyBSD support a coarser clock, and on other platforms, it's treated asClockType.normal.
precise
Uses a more precise clock than the normal one (which is already very precise), but it takes longer to get the time. Similarly toClockType.coarse, if it's used on a system that does not support a more precise clock than the normal one, it's treated as equivalent toClockType.normal.
Currently, only FreeBSD/DragonFlyBSD supports a more precise clock, where it usesCLOCK_MONOTONIC_PRECISE for the monotonic time andCLOCK_REALTIME_PRECISE for the wall clock time.
processCPUTime
Linux,OpenBSD,Solaris-Only
UsesCLOCK_PROCESS_CPUTIME_ID.
raw
Linux-Only
UsesCLOCK_MONOTONIC_RAW.
second
Uses a clock that has a precision of one second (contrast to the coarse clock, which has sub-second precision like the normal clock does).
FreeBSD/DragonFlyBSD are the only systems which specifically have a clock set up for this (it hasCLOCK_SECOND to use withclock_gettime which takes advantage of an in-kernel cached value), but on other systems, the fastest function available will be used, and the resultingSysTime will be rounded down to the second if the clock that was used gave the time at a more precise resolution. So, it's guaranteed that the time will be given at a precision of one second and it's likely the case that will be faster thanClockType.normal, since there tend to be several options on a system to get the time at low resolutions, and they tend to be faster than getting the time at high resolutions.
So, the primary difference betweenClockType.coarse andClockType.second is thatClockType.coarse sacrifices some precision in order to get speed but is still fairly precise, whereasClockType.second tries to be as fast as possible at the expense of all sub-second precision.
threadCPUTime
Linux,OpenBSD,Solaris-Only
UsesCLOCK_THREAD_CPUTIME_ID.
uptime
DragonFlyBSD,FreeBSD,OpenBSD-Only
UsesCLOCK_UPTIME.
uptimeCoarse
FreeBSD-Only
UsesCLOCK_UPTIME_FAST.
uptimePrecise
FreeBSD-Only
UsesCLOCK_UPTIME_PRECISE.
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 withcore.thread.Thread.sleep.
In std.datetime, it is also used as the result of various arithmetic operations on time points.
Use thedur function or one of its non-generic aliases to createDurations.
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 usesDurations when dealing with months or years. Rather, functions specific to months and years are defined. For instance,std.datetime.Date hasadd!"years" andadd!"months" for adding years and months rather than creating a Duration of years or months and adding that to astd.datetime.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));
Examples:
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);
voidtoString(SinkT)(scope SinkTsink) const scope;
Converts thisDuration to astring.
The string is meant to be human readable, not machine parseable (e.g. whether there is an's' on the end of the unit name usually depends on whether it's plural or not, and empty units are not included unless the Duration iszero). Any code needing a specific string format should usetotal orsplit to get the units needed to create the desired string format and create the string itself.
The format returned by toString may or may not change in the future.
Parameters:
SinkTsinkA sink object, expected to be a delegate or aggregate implementingopCall that accepts ascope const(char)[] as argument.
static pure nothrow @nogc @property @safe Durationzero();
ADuration of0. It's shorter than doing something likedur!"seconds"(0) and more explicit thanDuration.init.
static pure nothrow @nogc @property @safe Durationmax();
LargestDuration possible.
static pure nothrow @nogc @property @safe Durationmin();
Most negativeDuration possible.
pure nothrow @nogc @safe intopCmp(Durationrhs) const;
Compares thisDuration with the givenDuration.
Returns:
this < rhs< 0
this == rhs0
this > rhs> 0
nothrow @nogc DurationopBinary(string op)(const Durationrhs) const
if (op == "+" || op == "-" || op == "%");
Adds, subtracts or calculates the modulo of two durations.
The legal types of arithmetic forDuration using this operator are
Duration+Duration-->Duration
Duration-Duration-->Duration
Duration%Duration-->Duration
Parameters:
DurationrhsThe duration to add to or subtract from thisDuration.
nothrow @nogc DurationopBinaryRight(string op, D)(Dlhs) const
if ((op == "+" || op == "-") && is(immutable(D) == immutable(TickDuration)));
TickDuration is Deprecated
Adds or subtracts two durations.
The legal types of arithmetic forDuration using this operator are
TickDuration+Duration-->Duration
TickDuration-Duration-->Duration
Parameters:
DlhsTheTickDuration to add to thisDuration or to subtract thisDuration from.
nothrow @nogc ref DurationopOpAssign(string op)(const Durationrhs)
if (op == "+" || op == "-" || op == "%");
Adds, subtracts or calculates the modulo of two durations as well as assigning the result to thisDuration.
The legal types of arithmetic forDuration using this operator are
Duration+Duration-->Duration
Duration-Duration-->Duration
Duration%Duration-->Duration
Parameters:
DurationrhsThe duration to add to or subtract from thisDuration.
nothrow @nogc DurationopBinary(string op)(longvalue) const
if (op == "*" || op == "/");
Multiplies or divides the duration by an integer value.
The legal types of arithmetic forDuration using this operator overload are
Duration*long-->Duration
Duration/long-->Duration
Parameters:
longvalueThe value to multiply thisDuration by.
nothrow @nogc ref DurationopOpAssign(string op)(longvalue)
if (op == "*" || op == "/");
Multiplies/Divides the duration by an integer value as well as assigning the result to thisDuration.
The legal types of arithmetic forDuration using this operator overload are
Duration*long-->Duration
Duration/long-->Duration
Parameters:
longvalueThe value to multiply/divide thisDuration by.
nothrow @nogc longopBinary(string op)(Durationrhs) const
if (op == "/");
Divides two durations.
The legal types of arithmetic forDuration using this operator are
Duration/Duration-->long
Parameters:
DurationrhsThe duration to divide thisDuration by.
nothrow @nogc DurationopBinaryRight(string op)(longvalue) const
if (op == "*");
Multiplies an integral value and aDuration.
The legal types of arithmetic forDuration using this operator overload are
long*Duration-->Duration
Parameters:
longvalueThe number of units to multiply thisDuration by.
nothrow @nogc DurationopUnary(string op)() const
if (op == "-");
Returns the negation of thisDuration.
nothrow @nogc TickDurationopCast(T)() const
if (is(immutable(T) == immutable(TickDuration)));
TickDuration is Deprecated
Returns aTickDuration with the same number of hnsecs as thisDuration. Note that the conventional way to convert betweenDuration andTickDuration is usingstd.conv.to, e.g.:duration.to!TickDuration()
nothrow @nogc boolopCast(T : bool)() const;
Allow Duration to be used as a boolean.
Returns:
true if this duration is non-zero.
templatesplit(units...) if (allAreAcceptedUnits!("weeks", "days", "hours", "minutes", "seconds", "msecs", "usecs", "hnsecs", "nsecs")([units]) && unitsAreInDescendingOrder([units]))
Splits out the Duration into the given units.
split takes the list of time units to split out as template arguments. The time unit strings must be given in decreasing order. How it returns the values for those units depends on the overload used.
The overload which accepts function arguments takes integral types in the order that the time unit strings were given, and those integers are passed byref. split assigns the values for the units to each corresponding integer. Any integral type may be used, but no attempt is made to prevent integer overflow, so don't use small integral types in circumstances where the values for those units aren't likely to fit in an integral type that small.
The overload with no arguments returns the values for the units in a struct with members whose names are the same as the given time unit strings. The members are alllongs. This overload will also work with no time strings being given, in which caseall of the time units from weeks through hnsecs will be provided (but no nsecs, since it would always be0).
For both overloads, the entire value of the Duration is split among the units (rather than splitting the Duration across all units and then only providing the values for the requested units), so if only one unit is given, the result is equivalent tototal.
"nsecs" is accepted by split, but"years" and"months" are not.
For negative durations, all of the split values will be negative.
Examples:
{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);}
nothrow @nogc voidsplit(Args...)(out Argsargs) const
if (units.length != 0 && (args.length == units.length) && allAreMutableIntegralTypes!Args);

nothrow @nogc autosplit() const;
Ditto
nothrow @nogc @property longtotal(string units)() const
if (units == "weeks" || units == "days" || units == "hours" || units == "minutes" || units == "seconds" || units == "msecs" || units == "usecs" || units == "hnsecs" || units == "nsecs");

pure nothrow @safe stringtoString() const;
Returns the total number of the given units in thisDuration. So, unlikesplit, it does not strip out the larger units.
Examples:
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);
Examples:
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");
pure nothrow @nogc @property @safe boolisNegative() const;
Returns whether thisDuration is negative.
pure nothrow @nogc @safe Tto(string units, T, D)(Dtd)
if (is(immutable(D) == immutable(TickDuration)) && (units == "seconds" || units == "msecs" || units == "usecs" || units == "hnsecs" || units == "nsecs"));
TickDuration is DEPRECATED
Converts aTickDuration to the given units as either an integral value or a floating point value.
Parameters:
unitsThe units to convert to. Accepts"seconds" and smaller only.
TThe type to convert to (either an integral type or a floating point type).
DtdThe TickDuration to convert
Examples:
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);
pure nothrow @nogc @safe Durationdur(string units)(longlength)
if (units == "weeks" || units == "days" || units == "hours" || units == "minutes" || units == "seconds" || units == "msecs" || units == "usecs" || units == "hnsecs" || units == "nsecs");

aliasweeks = dur!"weeks".dur;

aliasdays = dur!"days".dur;

aliashours = dur!"hours".dur;

aliasminutes = dur!"minutes".dur;

aliasseconds = dur!"seconds".dur;

aliasmsecs = dur!"msecs".dur;

aliasusecs = dur!"usecs".dur;

aliashnsecs = dur!"hnsecs".dur;

aliasnsecs = dur!"nsecs".dur;
These allow you to construct aDuration from the given time units with the given length.
You can either use the generic functiondur and give it the units as astring or use the named aliases.
The possible values for units are"weeks","days","hours","minutes","seconds","msecs" (milliseconds),"usecs", (microseconds),"hnsecs" (hecto-nanoseconds, i.e. 100 ns), and"nsecs".
Parameters:
unitsThe time units of theDuration (e.g."days").
longlengthThe number of units in theDuration.
Examples:
// 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);
aliasMonoTime = MonoTimeImpl!(ClockType.normal).MonoTimeImpl;
alias forMonoTimeImpl instantiated withClockType.normal. This is what most programs should use. It's also what much ofMonoTimeImpl 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 bystd.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, whereasstd.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 theticks andticksPerSecond 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 ofMonoTimeImpl's documentation. ButMonoTimeImpl can be instantiated with other clock types for those rare programs that need it.
See Also:
static nothrow @nogc @property @trusted MonoTimeImplcurrTime();
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.
On Windows,QueryPerformanceCounter is used. On Mac OS X,mach_absolute_time is used, while on other POSIX systems,clock_gettime is used.
Warning: On some systems, the monotonic clock may stop counting when the computer goes to sleep or hibernates. So, the monotonic clock may indicate less time than has actually passed if that occurs. This is known to happen on Mac OS X. It has not been tested whether it occurs on either Windows or Linux.
MonoTimeImplzero();
AMonoTime of0 ticks. It's provided to be consistent withDuration.zero, and it's more explicit thanMonoTime.init.
MonoTimeImplmax();
LargestMonoTime possible.
MonoTimeImplmin();
Most negativeMonoTime possible.
pure nothrow @nogc intopCmp(MonoTimeImplrhs) const;
Compares this MonoTime with the given MonoTime.
Returns:
this < rhs< 0
this == rhs0
this > rhs> 0
pure nothrow @nogc DurationopBinary(string op)(MonoTimeImplrhs) const
if (op == "-");
Subtracting two MonoTimes results in aDuration representing the amount of time which elapsed between them.
The primary way that programs should time how long something takes is to do
MonoTime 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.
pure nothrow @nogc MonoTimeImplopBinary(string op)(Durationrhs) const
if (op == "+" || op == "-");

pure nothrow @nogc ref MonoTimeImplopOpAssign(string op)(Durationrhs)
if (op == "+" || op == "-");
Adding or subtracting aDuration to/from a MonoTime results in a MonoTime which is adjusted by that amount.
pure nothrow @nogc @property longticks() const;
The number of ticks in the monotonic time.
Most programs should not use this directly, but it's exposed for those few programs that need it.
The main reasons that a program might need to use ticks directly is if the system clock has higher precision than hnsecs, and the program needs that higher precision, or if the program needs to avoid the rounding errors caused by converting to hnsecs.
static pure nothrow @nogc @property longticksPerSecond();
The number of ticks that MonoTime has per second - i.e. the resolution or frequency of the system's monotonic clock.
e.g. if the system clock had a resolution of microseconds, then ticksPerSecond would be1_000_000.
pure nothrow stringtoString() const;
pure nothrow @nogc @safe longconvClockFreq(longticks, longsrcTicksPerSecond, longdstTicksPerSecond);
Converts the given time from one clock frequency/resolution to another.
See Also:
Examples:
// 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);
pure nothrow @nogc @safe longticksToNSecs(longticks);
Convenience wrapper aroundconvClockFreq which converts ticks at a clock frequency ofMonoTime.ticksPerSecond to nanoseconds.
It's primarily of use whenMonoTime.ticksPerSecond is greater than hecto-nanosecond resolution, and an application needs a higher precision than hecto-nanoceconds.
See Also:
Examples:
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));
pure nothrow @nogc @safe longnsecsToTicks(longticks);
The reverse ofticksToNSecs.
structTickDuration;
Warning: TickDuration is deprecated. Please useMonoTime for the cases where a monotonic timestamp is needed andDuration 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.
deprecated static immutable longticksPerSec;
The number of ticks that the system clock has in one second.
IfticksPerSec is0, then thenTickDuration failed to get the value ofticksPerSec on the current system, andTickDuration is not going to work. That would be highly abnormal though.
deprecated static immutable TickDurationappOrigin;
The tick of the system clock (as aTickDuration) when the application started.
deprecated static pure nothrow @nogc @property @safe TickDurationzero();
It's the same asTickDuration(0), but it's provided to be consistent withDuration, which provides azero property.
deprecated static pure nothrow @nogc @property @safe TickDurationmax();
LargestTickDuration possible.
deprecated static pure nothrow @nogc @property @safe TickDurationmin();
Most negativeTickDuration possible.
deprecated longlength;
The number of system ticks in thisTickDuration.
You can convert thislength into the number of seconds by dividing it byticksPerSec (or using one the appropriate property function to do it).
deprecated pure nothrow @nogc @property @safe longseconds() const;
Returns the total number of seconds in thisTickDuration.
deprecated pure nothrow @nogc @property @safe longmsecs() const;
Returns the total number of milliseconds in thisTickDuration.
deprecated pure nothrow @nogc @property @safe longusecs() const;
Returns the total number of microseconds in thisTickDuration.
deprecated pure nothrow @nogc @property @safe longhnsecs() const;
Returns the total number of hecto-nanoseconds in thisTickDuration.
deprecated pure nothrow @nogc @property @safe longnsecs() const;
Returns the total number of nanoseconds in thisTickDuration.
pure nothrow @nogc @safe TickDurationfrom(string units)(longlength)
if (units == "seconds" || units == "msecs" || units == "usecs" || units == "hnsecs" || units == "nsecs");
This allows you to construct aTickDuration from the given time units with the given length.
Parameters:
unitsThe time units of theTickDuration (e.g."msecs").
longlengthThe number of units in theTickDuration.
pure nothrow @nogc @safe DurationopCast(T)() const
if (is(immutable(T) == immutable(Duration)));
Returns aDuration with the same number of hnsecs as thisTickDuration. Note that the conventional way to convert betweenTickDuration andDuration is usingstd.conv.to, e.g.:tickDuration.to!Duration()
pure nothrow @nogc ref @safe TickDurationopOpAssign(string op)(TickDurationrhs)
if (op == "+" || op == "-");
Adds or subtracts twoTickDurations as well as assigning the result to thisTickDuration.
The legal types of arithmetic forTickDuration using this operator are
TickDuration+=TickDuration-->TickDuration
TickDuration-=TickDuration-->TickDuration
Parameters:
TickDurationrhsTheTickDuration to add to or subtract from thisTickDuration.
pure nothrow @nogc @safe TickDurationopBinary(string op)(TickDurationrhs) const
if (op == "+" || op == "-");
Adds or subtracts twoTickDurations.
The legal types of arithmetic forTickDuration using this operator are
TickDuration+TickDuration-->TickDuration
TickDuration-TickDuration-->TickDuration
Parameters:
TickDurationrhsTheTickDuration to add to or subtract from thisTickDuration.
pure nothrow @nogc @safe TickDurationopUnary(string op)() const
if (op == "-");
Returns the negation of thisTickDuration.
deprecated pure nothrow @nogc @safe intopCmp(TickDurationrhs) const;
operator overloading "<, >, <=, >="
pure nothrow @nogc @safe voidopOpAssign(string op, T)(Tvalue)
if (op == "*" && (__traits(isIntegral, T) || __traits(isFloating, T)));
The legal types of arithmetic forTickDuration using this operator overload are
TickDuration*long-->TickDuration
TickDuration*floating point-->TickDuration
Parameters:
TvalueThe value to divide from this duration.
pure @safe voidopOpAssign(string op, T)(Tvalue)
if (op == "/" && (__traits(isIntegral, T) || __traits(isFloating, T)));
The legal types of arithmetic forTickDuration using this operator overload are
TickDuration/long-->TickDuration
TickDuration/floating point-->TickDuration
Parameters:
TvalueThe value to divide from thisTickDuration.
Throws:
TimeException if an attempt to divide by0 is made.
pure nothrow @nogc @safe TickDurationopBinary(string op, T)(Tvalue) const
if (op == "*" && (__traits(isIntegral, T) || __traits(isFloating, T)));
The legal types of arithmetic forTickDuration using this operator overload are
TickDuration*long-->TickDuration
TickDuration*floating point-->TickDuration
Parameters:
TvalueThe value to divide from thisTickDuration.
pure @safe TickDurationopBinary(string op, T)(Tvalue) const
if (op == "/" && (__traits(isIntegral, T) || __traits(isFloating, T)));
The legal types of arithmetic forTickDuration using this operator overload are
TickDuration/long-->TickDuration
TickDuration/floating point-->TickDuration
Parameters:
TvalueThe value to divide from thisTickDuration.
Throws:
TimeException if an attempt to divide by0 is made.
deprecated pure nothrow @nogc @safe this(longticks);
Parameters:
longticksThe number of ticks in the TickDuration.
deprecated static nothrow @nogc @property @trusted TickDurationcurrSystemTick();
The 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, not for getting the current system time.
On Windows,QueryPerformanceCounter is used. On Mac OS X,mach_absolute_time is used, while on other Posix systems,clock_gettime is used. Ifmach_absolute_time orclock_gettime is unavailable, then Posix systems usegettimeofday (the decision is made whenTickDuration is compiled), which unfortunately, is not monotonic, but ifmach_absolute_time andclock_gettime aren't available, thengettimeofday is the best that there is.
Warning: On some systems, the monotonic clock may stop counting when the computer goes to sleep or hibernates. So, the monotonic clock could be off if that occurs. This is known to happen on Mac OS X. It has not been tested whether it occurs on either Windows or on Linux.
Throws:
TimeException if it fails to get the time.
pure nothrow @nogc @safe longconvert(string from, string to)(longvalue)
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"));
Generic 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, but years and months cannot be converted to or from smaller units (due to the varying number of days in a month or year).
Parameters:
fromThe units of time to convert from.
toThe units of time to convert to.
longvalueThe value to convert.
Examples:
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);
classTimeException:object.Exception;
Exception type used by core.time.
pure nothrow @safe this(stringmsg, stringfile = __FILE__, size_tline = __LINE__, Throwablenext = null);
Parameters:
stringmsgThe message for the exception.
stringfileThe file where the exception occurred.
size_tlineThe line number where the exception occurred.
ThrowablenextThe previous exception in the chain of exceptions, if any.
pure nothrow @safe this(stringmsg, Throwablenext, stringfile = __FILE__, size_tline = __LINE__);
Parameters:
stringmsgThe message for the exception.
ThrowablenextThe previous exception in the chain of exceptions.
stringfileThe file where the exception occurred.
size_tlineThe line number where the exception occurred.
pure nothrow @nogc @safe Durationabs(Durationduration);

deprecated pure nothrow @nogc @safe TickDurationabs(TickDurationduration);
Returns the absolute value of a duration.
Copyright © 1999-2026 by theD Language Foundation | Page generated byDdoc on Sat Feb 21 04:06:13 2026

[8]ページ先頭

©2009-2026 Movatter.jp