It is necessary to convert strings like "10 January 16 14:10:09" to Unix time "1452427809". How does it work in C ++?

    1 answer 1

    There is such a function strptime , which should be suitable for your needs.

    Sample code for your case:

     #include <iostream> #include <ctime> int main() { struct tm tm = { }; strptime("10 January 16 14:10:09", "%d %b %y %H:%M:%S", &tm); std::cout << mktime(&tm) << "\n"; } 

    Result:

     1452402609 

    As noted in the comments, the function is not included in the C ++ Standard, and is a POSIX function. Those. in some implementations, for example under Windows, it may be unavailable. In this case, you can use the source code from here .

    • But, alas, it is not included in the C ++ standard ... However, it is even worse to do the analysis with your hands :) - Harry
    • one
      @Harry yep it's POSIX. - 伪位蔚蠂慰位蠀蟿