Change QTimeZone's offset range into constants, not an enum

Use static constexpr int values instead of abusing enum.

[ChangeLog][QtCore][QTimeZone] The MinUtcOffsetSecs and
MaxUtcOffsetSecs constants are now static constexpr members of
QTimeZone, rather than members of an anonymous enum. Their values are
now 16 hours either side of zero, to allow for some historical zones.

Change-Id: I1c3a0f85a2b83b5010f021ca0f5ca5baefbf32e4
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Venugopal Shivashankar <Venugopal.Shivashankar@qt.io>
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
This commit is contained in:
Edward Welbourne 2023-03-28 11:21:31 +02:00
parent 127e33d4c6
commit 5dabac2c9c
3 changed files with 38 additions and 33 deletions

View File

@ -254,31 +254,34 @@ Q_GLOBAL_STATIC(QTimeZoneSingleton, global_tz);
*/ */
/*! /*!
\enum QTimeZone::anonymous \variable QTimeZone::MinUtcOffsetSecs
\brief Timezone offsets from UTC are expected to be no lower than this.
This enumeration provides constants bounding the range of plausible timezone The lowest UTC offset of any early 21st century timezone is -12 hours (Baker
offsets from UTC, measured in seconds. Island, USA), or 12 hours west of Greenwich.
Sane modern zones' UTC offsets range from -14 to +12 hours. Historically, until 1844, The Philippines (then controlled by Spain) used
No known modern zone has offset > 12 hrs West of Greenwich (Baker Island, USA). the same date as Spain's American holdings, so had offsets close to 16 hours
No known modern zone has offset > 14 hrs East of Greenwich west of Greenwich. As The Philippines was using local solar mean time, it is
(Kiritimati, Christmas Island, Kiribati). possible some outlying territory of it may have been operating at more than
Note that there are zones whose offsets differ by more than a day. 16 hours west of Greenwich, but no early 21st century timezone traces its
history back to such an extreme.
Historically, before 1867, when Russia sold Alaska to America, Alaska used the \sa MaxUtcOffsetSecs
same date as Russia, so had offsets over 15 hours East of Greenwich. */
Earlier still, until 1844, The Philippines (then controlled by Spain) used the /*!
same date as Spain's American holdings, so had offsets close to 16 hours West \variable QTimeZone::MaxUtcOffsetSecs
of Greenwich. \brief Timezone offsets from UTC are expected to be no higher than this.
Each made its one-day transition to cross the international date line while
using local solar mean time, before adopting a unified time-zone, so the
offsets within their territories were variable.
As a result, The Philippines might have exceeded 16 hours as offset at the
extremities of its territory, but no modern zone's representative location was
at such an extremity.
\value MinUtcOffsetSecs -16 * 3600, The highest UTC offset of any early 21st century timezone is +14 hours
\value MaxUtcOffsetSecs +16 * 3600 (Christmas Island, Kiribati, Kiritimati), or 14 hours east of Greenwich.
Historically, before 1867, when Russia sold Alaska to America, Alaska used
the same date as Russia, so had offsets over 15 hours east of Greenwich. As
Alaska was using local solar mean time, its offsets varied, but all were
less than 16 hours east of Greenwich.
\sa MinUtcOffsetSecs
*/ */
#if QT_CONFIG(timezone) #if QT_CONFIG(timezone)
@ -489,6 +492,8 @@ QTimeZone::QTimeZone(const QByteArray &ianaId)
returned instance is equivalent to the lightweight time representation returned instance is equivalent to the lightweight time representation
\c{QTimeZone::fromSecondsAfterUtc(offsetSeconds)}, albeit implemented as a \c{QTimeZone::fromSecondsAfterUtc(offsetSeconds)}, albeit implemented as a
time zone. time zone.
\sa MinUtcOffsetSecs, MaxUtcOffsetSecs
*/ */
QTimeZone::QTimeZone(int offsetSeconds) QTimeZone::QTimeZone(int offsetSeconds)
@ -516,7 +521,8 @@ QTimeZone::QTimeZone(int offsetSeconds)
This constructor is only available when feature \c timezone is enabled. This constructor is only available when feature \c timezone is enabled.
\sa id(), offsetFromUtc(), displayName(), abbreviation(), territory(), comment() \sa id(), offsetFromUtc(), displayName(), abbreviation(), territory(), comment(),
MinUtcOffsetSecs, MaxUtcOffsetSecs
*/ */
QTimeZone::QTimeZone(const QByteArray &ianaId, int offsetSeconds, const QString &name, QTimeZone::QTimeZone(const QByteArray &ianaId, int offsetSeconds, const QString &name,
@ -629,7 +635,8 @@ QTimeZone QTimeZone::asBackendZone() const
Qt::OffsetFromUTC. An invalid time zone, when returned, has Qt::TimeZone as Qt::OffsetFromUTC. An invalid time zone, when returned, has Qt::TimeZone as
its timeSpec(). its timeSpec().
\sa QTimeZone(int), asBackendZone(), fixedSecondsAheadOfUtc() \sa QTimeZone(int), asBackendZone(), fixedSecondsAheadOfUtc(),
MinUtcOffsetSecs, MaxUtcOffsetSecs
*/ */
/*! /*!

View File

@ -83,14 +83,12 @@ class Q_CORE_EXPORT QTimeZone
public: public:
// Sane UTC offsets range from -16 to +16 hours: // Sane UTC offsets range from -16 to +16 hours:
enum { static constexpr int MinUtcOffsetSecs = -16 * 3600;
// No known modern zone > 12 hrs West of Greenwich. // No known modern zone > 12 hrs West of Greenwich.
// Until 1844, Asia/Manila (in The Philippines) was at 15:56 West. // Until 1844, Asia/Manila (in The Philippines) was at 15:56 West.
MinUtcOffsetSecs = -16 * 3600, static constexpr int MaxUtcOffsetSecs = +16 * 3600;
// No known modern zone > 14 hrs East of Greenwich. // No known modern zone > 14 hrs East of Greenwich.
// Until 1867, America/Metlakatla (in Alaska) was at 15:13:42 East. // Until 1867, America/Metlakatla (in Alaska) was at 15:13:42 East.
MaxUtcOffsetSecs = +16 * 3600
};
enum Initialization { LocalTime, UTC }; enum Initialization { LocalTime, UTC };

View File

@ -1163,8 +1163,8 @@ void tst_QTimeZone::utcTest()
QCOMPARE(tz.daylightTimeOffset(now), 0); QCOMPARE(tz.daylightTimeOffset(now), 0);
// Test validity range of UTC offsets: // Test validity range of UTC offsets:
int min = int(QTimeZone::MinUtcOffsetSecs); int min = QTimeZone::MinUtcOffsetSecs;
int max = int(QTimeZone::MaxUtcOffsetSecs); int max = QTimeZone::MaxUtcOffsetSecs;
QCOMPARE(QTimeZone(min - 1).isValid(), false); QCOMPARE(QTimeZone(min - 1).isValid(), false);
QCOMPARE(QTimeZone(min).isValid(), true); QCOMPARE(QTimeZone(min).isValid(), true);
QCOMPARE(QTimeZone(min + 1).isValid(), true); QCOMPARE(QTimeZone(min + 1).isValid(), true);