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

Alex sascha.orlov at gmail.com
Tue Feb 19 16:55:10 UTC 2019


On Tuesday, 19 February 2019 at 16:44:23 UTC, drug wrote:
> On 19.02.2019 19:35, Alex wrote:
>> On Tuesday, 19 February 2019 at 16:26:33 UTC, drug wrote:
>>>
>>> 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).
>> 
>> If you need a real (double, float) parameter, just use it. 
>> Transport the units separately...
> Could you elaborate what you mean?
> ```
> 	auto point = vec3(1.0, 2.0, 3.0);
> 	vec3 velocity = ...;
>
> 	auto t = dur!"msecs"(500);
>
> 	...
>
> 	auto new_point = point + velocity * t.total!"msecs"/1e3;
> ```
> How can I use a real parameter and transport units separately 
> here?

like
´´´
import std.datetime;

void main()
{
     import std.stdio;
	auto d = Data!"msecs"();
     writeln(d.point);
     writeln(d.t);
}

struct Data(string units)
{
     real[3] point = [1.0, 2.0, 3.0];
     real[3] velocity = ...;
     auto t = dur!units(500);
}
´´´


More information about the Digitalmars-d-learn mailing list