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 core.thread.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, std.datetime.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 std.datetime.Date. But Duration is used when dealing with weeks or smaller.

Members

Functions

isNegative
bool isNegative()

Returns whether this Duration is negative.

opBinary
Duration opBinary(D rhs)

Adds, subtracts or calculates the modulo of two durations.

opBinary
Duration opBinary(long value)

Multiplies or divides the duration by an integer value.

opBinary
long opBinary(Duration rhs)

Divides two durations.

opBinaryRight
Duration opBinaryRight(D lhs)

Adds or subtracts two durations.

opBinaryRight
Duration opBinaryRight(long value)

Multiplies an integral value and a Duration.

opCast
TickDuration opCast()

Returns a TickDuration with the same number of hnsecs as this Duration. Note that the conventional way to convert between Duration and TickDuration is using std.conv.to, e.g.: duration.to!TickDuration()

opCast
bool opCast()

Allow Duration to be used as a boolean.

opCmp
int opCmp(Duration rhs)

Compares this Duration with the given Duration.

opOpAssign
Duration opOpAssign(const scope D rhs)

Adds, subtracts or calculates the modulo of two durations as well as assigning the result to this Duration.

opOpAssign
Duration opOpAssign(long value)

Multiplies/Divides the duration by an integer value as well as assigning the result to this Duration.

opUnary
Duration opUnary()

Returns the negation of this Duration.

toString
string toString()

Converts this Duration to a string.

total
long total()

Returns the total number of the given units in this Duration. So, unlike split, it does not strip out the larger units.

Static functions

max
Duration max()

Largest Duration possible.

min
Duration min()

Most negative Duration possible.

zero
Duration zero()

A Duration of 0. It's shorter than doing something like dur!"seconds"(0) and more explicit than Duration.init.

Templates

split
template split(units...)

Splits out the Duration into the given units.

Examples

1 import std.datetime;
2 
3 assert(dur!"days"(12) == dur!"hnsecs"(10_368_000_000_000L));
4 assert(dur!"hnsecs"(27) == dur!"hnsecs"(27));
5 assert(std.datetime.Date(2010, 9, 7) + dur!"days"(5) ==
6        std.datetime.Date(2010, 9, 12));
7 
8 assert(days(-12) == dur!"hnsecs"(-10_368_000_000_000L));
9 assert(hnsecs(-27) == dur!"hnsecs"(-27));
10 assert(std.datetime.Date(2010, 9, 7) - std.datetime.Date(2010, 10, 3) ==
11        days(-26));
1 import core.time;
2 
3 // using the dur template
4 auto numDays = dur!"days"(12);
5 
6 // using the days function
7 numDays = days(12);
8 
9 // alternatively using UFCS syntax
10 numDays = 12.days;
11 
12 auto myTime = 100.msecs + 20_000.usecs + 30_000.hnsecs;
13 assert(myTime == 123.msecs);
Suggestion Box / Bug Report