Search code examples
c++timestandardsc++-chronoseconds

Convert double to struct tm


I have a double containing seconds. I would like to convert this into a struct tm.

I can't find a standard function which accomplishes this. Do I have to fill out the struct tm by hand?

I just accidentally asked this about converting to a time_t and http://www.StackOverflow.com will not let me post unless I link it.


Solution

  • MSalters answer is correct, but I thought I'd add a bit of detail on how you should convert to time_t and how you should convert to tm.

    So given a number of seconds in double input you can use the implementation dependent method of casting:

    const auto temp = static_cast<time_t>(input);
    

    But since time_t is implementation defined there is no way to know that this is a primitive that can simply be cast to. So the guaranteed method would be to use the chrono library's implementation independent method of converting:

    const auto temp = chrono::system_clock::to_time_t(chrono::system_clock::time_point(chrono::duration_cast<chrono::seconds>(chrono::duration<double>(input))));
    

    The conversion options are discussed in more detail here: https://stackoverflow.com/a/50495821/2642059 but once you have obtained your time_t through one of these methods you can simply use localtime to convert temp to a struct tm.

    const auto output = *localtime(&temp);
    

    Note the dereference is important. It will use the default copy assignment operator so output is captured by value, which is essential because:

    The structure may be shared between std::gmtime, std::localtime, and std::ctime, and may be overwritten on each invocation.