6.0.0-rc4Source

QTimeZone Class

The QTimeZone class identifies how a time representation relates to UTC. More…

Header#include <QTimeZone>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
Since5.2

List of all members, including inherited members

Public Types

Public Functions

QTimeZone()
QTimeZone(Initialization spec)
QTimeZone(const QByteArray &ianaId)
QTimeZone(int offsetSeconds)
QTimeZone(const QByteArray &zoneId, int offsetSeconds, const QString &name, const QString &abbreviation, QLocale::Territory territory = QLocale::AnyTerritory, const QString &comment = QString())
QTimeZone(const QTimeZone &other)
QTimeZone(QTimeZone &&other)
~QTimeZone()
QTimeZone &operator=(const QTimeZone &other)
QTimeZone &operator=(QTimeZone &&other)
voidswap(QTimeZone &other)
boolisValid() const
QByteArrayid() const
QLocale::Territoryterritory() const
QLocale::Countrycountry() const
QStringcomment() const
QStringdisplayName(const QDateTime &atDateTime, NameType nameType = DefaultName, const QLocale &locale = QLocale()) const
QStringdisplayName(TimeType timeType, NameType nameType = DefaultName, const QLocale &locale = QLocale()) const
QStringabbreviation(const QDateTime &atDateTime) const
intoffsetFromUtc(const QDateTime &atDateTime) const
intstandardTimeOffset(const QDateTime &atDateTime) const
intdaylightTimeOffset(const QDateTime &atDateTime) const
boolhasDaylightTime() const
boolisDaylightTime(const QDateTime &atDateTime) const
OffsetDataoffsetData(const QDateTime &forDateTime) const
boolhasTransitions() const
OffsetDatanextTransition(const QDateTime &afterDateTime) const
OffsetDatapreviousTransition(const QDateTime &beforeDateTime) const
OffsetDataListtransitions(const QDateTime &fromDateTime, const QDateTime &toDateTime) const
Qt::TimeSpectimeSpec() const
intfixedSecondsAheadOfUtc() const
boolisUtcOrFixedOffset() const

Static Public Members

Detailed Description

A QTimeZone is UTC, a fixed offset from UTC, local time, or a zone of the IANA time zone database such as "Europe/Berlin", with its history of offset changes and daylight-saving rules. Use it with QDateTime to represent and convert times in that zone:

const QTimeZone berlin("Europe/Berlin");
QDateTime meeting(QDate(2024, 7, 1), QTime(15, 0), berlin);
meeting.offsetFromUtc();                      // 7200
meeting.toTimeZone(QTimeZone("America/New_York")).time(); // 09:00

See also QDateTime.

Differences from Qt

Zones are read from the TZif files of the system's time zone database (/usr/share/zoneinfo, or the directory in TZDIR), including the POSIX rule that continues each zone after its last transition. There is no ICU or CLDR data, so displayName() gives abbreviations and offsets, not localized names such as "Central European Standard Time", and there are no Windows zone ID conversions. availableTimeZoneIds() lists the zones of tzdata.zi or zone1970.tab.

On Zephyr the system time zone is UTC, and zones other than UTC and fixed offsets need a time zone database on a mounted file system, named by TZDIR. See Nt on Zephyr.

Member Type Documentation

enum QTimeZone::Initialization

ConstantDescription
QTimeZone::LocalTimeThe local time of the system.
QTimeZone::UTCCoordinated Universal Time.

This function was introduced in 6.5.

enum QTimeZone::NameType

ConstantDescription
QTimeZone::DefaultNameThe abbreviation if one is known, otherwise the offset name.
QTimeZone::LongNameThe long name. Nt has no localized names, so this is the custom name of the zone or else its IANA ID.
QTimeZone::ShortNameThe abbreviation, such as "CEST".
QTimeZone::OffsetNameThe offset, such as "UTC+02:00".

typedef QTimeZone::OffsetDataList

A list of OffsetData, as returned by transitions().

enum QTimeZone::TimeType

ConstantDescription
QTimeZone::StandardTimeThe standard time of the zone.
QTimeZone::DaylightTimeThe daylight-saving time of the zone.
QTimeZone::GenericTimeThe time of the zone without regard to daylight saving.

Member Function Documentation

QTimeZone::QTimeZone()

Constructs an invalid time zone.

See also isValid().

QTimeZone::QTimeZone(Initialization spec)

Constructs the time zone spec: local time or UTC.

This function was introduced in 6.5.

[explicit] QTimeZone::QTimeZone(const QByteArray &ianaId)

Constructs the time zone with the IANA ID ianaId, such as "Europe/Berlin". The IDs "UTC" and "UTC+hh:mm" give UTC and fixed offsets, and a POSIX TZ rule such as "CET-1CEST,M3.5.0,M10.5.0/3" is accepted too. The zone is invalid if the ID is not available.

See also isTimeZoneIdAvailable() and availableTimeZoneIds().

[explicit] QTimeZone::QTimeZone(int offsetSeconds)

Constructs a time zone with the fixed offset offsetSeconds from UTC. Its ID is "UTC+hh:mm", or "UTC" for an offset of 0. The zone is invalid if the offset is outside MinUtcOffsetSecs to MaxUtcOffsetSecs.

See also fromSecondsAheadOfUtc().

QTimeZone::QTimeZone(const QByteArray &zoneId, int offsetSeconds, const QString &name, const QString &abbreviation, QLocale::Territory territory = QLocale::AnyTerritory, const QString &comment = QString())

Constructs a custom time zone with the ID zoneId and the fixed offset offsetSeconds, the display name name, the abbreviation abbreviation, the territory territory and the comment comment.

QTimeZone::QTimeZone(const QTimeZone &other)

Constructs a copy of other.

QTimeZone::QTimeZone(QTimeZone &&other)

