MonoTime longevity

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Dec 22 12:07:58 PST 2015


On 12/22/15 2:48 PM, Tanel Tagaväli wrote:
> I discovered something potentially troublesome in druntime.
> Namely, applications that use MonoTime will break if run 18 hours after
> booting, according to a short program I wrote.
>
> Here's my code:
>
> ```
> import core.time : MonoTime;
> auto mt = MonoTime.currTime;
> import std.stdio : writeln;
> writeln(mt.ticks / mt.ticksPerSecond / 60 / 60); // prints 18 (hours)
> shortly after boot
> ```
>
> `mt.ticksPerSecond` is `1_000_000_000` on my system.
> I really hope I'm doing something wrong.

MonoTime uses whatever precision is given to it by the OS. So if on your 
OS, ticksPerSecond is 1e9, then your OS clock wraps at 18 hours as well.

In a recent version of D, MonoTime was changed to support multiple 
clocks. Use MonoTimeImpl!SomeClockType to use a clock with a different 
number of ticks per second if one exists.

You can also use std.datetime.Clock

-Steve


More information about the Digitalmars-d-learn mailing list