Duration.split

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 ref. 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 longs. 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 0).

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

"nsecs" is accepted by split, but "years" and "months" are not.

For negative durations, all of the split values will be negative.

  1. void split(out Args args)
    template split(units...)
    @safe pure
    void
    split
    const nothrow @nogc
    (
    Args...
    )
    (
    out Args args
    )
    if (
    units.length != 0 &&
    args.length == units.length
    &&
    allAreMutableIntegralTypes!Args
    )
    if (
    allAreAcceptedUnits!("weeks", "days", "hours", "minutes", "seconds", "msecs", "usecs", "hnsecs", "nsecs")(units) &&
    unitsAreInDescendingOrder(units)
    )
  2. auto split()

Members

Functions

split
void split(out Args args)
auto split()

Ditto

Examples

1 {
2     auto d = dur!"days"(12) + dur!"minutes"(7) + dur!"usecs"(501223);
3     long days;
4     int seconds;
5     short msecs;
6     d.split!("days", "seconds", "msecs")(days, seconds, msecs);
7     assert(days == 12);
8     assert(seconds == 7 * 60);
9     assert(msecs == 501);
10 
11     auto splitStruct = d.split!("days", "seconds", "msecs")();
12     assert(splitStruct.days == 12);
13     assert(splitStruct.seconds == 7 * 60);
14     assert(splitStruct.msecs == 501);
15 
16     auto fullSplitStruct = d.split();
17     assert(fullSplitStruct.weeks == 1);
18     assert(fullSplitStruct.days == 5);
19     assert(fullSplitStruct.hours == 0);
20     assert(fullSplitStruct.minutes == 7);
21     assert(fullSplitStruct.seconds == 0);
22     assert(fullSplitStruct.msecs == 501);
23     assert(fullSplitStruct.usecs == 223);
24     assert(fullSplitStruct.hnsecs == 0);
25 
26     assert(d.split!"minutes"().minutes == d.total!"minutes");
27 }
28 
29 {
30     auto d = dur!"days"(12);
31     assert(d.split!"weeks"().weeks == 1);
32     assert(d.split!"days"().days == 12);
33 
34     assert(d.split().weeks == 1);
35     assert(d.split().days == 5);
36 }
37 
38 {
39     auto d = dur!"days"(7) + dur!"hnsecs"(42);
40     assert(d.split!("seconds", "nsecs")().nsecs == 4200);
41 }
42 
43 {
44     auto d = dur!"days"(-7) + dur!"hours"(-9);
45     auto result = d.split!("days", "hours")();
46     assert(result.days == -7);
47     assert(result.hours == -9);
48 }
Suggestion Box / Bug Report