Thread overview
casting SysTime to ubyte[]
Jan 12, 2015
Laeeth Isharc
Jan 12, 2015
Daniel Kozák
Jan 12, 2015
Jonathan M Davis
Jan 14, 2015
Laeeth Isharc
January 12, 2015
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?

Thanks.
January 12, 2015
On 1/12/15 8:59 AM, Laeeth Isharc 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?

You can always cast one pointer to another without complaint:

(cast(ubyte *)&a)[0..a.sizeof];

-Steve

January 12, 2015
V Mon, 12 Jan 2015 13:59:27 +0000
Laeeth Isharc via Digitalmars-d-learn
<digitalmars-d-learn@puremagic.com> napsáno:

> 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?
> 
> Thanks.

import std.datetime;
import std.stdio;
import std.conv;

void main(string[] arg)
{
    auto a=Clock.currTime();
    auto b= (cast(ubyte*)&a)[0 .. a.sizeof];
    writefln("%s",b);
}

January 12, 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

January 14, 2015
> 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

Thanks for this.  I still with my C habits had the idea the time would just be a flat struct.  So in this case better to write it as a string, which is what I have already done.  I just wondered why the other approach didn't work, and now I understand.