core.time

Module containing core time functionality, such as Duration (which represents a duration of time).

Various functions take a string (or strings) to represent a unit of time (e.g. ). 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.





License:
.

Authors:
Jonathan M Davis and Kato Shoichi

Source:


struct Duration;
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 .

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

Use the function or on of its non-generic aliases to create s.

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 s when dealing with months or years. Rather, functions specific to months and years are defined. For instance, has and for adding years and months rather than creating a Duration of years or months and adding that to a . But Duration is used when dealing with weeks or smaller.

Examples:
import core.time;

// using the dur template
auto numDays = dur!"days"(12);

// using the days function
numDays = days(12);

// alternatively using UFCS syntax
numDays = 12.days;

auto myTime = 100.msecs + 20_000.usecs + 30_000.hnsecs;
assert(myTime == 123.msecs);


static pure nothrow @property @safe Duration zero();
A of . It's shorter than doing something like and more explicit than .

static pure nothrow @property @safe Duration max();
Largest possible.

static pure nothrow @property @safe Duration min();
Most negative possible.

const pure nothrow @safe int opCmp(Duration rhs);
Compares this with the given .

Returns:
this < rhs < 0
this == rhs 0
this > rhs > 0


const pure nothrow @safe Duration opBinary(string op, D)(D rhs) if ((op == "+" || op == "-") && (is(_Unqual!D == Duration) || is(_Unqual!D == TickDuration)));
Adds or subtracts two durations.

The legal types of arithmetic for using this operator are

Duration + Duration --> Duration
Duration - Duration --> Duration
Duration + TickDuration --> Duration
Duration - TickDuration --> Duration


Params:
D rhs The duration to add to or subtract from this .

const pure nothrow @safe Duration opBinaryRight(string op, D)(D lhs) if ((op == "+" || op == "-") && is(_Unqual!D == TickDuration));
Adds or subtracts two durations.

The legal types of arithmetic for using this operator are

TickDuration + Duration --> Duration
TickDuration - Duration --> Duration


Params:
D lhs The to add to this or to subtract this from.

pure nothrow ref @safe Duration opOpAssign(string op, D)(in D rhs) if ((op == "+" || op == "-") && (is(_Unqual!D == Duration) || is(_Unqual!D == TickDuration)));
Adds or subtracts two durations as well as assigning the result to this .

The legal types of arithmetic for using this operator are

Duration + Duration --> Duration
Duration - Duration --> Duration
Duration + TickDuration --> Duration
Duration - TickDuration --> Duration


Params:
D rhs The duration to add to or subtract from this .

const pure nothrow @safe Duration opBinary(string op)(long value) if (op == "*");
The legal types of arithmetic for using this operator overload are

Duration * long --> Duration


Params:
long value The value to multiply this by.

pure nothrow ref @safe Duration opOpAssign(string op)(long value) if (op == "*");
The legal types of arithmetic for using this operator overload are

Duration * long --> Duration


Params:
long value The value to multiply this by.

const pure @safe Duration opBinary(string op)(long value) if (op == "/");
The legal types of arithmetic for using this operator overload are

Duration / long --> Duration


Params:
long value The value to divide from this duration.

Throws:
if an attempt to divide by is made.

pure ref @safe Duration opOpAssign(string op)(long value) if (op == "/");
The legal types of arithmetic for using this operator overload are

Duration / long --> Duration


Params:
long value The value to divide from this .

Throws:
if an attempt to divide by is made.

const pure nothrow @safe Duration opBinaryRight(string op)(long value) if (op == "*");
Multiplies an integral value and a .

The legal types of arithmetic for using this operator overload are

long * Duration --> Duration


Params:
long value The number of units to multiply this by.

const pure nothrow @safe Duration opUnary(string op)() if (op == "-");
Returns the negation of this .

const pure nothrow @safe TickDuration opCast(T)() if (is(_Unqual!T == TickDuration));
Returns a with the same number of hnsecs as this . Note that the conventional way to convert between and is using , e.g.:

template split(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 by . 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 all s. This overload will also work with no time strings being given, in which case all of the time units from weeks through hnsecs will be provided (but no nsecs, since it would always be ).

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

is accepted by split, but and 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);
}


const pure nothrow @safe void split(Args...)(out Args args) if (units.length != 0 && args.length == units.length && allAreMutableIntegralTypes!Args);
const pure nothrow @safe auto split();
Ditto

