View source code
Display the source code in core/time.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
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.

Function core.time.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(string from, string to) (
  long value
) pure nothrow @nogc @safe
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

NameDescription
from The units of time to convert from.
to The units of time to convert to.
value The value to convert.

Example

writeln(convert!("years", "months")(1)); // 12
writeln(convert!("months", "years")(12)); // 1

writeln(convert!("weeks", "days")(1)); // 7
writeln(convert!("hours", "seconds")(1)); // 3600
writeln(convert!("seconds", "days")(1)); // 0
writeln(convert!("seconds", "days")(86_400)); // 1

writeln(convert!("nsecs", "nsecs")(1)); // 1
writeln(convert!("nsecs", "hnsecs")(1)); // 0
writeln(convert!("hnsecs", "nsecs")(1)); // 100
writeln(convert!("nsecs", "seconds")(1)); // 0
writeln(convert!("seconds", "nsecs")(1)); // 1_000_000_000

Authors

Jonathan M Davis and Kato Shoichi

License

Boost License 1.0.