lundi 1 août 2022

mktime returning the wrong time

I have two functions for creating a date string, and parsing a date string. The creation works, but the parser seems to add 5 hours to the time. For context, my time zone is EST, 5 hours behind GMT. How can this be fixed? Below are the functions:

std::string build_date(time_t rawtime) {
    struct tm* timeinfo = gmtime(&rawtime);
    std::stringstream ss;
    ss.imbue(std::locale(setlocale(LC_ALL, "C")));
    ss << std::put_time(timeinfo, "%a, %d %b %Y %T %Z");
    return ss.str();
}

time_t parse_date(const std::string& date) {
    struct tm timeinfo = {0};
    std::istringstream ss(date);
    ss.imbue(std::locale(setlocale(LC_ALL, "C")));
    ss >> std::get_time(&timeinfo, "%a, %d %b %Y %T %Z");
    return mktime(&timeinfo);
}

Aucun commentaire:

Enregistrer un commentaire