File indexing completed on 2025-09-17 09:09:36
0001
0002
0003
0004
0005 #ifndef QTIMEZONE_H
0006 #define QTIMEZONE_H
0007
0008 #include <QtCore/qcompare.h>
0009 #include <QtCore/qdatetime.h>
0010 #include <QtCore/qlocale.h>
0011 #include <QtCore/qswap.h>
0012 #include <QtCore/qtclasshelpermacros.h>
0013
0014 #include <chrono>
0015
0016 #if QT_CONFIG(timezone) && (defined(Q_OS_DARWIN) || defined(Q_QDOC))
0017 Q_FORWARD_DECLARE_CF_TYPE(CFTimeZone);
0018 Q_FORWARD_DECLARE_OBJC_CLASS(NSTimeZone);
0019 #endif
0020
0021 QT_BEGIN_NAMESPACE
0022
0023 class QTimeZonePrivate;
0024
0025 class Q_CORE_EXPORT QTimeZone
0026 {
0027 struct ShortData
0028 {
0029 #if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
0030 quintptr mode : 2;
0031 #endif
0032 qintptr offset : sizeof(void *) * 8 - 2;
0033
0034 #if Q_BYTE_ORDER == Q_BIG_ENDIAN
0035 quintptr mode : 2;
0036 #endif
0037
0038
0039
0040
0041
0042 constexpr ShortData(Qt::TimeSpec spec, int secondsAhead = 0)
0043 #if Q_BYTE_ORDER == Q_BIG_ENDIAN
0044 : offset(spec == Qt::OffsetFromUTC ? secondsAhead : 0),
0045 mode((int(spec) + 1) & 3)
0046 #else
0047 : mode((int(spec) + 1) & 3),
0048 offset(spec == Qt::OffsetFromUTC ? secondsAhead : 0)
0049 #endif
0050 {
0051 }
0052 friend constexpr bool operator==(ShortData lhs, ShortData rhs)
0053 { return lhs.mode == rhs.mode && lhs.offset == rhs.offset; }
0054 constexpr Qt::TimeSpec spec() const { return Qt::TimeSpec((mode + 3) & 3); }
0055 };
0056
0057 union Data
0058 {
0059 Data() noexcept;
0060 Data(ShortData sd) : s(sd) {}
0061 Data(const Data &other) noexcept;
0062 Data(Data &&other) noexcept : d(std::exchange(other.d, nullptr)) {}
0063 Data &operator=(const Data &other) noexcept;
0064 Data &operator=(Data &&other) noexcept { swap(other); return *this; }
0065 ~Data();
0066
0067 void swap(Data &other) noexcept { qt_ptr_swap(d, other.d); }
0068
0069 bool isShort() const { return s.mode; }
0070
0071
0072 template <typename Stream, typename Wrap>
0073 void serialize(Stream &out, const Wrap &wrap) const;
0074
0075 Data(QTimeZonePrivate *dptr) noexcept;
0076 Data &operator=(QTimeZonePrivate *dptr) noexcept;
0077 const QTimeZonePrivate *operator->() const { Q_ASSERT(!isShort()); return d; }
0078 QTimeZonePrivate *operator->() { Q_ASSERT(!isShort()); return d; }
0079
0080 QTimeZonePrivate *d = nullptr;
0081 ShortData s;
0082 };
0083 QTimeZone(ShortData sd) : d(sd) {}
0084
0085 public:
0086
0087 static constexpr int MinUtcOffsetSecs = -16 * 3600;
0088
0089
0090 static constexpr int MaxUtcOffsetSecs = +16 * 3600;
0091
0092
0093
0094 enum Initialization { LocalTime, UTC };
0095
0096 QTimeZone() noexcept;
0097 Q_IMPLICIT QTimeZone(Initialization spec) noexcept
0098 : d(ShortData(spec == UTC ? Qt::UTC : Qt::LocalTime)) {}
0099
0100 #if QT_CONFIG(timezone)
0101 explicit QTimeZone(int offsetSeconds);
0102 explicit QTimeZone(const QByteArray &ianaId);
0103 QTimeZone(const QByteArray &zoneId, int offsetSeconds, const QString &name,
0104 const QString &abbreviation, QLocale::Territory territory = QLocale::AnyTerritory,
0105 const QString &comment = QString());
0106 #endif
0107
0108 QTimeZone(const QTimeZone &other) noexcept;
0109 QTimeZone(QTimeZone &&other) noexcept : d(std::move(other.d)) {}
0110 ~QTimeZone();
0111
0112 QTimeZone &operator=(const QTimeZone &other);
0113 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QTimeZone)
0114
0115 void swap(QTimeZone &other) noexcept
0116 { d.swap(other.d); }
0117
0118 #if QT_CORE_REMOVED_SINCE(6, 7)
0119 bool operator==(const QTimeZone &other) const;
0120 bool operator!=(const QTimeZone &other) const;
0121 #endif
0122
0123 bool isValid() const;
0124
0125 static QTimeZone fromDurationAheadOfUtc(std::chrono::seconds offset)
0126 {
0127 return QTimeZone((offset.count() >= MinUtcOffsetSecs && offset.count() <= MaxUtcOffsetSecs)
0128 ? ShortData(offset.count() ? Qt::OffsetFromUTC : Qt::UTC,
0129 int(offset.count()))
0130 : ShortData(Qt::TimeZone));
0131 }
0132 static QTimeZone fromSecondsAheadOfUtc(int offset)
0133 {
0134 return fromDurationAheadOfUtc(std::chrono::seconds{offset});
0135 }
0136 constexpr Qt::TimeSpec timeSpec() const noexcept { return d.s.spec(); }
0137 constexpr int fixedSecondsAheadOfUtc() const noexcept
0138 { return timeSpec() == Qt::OffsetFromUTC ? int(d.s.offset) : 0; }
0139
0140 static constexpr bool isUtcOrFixedOffset(Qt::TimeSpec spec) noexcept
0141 { return spec == Qt::UTC || spec == Qt::OffsetFromUTC; }
0142 constexpr bool isUtcOrFixedOffset() const noexcept { return isUtcOrFixedOffset(timeSpec()); }
0143
0144 #if QT_CONFIG(timezone)
0145 QTimeZone asBackendZone() const;
0146
0147 enum TimeType {
0148 StandardTime = 0,
0149 DaylightTime = 1,
0150 GenericTime = 2
0151 };
0152
0153 enum NameType {
0154 DefaultName = 0,
0155 LongName = 1,
0156 ShortName = 2,
0157 OffsetName = 3
0158 };
0159
0160 struct OffsetData {
0161 QString abbreviation;
0162 QDateTime atUtc;
0163 int offsetFromUtc;
0164 int standardTimeOffset;
0165 int daylightTimeOffset;
0166 };
0167 typedef QList<OffsetData> OffsetDataList;
0168
0169 bool hasAlternativeName(QByteArrayView alias) const;
0170 QByteArray id() const;
0171 QLocale::Territory territory() const;
0172 # if QT_DEPRECATED_SINCE(6, 6)
0173 QT_DEPRECATED_VERSION_X_6_6("Use territory() instead")
0174 QLocale::Country country() const;
0175 # endif
0176 QString comment() const;
0177
0178 QString displayName(const QDateTime &atDateTime, NameType nameType = DefaultName,
0179 const QLocale &locale = QLocale()) const;
0180 QString displayName(TimeType timeType, NameType nameType = DefaultName,
0181 const QLocale &locale = QLocale()) const;
0182 QString abbreviation(const QDateTime &atDateTime) const;
0183
0184 int offsetFromUtc(const QDateTime &atDateTime) const;
0185 int standardTimeOffset(const QDateTime &atDateTime) const;
0186 int daylightTimeOffset(const QDateTime &atDateTime) const;
0187
0188 bool hasDaylightTime() const;
0189 bool isDaylightTime(const QDateTime &atDateTime) const;
0190
0191 OffsetData offsetData(const QDateTime &forDateTime) const;
0192
0193 bool hasTransitions() const;
0194 OffsetData nextTransition(const QDateTime &afterDateTime) const;
0195 OffsetData previousTransition(const QDateTime &beforeDateTime) const;
0196 OffsetDataList transitions(const QDateTime &fromDateTime, const QDateTime &toDateTime) const;
0197
0198 static QByteArray systemTimeZoneId();
0199 static QTimeZone systemTimeZone();
0200 static QTimeZone utc();
0201
0202 static bool isTimeZoneIdAvailable(const QByteArray &ianaId);
0203
0204 static QList<QByteArray> availableTimeZoneIds();
0205 static QList<QByteArray> availableTimeZoneIds(QLocale::Territory territory);
0206 static QList<QByteArray> availableTimeZoneIds(int offsetSeconds);
0207
0208 static QByteArray ianaIdToWindowsId(const QByteArray &ianaId);
0209 static QByteArray windowsIdToDefaultIanaId(const QByteArray &windowsId);
0210 static QByteArray windowsIdToDefaultIanaId(const QByteArray &windowsId,
0211 QLocale::Territory territory);
0212 static QList<QByteArray> windowsIdToIanaIds(const QByteArray &windowsId);
0213 static QList<QByteArray> windowsIdToIanaIds(const QByteArray &windowsId,
0214 QLocale::Territory territory);
0215
0216 # if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0217 static QTimeZone fromCFTimeZone(CFTimeZoneRef timeZone);
0218 CFTimeZoneRef toCFTimeZone() const Q_DECL_CF_RETURNS_RETAINED;
0219 static QTimeZone fromNSTimeZone(const NSTimeZone *timeZone);
0220 NSTimeZone *toNSTimeZone() const Q_DECL_NS_RETURNS_AUTORELEASED;
0221 # endif
0222
0223 # if __cpp_lib_chrono >= 201907L || defined(Q_QDOC)
0224 QT_POST_CXX17_API_IN_EXPORTED_CLASS
0225 static QTimeZone fromStdTimeZonePtr(const std::chrono::time_zone *timeZone)
0226 {
0227 if (!timeZone)
0228 return QTimeZone();
0229 const std::string_view timeZoneName = timeZone->name();
0230 return QTimeZone(QByteArrayView(timeZoneName).toByteArray());
0231 }
0232 # endif
0233 #endif
0234 private:
0235 friend Q_CORE_EXPORT bool comparesEqual(const QTimeZone &lhs, const QTimeZone &rhs) noexcept;
0236 Q_DECLARE_EQUALITY_COMPARABLE(QTimeZone)
0237
0238 #ifndef QT_NO_DATASTREAM
0239 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &ds, const QTimeZone &tz);
0240 #endif
0241 #ifndef QT_NO_DEBUG_STREAM
0242 friend Q_CORE_EXPORT QDebug operator<<(QDebug dbg, const QTimeZone &tz);
0243 #endif
0244 QTimeZone(QTimeZonePrivate &dd);
0245 friend class QTimeZonePrivate;
0246 friend class QDateTime;
0247 friend class QDateTimePrivate;
0248 Data d;
0249 };
0250
0251 #if QT_CONFIG(timezone)
0252 Q_DECLARE_TYPEINFO(QTimeZone::OffsetData, Q_RELOCATABLE_TYPE);
0253 #endif
0254 Q_DECLARE_SHARED(QTimeZone)
0255
0256 #ifndef QT_NO_DATASTREAM
0257 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &ds, const QTimeZone &tz);
0258 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &ds, QTimeZone &tz);
0259 #endif
0260
0261 #ifndef QT_NO_DEBUG_STREAM
0262 Q_CORE_EXPORT QDebug operator<<(QDebug dbg, const QTimeZone &tz);
0263 #endif
0264
0265 #if QT_CONFIG(timezone) && __cpp_lib_chrono >= 201907L
0266
0267 template <typename>
0268 inline QDateTime QDateTime::fromStdZonedTime(const std::chrono::zoned_time<
0269 std::chrono::milliseconds,
0270 const std::chrono::time_zone *
0271 > &time)
0272 {
0273 const auto sysTime = time.get_sys_time();
0274 const QTimeZone timeZone = QTimeZone::fromStdTimeZonePtr(time.get_time_zone());
0275 return fromMSecsSinceEpoch(sysTime.time_since_epoch().count(), timeZone);
0276 }
0277 #endif
0278
0279 QT_END_NAMESPACE
0280
0281 #endif