From 1119cd4ece6555be82212b273d05d581bffb79d1 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 21 Mar 2019 16:10:09 +0100 Subject: Correct a misguided assertion in QTzTimeZonePrivate Without ICU, the TZ-DB backend for time-zones tripped over an assertion when running tst_QTimeZone::stressTest(), which happened to probe a zone between its last transition and the first transition of a POSIX rule that followed it. The code assumed there was no interval between these two; apparently, there can be. Change-Id: I3d0ad41fec0a255db2f9bfac54d33aa9b83938e8 Reviewed-by: Thiago Macieira --- src/corelib/tools/qtimezoneprivate_tz.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/corelib/tools/qtimezoneprivate_tz.cpp') diff --git a/src/corelib/tools/qtimezoneprivate_tz.cpp b/src/corelib/tools/qtimezoneprivate_tz.cpp index bed62a02bd..fa4ec031fa 100644 --- a/src/corelib/tools/qtimezoneprivate_tz.cpp +++ b/src/corelib/tools/qtimezoneprivate_tz.cpp @@ -1022,8 +1022,10 @@ QTimeZonePrivate::Data QTzTimeZonePrivate::previousTransition(qint64 beforeMSecs [beforeMSecsSinceEpoch] (const QTimeZonePrivate::Data &at) { return at.atMSecsSinceEpoch < beforeMSecsSinceEpoch; }); - Q_ASSERT(it > posixTrans.cbegin()); - return *--it; + if (it > posixTrans.cbegin()) + return *--it; + // else: it fell between the last transition and the first of the POSIX rule. + return dataForTzTransition(m_tranTimes.last()); } // Otherwise if we can find a valid tran then use its rule -- cgit v1.2.3 From 03fadc26e7617aece89949bc7d0acf50f6f050a9 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 21 Mar 2019 15:07:48 +0100 Subject: Fix broken data for time-zones with no transitions While an invalid time-zone shall have no transitions, so may various constant zones, like UTC. The TZ data may include only the POSIX rule for such a zone, in which case we should use it, even if there are no transitions. Broke out a piece of repeated code as a common method, in the process, since I was complicating it further. Added test for the case that revealed this; and made sure we see a warning if any of the checkOffset() tests gets skipped because its zone is unsupported. Fixes: QTBUG-74614 Change-Id: Ic8e039a2a9b3f4e0f567585682a94f4b494b558d Reviewed-by: Thiago Macieira --- src/corelib/tools/qtimezoneprivate_tz.cpp | 46 +++++++++++++++---------------- 1 file changed, 22 insertions(+), 24 deletions(-) (limited to 'src/corelib/tools/qtimezoneprivate_tz.cpp') diff --git a/src/corelib/tools/qtimezoneprivate_tz.cpp b/src/corelib/tools/qtimezoneprivate_tz.cpp index fa4ec031fa..f75a61977d 100644 --- a/src/corelib/tools/qtimezoneprivate_tz.cpp +++ b/src/corelib/tools/qtimezoneprivate_tz.cpp @@ -943,19 +943,21 @@ QTimeZonePrivate::Data QTzTimeZonePrivate::dataForTzTransition(QTzTransitionTime return data; } -QTimeZonePrivate::Data QTzTimeZonePrivate::data(qint64 forMSecsSinceEpoch) const +QVector QTzTimeZonePrivate::getPosixTransitions(qint64 msNear) const { - // If we have no rules (so probably an invalid tz), return invalid data: - if (!m_tranTimes.size()) - return invalidData(); + const int year = QDateTime::fromMSecsSinceEpoch(msNear, Qt::UTC).date().year(); + // The Data::atMSecsSinceEpoch of the single entry if zone is constant: + qint64 atTime = m_tranTimes.isEmpty() ? msNear : m_tranTimes.last().atMSecsSinceEpoch; + return calculatePosixTransitions(m_posixRule, year - 1, year + 1, atTime); +} - // If the required time is after the last transition and we have a POSIX rule then use it - if (m_tranTimes.last().atMSecsSinceEpoch < forMSecsSinceEpoch +QTimeZonePrivate::Data QTzTimeZonePrivate::data(qint64 forMSecsSinceEpoch) const +{ + // If the required time is after the last transition (or there were none) + // and we have a POSIX rule then use it: + if ((m_tranTimes.isEmpty() || m_tranTimes.last().atMSecsSinceEpoch < forMSecsSinceEpoch) && !m_posixRule.isEmpty() && forMSecsSinceEpoch >= 0) { - const int year = QDateTime::fromMSecsSinceEpoch(forMSecsSinceEpoch, Qt::UTC).date().year(); - QVector posixTrans = - calculatePosixTransitions(m_posixRule, year - 1, year + 1, - m_tranTimes.last().atMSecsSinceEpoch); + QVector posixTrans = getPosixTransitions(forMSecsSinceEpoch); auto it = std::partition_point(posixTrans.cbegin(), posixTrans.cend(), [forMSecsSinceEpoch] (const QTimeZonePrivate::Data &at) { return at.atMSecsSinceEpoch <= forMSecsSinceEpoch; @@ -986,13 +988,11 @@ bool QTzTimeZonePrivate::hasTransitions() const QTimeZonePrivate::Data QTzTimeZonePrivate::nextTransition(qint64 afterMSecsSinceEpoch) const { - // If the required time is after the last transition and we have a POSIX rule then use it - if (m_tranTimes.size() > 0 && m_tranTimes.last().atMSecsSinceEpoch < afterMSecsSinceEpoch + // If the required time is after the last transition (or there were none) + // and we have a POSIX rule then use it: + if ((m_tranTimes.isEmpty() || m_tranTimes.last().atMSecsSinceEpoch < afterMSecsSinceEpoch) && !m_posixRule.isEmpty() && afterMSecsSinceEpoch >= 0) { - const int year = QDateTime::fromMSecsSinceEpoch(afterMSecsSinceEpoch, Qt::UTC).date().year(); - QVector posixTrans = - calculatePosixTransitions(m_posixRule, year - 1, year + 1, - m_tranTimes.last().atMSecsSinceEpoch); + QVector posixTrans = getPosixTransitions(afterMSecsSinceEpoch); auto it = std::partition_point(posixTrans.cbegin(), posixTrans.cend(), [afterMSecsSinceEpoch] (const QTimeZonePrivate::Data &at) { return at.atMSecsSinceEpoch <= afterMSecsSinceEpoch; @@ -1011,21 +1011,19 @@ QTimeZonePrivate::Data QTzTimeZonePrivate::nextTransition(qint64 afterMSecsSince QTimeZonePrivate::Data QTzTimeZonePrivate::previousTransition(qint64 beforeMSecsSinceEpoch) const { - // If the required time is after the last transition and we have a POSIX rule then use it - if (m_tranTimes.size() > 0 && m_tranTimes.last().atMSecsSinceEpoch < beforeMSecsSinceEpoch + // If the required time is after the last transition (or there were none) + // and we have a POSIX rule then use it: + if ((m_tranTimes.isEmpty() || m_tranTimes.last().atMSecsSinceEpoch < beforeMSecsSinceEpoch) && !m_posixRule.isEmpty() && beforeMSecsSinceEpoch > 0) { - const int year = QDateTime::fromMSecsSinceEpoch(beforeMSecsSinceEpoch, Qt::UTC).date().year(); - QVector posixTrans = - calculatePosixTransitions(m_posixRule, year - 1, year + 1, - m_tranTimes.last().atMSecsSinceEpoch); + QVector posixTrans = getPosixTransitions(beforeMSecsSinceEpoch); auto it = std::partition_point(posixTrans.cbegin(), posixTrans.cend(), [beforeMSecsSinceEpoch] (const QTimeZonePrivate::Data &at) { return at.atMSecsSinceEpoch < beforeMSecsSinceEpoch; }); if (it > posixTrans.cbegin()) return *--it; - // else: it fell between the last transition and the first of the POSIX rule. - return dataForTzTransition(m_tranTimes.last()); + // It fell between the last transition (if any) and the first of the POSIX rule: + return m_tranTimes.isEmpty() ? invalidData() : dataForTzTransition(m_tranTimes.last()); } // Otherwise if we can find a valid tran then use its rule -- cgit v1.2.3