View source code Display the source code in core/time.d from which this page was generated on github. 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 using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

Struct core.time.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.

Properties

Name Type Description
days [get] long Deprecated. Please use split instead. Too frequently, get or one of the individual unit getters is used when the function that gave the desired behavior was total. This should make it more explicit and help prevent bugs. This function will be removed in June 2015.
fracSec [get] FracSec Deprecated. Please use split instead. Too frequently, get or one of the individual unit getters is used when the function that gave the desired behavior was total. This should make it more explicit and help prevent bugs. This function will be removed in June 2015.
hours [get] long Deprecated. Please use split instead. Too frequently, get or one of the individual unit getters is used when the function that gave the desired behavior was total. This should make it more explicit and help prevent bugs. This function will be removed in June 2015.
isNegative [get] bool Returns whether this Duration is negative.
max [get] Duration Largest Duration possible.
min [get] Duration Most negative Duration possible.
minutes [get] long Deprecated. Please use split instead. Too frequently, get or one of the individual unit getters is used when the function that gave the desired behavior was total. This should make it more explicit and help prevent bugs. This function will be removed in June 2015.
seconds [get] long Deprecated. Please use split instead. Too frequently, get or one of the individual unit getters is used when the function that gave the desired behavior was total. This should make it more explicit and help prevent bugs. This function will be removed in June 2015.
total [get] long Returns the total number of the given units in this Duration. So, unlike split, it does not strip out the larger units.
weeks [get] long Deprecated. Please use split instead. Too frequently, get or one of the individual unit getters is used when the function that gave the desired behavior was total. This should make it more explicit and help prevent bugs. This function will be removed in June 2015.
zero [get] Duration A Duration of 0. It's shorter than doing something like dur!"seconds"(0) and more explicit than Duration.init.

Methods

Name Description
get Deprecated. Please use split instead. Too frequently, get or one of the individual unit getters is used when the function that gave the desired behavior was total. This should make it more explicit and help prevent bugs. This function will be removed in June 2015.
opBinary Adds or subtracts two durations.
opBinary The legal types of arithmetic for Duration using this operator overload are
opBinary The legal types of arithmetic for Duration using this operator overload are
opBinaryRight Adds or subtracts two durations.
opBinaryRight Multiplies an integral value and a Duration.
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()
opCmp Compares this Duration with the given Duration.
opOpAssign The legal types of arithmetic for Duration using this operator overload are
opOpAssign The legal types of arithmetic for Duration using this operator overload are
opOpAssign Adds or subtracts two durations as well as assigning the result to this Duration.
opUnary Returns the negation of this Duration.
split Splits out the Duration into the given units.
toString Converts this Duration to a string.

Templates

Name Description
split Splits out the Duration into the given units.

Examples

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));

Example

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);

Authors

Jonathan M Davis and Kato Shoichi

License

Boost License 1.0.

Comments