casting SysTime to ubyte[]

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 12 09:05:09 PST 2015


On Monday, January 12, 2015 13:59:27 Laeeth Isharc via Digitalmars-d-learn wrote:
> import std.datetime;
> import std.stdio;
> import std.conv;
>
> void main(string[] arg)
> {
>   auto a=Clock.currTime();
>   auto b=cast(ubyte[])a;
>   writefln("%s",b);
> }
>
> how do i get the time as a binary representation I can write to a
> file?

I really wouldn't advise doing that. SysTime contains a long which
represents the time in hnsecs since midnight, January 1st, 1 A.D., and that
could be written to a file quite easily. But it also contains a reference to
a TimeZone object, so what you're doing would just be writing its address to
disk, which wouldn't do you any good at all, since that's specific to each
run of the program, even assuming that the object exists in both runs of the
program (which it would for UTC or LocalTime but not for user-constructed
time zones).

So, writing the stdTime (horrible name, I know) property to disk would work
just fine (that's the hnsecs as a long), but you're going to have to do
something smarter than that if you want to retain the time zone. And you're
not going to want to try and simply cast a SysTime to a ubyte[] and do
anything practical with that regardless.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list