QTimeZone Class
The QTimeZone class identifies how a time representation relates to UTC. More…
| Header | #include <QTimeZone> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Since | 5.2 |
List of all members, including inherited members
Public Types
| enum | Initialization |
| enum | TimeType |
| enum | NameType |
| typedef | OffsetDataList |
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) |
| void | swap(QTimeZone &other) |
| bool | isValid() const |
| QByteArray | id() const |
| QLocale::Territory | territory() const |
| QLocale::Country | country() const |
| QString | comment() const |
| QString | displayName(const QDateTime &atDateTime, NameType nameType = DefaultName, const QLocale &locale = QLocale()) const |
| QString | displayName(TimeType timeType, NameType nameType = DefaultName, const QLocale &locale = QLocale()) const |
| QString | abbreviation(const QDateTime &atDateTime) const |
| int | offsetFromUtc(const QDateTime &atDateTime) const |
| int | standardTimeOffset(const QDateTime &atDateTime) const |
| int | daylightTimeOffset(const QDateTime &atDateTime) const |
| bool | hasDaylightTime() const |
| bool | isDaylightTime(const QDateTime &atDateTime) const |
| OffsetData | offsetData(const QDateTime &forDateTime) const |
| bool | hasTransitions() const |
| OffsetData | nextTransition(const QDateTime &afterDateTime) const |
| OffsetData | previousTransition(const QDateTime &beforeDateTime) const |
| OffsetDataList | transitions(const QDateTime &fromDateTime, const QDateTime &toDateTime) const |
| Qt::TimeSpec | timeSpec() const |
| int | fixedSecondsAheadOfUtc() const |
| bool | isUtcOrFixedOffset() const |
Static Public Members
| int | MinUtcOffsetSecs |
| int | MaxUtcOffsetSecs |
| QTimeZone | fromSecondsAheadOfUtc(int offset) |
| QTimeZone | utc() |
| QTimeZone | systemTimeZone() |
| QByteArray | systemTimeZoneId() |
| bool | isTimeZoneIdAvailable(const QByteArray &ianaId) |
| QList<QByteArray> | availableTimeZoneIds() |
| QList<QByteArray> | availableTimeZoneIds(QLocale::Territory territory) |
| QList<QByteArray> | availableTimeZoneIds(int offsetSeconds) |
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
| Constant | Description |
|---|---|
QTimeZone::LocalTime | The local time of the system. |
QTimeZone::UTC | Coordinated Universal Time. |
This function was introduced in 6.5.
enum QTimeZone::NameType
| Constant | Description |
|---|---|
QTimeZone::DefaultName | The abbreviation if one is known, otherwise the offset name. |
QTimeZone::LongName | The long name. Nt has no localized names, so this is the custom name of the zone or else its IANA ID. |
QTimeZone::ShortName | The abbreviation, such as "CEST". |
QTimeZone::OffsetName | The offset, such as "UTC+02:00". |
typedef QTimeZone::OffsetDataList
A list of OffsetData, as returned by transitions().
enum QTimeZone::TimeType
| Constant | Description |
|---|---|
QTimeZone::StandardTime | The standard time of the zone. |
QTimeZone::DaylightTime | The daylight-saving time of the zone. |
QTimeZone::GenericTime | The 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.
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().
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".
QList<QByteArray> QTimeZone::availableTimeZoneIds()
Returns the IDs of all zones of the time zone database, sorted, including "UTC".
QList<QByteArray> QTimeZone::availableTimeZoneIds(QLocale::Territory territory)
This is an overloaded function.
Returns the IDs of the zones listed for territory in zone.tab.
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.
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.
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.
QTimeZone QTimeZone::systemTimeZone()
Returns the time zone of the system, as a zone with the system's IANA ID.
See also systemTimeZoneId().
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.
QTimeZone QTimeZone::utc()
Returns the UTC time zone.
Member Variable Documentation
int QTimeZone::MaxUtcOffsetSecs
The largest supported offset from UTC, +16 hours.
int QTimeZone::MinUtcOffsetSecs
The smallest supported offset from UTC, -16 hours.