I have a C ++ client that talks to a C # server, and I have custom serialization for custom types.
Now I need to pass the date and time structure from C ++ to C #. Googling led to the following approach:
- Fill in the date / time in the SYSTEMTIME structure (this is not the current system time, but rather will be filled in by the program based on certain conditions)
- Convert SYSTEMTIME to FILETIME
- Convert FILETIME to __int64 (this gives a 64-bit value representing an interval of 100 nanoseconds since January 1, 1601)
- Use this 64-bit value in the constructor for DateTime in C #.
Just to test the waters, I wrote 2 snippets of one in (native) C ++, which performs steps 1,2,3, and the other in C #, which performs Step4. There is no automatic call from C ++ to C #.
I manually pulled the value from step 3 (130220830133980000L) and used it in C #, and here is the code snippet and the result that I get.
C ++ code
SYSTEMTIME st; GetSystemTime(&st); printf("\n In C++ : %04d:%02d:%02d:%02d:%02d:%02d:%03d\n", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); FILETIME fileTime; if(!SystemTimeToFileTime(&st, &fileTime)) { wcout << "Conversion from system time to file time failed. Error : " << GetLastError(); } __int64 ticks = (((ULONGLONG) fileTime.dwHighDateTime) << 32) + fileTime.dwLowDateTime;
C # code
DateTime dob = new DateTime(130220830133980000L, DateTimeKind.Utc); Console.WriteLine("In C# : " + dob.Year + ":" + dob.Month + ":" + dob.Day + ":" + dob.Hour + ":" + dob.Minute + ":" + dob.Second + ":" + dob.Millisecond);
Exit:
In C ++: 2013: 08: 27: 13: 16: 53: 398
In C #: 413: 8: 27: 13: 16: 53: 398
All values ββexcept part of the year can be restored correctly. 2013 in C ++ becomes 413 in C #. I canβt understand why part of the year is changing.
Am I doing the conversion right? If there is an alternative approach for transferring date and time data from C ++ to C # and vice versa?