Parsing a date / time string using std :: get_time need separators?

Is a delimiter required for parsing a string using std :: get_time? I can not find the link to say that it is. I am trying to parse an ISO date and time string such as "20140105T123456" - for example:

For instance,

#include <iostream> #include <sstream> #include <locale> #include <iomanip> #include <ctime> int main(int argc, char* argv[]) { std::tm t = { 0 }; // fails std::istringstream ss("20141105T123456"); ss >> std::get_time(&t, "%Y%m%dT%H%M%S"); // works //std::istringstream ss("2014 11 05 T 12 34 56"); //ss >> std::get_time(&t, "%Y %m %d T %H %M %S"); std::ostringstream os; std::cout << std::put_time(&t, "%c") << std::endl; } 

I am using Visual Studio 2013. I tried to use Linux, but I have the latest version of GCC 4.7.3, which does not yet support get_time.

Is there a deep mistake on my part or do you need separators?

+6
source share
2 answers

According to the description of the second parameter std::get_time , no delimiters are required.

A format string consists of zero or more conversion specifications, white space characters, and ordinary characters (except%). each regular character is expected to correspond to one character in the input stream in a case-insensitive comparison. Each space character corresponds to an arbitrary space in the input string. Each conversion specification begins with a% character, optionally followed by an E or O modifier (ignored if not supported by the locale), and then which determines the behavior of the specifier. Format specifiers correspond to the POSIX strptime () function

On my Mac, I use clang ++ (Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)) to compile your code and run the program, exit: Sun Nov 5 12:34:56 2014 . Actually, I have gcc 4.8.2 installed, but it does not support std::get_time and std::put_time . Then I look at the implementation status for this function and find which is not implemented in GCC 4.8.0

This failed in VS2013, after calling std::get_time all elements in t are 0. The specification formats do not do what is expected here on Windows. This is not your fault.

+5
source

The reference to the POSIX strptime () function in the standard causes some confusion. The POSIX standard indicates that conversion operators are separated by characters other than alpha.

The application must ensure that there are spaces or other non-alphanumeric characters between any two conversion specifications.

The strptime GLIBC version (and IMO, any normal implementation) does not require these delimiters. [ http://man7.org/linux/man-pages/man3/strptime.3.html - see the Notes section.]

The necessary separators make it impossible to parse the ISO-8601 date and time syntactic formats. Since C ++ is also an ISO standard, its strptime () function should be able to parse this format.

I think it is necessary to clarify the documentation of std :: get_time () to explicitly indicate that no separators are needed and justify this statement, pointing to ISO-8601. It should be further clarified that the POSIX standard strptime () defines only conversion specifiers that should be supported by the corresponding implementation.

0
source

All Articles