Reputation: 1358
I'm trying to adapt a .Net code snippet to good-old C without pulling in masses of C++.Net classes. It looks something like this:
Int64 ticks = DateTime.Now.ToBinary();
WriteSigned64(address, ticks);
Is there an equivalent in C/C++ compiled for a Win32 process? The only things I've found use alternative and somewhat limited 32 bit structs. I also have to write this field to be compatible with the .Net version.
Upvotes: 1
Views: 1580
Reputation: 241555
There isn't any direct way of doing what you asked and still have it perform the exact same as the code you provided.
DateTime.ToBinary()
is not just the ticks. It encodes both the Ticks
and the Kind
properties of the DateTime
object into a single binary value. Refer to the documentation on MSDN. You may also wish to refer to the .NET Reference Source.
To illustrate, consider the following C# code:
DateTime dt = DateTime.Now;
Console.WriteLine(dt.Kind);
Console.WriteLine(dt.Ticks);
Console.WriteLine(dt.ToBinary());
Sample Output:
Local
635476072989775339
-8587895711865000469
You should also recognize that DateTime.Now
returns a Local
kind, which means the ticks are based on the local time zone of the computer the code is running on. If you take the ticks (or binary) from one system and recreate a DateTime
on a system with a different time zone, you will get a different value than you originally encoded.
By contrast, time_t
and most other time related types in C/C++ are based on UTC, so there is no ambiguity in the value.
Upvotes: 1
Reputation: 148965
You can use the function time_t time(time_t *t);
, declared in time.h
. It gives you the number of seconds from the Epoch (1970-01-01 00:00:00 +0000 (UTC)).
time_t
in all reasonably recent version is a 64 bits integer.
It is a C function, but it can be called from a C++ program.
Upvotes: 2