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

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

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

Meta