const pure nothrow @safe long get(string units)() if (units == "weeks" || units == "days" || units == "hours" || units == "minutes" || units == "seconds");
Deprecated. Please use instead. Too frequently, get or one of the individual unit getters is used when the function that gave the desired behavior was . This should make it more explicit and help prevent bugs. This function will be removed in June 2015.

Returns the number of the given units in this (minus the larger units).

is equivalent to .

Examples:
assert(dur!"weeks"(12).get!"weeks" == 12);
assert(dur!"weeks"(12).get!"days" == 0);

assert(dur!"days"(13).get!"weeks" == 1);
assert(dur!"days"(13).get!"days" == 6);

assert(dur!"hours"(49).get!"days" == 2);
assert(dur!"hours"(49).get!"hours" == 1);


deprecated const pure nothrow @property @safe long weeks();
Deprecated. Please use instead. Too frequently, or one of the individual unit getters is used when the function that gave the desired behavior was . This should make it more explicit and help prevent bugs. This function will be removed in June 2015.

Returns the number of weeks in this (minus the larger units).

Examples:
assert(dur!"weeks"(12).weeks == 12);
assert(dur!"days"(13).weeks == 1);


deprecated const pure nothrow @property @safe long days();
Deprecated. Please use instead. Too frequently, or one of the individual unit getters is used when the function that gave the desired behavior was . This should make it more explicit and help prevent bugs. This function will be removed in June 2015.

Returns the number of days in this (minus the larger units).

Examples:
assert(dur!"weeks"(12).days == 0);
assert(dur!"days"(13).days == 6);
assert(dur!"hours"(49).days == 2);


deprecated const pure nothrow @property @safe long hours();
Deprecated. Please use instead. Too frequently, or one of the individual unit getters is used when the function that gave the desired behavior was . This should make it more explicit and help prevent bugs. This function will be removed in June 2015.

Returns the number of hours in this (minus the larger units).

Examples:
assert(dur!"days"(8).hours == 0);
assert(dur!"hours"(49).hours == 1);
assert(dur!"minutes"(121).hours == 2);


deprecated const pure nothrow @property @safe long minutes();
Deprecated. Please use instead. Too frequently, or one of the individual unit getters is used when the function that gave the desired behavior was . This should make it more explicit and help prevent bugs. This function will be removed in June 2015.

Returns the number of minutes in this (minus the larger units).

Examples:
assert(dur!"hours"(47).minutes == 0);
assert(dur!"minutes"(127).minutes == 7);
assert(dur!"seconds"(121).minutes == 2);


deprecated const pure nothrow @property @safe long seconds();
Deprecated. Please use instead. Too frequently, or one of the individual unit getters is used when the function that gave the desired behavior was . This should make it more explicit and help prevent bugs. This function will be removed in June 2015.

Returns the number of seconds in this (minus the larger units).

Examples:
assert(dur!"minutes"(47).seconds == 0);
assert(dur!"seconds"(127).seconds == 7);
assert(dur!"msecs"(1217).seconds == 1);


deprecated const pure nothrow @property @safe FracSec fracSec();
Deprecated. Please use instead. Too frequently, or one of the individual unit getters is used when the function that gave the desired behavior was . This should make it more explicit and help prevent bugs. This function will be removed in June 2015.

Returns the fractional seconds past the second in this .

Examples:
assert(dur!"msecs"(1000).fracSec == FracSec.from!"msecs"(0));
assert(dur!"msecs"(1217).fracSec == FracSec.from!"msecs"(217));
assert(dur!"usecs"(43).fracSec == FracSec.from!"usecs"(43));
assert(dur!"hnsecs"(50_007).fracSec == FracSec.from!"hnsecs"(50_007));
assert(dur!"nsecs"(62_127).fracSec == FracSec.from!"nsecs"(62_100));

assert(dur!"msecs"(-1000).fracSec == FracSec.from!"msecs"(-0));
assert(dur!"msecs"(-1217).fracSec == FracSec.from!"msecs"(-217));
assert(dur!"usecs"(-43).fracSec == FracSec.from!"usecs"(-43));
assert(dur!"hnsecs"(-50_007).fracSec == FracSec.from!"hnsecs"(-50_007));
assert(dur!"nsecs"(-62_127).fracSec == FracSec.from!"nsecs"(-62_100));


const pure nothrow @property @safe long total(string units)() if (units == "weeks" || units == "days" || units == "hours" || units == "minutes" || units == "seconds" || units == "msecs" || units == "usecs" || units == "hnsecs" || units == "nsecs");
Returns the total number of the given units in this . So, unlike , 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);


const pure nothrow @safe string toString();
Converts this to a .

const pure nothrow @property @safe bool isNegative();
Returns whether this is negative.

