Time from timestamp?
Chris Williams via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Jan 30 14:18:44 PST 2015
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
The only method I see which takes an epoch-style timestamp, so
that I can convert it to something printable, is the SysTime
constructor:
pure nothrow @safe this(long stdTime, immutable TimeZone tz =
null);
According to the reference, this seems to take the value in
hnsecs. My expectation would be that this means multiplying my
initial value by 1_000_000. But if I do that, I get a random date
2500 years in the future.
I created this sample code:
void main() {
long time = 1421865781342L;
writefln("%s", SysTime(time));
writefln("%s", SysTime(time * 10L));
writefln("%s", SysTime(time * 100L));
writefln("%s", SysTime(time * 1_000L));
writefln("%s", SysTime(time * 10_000L));
writefln("%s", SysTime(time * 100_000L));
writefln("%s", SysTime(time * 1_000_000L));
writefln("%s", Clock.currTime.stdTime);
}
Outputs:
0001-Jan-02 07:36:48.5781342
0001-Jan-17 03:04:47.781342
0001-Jun-14 05:44:39.81342
0005-Jul-04 08:23:20.1342
0046-Jan-21 10:50:03.342
0451-Jul-28 11:17:15.42
4506-Sep-18 16:42:14.2
635582516
My expectation would be that the final line would be something
beginning with "14" at least. Playing around with possible
multipliers, there doesn't even seem to be any integer value that
would allow conversion between the timestamp I received and
whatever SysTime expects.
I'm using:
DMD64 D Compiler v2.066.1
Ubuntu from .deb package
Is this a bug, or am I doing something wrong?
More information about the Digitalmars-d-learn
mailing list