From 536b918ecaed0b8a04ca2b7c0884eea12ddb6931 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Tue, 16 May 2017 16:43:42 +0200 Subject: QTimeZone: make UTC offset range available for other code to reuse When QDateTimeParser learns to parse zones, it'll need to know the valid range of offsets. Change-Id: I44cd88a140ebaf6a2b98b0f9a1be0cbc7a35bae4 Reviewed-by: Thiago Macieira --- src/corelib/tools/qtimezone.cpp | 7 ++----- src/corelib/tools/qtimezone.h | 8 ++++++++ src/corelib/tools/qtimezoneprivate.cpp | 2 ++ 3 files changed, 12 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/corelib/tools/qtimezone.cpp b/src/corelib/tools/qtimezone.cpp index ec2f7c4af6..0b331be503 100644 --- a/src/corelib/tools/qtimezone.cpp +++ b/src/corelib/tools/qtimezone.cpp @@ -338,12 +338,9 @@ QTimeZone::QTimeZone(const QByteArray &ianaId) */ QTimeZone::QTimeZone(int offsetSeconds) + : d((offsetSeconds >= MinUtcOffsetSecs && offsetSeconds <= MaxUtcOffsetSecs) + ? new QUtcTimeZonePrivate(offsetSeconds) : nullptr) { - // offsetSeconds must fall between -14:00 and +14:00 hours - if (offsetSeconds >= -50400 && offsetSeconds <= 50400) - d = new QUtcTimeZonePrivate(offsetSeconds); - else - d = 0; } /*! diff --git a/src/corelib/tools/qtimezone.h b/src/corelib/tools/qtimezone.h index bd87139f5b..29d8a4dbe5 100644 --- a/src/corelib/tools/qtimezone.h +++ b/src/corelib/tools/qtimezone.h @@ -59,6 +59,14 @@ class QTimeZonePrivate; class Q_CORE_EXPORT QTimeZone { public: + // Sane UTC offsets range from -14 to +14 hours: + enum { + // No known zone > 12 hrs West of Greenwich (Baker Island, USA) + MinUtcOffsetSecs = -14 * 3600, + // No known zone > 14 hrs East of Greenwich (Kiritimati, Christmas Island, Kiribati) + MaxUtcOffsetSecs = +14 * 3600 + }; + enum TimeType { StandardTime = 0, DaylightTime = 1, diff --git a/src/corelib/tools/qtimezoneprivate.cpp b/src/corelib/tools/qtimezoneprivate.cpp index 7b780ecf7d..e45dd48de0 100644 --- a/src/corelib/tools/qtimezoneprivate.cpp +++ b/src/corelib/tools/qtimezoneprivate.cpp @@ -258,6 +258,8 @@ QTimeZonePrivate::Data QTimeZonePrivate::dataForLocalTime(qint64 forLocalMSecs, brackets the correct time and at most one DST transition. */ const qint64 sixteenHoursInMSecs(16 * 3600 * 1000); + Q_STATIC_ASSERT(-sixteenHoursInMSecs / 1000 < QTimeZone::MinUtcOffsetSecs + && sixteenHoursInMSecs / 1000 > QTimeZone::MaxUtcOffsetSecs); /* Offsets are Local - UTC, positive to the east of Greenwich, negative to the west; DST offset always exceeds standard offset, when DST applies. -- cgit v1.2.3