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 to create s.
You cannot create a duration of months or years because the variable number
of days in a month or a year makes it so that you cannot convert between
months or years and smaller units without a specific date. Any type or
function which handles months or years has other functions for handling
those rather than using durations. For instance, has
and for adding years and months, rather than
creating a duration of years or months and adding that to a
. If you're dealing with weeks or smaller, however,
durations are what you use.
Examples:
assert(dur!"days"(12) == Duration(10_368_000_000_000L));
assert(dur!"hnsecs"(27) == Duration(27));
assert(std.datetime.Date(2010, 9, 7) + dur!"days"(5) ==
std.datetime.Date(2010, 9, 12));
assert(dur!"days"(-12) == Duration(-10_368_000_000_000L));
assert(dur!"hnsecs"(-27) == Duration(-27));
assert(std.datetime.Date(2010, 9, 7) - std.datetime.Date(2010, 10, 3) ==
dur!"days"(-26));
- 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);
- 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:
rhs |
The duration to add to or subtract from this . |
- const pure nothrow @safe Duration opBinaryRight(string op, D)(D lhs);
- Adds or subtracts two durations.
The legal types of arithmetic for using this operator are
TickDuration | + | Duration | --> | Duration |
TickDuration | - | Duration | --> | Duration |
Params:
lhs |
The to add to this or to
subtract this from. |
- pure nothrow @safe Duration opOpAssign(string op, D)(in D rhs);
- 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:
rhs |
The duration to add to or subtract from this . |
- const pure nothrow @safe Duration opBinary(string op)(long value);
- The legal types of arithmetic for using this operator
overload are
Duration | * | long | --> | Duration |
Params:
value |
The value to multiply this by. |
- pure nothrow @safe Duration opOpAssign(string op)(long value);
- The legal types of arithmetic for using this operator
overload are
Duration | * | long | --> | Duration |
Params:
value |
The value to multiply this by. |
- const pure @safe Duration opBinary(string op)(long value);
- The legal types of arithmetic for using this operator
overload are
Duration | / | long | --> | Duration |
Params:
value |
The value to divide from this duration. |
Throws:
if an attempt to divide by is made.
- pure @safe Duration opOpAssign(string op)(long value);
- The legal types of arithmetic for using this operator
overload are
Duration | / | long | --> | Duration |
Params:
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);
- Multiplies an integral value and a .
The legal types of arithmetic for using this operator
overload are
long | * | Duration | --> | Duration |
Params:
value |
The number of units to multiply this by. |
- const pure nothrow @safe Duration opUnary(string op)();
- Returns the negation of this .
- const pure nothrow @safe TickDuration opCast(T)();
- Returns a with the same number of hnsecs as this
.
- const pure nothrow @safe long get(string units)();
- Returns the number of the given units in this
(minus the larger units).
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);
- const pure nothrow @property @safe long weeks();
- Returns the number of weeks in this
(minus the larger units).
Examples:
assert(dur!"weeks"(12).weeks == 12);
assert(dur!"days"(13).weeks == 1);
- const pure nothrow @property @safe long days();
- 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);
- const pure nothrow @property @safe long hours();
- 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);
- const pure nothrow @property @safe long minutes();
- 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);
- const pure nothrow @property @safe long seconds();
- 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);
- const pure nothrow @property @safe FracSec fracSec();
- Returns the fractional seconds passed 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 @safe long total(string units)();
- 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.
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:
tuFrom |
The units of time to covert from. |
tuFrom |
The units of time to covert type. |
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);