0

This is a follow up question to this question: Standard conformant way of converting std::time_t to System::DateTime?

I need to convert a c++11 time_t to .NET DateTime by a string. The program needs to take the value of the current time_t, create a string which is going to be written to a file, and then another program which is written in C# is going to read that value and convert it to DateTime.

The problem is that I don't have access to the C# code so I need to make the needed string in the c++ side. Currently, the C# side knows how to convert strings in this format:

2018-05-13T10:03:18.4195735+03:00

I'm not sure if this is a regular conventional string that represents a DateTime and how exactly to create it.

Edit: In addition to the time_t value I also got the milliseconds value as a chrono::seconds::rep so you can just assume I got both

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203
  • 1
    While time-keeping using `time_t` is implementation defined, on most systems it's the number of seconds (as an integer) from some epoch. That means you can never get the *exact* format you want, since that includes fractions of a second, which is then not possible with `time_t`. If you need fractions of a second you need to use platform-specific functions to get the date and time. For example [`GetSystemTime`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724390(v=vs.85).aspx) in Windows. Once you have that, it's just a question of formatting the string correctly. – Some programmer dude Jul 16 '18 at 07:18
  • @Someprogrammerdude I am currently also saving the milliseconds so you can assume I got both time_t and the milliseconds value – CodeMonkey Jul 16 '18 at 07:21

1 Answers1

2

This is the ISO 8601 format, an international standard to represente date and time.

In your case, you have <date>T<time><zone>

<date> = 2018-05-13 (YYYY-MM-DD)
<time> = 10:03:18.4195735 (hh:mm:ss.sssssss)
<zone> = +03:00

You can see more information about this convention here: ISO 8601

Probably they're using something to parse it to DateTime.

I don't know much about C++, but looks like you can use GetTimeZoneInformation to get the timezone (Windows only). The time and date I think you already have, though they're asking for more precision than milliseconds. Looks like you can get only 1/1000000 of second in C++ with chrono (one nanosecond), which would be 6 decimal places. Do they really need it so precise? If not you can just add a 0 in the 7th decimal place.

Nathalia Soragge
  • 1,415
  • 6
  • 21