How to convert `std.datetime.Duration` to seconds of floating point type?
drug2004 at bk.ru
Tue Feb 19 16:26:33 UTC 2019
On 19.02.2019 18:07, Dennis wrote:
> 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;
>> 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."
Well, I understand that using floating point values to represent time
internally is a bad idea and I totally agree. But some convenient API to
convert Duration to floating point and vice versa would be useful
because in mechanics for example you often need to express time in
seconds with fractional part. In this regard std::chrono is more
expressive (the only one though, in general std.datetime is much more
powerful than std::chrono).
More information about the Digitalmars-d-learn