Time from timestamp?

Chris Williams via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 30 18:26:55 PST 2015


On Saturday, 31 January 2015 at 00:14:37 UTC, Steven 
Schveighoffer wrote:
> On 1/30/15 5:18 PM, Chris Williams wrote:
>> I'm attempting to print a human-readable version of a 
>> timestamp. The
>> timestamp is coming from an external service, via JSON. An 
>> example is:
>>
>> 1421865781342
>>
>> Which I know to be:
>>
>> 2015-01-21T18:43:01.342Z
>>
>
> http://dlang.org/phobos/std_datetime.html#.unixTimeToStdTime
>
> It's kind of convoluted because there is no epoch, but you can 
> make one:
>
> import std.datetime;
> import std.stdio;
>
> void main(string[] args)
> {
>     // can't make this enum because of time zone...
>     auto epoch = SysTime(unixTimeToStdTime(0), UTC());
>     writeln(epoch + 1_421_865_781_342.msecs);
> }
>
> output:
> 2015-Jan-21 18:43:01.342Z
>
> Note the reason your code didn't work is because SysTime uses 
> 1/1/1 as the epoch.
>
> -Steve

D'oh, I missed that in the description:

"and convert it to hnsecs in UTC since midnight, January 1st, 1 
A.D. UTC"

That does explain it. I also didn't spot the declaration of 
unixTimeToStdTime(), which assuredly helps.

Thank you!


More information about the Digitalmars-d-learn mailing list