Warning, file /include/QtCore/qtimezone.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
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 QTimeZone(Qt::TimeSpec) Q_DECL_EQ_DELETE_X(
0085 "Would be treated as int offsetSeconds. "
0086 "Use QTimeZone::UTC or QTimeZone::LocalTime instead.");
0087
0088 public:
0089
0090 static constexpr int MinUtcOffsetSecs = -16 * 3600;
0091
0092
0093 static constexpr int MaxUtcOffsetSecs = +16 * 3600;
0094
0095
0096
0097 enum Initialization { LocalTime, UTC };
0098
0099 QTimeZone() noexcept;
0100 Q_IMPLICIT QTimeZone(Initialization spec) noexcept
0101 : d(ShortData(spec == UTC ? Qt::UTC : Qt::LocalTime)) {}
0102
0103 #if QT_CONFIG(timezone)
0104 explicit QTimeZone(int offsetSeconds);
0105 explicit QTimeZone(const QByteArray &ianaId);
0106 QTimeZone(const QByteArray &zoneId, int offsetSeconds, const QString &name,
0107 const QString &abbreviation, QLocale::Territory territory = QLocale::AnyTerritory,
0108 const QString &comment = QString());
0109 #endif
0110
0111 QTimeZone(const QTimeZone &other) noexcept;
0112 QTimeZone(QTimeZone &&other) noexcept : d(std::move(other.d)) {}
0113 ~QTimeZone();
0114
0115 QTimeZone &operator=(const QTimeZone &other);
0116 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QTimeZone)
0117
0118 void swap(QTimeZone &other) noexcept
0119 { d.swap(other.d); }
0120
0121 #if QT_CORE_REMOVED_SINCE(6, 7)
0122 bool operator==(const QTimeZone &other) const;
0123 bool operator!=(const QTimeZone &other) const;
0124 #endif
0125
0126 bool isValid() const;
0127
0128 static QTimeZone fromDurationAheadOfUtc(std::chrono::seconds offset)
0129 {
0130 return QTimeZone((offset.count() >= MinUtcOffsetSecs && offset.count() <= MaxUtcOffsetSecs)
0131 ? ShortData(offset.count() ? Qt::OffsetFromUTC : Qt::UTC,
0132 int(offset.count()))
0133 : ShortData(Qt::TimeZone));
0134 }
0135 static QTimeZone fromSecondsAheadOfUtc(int offset)
0136 {
0137 return fromDurationAheadOfUtc(std::chrono::seconds{offset});
0138 }
0139 constexpr Qt::TimeSpec timeSpec() const noexcept { return d.s.spec(); }
0140 constexpr int fixedSecondsAheadOfUtc() const noexcept
0141 { return timeSpec() == Qt::OffsetFromUTC ? int(d.s.offset) : 0; }
0142
0143 static constexpr bool isUtcOrFixedOffset(Qt::TimeSpec spec) noexcept
0144 { return spec == Qt::UTC || spec == Qt::OffsetFromUTC; }
0145 constexpr bool isUtcOrFixedOffset() const noexcept { return isUtcOrFixedOffset(timeSpec()); }
0146
0147 #if QT_CONFIG(timezone)
0148 QTimeZone asBackendZone() const;
0149
0150 enum TimeType {
0151 StandardTime = 0,
0152 DaylightTime = 1,
0153 GenericTime = 2
0154 };
0155
0156 enum NameType {
0157 DefaultName = 0,
0158 LongName = 1,
0159 ShortName = 2,
0160 OffsetName = 3
0161 };
0162
0163 struct OffsetData {
0164 QString abbreviation;
0165 QDateTime atUtc;
0166 int offsetFromUtc;
0167 int standardTimeOffset;
0168 int daylightTimeOffset;
0169 };
0170 typedef QList<OffsetData> OffsetDataList;
0171
0172 bool hasAlternativeName(QByteArrayView alias) const;
0173 QByteArray id() const;
0174 QLocale::Territory territory() const;
0175 # if QT_DEPRECATED_SINCE(6, 6)
0176 QT_DEPRECATED_VERSION_X_6_6("Use territory() instead")
0177 QLocale::Country country() const;
0178 # endif
0179 QString comment() const;
0180
0181 QString displayName(const QDateTime &atDateTime, NameType nameType = DefaultName,
0182 const QLocale &locale = QLocale()) const;
0183 QString displayName(TimeType timeType, NameType nameType = DefaultName,
0184 const QLocale &locale = QLocale()) const;
0185 QString abbreviation(const QDateTime &atDateTime) const;
0186
0187 int offsetFromUtc(const QDateTime &atDateTime) const;
0188 int standardTimeOffset(const QDateTime &atDateTime) const;
0189 int daylightTimeOffset(const QDateTime &atDateTime) const;
0190
0191 bool hasDaylightTime() const;
0192 bool isDaylightTime(const QDateTime &atDateTime) const;
0193
0194 OffsetData offsetData(const QDateTime &forDateTime) const;
0195
0196 bool hasTransitions() const;
0197 OffsetData nextTransition(const QDateTime &afterDateTime) const;
0198 OffsetData previousTransition(const QDateTime &beforeDateTime) const;
0199 OffsetDataList transitions(const QDateTime &fromDateTime, const QDateTime &toDateTime) const;
0200
0201 static QByteArray systemTimeZoneId();
0202 static QTimeZone systemTimeZone();
0203 static QTimeZone utc();
0204
0205 static bool isTimeZoneIdAvailable(const QByteArray &ianaId);
0206
0207 static QList<QByteArray> availableTimeZoneIds();
0208 static QList<QByteArray> availableTimeZoneIds(QLocale::Territory territory);
0209 static QList<QByteArray> availableTimeZoneIds(int offsetSeconds);
0210
0211 static QByteArray ianaIdToWindowsId(const QByteArray &ianaId);
0212 static QByteArray windowsIdToDefaultIanaId(const QByteArray &windowsId);
0213 static QByteArray windowsIdToDefaultIanaId(const QByteArray &windowsId,
0214 QLocale::Territory territory);
0215 static QList<QByteArray> windowsIdToIanaIds(const QByteArray &windowsId);
0216 static QList<QByteArray> windowsIdToIanaIds(const QByteArray &windowsId,
0217 QLocale::Territory territory);
0218
0219 # if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0220 static QTimeZone fromCFTimeZone(CFTimeZoneRef timeZone);
0221 CFTimeZoneRef toCFTimeZone() const Q_DECL_CF_RETURNS_RETAINED;
0222 static QTimeZone fromNSTimeZone(const NSTimeZone *timeZone);
0223 NSTimeZone *toNSTimeZone() const Q_DECL_NS_RETURNS_AUTORELEASED;
0224 # endif
0225
0226 # if __cpp_lib_chrono >= 201907L || defined(Q_QDOC)
0227 QT_POST_CXX17_API_IN_EXPORTED_CLASS
0228 static QTimeZone fromStdTimeZonePtr(const std::chrono::time_zone *timeZone)
0229 {
0230 if (!timeZone)
0231 return QTimeZone();
0232 const std::string_view timeZoneName = timeZone->name();
0233 return QTimeZone(QByteArrayView(timeZoneName).toByteArray());
0234 }
0235 # endif
0236 #endif
0237 private:
0238 friend Q_CORE_EXPORT bool comparesEqual(const QTimeZone &lhs, const QTimeZone &rhs) noexcept;
0239 Q_DECLARE_EQUALITY_COMPARABLE(QTimeZone)
0240
0241 #ifndef QT_NO_DATASTREAM
0242 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &ds, const QTimeZone &tz);
0243 #endif
0244 #ifndef QT_NO_DEBUG_STREAM
0245 friend Q_CORE_EXPORT QDebug operator<<(QDebug dbg, const QTimeZone &tz);
0246 #endif
0247 QTimeZone(QTimeZonePrivate &dd);
0248 friend class QTimeZonePrivate;
0249 friend class QDateTime;
0250 friend class QDateTimePrivate;
0251 Data d;
0252 };
0253
0254 #if QT_CONFIG(timezone)
0255 Q_DECLARE_TYPEINFO(QTimeZone::OffsetData, Q_RELOCATABLE_TYPE);
0256 #endif
0257 Q_DECLARE_SHARED(QTimeZone)
0258
0259 #ifndef QT_NO_DATASTREAM
0260 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &ds, const QTimeZone &tz);
0261 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &ds, QTimeZone &tz);
0262 #endif
0263
0264 #ifndef QT_NO_DEBUG_STREAM
0265 Q_CORE_EXPORT QDebug operator<<(QDebug dbg, const QTimeZone &tz);
0266 #endif
0267
0268 #if QT_CONFIG(timezone) && __cpp_lib_chrono >= 201907L
0269
0270 template <typename>
0271 inline QDateTime QDateTime::fromStdZonedTime(const std::chrono::zoned_time<
0272 std::chrono::milliseconds,
0273 const std::chrono::time_zone *
0274 > &time)
0275 {
0276 const auto sysTime = time.get_sys_time();
0277 const QTimeZone timeZone = QTimeZone::fromStdTimeZonePtr(time.get_time_zone());
0278 return fromMSecsSinceEpoch(sysTime.time_since_epoch().count(), timeZone);
0279 }
0280 #endif
0281
0282 QT_END_NAMESPACE
0283
0284 #endif