pure nothrow @safe Duration dur(string units)(long length) if (units == "weeks" || units == "days" || units == "hours" || units == "minutes" || units == "seconds" || units == "msecs" || units == "usecs" || units == "hnsecs" || units == "nsecs");
alias weeks = dur!"weeks".dur;
alias days = dur!"days".dur;
alias hours = dur!"hours".dur;
alias minutes = dur!"minutes".dur;
alias seconds = dur!"seconds".dur;
alias msecs = dur!"msecs".dur;
alias usecs = dur!"usecs".dur;
alias hnsecs = dur!"hnsecs".dur;
alias nsecs = dur!"nsecs".dur;
These allow you to construct a from the given time units with the given length.

You can either use the generic function and give it the units as a or use the named aliases.

The possible values for units are , , , , , (milliseconds), , (microseconds), (hecto-nanoseconds, i.e. 100 ns), and .

Examples:
// Generic
assert(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-generic
assert(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);


Params:
units The time units of the (e.g. ).
long length The number of units in the .

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

static immutable long ticksPerSec;
The number of ticks that the system clock has in one second.

If is , then then failed to get the value of on the current system, and is not going to work. That would be highly abnormal though.

static immutable TickDuration appOrigin;
The tick of the system clock (as a ) when the application started.

static pure nothrow @property @safe TickDuration zero();
It's the same as , but it's provided to be consistent with and , which provide properties.

static pure nothrow @property @safe TickDuration max();
Largest possible.

static pure nothrow @property @safe TickDuration min();
Most negative possible.

long length;
The number of system ticks in this .

You can convert this into the number of seconds by dividing it by (or using one the appropriate property function to do it).

const pure nothrow @safe T to(string units, T)() if ((units == "seconds" || units == "msecs" || units == "usecs" || units == "hnsecs" || units == "nsecs") && (__traits(isIntegral, T) && T.sizeof >= 4 || __traits(isFloating, T)));
Converts this to the given units as either an integral value or a floating point value.

Params:
units The units to convert to. Accepts and smaller only.
T The type to convert to (either an integral type or a floating point type).

const pure nothrow @property @safe long seconds();
Returns the total number of seconds in this .

const pure nothrow @property @safe long msecs();
Returns the total number of milliseconds in this .

const pure nothrow @property @safe long usecs();
Returns the total number of microseconds in this .

const pure nothrow @property @safe long hnsecs();
Returns the total number of hecto-nanoseconds in this .

const pure nothrow @property @safe long nsecs();
Returns the total number of nanoseconds in this .

pure nothrow @safe TickDuration from(string units)(long length) if (units == "seconds" || units == "msecs" || units == "usecs" || units == "hnsecs" || units == "nsecs");
This allows you to construct a from the given time units with the given length.

Params:
units The time units of the (e.g. ).
long length The number of units in the .

const pure nothrow @safe Duration opCast(T)() if (is(_Unqual!T == Duration));
Returns a with the same number of hnsecs as this . Note that the conventional way to convert between and is using , e.g.:

pure nothrow ref @safe TickDuration opOpAssign(string op)(TickDuration rhs) if (op == "+" || op == "-");
Adds or subtracts two s as well as assigning the result to this .

The legal types of arithmetic for using this operator are

TickDuration += TickDuration --> TickDuration
TickDuration -= TickDuration --> TickDuration


Params:
TickDuration rhs The to add to or subtract from this .

const pure nothrow @safe TickDuration opBinary(string op)(TickDuration rhs) if (op == "+" || op == "-");
Adds or subtracts two s.

The legal types of arithmetic for using this operator are

TickDuration + TickDuration --> TickDuration
TickDuration - TickDuration --> TickDuration


Params:
TickDuration rhs The to add to or subtract from this .

const pure nothrow @safe TickDuration opUnary(string op)() if (op == "-");
Returns the negation of this .

const pure nothrow @safe int opCmp(TickDuration rhs);
operator overloading "<, >, <=, >="

pure nothrow @safe void opOpAssign(string op, T)(T value) if (op == "*" && (__traits(isIntegral, T) || __traits(isFloating, T)));
The legal types of arithmetic for using this operator overload are

TickDuration * long --> TickDuration
TickDuration * floating point --> TickDuration


Params:
T value The value to divide from this duration.

pure @safe void opOpAssign(string op, T)(T value) if (op == "/" && (__traits(isIntegral, T) || __traits(isFloating, T)));
The legal types of arithmetic for using this operator overload are

TickDuration / long --> TickDuration
TickDuration / floating point --> TickDuration


Params:
T value The value to divide from this .

Throws:
if an attempt to divide by is made.

const pure nothrow @safe TickDuration opBinary(string op, T)(T value) if (op == "*" && (__traits(isIntegral, T) || __traits(isFloating, T)));
The legal types of arithmetic for using this operator overload are

TickDuration * long --> TickDuration
TickDuration * floating point --> TickDuration


Params:
T value The value to divide from this .

const pure @safe TickDuration opBinary(string op, T)(T value) if (op == "/" && (__traits(isIntegral, T) || __traits(isFloating, T)));
The legal types of arithmetic for using this operator overload are

TickDuration / long --> TickDuration
TickDuration / floating point --> TickDuration


Params:
T value The value to divide from this .

Throws:
if an attempt to divide by is made.

pure nothrow @safe this(long ticks);
Params:
long ticks The number of ticks in the TickDuration.

static nothrow @property @trusted TickDuration currSystemTick();
The current system tick. The number of ticks per second varies from system to system. 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, is used. On Mac OS X, is used, while on other Posix systems, is used. If or is unavailable, then Posix systems use (the decision is made when is compiled), which unfortunately, is not monotonic, but if and aren't available, then is the 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:
if it fails to get the time.

pure nothrow @safe 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"));
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).

