C++ Converting A String to a time_t variable

前端 未结 1 1796
不思量自难忘°
不思量自难忘° 2021-01-26 06:10

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 pas

相关标签:
1条回答
  • 2021-01-26 06:46

    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.

    0 讨论(0)
提交回复
热议问题