Discussion:
[Interest] QDateTime and std::chrono
Roger Leigh
2018-11-02 10:19:50 UTC
Permalink
Hi folks,

Is there any recommended way to convert a QDateTime to a
std::chrono::timepoint? I'd like to take the value of
QFileInfo::lastModified() and pass it to an interface using e.g.
std::chrono<high_resolution_clock, milliseconds> (or nanoseconds).

However, making this work portably, including all the epoch and leap
second issues, looks complicated and error-prone. Has anyone got any
suggestions for how best to do this?


Thanks,
Roger
Jérôme Godbout
2018-11-02 15:40:49 UTC
Permalink
Hi,
Maybe you can pass by a string, this would be highly inefficient but could be simple enough. I guess you should make the time into UTC too. You could use QString to std::string for the string stream. And do the following:

std::tm tm = {};
std::stringstream ss("Jan 9 2014 12:35:34"); // Change this for the QDateTime toString().toStdString()
ss >> std::get_time(&tm, "%b %d %Y %H:%M:%S");
auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm));

I don't think it's the best solution, but could work easily if performance is not an issue. Hope someone have a better plan...

Jerome

-----Original Message-----
From: Interest <interest-bounces+godboutj=***@qt-project.org> On Behalf Of Roger Leigh
Sent: November 2, 2018 6:20 AM
To: interestqt-project.org <***@qt-project.org>
Subject: [Interest] QDateTime and std::chrono

Hi folks,

Is there any recommended way to convert a QDateTime to a std::chrono::timepoint? I'd like to take the value of
QFileInfo::lastModified() and pass it to an interface using e.g.
std::chrono<high_resolution_clock, milliseconds> (or nanoseconds).

However, making this work portably, including all the epoch and leap second issues, looks complicated and error-prone. Has anyone got any suggestions for how best to do this?


Thanks,
Roger
Tomasz Siekierda
2018-11-02 19:51:04 UTC
Permalink
On Fri, 2 Nov 2018 at 17:51, Giuseppe D'Angelo via Interest
Last, but not least, note that std::system_clock is a Unix clock only
starting in C++2a; before you had no guarantees. Does anyone know if
QDateTime::toMSecsSinceEpoch() returns UTC time or Unix time?
UTC time, according to docs
https://doc.qt.io/qt-5/qdatetime.html#toMSecsSinceEpoch

Loading...