mardi 18 août 2020

Inconsistent results with get_time

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.

Aucun commentaire:

Enregistrer un commentaire