8

When trying to compile the following example:

std::chrono::sys_time<std::chrono::microseconds> timestamp; std::stringstream ss = foo(); ss >> std::chrono::parse("%Y-%m-%d %T", timestamp); 

I get:

error: ‘parse’ is not a member of ‘std::chrono’ 15 | ss >> std::chrono::parse("%Y-%m-%d %T", timestamp); | ^~~~~ 

I was not expecting this, as I'm using the latest g++ I could find.

More info:

$ g++-11 --version g++-11 (Ubuntu 11.1.0-1ubuntu1~18.04.1) 11.1.0 $ g++-11 -v Using built-in specs. COLLECT_GCC=g++-11 COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu Configured with: (...) Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 11.1.0 (Ubuntu 11.1.0-1ubuntu1~18.04.1) 

I'm compiling with:

g++-11 -std=c++2a -o test time.cc 

Is this just not supported in g++-11.1.0?

4
  • 1
    Does GCC advertise support for this in its standard library? Commented Aug 12, 2021 at 11:40
  • Possibly a duplicate question of stackoverflow.com/questions/67906096/… ? Commented Aug 12, 2021 at 11:51
  • see en.cppreference.com/w/cpp/compiler_support, only visual studio has a complete implementation (search for "Calendar and timezone") Commented Aug 12, 2021 at 13:18
  • Dammit. I find it so frustrating that there's no default way in C or C++ to parse a timestamp with microsecond precision. I ended up rolling my own with sscanf writing into a struct tm for all fields except for ts_sec, which I set to zero. Then I use timegm to get the timestamp in seconds, and add things up with a float that I parse in scanf for number of seconds (taking in to account the units ofc). Commented Aug 12, 2021 at 13:21

2 Answers 2

9

Until your favorite C++ vendor ships std::chrono::parse, there exists a free, open-source preview of this part of C++20.

std::chrono::sys_time<std::chrono::microseconds> timestamp; std::stringstream ss = foo(); ss >> date::parse("%Y-%m-%d %T", timestamp); 
Sign up to request clarification or add additional context in comments.

2 Comments

which part corresponds to the support of this in en.cppreference.com/w/cpp/compiler_support? will it be supported in gcc13?
Search for "Calendar" at that link.
0

well i have a g++ 13.2.0 and this is a part of its chrono

#if __cplusplus >= 202002L // TODO formatting and parsing // # undef __cpp_lib_chrono // # define __cpp_lib_chrono 201907L #endif 

5 Comments

g++ 14 will ship with full C++20 chrono support. One can experiment with it today at wandbox.org.
g++ 14.3 does not contain chrono::parse, and even 15.1 does not. Can't believe it's been 5 years already, and still there is no release version for this??
Compiler Explorer appears to indicate that you are mistaken: gcc.godbolt.org/z/cEdj4q6fE
Perhaps add -std=c++20
I would recommend editing this and explaining what that commented code means

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.