Epoch time + msecs

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Nov 13 19:09:58 PST 2015


On Friday, November 13, 2015 18:00:13 Handyman via Digitalmars-d-learn wrote:
> How to get current time as a float (or a double or a real) as a
> Unix epoch + milliseconds (e.g, 1447437383.465, or even
> 1447437383.46512 with finer resolution)?   I read
> http://dlang.org/intro-to-datetime.html and the docs of course.
> I came this far
>
>     auto ct = Clock.currTime();
>     auto milliseconds = ct.fracSec.msecs;
>     auto epoch = ct.toUnixTime();
>
> But I think this is not the shortest way and I don't know how to
> combine and cast into a float (or a double or a real).
>
> My goal was to multiply by 1000 and feed as this number as a seed
> to D's random number generator.  But there may be beter ways to
> make a seed.

Well, there's what Steven showed you if you really want to use unix time,
but if all you care about is getting a value for a seed, then you could just
use stdTime. e.g.

auto seed = Clock.currTime().stdTime;

It'll be in hecto-nanoseconds, so you should get more variation than with
unix time as milliseconds, and you don't have to do any math.

Alternatively, std.random.unpredictable seed is there specifically to
provide an unpredictable seed. It's probably best to just use that if you
don't have a good reason not to.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list