How to convert `std.datetime.Duration` to seconds of floating point type?

Dennis dkorpel at gmail.com
Tue Feb 19 15:07:13 UTC 2019


On Tuesday, 19 February 2019 at 14:34:09 UTC, drug wrote:
> Now I do it this way (https://run.dlang.io/is/8kVibT):
> ```
> 	auto some_duration = dur!"msecs"(500);
> 	auto seconds = some_duration.total!"hnsecs" / 10_000_000.0;
> 	assert(seconds.approxEqual(0.5));
> ```
> is there better way to do it?
>

What you're doing now is the way to do it. This is by design, as 
Jonathan M. Davis explains:

"In general, using floating point values with time is an 
incredibly bad idea. It can certainly make sense when printing 
stuff out, but using it in calculations is just asking for 
trouble given all of the unnecessary imprecision that it adds. 
So, Duration does not directly support floating point values at 
all, and that's very much on purpose.
(...)
And if you're just trying to print out the a duration as a 
floating point value, because it's nice to view that way, then 
that's fine, but you'll need to do the conversion yourself. And 
it's not that hard."

https://forum.dlang.org/post/mailman.3711.1453121204.22025.digitalmars-d-learn@puremagic.com




More information about the Digitalmars-d-learn mailing list