4

I'm trying to use the <chrono> library to parse a POSIX date and time string, no matter what the global locale is, based on this code:

#include <iostream>
#include <locale>
#include <sstream>
#include <string>
#include <chrono>

using namespace std;

void printPosixDateString(string const &str) {
    istringstream stringStream{};
    stringStream.str(str);
    
    locale cLocale{"C"};//locale of C/POSIX language
    stringStream.imbue(cLocale);

    chrono::sys_seconds timeHolder{};//default value is unix time

    stringStream >> chrono::parse("%c", timeHolder);

    if(stringStream.fail()) {
        cout << "Error while parsing date string : " << stringStream.str() << endl;
    }
    else {
        cout << timeHolder;//should print the date object
    }
}

int main(int argc, char **argv) {
    locale french("fr_FR.UTF8");
    locale::global(french);
    cout.imbue(french);
    printPosixDateString("Fri Jul 5 14:58:21 2019");//must work no matter the global locale

    return 0;
}

The problem is that it always print the "Error while parsing date string: Fri Jul 5 14:58:21 2019" message.

Do you please have any explanations for this ?

I am on Arch Linux, Kernel: Linux 6.10.7-arch1-1, on a typical x86-64 computer. I use g++ 14.2.1, tried with C++20 and C++23 (which gave the same outputs) and build my file like this: g++ -std=c++20 -Wall -o main main.cpp

I hand-wrote the date string, in case I would have missed an invisible bizzare character.

I don't want to write the format myself, as I want to be able to use the "%c" locale datetime format, so that I can easily update this code to apply it to other locales.

I was expecting this output: 2019-07-5 14:58:21, but only got Error while parsing date string: Fri Jul 5 14:58:21 2019 for every of my tries.

6
  • You can try running this to get what the current locale uses as its date format to see if it matches your current string. credit to stackoverflow.com/questions/73893070/… Commented Sep 23, 2024 at 17:49
  • @NathanOliver why %x (localized date representation) and not %c (standard date and time string) to match the parse? coliru.stacked-crooked.com/a/25f5a3bc7451aa72 Commented Sep 23, 2024 at 17:52
  • @NathanOliver it gives me the output 09/23/24 (but it isn't the same format, so it is normal that this doesn't match the input I tried) Commented Sep 23, 2024 at 17:52
  • @RemyLebeau Doesn't chrono::parse("%c", timeHolder) expect timeHolder to be in the current locale's format? Commented Sep 23, 2024 at 17:55
  • 1
    This appears to me to have nothing to do with locales. It only has to do with %c being equivalent to %a %b %e %T %Y in the "C" locale. If you strip all locale code out of the example it still fails. Commented Sep 23, 2024 at 17:58

1 Answer 1

6

This looks like it could be a gcc bug to me. I recommend a bug report. I note that your code works if you change "%c" to "%a %b %e %T %Y" which is what %c should expand to for the "C" locale.

Sign up to request clarification or add additional context in comments.

11 Comments

Maybe the locale is not what we expect? Maybe OP should check with unset environment to get posix standard first?
I recommend submitting a bug report to gcc.gnu.org/bugzilla. I wouldn't bother with clang at this time as their C++20 chrono support is still under (independent) development.
GCC's chrono::parse uses the std::time_get facet for parsing the %c format, so this is a bug in GCC's std::time_get rather than std::chrono::parse itself. I thought it might be gcc.gnu.org/PR45896 but changing 5 to 05 in the input string doesn't change anything. I'll comment further once it's reported to bugzilla, thanks for finding this problem.
Oh well this is fun, we simply use "" for the %c format in the C locale: gcc.gnu.org/cgit/gcc/tree/libstdc++-v3/config/locale/gnu/… (see lines 79 )! At least it will be an easy fix.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.