c++ - std::get_time() in gcc 6.3 and clang 4.0 doesn't work for full month names -
i want parse date string std::get_time(), seems code works on vc++ 2015, not work on gcc 6.3 , clang 4.0. here mcve:
#include <iostream> #include <sstream> #include <string> #include <iomanip> int main() { std::string line = "february 4 1993 15:21"; std::tm date_1 = {}; std::stringstream ss(line); ss >> std::get_time(&date_1, "%b %d %y %h:%m"); if(ss.fail()) { std::cout << "parse failed\n"; } std::cout << date_1.tm_year << "\n"; std::cout << date_1.tm_mon << "\n"; std::cout << date_1.tm_mday << "\n"; std::cout << date_1.tm_hour << "\n"; std::cout << date_1.tm_min << "\n"; return 0; } here's result vc++ 2015:
93 1 4 15 21 here results gcc 6.3.2 , clang 4.0 (i've used compilers on ideone.com , coliru) - both , without c++14 flags:
parse failed 0 1 0 0 0 it work when use abbreviated month names, such feb, aug etc.
i've tried setting different locales on stringstream (en_us, en_gb + utf-8 versions) either made no difference or resulted in runtime errors. i've tried changing separators no difference well.
i've read on cpp reference %b in std::get_time():
parses month name, either full or abbreviated, e.g. oct
is bug/missing functionality in both gcc , clang or compilers, contrary what's written above, free handle abbreviated names depending on implementation? standard that? coincidence 2 compilers exhibit same behaviour here?
as noted in comments, libstdc++ bug. not noted how work around it.
use %b instead of %b, , libstdc++ parse. parsing, %b , %b should have identical behavior. formatting, %b outputs locale's full month name while %b formats abbreviated month name.
finally, may interested in howard hinnant's free, open-source date-time library allows parse directly <chrono> time points , durations:
#include "date.h" #include <iostream> #include <sstream> int main() { using namespace date; using namespace std::chrono; std::istringstream in{"february 4 1993 15:21"}; sys_time<minutes> tp; in >> parse("%b %d %y %h:%m", tp); if (in.fail()) return 1; std::cout << tp << '\n'; } output:
1993-02-04 15:21 in example above sys_time<minutes> type alias std::chrono::time_point<std::chrono::system_clock, std::chrono::minutes>.
Comments
Post a Comment