Search code examples
c++gccc++20c++-chrono

Chrono : Different output for the same program using gcc


This code displays different output for different versions of gcc compiler

#include "date.h"
#include <iostream>
#include <chrono>
#include <string>
#include <set>
#include <ranges>
#include <algorithm>

using namespace date;
using namespace std;
using namespace std::chrono;

using time_point_t = std::chrono::sys_time<std::chrono::microseconds>;

time_point_t parse_tp(std::string s) {
    time_point_t tp;
    std::istringstream in(s);
    in >> parse("%Y%m%d-%T", tp);
    return tp;
}

int
main()
{
    auto tp = parse_tp("20240304-13:00:00.002");
    std::set<time_point_t> s = {tp, tp + std::chrono::seconds(1)};
    std::cout << *std::ranges::lower_bound(s, tp + std::chrono::seconds(1));
}
Compiler Output
gcc 13 2024-03-04 13:00:01.002000
gcc trunk 2024-03-04 13:00:01.001999

What makes the output different?

Is it a bug?


Solution

  • The gcc-13 version is calling date::parse, and the gcc-trunk version is calling std::chrono::parse. These are supposed to do the same thing, but are independent implementations.

    Yesterday I filed https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114244 which is in this area, but may or may not impact this specific example.

    See the comment below by Ted Lyngmo for a good explanation of this behavior.