C# time to C++ time_t
-
I am sending binary data between C# and C++ successfully. However, I would like to convert the C++ time_t, which is a 32 bit long in C++ to a C# DateTime. Does anyone have a solution off hand for converting time_t to a C# DateTime value and from a C# DateTime to a C++ time_t?
-
I am sending binary data between C# and C++ successfully. However, I would like to convert the C++ time_t, which is a 32 bit long in C++ to a C# DateTime. Does anyone have a solution off hand for converting time_t to a C# DateTime value and from a C# DateTime to a C++ time_t?
Since
time_t
is just an unsigned 32-bit integer, you can useuint
(alias forUInt32
), but I recommend you don't depending on how you use it because unsigned types in .NET are not CLS-compliant (i.e., less portable code). Whenever you need to pass it to unmanaged code or get it back, you can use theMarshalAsAttribute
withUnmanagedType.U4
. For instance, 0xffff is -1 for anInt32
while it is 65535 for aUInt64
. The value is still the same, it's just how it's interpreted. The marshaling I mentioned above will take care of this. The biggest hurdle has to do with the epoch (start time) of each.time_t
is the number of seconds that have ellapsed since 12:00 midnight, 1-1-1970. ADateTime
in .NET represents the number of ticks (100 nanoseconds) since 12:00 midnight, 1-1-0001 AD. This definitely becomes a hurdle as I'm sure you can see. One easy way to convert between the two is to create aDateTime
that starts at the epoch of atime_t
, and then add the number of seconds represented bytime_t
:DateTime dt = DateTime.Parse("1970-01-01T00:00:00");
dt.AddSeconds(time_t); // From wherever it comesIf you do this a lot, you might consider storing the return from
DateTime.Parse
as a read-only field of your class (thereadonly
keyword lets the field be set in the static or instance constructor and can never be set again, which is good since you can't use aconst
for a non-constant value like this).Microsoft MVP, Visual C# My Articles
-
Since
time_t
is just an unsigned 32-bit integer, you can useuint
(alias forUInt32
), but I recommend you don't depending on how you use it because unsigned types in .NET are not CLS-compliant (i.e., less portable code). Whenever you need to pass it to unmanaged code or get it back, you can use theMarshalAsAttribute
withUnmanagedType.U4
. For instance, 0xffff is -1 for anInt32
while it is 65535 for aUInt64
. The value is still the same, it's just how it's interpreted. The marshaling I mentioned above will take care of this. The biggest hurdle has to do with the epoch (start time) of each.time_t
is the number of seconds that have ellapsed since 12:00 midnight, 1-1-1970. ADateTime
in .NET represents the number of ticks (100 nanoseconds) since 12:00 midnight, 1-1-0001 AD. This definitely becomes a hurdle as I'm sure you can see. One easy way to convert between the two is to create aDateTime
that starts at the epoch of atime_t
, and then add the number of seconds represented bytime_t
:DateTime dt = DateTime.Parse("1970-01-01T00:00:00");
dt.AddSeconds(time_t); // From wherever it comesIf you do this a lot, you might consider storing the return from
DateTime.Parse
as a read-only field of your class (thereadonly
keyword lets the field be set in the static or instance constructor and can never be set again, which is good since you can't use aconst
for a non-constant value like this).Microsoft MVP, Visual C# My Articles
-
Since
time_t
is just an unsigned 32-bit integer, you can useuint
(alias forUInt32
), but I recommend you don't depending on how you use it because unsigned types in .NET are not CLS-compliant (i.e., less portable code). Whenever you need to pass it to unmanaged code or get it back, you can use theMarshalAsAttribute
withUnmanagedType.U4
. For instance, 0xffff is -1 for anInt32
while it is 65535 for aUInt64
. The value is still the same, it's just how it's interpreted. The marshaling I mentioned above will take care of this. The biggest hurdle has to do with the epoch (start time) of each.time_t
is the number of seconds that have ellapsed since 12:00 midnight, 1-1-1970. ADateTime
in .NET represents the number of ticks (100 nanoseconds) since 12:00 midnight, 1-1-0001 AD. This definitely becomes a hurdle as I'm sure you can see. One easy way to convert between the two is to create aDateTime
that starts at the epoch of atime_t
, and then add the number of seconds represented bytime_t
:DateTime dt = DateTime.Parse("1970-01-01T00:00:00");
dt.AddSeconds(time_t); // From wherever it comesIf you do this a lot, you might consider storing the return from
DateTime.Parse
as a read-only field of your class (thereadonly
keyword lets the field be set in the static or instance constructor and can never be set again, which is good since you can't use aconst
for a non-constant value like this).Microsoft MVP, Visual C# My Articles
"For instance, 0xffff is -1 for an Int32 while it is 65535 for a UInt64" I think that you'll find that 0xffff is 65535 for Int32 as well as in UInt16, UInt32, Int64 & UInt64 but -1 in Int16. 0xffffffff is -1 in Int32 & 4,294,967,295 in UInt32, Int64 & UInt64. Regards Brewman
-
"For instance, 0xffff is -1 for an Int32 while it is 65535 for a UInt64" I think that you'll find that 0xffff is 65535 for Int32 as well as in UInt16, UInt32, Int64 & UInt64 but -1 in Int16. 0xffffffff is -1 in Int32 & 4,294,967,295 in UInt32, Int64 & UInt64. Regards Brewman
Oops, that last part of the quote was actually supposed to be
UInt32
, but you're right - I obviously couldn't perform basic math that day. :-OMicrosoft MVP, Visual C# My Articles