Harmonize zoneMSecsToEpochMSecs() and localMSecsToEpochMSecs()

Now that the latter calls the former in its fall-back, this saves that
call the need to follow up with determining DST and abbreviation. This
also lets refreshZonedDateTime() update its DST guess for timezones as
well as for local time.

Change-Id: I820b65c1d6db78619defe2af5e947cb98ae336f0
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Edward Welbourne 2021-02-26 14:58:38 +01:00
parent 35412acd88
commit fc6629b6bb
2 changed files with 27 additions and 26 deletions

View File

@ -2788,18 +2788,8 @@ qint64 QDateTimePrivate::localMSecsToEpochMSecs(qint64 localMsecs,
// Use the system zone:
const auto sys = QTimeZone::systemTimeZone();
if (sys.isValid()) {
const qint64 utcMsecs =
QDateTimePrivate::zoneMSecsToEpochMSecs(localMsecs, sys,
QDateTimePrivate::UnknownDaylightTime,
localDate, localTime);
if (abbreviation && sys.isValid())
*abbreviation = sys.d->abbreviation(utcMsecs);
if (daylightStatus) {
*daylightStatus = sys.isValid() && sys.d->isDaylightTime(utcMsecs)
? QDateTimePrivate::DaylightTime
: QDateTimePrivate::StandardTime;
}
return utcMsecs;
return QDateTimePrivate::zoneMSecsToEpochMSecs(localMsecs, sys, daylightStatus,
localDate, localTime, abbreviation);
}
#endif // timezone
// Kludge
@ -2964,7 +2954,7 @@ static void refreshZonedDateTime(QDateTimeData &d, Qt::TimeSpec spec)
// else spec == Qt::TimeZone, so check zone is valid:
} else if (d->m_timeZone.isValid()) {
epochMSecs = QDateTimePrivate::zoneMSecsToEpochMSecs(
msecs, d->m_timeZone, dstStatus, &testDate, &testTime);
msecs, d->m_timeZone, &dstStatus, &testDate, &testTime);
#endif // timezone
} // else: testDate, testTime haven't been set, so are invalid.
const bool ok = testDate.isValid() && testTime.isValid();
@ -3301,13 +3291,19 @@ inline QDateTime::Data QDateTimePrivate::create(QDate toDate, QTime toTime,
// Convert a TimeZone time expressed in zone msecs encoding into a UTC epoch msecs
// DST transitions are disambiguated by hint.
inline qint64 QDateTimePrivate::zoneMSecsToEpochMSecs(qint64 zoneMSecs, const QTimeZone &zone,
DaylightStatus hint,
QDate *zoneDate, QTime *zoneTime)
DaylightStatus *hint,
QDate *zoneDate, QTime *zoneTime,
QString *abbreviation)
{
Q_ASSERT(zone.isValid());
// Get the effective data from QTimeZone
QTimeZonePrivate::Data data = zone.d->dataForLocalTime(zoneMSecs, int(hint));
DaylightStatus dst = hint ? *hint : UnknownDaylightTime;
QTimeZonePrivate::Data data = zone.d->dataForLocalTime(zoneMSecs, int(dst));
if (data.offsetFromUtc == QTimeZonePrivate::invalidSeconds()) {
if (hint)
*hint = QDateTimePrivate::UnknownDaylightTime;
if (abbreviation)
*abbreviation = QString();
if (zoneDate)
*zoneDate = QDate();
if (zoneTime)
@ -3325,6 +3321,13 @@ inline qint64 QDateTimePrivate::zoneMSecsToEpochMSecs(qint64 zoneMSecs, const QT
})((zoneMSecs - data.atMSecsSinceEpoch) / MSECS_PER_SEC));
msecsToTime(data.atMSecsSinceEpoch + data.offsetFromUtc * MSECS_PER_SEC,
zoneDate, zoneTime);
if (hint) {
*hint = data.daylightTimeOffset
? QDateTimePrivate::DaylightTime
: QDateTimePrivate::StandardTime;
}
if (abbreviation)
*abbreviation = data.abbreviation;
}
return data.atMSecsSinceEpoch;
}
@ -4246,10 +4249,9 @@ static inline void massageAdjustedDateTime(QDateTimeData &d, QDate date, QTime t
QDateTimePrivate::localMSecsToEpochMSecs(timeToMSecs(date, time), &status, &date, &time);
#if QT_CONFIG(timezone)
} else if (spec == Qt::TimeZone && d->m_timeZone.isValid()) {
QDateTimePrivate::DaylightStatus status = QDateTimePrivate::UnknownDaylightTime;
QDateTimePrivate::zoneMSecsToEpochMSecs(timeToMSecs(date, time),
d->m_timeZone,
QDateTimePrivate::UnknownDaylightTime,
&date, &time);
d->m_timeZone, &status, &date, &time);
#endif // timezone
}
setDateTime(d, date, time);

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Copyright (C) 2021 The Qt Company Ltd.
** Copyright (C) 2016 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
@ -116,6 +116,11 @@ public:
#if QT_CONFIG(timezone)
static QDateTime::Data create(QDate toDate, QTime toTime, const QTimeZone & timeZone);
static qint64 zoneMSecsToEpochMSecs(qint64 msecs, const QTimeZone &zone,
DaylightStatus *hint = nullptr,
QDate *localDate = nullptr, QTime *localTime = nullptr,
QString *abbreviation = nullptr);
#endif // timezone
static bool epochMSecsToLocalTime(qint64 msecs, QDate *localDate, QTime *localTime,
@ -131,12 +136,6 @@ public:
#if QT_CONFIG(timezone)
QTimeZone m_timeZone;
#endif // timezone
#if QT_CONFIG(timezone)
static qint64 zoneMSecsToEpochMSecs(qint64 msecs, const QTimeZone &zone,
DaylightStatus hint = UnknownDaylightTime,
QDate *localDate = nullptr, QTime *localTime = nullptr);
#endif // timezone
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QDateTimePrivate::StatusFlags)