7

I have a function that works under C++14 using the date.h library but I'm converting my program to use C++20 and it's no longer working. What am I doing wrong, please?

My C++14/date.h code is as follows:

#include <date/date.h> // latest, installed via vcpkg #include <chrono> auto StringToUnix(const std::string& source) -> std::time_t { auto in = std::istringstream(source); auto tp = date::sys_seconds{}; in >> date::parse("%Y-%m-%d %H:%M:%S", tp); return std::chrono::system_clock::to_time_t(tp); } 

My converted C++20 function is as follows:

#include <chrono> auto StringToUnix(const std::string& source) -> std::time_t { using namespace std::chrono; auto in = std::istringstream(source); auto tp = sys_seconds{}; in >> parse("%Y-%m-%d %H:%M:%S", tp); return system_clock::to_time_t(tp); } 

The error I'm receiving in VS2019 Community (latest) is:

E0304 no instance of overloaded function "parse" matches the argument list 

Are there any subtle changes that have been made and I'm missing? What could be causing this error, please?

1
  • Does your compiler support the new date functionality? Check your compiler's documentation to see how much of C++20 it supports, as no compiler offers 100% support yet. Commented Jun 9, 2021 at 14:44

1 Answer 1

11

There's a bug in the spec that is in the process of being fixed. And VS2019 faithfully reproduced the spec. Wrap your format string in string{}, or give it a trailing s literal to turn it into a string, and this will work around the bug.

in >> parse("%Y-%m-%d %H:%M:%S"s, tp); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.