C++ Converting A String to a time_t variable

14,112

The code you posted is correct.

This leads me to believe that your input string (startTime) is not in the format you are expecting, and therefore sscanf cannot parse out the values.

Example:

#include <iostream>

int main()
{
    std::string startTime = "2016/05/18 13:10:00";

    time_t tStart;
    int yy, month, dd, hh, mm, ss;
    struct tm whenStart;
    const char *zStart = startTime.c_str();

    sscanf(zStart, "%d/%d/%d %d:%d:%d", &yy, &month, &dd, &hh, &mm, &ss);
    whenStart.tm_year = yy - 1900;
    whenStart.tm_mon = month - 1;
    whenStart.tm_mday = dd;
    whenStart.tm_hour = hh;
    whenStart.tm_min = mm;
    whenStart.tm_sec = ss;
    whenStart.tm_isdst = -1;

    tStart = mktime(&whenStart);

    std::cout << tStart << std::endl;
}

Output:

1463595000

Have you sanity checked your inputs?

Please note that you can check the return value of sscanf to verify if it worked as you expected.

Return value
Number of receiving arguments successfully assigned, or EOF if read failure occurs before the first receiving argument was assigned.

If the return value is not 6, then the input string is incorrect.

int num_args = sscanf(zStart, "%d/%d/%d %d:%d:%d", &yy, &month, &dd, &hh, &mm, &ss);
if (num_args != 6)
{
    std::cout << "error in format string " << startTime << '\n';
    return 1;
}

As a rule of thumb you shouldn't ever assume that your inputs will be correct. As such, defensive programming is a good habit to get into.

Share:
14,112
Adam F
Author by

Adam F

Updated on June 04, 2022

Comments

  • Adam F
    Adam F almost 2 years

    I'm working on a C++ function that is supposed to figure out if a specified event happened between two time points. The event name, start datetime, and end datetime are all passed in from Lua as strings. Because of that, I need to parse my datetime strings into time_t variables. Based on what I've seen on StackOverflow and other forums, this code should work:

    time_t tStart;
    int yy, month, dd, hh, mm, ss;
    struct tm whenStart = {0};
    const char *zStart = startTime.c_str();
    
    sscanf(zStart, "%d/%d/%d %d:%d:%d", &yy, &month, &dd, &hh, &mm, &ss);
    whenStart.tm_year = yy - 1900;
    whenStart.tm_mon = month - 1;
    whenStart.tm_mday = dd;
    whenStart.tm_hour = hh;
    whenStart.tm_min = mm;
    whenStart.tm_sec = ss;
    whenStart.tm_isdst = -1;
    
    tStart = mktime(&whenStart);
    

    However, tStart appears to be assigned the value of -1 here. If I use strftime to reconstruct a string from whenStart, that tm structure appears to have been made completely correctly. Somehow mktime() is not liking the structure, I think. What is wrong with this code?

    Also, before you answer, know that I already tried using a strptime() call. For reasons that are unclear to me, this function gets rejected with an "undefined reference to 'strptime'" error. The various descriptions I've found for how to fix this problem only serve to destroy the rest of the code base I'm working with, so I would rather avoid messing with _XOPEN_SOURCE or similar redefinitions.

    Thanks for your help!