QTimeZone Class

The QTimeZone class converts between UTC and local time in a specific time zone. More...

Header: #include <QTimeZone>
qmake: QT += core
Since: Qt 5.2

This class was introduced in Qt 5.2.

Note: All functions in this class are thread-safe.

Public Types

struct OffsetData
enum NameType { DefaultName, LongName, ShortName, OffsetName }
typedef OffsetDataList
enum TimeType { StandardTime, DaylightTime, GenericTime }
enum anonymous { MinUtcOffsetSecs, MaxUtcOffsetSecs }

Detailed Description

This class provides a stateless calculator for time zone conversions between UTC and the local time in a specific time zone. By default it uses the host system time zone data to perform these conversions.

This class is primarily designed for use in QDateTime; most applications will not need to access this class directly and should instead use QDateTime with a Qt::TimeSpec of Qt::TimeZone.

Note: For consistency with QDateTime, QTimeZone does not account for leap seconds.

Remarks

IANA Time Zone IDs

QTimeZone uses the IANA time zone IDs as defined in the IANA Time Zone Database (http://www.iana.org/time-zones). This is to ensure a standard ID across all supported platforms. Most platforms support the IANA IDs and the IANA Database natively, but for Windows a mapping is required to the native IDs. See below for more details.

The IANA IDs can and do change on a regular basis, and can vary depending on how recently the host system data was updated. As such you cannot rely on any given ID existing on any host system. You must use availableTimeZoneIds() to determine what IANA IDs are available.

The IANA IDs and database are also know as the Olson IDs and database, named after their creator.

UTC Offset Time Zones

A default UTC time zone backend is provided which is always guaranteed to be available. This provides a set of generic Offset From UTC time zones in the range UTC-14:00 to UTC+14:00. These time zones can be created using either the standard ISO format names "UTC+00:00" as listed by availableTimeZoneIds(), or using the number of offset seconds.

Windows Time Zones

Windows native time zone support is severely limited compared to the standard IANA TZ Database. Windows time zones cover larger geographic areas and are thus less accurate in their conversions. They also do not support as much historic conversion data and so may only be accurate for the current year.

QTimeZone uses a conversion table derived form the Unicode CLDR data to map between IANA IDs and Windows IDs. Depending on your version of Windows and Qt, this table may not be able to provide a valid conversion, in which "UTC" will be returned.

QTimeZone provides a public API to use this conversion table. The Windows ID used is the Windows Registry Key for the time zone which is also the MS Exchange EWS ID as well, but is different to the Time Zone Name (TZID) and COD code used by MS Exchange in versions before 2007.

System Time Zone

QTimeZone does not support any concept of a system or default time zone. If you require a QDateTime that uses the current system time zone at any given moment then you should use a Qt::TimeSpec of Qt::LocalTime.

The method systemTimeZoneId() returns the current system IANA time zone ID which on Unix-like systems will always be correct. On Windows this ID is translated from the Windows system ID using an internal translation table and the user's selected country. As a consequence there is a small chance any Windows install may have IDs not known by Qt, in which case "UTC" will be returned.

Creating a new QTimeZone instance using the system time zone ID will only produce a fixed named copy of the time zone, it will not change if the system time zone changes.

Time Zone Offsets

The difference between UTC and the local time in a time zone is expressed as an offset in seconds from UTC, i.e. the number of seconds to add to UTC to obtain the local time. The total offset is comprised of two component parts, the standard time offset and the daylight-saving time offset. The standard time offset is the number of seconds to add to UTC to obtain standard time in the time zone. The daylight-saving time offset is the number of seconds to add to the standard time offset to obtain daylight-saving time (abbreviated DST and sometimes called "daylight time" or "summer time") in the time zone.

Note that the standard and DST offsets for a time zone may change over time as countries have changed DST laws or even their standard time offset.

License

This class includes data obtained from the CLDR data files under the terms of the Unicode Data Files and Software License. See Unicode Common Locale Data Repository (CLDR) for details.

See also QDateTime.

Member Type Documentation

enum QTimeZone::NameType

The type of time zone name.

ConstantValueDescription
QTimeZone::DefaultName0The default form of the time zone name, e.g. LongName, ShortName or OffsetName
QTimeZone::LongName1The long form of the time zone name, e.g. "Central European Time"
QTimeZone::ShortName2The short form of the time zone name, usually an abbreviation, e.g. "CET"
QTimeZone::OffsetName3The standard ISO offset form of the time zone name, e.g. "UTC+01:00"

typedef QTimeZone::OffsetDataList

Synonym for QVector<OffsetData>.

enum QTimeZone::TimeType

The type of time zone time, for example when requesting the name. In time zones that do not apply DST, all three values may return the same result.

ConstantValueDescription
QTimeZone::StandardTime0The standard time in a time zone, i.e. when Daylight-Saving is not in effect. For example when formatting a display name this will show something like "Pacific Standard Time".
QTimeZone::DaylightTime1A time when Daylight-Saving is in effect. For example when formatting a display name this will show something like "Pacific daylight-saving time".
QTimeZone::GenericTime2A time which is not specifically Standard or Daylight-Saving time, either an unknown time or a neutral form. For example when formatting a display name this will show something like "Pacific Time".

enum QTimeZone::anonymous

Sane UTC offsets range from -14 to +14 hours. No known zone > 12 hrs West of Greenwich (Baker Island, USA). No known zone > 14 hrs East of Greenwich (Kiritimati, Christmas Island, Kiribati).

ConstantValueDescription
QTimeZone::MinUtcOffsetSecs-14 * 3600-14 * 3600,
QTimeZone::MaxUtcOffsetSecs+14 * 3600+14 * 3600