Can I get the time "Duration" in "nsecs" acurracy?
Bastiaan Veelo
Bastiaan at Veelo.net
Fri Jul 9 22:34:53 UTC 2021
On Friday, 9 July 2021 at 21:13:02 UTC, rempas wrote:
> On Friday, 9 July 2021 at 20:54:21 UTC, Paul Backus wrote:
>> On Friday, 9 July 2021 at 20:43:48 UTC, rempas wrote:
>>> I'm reading the library reference for
>>> [core.time](https://dlang.org/phobos/core_time.html#Duration)
>>> and It says that the duration is taken in "hnsecs" and I
>>> cannot understand if we can change that and choose the
>>> precision. Does anyone know if we can do that?
>>
>> It is stored internally in "hnsecs", but you can convert it to
>> other units using the `total` method [1]; for example,
>> `myDuration.total!"nsecs"`.
>>
>> [1] https://dlang.org/phobos/core_time.html#.Duration.total
>
> It doesn't work for me. I have the following code:
>
> ```
> MonoTime start = MonoTime.currTime();
> // Doing stuff
> MonoTime end = MonoTime.currTime();
> Duration dur = end - start;
> dur = dur.total!"nsecs";
> ```
>
> and I get the following error message:
>
> "Error: cannot implicitly convert expression \`dur.total()\` of
> type \`long\` to \`Duration`"
You cannot change the precision of `Duration`, it always counts
in multiples of 100 nsecs. You can count how many multiples of
other units fit into the same `Duration` using `total`, but it
will just return a number, not a “converted” `Duration`.
`toString` will produce an easy readable string (more or less),
as in https://run.dlang.io/is/baqKLG, where “hnsec” is the
smallest unit. If you think hectonanosecond is a weird unit, then
[you are not
alone](https://forum.dlang.org/post/pwotyniksrskdzmeafin@forum.dlang.org), and it [has been pointed out](https://forum.dlang.org/post/khppfxsyfefjksvrimkx@forum.dlang.org) that it does not comply with SI.
— Bastiaan.
More information about the Digitalmars-d-learn
mailing list