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

How to parse GMT+-H in C++20 with std::chrono::parse


std::chrono::parse and std::chrono::from_stream allows us to parse many different date/time formats.

But I recently got a string like this: 2022-07-10 22:00 GMT+2 - and I'm not sure what format should I use.

I tried: %F %T GMT%z - fails %F %T GMT%Z - fails %F %T %Z - fails

Or maybe I just have to parse time/date and then manually get the zone?


Solution

  • This is a little tricky.

    Putting this all together:

    istringstream in{"2022-07-10 22:00 GMT+2"};
    in.exceptions(ios::failbit);
    sys_seconds tp;
    in >> parse("%F %H:%M GMT%Ez", tp);
    cout << tp << '\n';
    

    Output:

    2022-07-10 20:00:00
    

    Note that the offset is applied and so tp represents a UTC timestamp. If you instead wanted the local time, then just change sys_seconds to local_seconds and the output changes to:

    2022-07-10 22:00:00
    

    The offset is parsed, but not applied.