mardi 31 mai 2016

Is std::get_time broken in g++ and clang++?

I was working with some time functions today and noticed that the standard conversion using %r (or %p) does not seem to work for input via std::get_time() on g++ or clang++. See this live code version for g++ and clang++. It does seem to work as expected under Windows with VC++ (see this closely related question). Also note that the effect is the same whether or not the imbue line is included. The locale on my Linux machine is set to "en_US.UTF-8" if it matters.

#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <ctime>

int main(){
    std::tm t{};
    std::stringstream ss{"1:23:45 PM"};
    ss.imbue(std::locale(std::cin.getloc(), 
             new std::time_get_byname<char>("en_US")));
    ss >> std::get_time(&t, "%r");
    if (ss.fail()) {
        std::cout << "Conversion failed\n" << std::put_time(&t, "%r") << '\n';
    } else {
        std::cout << std::put_time(&t, "%r") << '\n';
    }
}

Aucun commentaire:

Enregistrer un commentaire