convert

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

long
convert
@safe pure nothrow @nogc
(
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"
)
)
)

Parameters

from

The units of time to convert from.

to

The units of time to convert to.

value
Type: long

The value to convert.

Examples

1 assert(convert!("years", "months")(1) == 12);
2 assert(convert!("months", "years")(12) == 1);
3 
4 assert(convert!("weeks", "days")(1) == 7);
5 assert(convert!("hours", "seconds")(1) == 3600);
6 assert(convert!("seconds", "days")(1) == 0);
7 assert(convert!("seconds", "days")(86_400) == 1);
8 
9 assert(convert!("nsecs", "nsecs")(1) == 1);
10 assert(convert!("nsecs", "hnsecs")(1) == 0);
11 assert(convert!("hnsecs", "nsecs")(1) == 100);
12 assert(convert!("nsecs", "seconds")(1) == 0);
13 assert(convert!("seconds", "nsecs")(1) == 1_000_000_000);
Suggestion Box / Bug Report