diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp index 3dc5c1f71a1..235222043dc 100644 --- a/src/corelib/time/qdatetime.cpp +++ b/src/corelib/time/qdatetime.cpp @@ -5352,8 +5352,17 @@ QDateTime QDateTime::currentDateTimeUtc() Constructs a datetime representing the same point in time as \a time, using Qt::UTC as its specification. - The clock of \a time must be compatible with \c{std::chrono::system_clock}, - and the duration type must be convertible to \c{std::chrono::milliseconds}. + The clock of \a time must be compatible with + \c{std::chrono::system_clock}; in particular, a conversion + supported by \c{std::chrono::clock_cast} must exist. After the + conversion, the duration type of the result must be convertible to + \c{std::chrono::milliseconds}. + + If this is not the case, the caller must perform the necessary + clock conversion towards \c{std::chrono::system_clock} and the + necessary conversion of the duration type + (cast/round/floor/ceil/...) so that the input to this function + satisfies the constraints above. \note This function requires C++20. diff --git a/src/corelib/time/qdatetime.h b/src/corelib/time/qdatetime.h index 56601e5d783..29da29a5d0e 100644 --- a/src/corelib/time/qdatetime.h +++ b/src/corelib/time/qdatetime.h @@ -510,6 +510,17 @@ public: #if __cpp_lib_chrono >= 201907L || defined(Q_QDOC) #if __cpp_concepts >= 201907L || defined(Q_QDOC) +private: + // The duration type of the result of a clock_cast. + // This duration may differ from the duration of the input. + template + using system_clock_cast_duration = decltype( + std::chrono::clock_cast( + std::declval &>() + ).time_since_epoch() + ); + +public: // Generic clock, as long as it's compatible with us (= system_clock) template static QDateTime fromStdTimePoint(const std::chrono::time_point &time) @@ -517,8 +528,12 @@ public: requires(const std::chrono::time_point &t) { // the clock can be converted to system_clock std::chrono::clock_cast(t); - // the duration can be converted to milliseconds - requires std::is_convertible_v; + // after the conversion to system_clock, the duration type + // we get is convertible to milliseconds + requires std::is_convertible_v< + system_clock_cast_duration, + std::chrono::milliseconds + >; } { const auto sysTime = std::chrono::clock_cast(time);