Move-constructs a time zone from other, which becomes invalid.

QTimeZone::~QTimeZone()

Destroys the time zone.

QString QTimeZone::abbreviation(const QDateTime &atDateTime) const

Returns the abbreviation of the zone at atDateTime, such as "CEST".

[static] QList<QByteArray> QTimeZone::availableTimeZoneIds()

Returns the IDs of all zones of the time zone database, sorted, including "UTC".

[static] QList<QByteArray> QTimeZone::availableTimeZoneIds(QLocale::Territory territory)

This is an overloaded function.

Returns the IDs of the zones listed for territory in zone.tab.

[static] QList<QByteArray> QTimeZone::availableTimeZoneIds(int offsetSeconds)

This is an overloaded function.

Returns the IDs of the zones whose standard time is currently offsetSeconds ahead of UTC.

QString QTimeZone::comment() const

Returns the comment of the zone from zone.tab, such as "most of Spain", or an empty string.

QLocale::Country QTimeZone::country() const

Same as territory().

int QTimeZone::daylightTimeOffset(const QDateTime &atDateTime) const

Returns the daylight-saving offset at atDateTime in seconds, or 0 if standard time is in effect.

QString QTimeZone::displayName(const QDateTime &atDateTime, NameType nameType = DefaultName, const QLocale &locale = QLocale()) const

Returns the name of the zone at atDateTime in the form nameType. The locale is accepted for compatibility; names are not localized.

See also abbreviation().

QString QTimeZone::displayName(TimeType timeType, NameType nameType = DefaultName, const QLocale &locale = QLocale()) const

This is an overloaded function.

Returns the name of the standard, daylight or generic time of the zone, timeType, in the form nameType, as currently in effect. The locale is accepted for compatibility.

int QTimeZone::fixedSecondsAheadOfUtc() const

Returns the offset of a UTC or fixed-offset zone, or 0 for other zones.

This function was introduced in 6.5.

[static] QTimeZone QTimeZone::fromSecondsAheadOfUtc(int offset)

Returns a zone with the fixed offset offset from UTC, in seconds.

This function was introduced in 6.5.

bool QTimeZone::hasDaylightTime() const

Returns true if the zone has or had daylight-saving time; otherwise returns false.

bool QTimeZone::hasTransitions() const

Returns true if the zone has transitions, that is, if its offset ever changes; otherwise returns false.

QByteArray QTimeZone::id() const

Returns the ID of the time zone: the IANA ID, "UTC", "UTC+hh:mm" for a fixed offset, or the ID of the system zone for local time. Returns an empty byte array for an invalid zone.

bool QTimeZone::isDaylightTime(const QDateTime &atDateTime) const

Returns true if daylight-saving time is in effect at atDateTime; otherwise returns false.

[static] bool QTimeZone::isTimeZoneIdAvailable(const QByteArray &ianaId)

Returns true if a zone with the ID ianaId can be created; otherwise returns false.

bool QTimeZone::isUtcOrFixedOffset() const

Returns true if the zone is UTC or a fixed offset; otherwise returns false.

This function was introduced in 6.5.

bool QTimeZone::isValid() const

Returns true if the time zone is valid; otherwise returns false.

OffsetData QTimeZone::nextTransition(const QDateTime &afterDateTime) const

Returns the first transition after afterDateTime, or an OffsetData with an invalid atUtc if there is none.

See also previousTransition() and transitions().

OffsetData QTimeZone::offsetData(const QDateTime &forDateTime) const

Returns the offsets of the zone at forDateTime.

int QTimeZone::offsetFromUtc(const QDateTime &atDateTime) const

Returns the offset from UTC of the zone at atDateTime, in seconds, including any daylight-saving offset.

See also standardTimeOffset() and daylightTimeOffset().

QTimeZone & QTimeZone::operator=(const QTimeZone &other)

Assigns other to this time zone and returns a reference to it.

QTimeZone & QTimeZone::operator=(QTimeZone &&other)

Move-assigns other to this time zone and returns a reference to it.

OffsetData QTimeZone::previousTransition(const QDateTime &beforeDateTime) const

Returns the last transition before beforeDateTime, or an OffsetData with an invalid atUtc if there is none.

See also nextTransition().

int QTimeZone::standardTimeOffset(const QDateTime &atDateTime) const

Returns the offset of standard time from UTC at atDateTime, in seconds.

void QTimeZone::swap(QTimeZone &other)

Swaps this zone with other.

[static] QTimeZone QTimeZone::systemTimeZone()

Returns the time zone of the system, as a zone with the system's IANA ID.

See also systemTimeZoneId().

[static] QByteArray QTimeZone::systemTimeZoneId()

Returns the IANA ID of the system time zone: from the TZ environment variable, /etc/timezone or the target of /etc/localtime, or "UTC".

QLocale::Territory QTimeZone::territory() const

Returns the territory of the zone, as listed in the zone.tab file of the time zone database, or QLocale::AnyTerritory.

This function was introduced in 6.2.

Qt::TimeSpec QTimeZone::timeSpec() const

Returns the time specification of the zone: Qt::LocalTime, Qt::UTC, Qt::OffsetFromUTC for a fixed offset, or Qt::TimeZone.

This function was introduced in 6.5.

OffsetDataList QTimeZone::transitions(const QDateTime &fromDateTime, const QDateTime &toDateTime) const

Returns the transitions between fromDateTime and toDateTime, both included.

[static] QTimeZone QTimeZone::utc()

Returns the UTC time zone.

Member Variable Documentation

[static, constexpr] int QTimeZone::MaxUtcOffsetSecs

The largest supported offset from UTC, +16 hours.

[static, constexpr] int QTimeZone::MinUtcOffsetSecs

The smallest supported offset from UTC, -16 hours.