www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - casting SysTime to ubyte[]

reply "Laeeth Isharc" <laeethnospam spammenot_laeeth.com> writes:
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.
Jan 12 2015
next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Jan 12 2015
prev sibling next sibling parent Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn writes:
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); }
Jan 12 2015
prev sibling parent reply Jonathan M Davis via Digitalmars-d-learn writes:
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
Jan 12 2015
parent "Laeeth Isharc" <Laeethnospam nospam.laeeth.com> writes:
 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.
Jan 14 2015