diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2022-01-24 17:56:30 +0100 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2022-05-22 14:22:37 +0200 |
commit | 1ae4ffefbbf6dd8d0e29b7253c9ea61b0ebccaa5 (patch) | |
tree | 703fcaa2fb63b8b176e7dd007d0c27e8821fe462 | |
parent | 54d81d118997fc4c238b7266571d220d7a6065f1 (diff) |
Implement in QLocalTime the offset functions V4 Date needs
Prepare to replace a large pile of #if-ery-laden tangled mess from the
implementation of V4 Date by implementing a cleaned-up version of one
of its offset calculations and using a recently refactored API of QDTP
to implement the other.
Task-number: QTBUG-95993
Change-Id: I469f67fb384543abeece9ce8b14bb294c8613033
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/time/qlocaltime.cpp | 46 | ||||
-rw-r--r-- | src/corelib/time/qlocaltime_p.h | 10 |
2 files changed, 56 insertions, 0 deletions
diff --git a/src/corelib/time/qlocaltime.cpp b/src/corelib/time/qlocaltime.cpp index 83a673f10d..5800fb0e93 100644 --- a/src/corelib/time/qlocaltime.cpp +++ b/src/corelib/time/qlocaltime.cpp @@ -232,6 +232,52 @@ int QDateTimeParser::startsWithLocalTimeZone(QStringView name) namespace QLocalTime { +#ifndef QT_BOOTSTRAPPED +// Even if local time is currently in DST, this returns the standard time offset +// (in seconds) nominally in effect at present: +int getCurrentStandardUtcOffset() +{ +#ifdef Q_OS_WIN + TIME_ZONE_INFORMATION tzInfo; + GetTimeZoneInformation(&tzInfo); + return -tzInfo.Bias * 60; +#else + qTzSet(); + const time_t curr = time(nullptr); + /* Set t to the UTC represntation of curr; the time whose local standard + time representation coincides with that differs from curr by local time's + standard offset. Note that gmtime() leaves the tm_isdst flag set to 0, + so mktime() will, even if local time is currently using DST, return the + time since epoch at which local standard time would have the same + representation as UTC's representation of curr. The fact that mktime() + also flips tm_isdst and updates the time fields to the DST-equivalent + time needn't concern us here; all that matters is that it returns the + time after epoch at which standard time's representation would have + matched UTC's, had it been in effect. + */ +# if defined(_POSIX_THREAD_SAFE_FUNCTIONS) + struct tm t; + if (gmtime_r(&curr, &t)) + return curr - qMkTime(&t); +# else + if (struct tm *tp = gmtime(&curr)) { + struct tm t = *tp; // Copy it quick, hopefully before it can get stomped + return curr - qMkTime(&t); + } +# endif + // We can't tell, presume UTC. + return 0; +#endif // Platform choice +} + +// This is local time's offset (in seconds), at the specified time, including +// any DST part. +int getUtcOffset(qint64 atMSecsSinceEpoch) +{ + return QDateTimePrivate::expressUtcAsLocal(atMSecsSinceEpoch).offset; +} +#endif // QT_BOOTSTRAPPED + // Calls the platform variant of localtime() for the given utcMillis, and // returns the local milliseconds, offset from UTC and DST status. QDateTimePrivate::ZoneState utcToLocal(qint64 utcMillis) diff --git a/src/corelib/time/qlocaltime_p.h b/src/corelib/time/qlocaltime_p.h index c3c565eee6..027b1bc05b 100644 --- a/src/corelib/time/qlocaltime_p.h +++ b/src/corelib/time/qlocaltime_p.h @@ -22,6 +22,16 @@ QT_BEGIN_NAMESPACE // Packaging system time_t functions namespace QLocalTime { +#ifndef QT_BOOTSTRAPPED +// Support for V4's Date implelenentation. +// Each returns offset from UTC in seconds (or 0 if unknown). +// V4 shall need to multiply by 1000. +// Offset is -ve East of Greenwich, +ve west of Greenwich. +// Add it to UTC seconds since epoch to get local seconds since nominal local epoch. +Q_CORE_EXPORT int getCurrentStandardUtcOffset(); +Q_CORE_EXPORT int getUtcOffset(qint64 atMSecsSinceEpoch); +#endif // QT_BOOTSTRAPPED + // Support for QDateTime QDateTimePrivate::ZoneState utcToLocal(qint64 utcMillis); QString localTimeAbbbreviationAt(qint64 local, QDateTimePrivate::DaylightStatus dst); |