lundi 18 juin 2018

Formatted timestring when parsed to a time_point gives an hour difference

I found the following problem. I have a string with a time (gotten from a file to which I putoutted using the reverse operations). However, when converting this to a time_point and outputting it to terminal, I find a difference of one hour. I have not been able to find out why. Any ideas?

#include <iostream>
#include <chrono>
#include <sstream>
#include <iomanip>

const char *const TIME_FORMAT = "%Y-%b-%d %H:%M:%S";

int main() {
    //Formatted timestring
    std::string timeString = "2018-Jun-18 09:03:43,24";

    //Logic to parce the string into a time struct
    std::istringstream ss(timeString);
    std::locale locale("");
    ss.imbue(locale);
    std::tm t = {};
    ss >> std::get_time(&t, TIME_FORMAT);

    if (ss.fail()) {
        //TODO throw exception;
        std::cout << "Parse failed\n";
    }

    //Convert to time_point
    time_t time = mktime(&t);
    auto time_point = std::chrono::system_clock::from_time_t(time);

    //Convert  to output string
    auto time_t_again = std::chrono::system_clock::to_time_t(time_point);
    std::cout << "timePoint: " << std::put_time(std::localtime(&time_t_again), TIME_FORMAT);
    return 0;
}

This outputs: timePoint: 2018-Jun-18 10:03:43 instead of the expected 2018-Jun-18 9:03:43

Aucun commentaire:

Enregistrer un commentaire