Params:
from The units of time to convert from.
to The units of time to convert to.
long value The 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);


struct FracSec;
Represents fractional seconds.

This is the portion of the time which is smaller than a second and it cannot hold values which would be greater than or equal to a second (or less than or equal to a negative second).

It holds hnsecs internally, but you can create it using either milliseconds, microseconds, or hnsecs. What it does is allow for a simple way to set or adjust the fractional seconds portion of a or a without having to worry about whether you're dealing with milliseconds, microseconds, or hnsecs.

's functions which take time unit strings do accept , but because the resolution of and is hnsecs, you don't actually get precision higher than hnsecs. is accepted merely for convenience. Any values given as nsecs will be converted to hnsecs using (which uses truncating division when converting to smaller units).

static pure nothrow @property @safe FracSec zero();
A of . It's shorter than doing something like and more explicit than .

pure @safe FracSec from(string units)(long value) if (units == "msecs" || units == "usecs" || units == "hnsecs" || units == "nsecs");
Create a from the given units (, , or ).

Params:
units The units to create a FracSec from.
long value The number of the given units passed the second.

Throws:
if the given value would result in a greater than or equal to second or less than or equal to seconds.

const pure nothrow @safe FracSec opUnary(string op)() if (op == "-");
Returns the negation of this .

const pure nothrow @property @safe int msecs();
The value of this as milliseconds.

pure @property @safe void msecs(int milliseconds);
The value of this as milliseconds.

Params:
int milliseconds The number of milliseconds passed the second.

Throws:
if the given value is not less than second and greater than a seconds.

const pure nothrow @property @safe int usecs();
The value of this as microseconds.

pure @property @safe void usecs(int microseconds);
The value of this as microseconds.

Params:
int microseconds The number of microseconds passed the second.

Throws:
if the given value is not less than second and greater than a seconds.

const pure nothrow @property @safe int hnsecs();
The value of this as hnsecs.

pure @property @safe void hnsecs(int hnsecs);
The value of this as hnsecs.

Params:
int hnsecs The number of hnsecs passed the second.

Throws:
if the given value is not less than second and greater than a seconds.

const pure nothrow @property @safe int nsecs();
The value of this as nsecs.

Note that this does not give you any greater precision than getting the value of this as hnsecs.

pure @property @safe void nsecs(long nsecs);
The value of this as nsecs.

Note that this does not give you any greater precision than setting the value of this as hnsecs.

Params:
long nsecs The number of nsecs passed the second.

Throws:
if the given value is not less than second and greater than a seconds.

const pure nothrow @safe string toString();
Converts this to a string.

class TimeException: object.Exception;
Exception type used by core.time.

pure nothrow @safe this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null);
Params:
string msg The message for the exception.
string file The file where the exception occurred.
size_t line The line number where the exception occurred.
Throwable next The previous exception in the chain of exceptions, if any.

pure nothrow @safe this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__);
Params:
string msg The message for the exception.
Throwable next The previous exception in the chain of exceptions.
string file The file where the exception occurred.
size_t line The line number where the exception occurred.

pure nothrow @safe Duration abs(Duration duration);
pure nothrow @safe TickDuration abs(TickDuration duration);
Returns the absolute value of a duration.


Page generated by Ddoc. Copyright 2010 - 2012