Inconsistent results with get_time

179 Views Asked by At

The program:

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

int main()
{
    std::string time_str = "2017-01-03 09:30:00";
    for(int i=0; i<5; i++)
    {
        std::istringstream ss(time_str);
        std::tm tm;
        ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
        time_t t = std::mktime(&tm);
        std::cout << (int)t << " " << tm.tm_hour << std::endl;
    }
}

This program has inconsistent behavior. On my local machine with MinGW 8.1.0, it prints

1483453800 8
1483457400 9
1483457400 9
1483457400 9
1483457400 9 

On Ideone, on both their Clang and GCC compilers, it prints

-1 9
-1 9
-1 9
-1 9
-1 9

What's up with this behavior? The Ideone output, which returns "-1" for time_t, suggests that I am messing up somewhere. The MinGW output, however, is even stranger because the first iteration of the loop has a different result from all the other iterations. My compiler and flags are in the comments.

Some other odd behavior is that if I change time_str to "1970-01-01 00:00:00", I get

18000 23
21600 0
21600 0
21600 0
21600 0

which is really weird both due to inconsistency and because epoch is stored as either 18000 or 21600 seconds instead of 0.

0

There are 0 best solutions below