QDateTime::fromStdTimePoint: fix a call to fromMSecsSinceEpoch

The overload taking a Qt::TimeSpec is deprecated; we need to use
the one taking a QTimeZone instead. However I can't just use
QTimeZone because this is inline code in qdatetime.h, and QTimeZone
is only forward declared there. Moving the definition in qtimezone.h is
possible (like it's done for zoned_time), but that is a source break
(requires a new inclusion from users) and also weird (this function
doesn't deal with timezones, why do we need to include qtimezone.h?).

Instead, just add another overload of fromStdTimePoint that takes
precisely sys_time<milliseconds>. As such, it can be always available
even in C++17, and implemented out-of-line.

Even if this is a new symbol, the new commit policy allows the
backport. I also don't think this qualifies as a new feature;
it's a cleanup that we forgot to do (if we had a C++20-enabled
configuration in the CI when we also did the fromMSecsSinceEpoch
deprecation, we would've caught this).

Pick-to: 6.5
Change-Id: Ica77da291be4bcda2ffc7c164316a2977974c386
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
(cherry picked from commit c4fc3a74b4525ad5d80e558707301a12e4d4cb19)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Giuseppe D'Angelo 2024-05-24 14:32:47 +02:00 committed by Qt Cherry-pick Bot
parent 8d6ed7acf1
commit 1752308123
2 changed files with 25 additions and 2 deletions

View File

@ -5350,7 +5350,7 @@ QDateTime QDateTime::currentDateTimeUtc()
\since 6.4
Constructs a datetime representing the same point in time as \a time,
using Qt::UTC as its specification.
using Qt::UTC as its time representation.
The clock of \a time must be compatible with
\c{std::chrono::system_clock}; in particular, a conversion
@ -5369,6 +5369,22 @@ QDateTime QDateTime::currentDateTimeUtc()
\sa toStdSysMilliseconds(), fromMSecsSinceEpoch()
*/
/*!
\since 6.4
\overload
Constructs a datetime representing the same point in time as \a time,
using Qt::UTC as its time representation.
*/
QDateTime QDateTime::fromStdTimePoint(
std::chrono::time_point<
std::chrono::system_clock,
std::chrono::milliseconds
> time)
{
return fromMSecsSinceEpoch(time.time_since_epoch().count(), QTimeZone::UTC);
}
/*!
\fn QDateTime QDateTime::fromStdTimePoint(const std::chrono::local_time<std::chrono::milliseconds> &time)
\since 6.4

View File

@ -508,6 +508,13 @@ public:
NSDate *toNSDate() const Q_DECL_NS_RETURNS_AUTORELEASED;
#endif
static QDateTime fromStdTimePoint(
std::chrono::time_point<
std::chrono::system_clock,
std::chrono::milliseconds
> time
);
#if __cpp_lib_chrono >= 201907L || defined(Q_QDOC)
#if __cpp_concepts >= 201907L || defined(Q_QDOC)
private:
@ -539,7 +546,7 @@ public:
const auto sysTime = std::chrono::clock_cast<std::chrono::system_clock>(time);
// clock_cast can change the duration, so convert it again to milliseconds
const auto timeInMSec = std::chrono::time_point_cast<std::chrono::milliseconds>(sysTime);
return fromMSecsSinceEpoch(timeInMSec.time_since_epoch().count(), Qt::UTC);
return fromStdTimePoint(timeInMSec);
}
#endif // __cpp_concepts