Jump to page: 1 2
Thread overview
Epoch time + msecs
Nov 13, 2015
Handyman
Nov 13, 2015
Handyman
Nov 14, 2015
Jonathan M Davis
Nov 14, 2015
Handyman
Nov 14, 2015
Nicholas Wilson
Nov 16, 2015
Kagamin
Nov 16, 2015
Marc Schütz
Nov 16, 2015
Kagamin
Nov 16, 2015
Kagamin
November 13, 2015
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.
November 13, 2015
On 11/13/15 1:00 PM, Handyman 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();

The toUnixTime call is going to truncate all the subseconds off.

What I would do is this:

auto t = (Clock.currTime() - SysTime.fromUnixTime(0)).total!"msecs" / 1000.0

Which is essentially what toUnixTime would do (but the fromUnixTime(0) is a constant internally).

-Steve
November 13, 2015
On Friday, 13 November 2015 at 18:27:42 UTC, Steven Schveighoffer wrote:
> On 11/13/15 1:00 PM, Handyman wrote:
> What I would do is this:
>
> auto t = (Clock.currTime() - SysTime.fromUnixTime(0)).total!"msecs" / 1000.0

Thanks, Steve.

   import std.stdio;
   import std.datetime;

   void main() {
      auto t = (Clock.currTime() - SysTime.fromUnixTime(0)).total!"msecs" / 1000.0;
      writeln(t);
   }

Gives an error: Error: no property 'fromUnixTime' for type 'SysTime'.

When I do

   auto t = (Clock.currTime() - unixTimeToStdTime(0)).total!"msecs" / 1000.0;

I get
Error: incompatible types for ((currTime(opCall())) - (unixTimeToStdTime(0))): 'SysTime' and 'long'

and when I do

auto t = (Clock.currTime() - SysTime.fromUnixTime(0)).total!"msecs" / 1000.0;

I get:
Error: no property 'fromUnixTime' for type 'SysTime'

I think I am spoiled by 'typeless' scriping languages.
November 13, 2015
On 11/13/15 3:15 PM, Handyman wrote:
> On Friday, 13 November 2015 at 18:27:42 UTC, Steven Schveighoffer wrote:
>> On 11/13/15 1:00 PM, Handyman wrote:
>> What I would do is this:
>>
>> auto t = (Clock.currTime() - SysTime.fromUnixTime(0)).total!"msecs" /
>> 1000.0
>
> Thanks, Steve.
>
>     import std.stdio;
>     import std.datetime;
>
>     void main() {
>        auto t = (Clock.currTime() -
> SysTime.fromUnixTime(0)).total!"msecs" / 1000.0;
>        writeln(t);
>     }
>
> Gives an error: Error: no property 'fromUnixTime' for type 'SysTime'.

gah, fromUnixTime seems to have been added in 2.069. If you cannot update to the latest compiler, this should work:

auto t = (Clock.currTime() - SysTime(unixTimeToStdTime(0))).total!"msecs" / 1000.0

-Steve
November 14, 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

November 14, 2015
On Friday, 13 November 2015 at 23:12:34 UTC, Steven Schveighoffer wrote:
> If you cannot update to the latest compiler

Didn't realised this feature is so new to the compiler.  In my case, it is no problem to update the compiler, and I will.  Thanks!

On Saturday, 14 November 2015 at 03:10:17 UTC, Jonathan M Davis wrote:
> auto seed = Clock.currTime().stdTime;
> (...) and you don't have to do any math.

Perfect.

On Saturday, 14 November 2015 at 03:10:17 UTC, Jonathan M Davis wrote:
> 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.

Of course.  That's why I mentioned my purpose of using Clock.currTime(), in the hope I got corrected in using the right and offical seed method which I failed to find, which brings me to another point.

The D docs seem very thorough and complete to me but less accessible in comparison to, e.g., Perl docs, Php docs, or Ruby docs.  In particular I have difficulties in understanding the headers of the standard library function specifications (e.g.,: auto uniform(string boundaries = "[)", T1, T2, UniformRandomNumberGenerator)(T1 a, T2 b, ref UniformRandomNumberGenerator urng) if (isFloatingPoint!(CommonType!(T1, T2)) && isUniformRNG!UniformRandomNumberGenerator);)

These headers are necessary and give loads of information. D is no toy language, that's for sure.  Maybe a tutorial should be written on how to read such headers and how to read the standard library docs in general.  I am making notes for myself, but these are of too little quality to be used in the public domain.

Like I said: I am spoiled by scripting languages.

Thanks again, your answers have helped me a lot.

November 14, 2015
On Saturday, 14 November 2015 at 12:14:42 UTC, Handyman wrote:

> The D docs seem very thorough and complete to me but less accessible in comparison to, e.g., Perl docs, Php docs, or Ruby docs.  In particular I have difficulties in understanding the headers of the standard library function specifications (e.g.,: auto uniform(string boundaries = "[)", T1, T2, UniformRandomNumberGenerator)(T1 a, T2 b, ref UniformRandomNumberGenerator urng) if (isFloatingPoint!(CommonType!(T1, T2)) && isUniformRNG!UniformRandomNumberGenerator);

 auto // the return type is deduced
 uniform // the name of the function
(string boundaries = "[)", T1, T2, URNG) // the set if compile time parameters
// a string that defaults to "[)" denoting a half open interval and three named types T1, T2 and URNG
(T1 a, T2 b, ref URNG urng) // the set of runtime parameters a T1 , a T2 and an URNG taken by reference
 if  (isFloatingPoint!(CommonType!(T1, T2)) && isUniformRNG!RNG); // a set of constraints to make sure you don't use the wrong types with it.
November 16, 2015
On Saturday, 14 November 2015 at 12:14:42 UTC, Handyman wrote:
> Of course.  That's why I mentioned my purpose of using Clock.currTime(), in the hope I got corrected in using the right and offical seed method which I failed to find, which brings me to another point.

Don't docs provide examples on how use (and seed) a generator? http://dlang.org/phobos/std_random.html#.Mt19937
November 16, 2015
On Monday, 16 November 2015 at 10:29:25 UTC, Kagamin wrote:
> On Saturday, 14 November 2015 at 12:14:42 UTC, Handyman wrote:
>> Of course.  That's why I mentioned my purpose of using Clock.currTime(), in the hope I got corrected in using the right and offical seed method which I failed to find, which brings me to another point.
>
> Don't docs provide examples on how use (and seed) a generator? http://dlang.org/phobos/std_random.html#.Mt19937

Hmmm... why is `unpredictableSeed` only a `uint`? Surely most PRNGs have more than 32 bits of internal state?
November 16, 2015
On Monday, 16 November 2015 at 10:47:36 UTC, Marc Schütz wrote:
> Hmmm... why is `unpredictableSeed` only a `uint`? Surely most PRNGs have more than 32 bits of internal state?

Maybe it's only worth 32 random bits? There's a rangified example too: http://dlang.org/phobos/std_random.html#.MersenneTwisterEngine.seed.2
« First   ‹ Prev
1 2