Search code examples
c++ctimedayofweekc++-chrono

Populate struct tm


Is there something in the standard C/C++ libraries which will populate a struct tm?

To be more specific about my problem: I want to be able to provide a date and populate the struct tm from that (for example: 1-1-2000 00:00:01.) For most of the fields I can just directly insert the data, but I just don't know tm_wday and tm_isdst.

I'm really looking for a way to populate those two without writing some complex day of the week state machine.


Solution

  • mktime sounds like your best bet. The struct tm you pass to it need not have values in the specified ranges; it normalizes the fields, including recalculating tm_wday and tm_yday. To have it attempt to determine if DST is in effect, set the tm_isdst member to negative before you call mktime.

    If you are converting from a string, you can use the get_time manipulator to extract the information into a tm.