Coverage Report

Created: 2025-09-08 07:52

/usr/include/QtCore/qtimezone.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (C) 2017 The Qt Company Ltd.
2
// Copyright (C) 2013 John Layt <jlayt@kde.org>
3
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5
#ifndef QTIMEZONE_H
6
#define QTIMEZONE_H
7
8
#include <QtCore/qcompare.h>
9
#include <QtCore/qdatetime.h>
10
#include <QtCore/qlocale.h>
11
#include <QtCore/qswap.h>
12
#include <QtCore/qtclasshelpermacros.h>
13
14
#include <chrono>
15
16
#if QT_CONFIG(timezone) && (defined(Q_OS_DARWIN) || defined(Q_QDOC))
17
Q_FORWARD_DECLARE_CF_TYPE(CFTimeZone);
18
Q_FORWARD_DECLARE_OBJC_CLASS(NSTimeZone);
19
#endif
20
21
QT_BEGIN_NAMESPACE
22
23
class QTimeZonePrivate;
24
25
class Q_CORE_EXPORT QTimeZone
26
{
27
    struct ShortData
28
    {
29
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
30
        quintptr mode : 2;
31
#endif
32
        qintptr offset : sizeof(void *) * 8 - 2;
33
34
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
35
        quintptr mode : 2;
36
#endif
37
38
        // mode is a cycled Qt::TimeSpec, (int(spec) + 1) % 4, so that zero
39
        // (lowest bits of a pointer) matches spec being Qt::TimeZone, for which
40
        // Data holds a QTZP pointer instead of ShortData.
41
        // Passing Qt::TimeZone gets the equivalent of a null QTZP; it is not short.
42
        constexpr ShortData(Qt::TimeSpec spec, int secondsAhead = 0)
43
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
44
            : offset(spec == Qt::OffsetFromUTC ? secondsAhead : 0),
45
              mode((int(spec) + 1) & 3)
46
#else
47
793k
            : mode((int(spec) + 1) & 3),
48
793k
              offset(spec == Qt::OffsetFromUTC ? secondsAhead : 0)
49
#endif
50
793k
        {
51
793k
        }
52
        friend constexpr bool operator==(ShortData lhs, ShortData rhs)
53
        { return lhs.mode == rhs.mode && lhs.offset == rhs.offset; }
54
        constexpr Qt::TimeSpec spec() const { return Qt::TimeSpec((mode + 3) & 3); }
55
    };
56
57
    union Data
58
    {
59
        Data() noexcept;
60
793k
        Data(ShortData sd) : s(sd) {}
61
        Data(const Data &other) noexcept;
62
0
        Data(Data &&other) noexcept : d(std::exchange(other.d, nullptr)) {}
63
        Data &operator=(const Data &other) noexcept;
64
0
        Data &operator=(Data &&other) noexcept { swap(other); return *this; }
65
        ~Data();
66
67
        void swap(Data &other) noexcept { qt_ptr_swap(d, other.d); }
68
        // isShort() is equivalent to s.spec() != Qt::TimeZone
69
        bool isShort() const { return s.mode; } // a.k.a. quintptr(d) & 3
70
71
        // Typse must support: out << wrap("C-strings");
72
        template <typename Stream, typename Wrap>
73
        void serialize(Stream &out, const Wrap &wrap) const;
74
75
        Data(QTimeZonePrivate *dptr) noexcept;
76
        Data &operator=(QTimeZonePrivate *dptr) noexcept;
77
        const QTimeZonePrivate *operator->() const { Q_ASSERT(!isShort()); return d; }
78
        QTimeZonePrivate *operator->() { Q_ASSERT(!isShort()); return d; }
79
80
        QTimeZonePrivate *d = nullptr;
81
        ShortData s;
82
    };
83
1.27k
    QTimeZone(ShortData sd) : d(sd) {}
84
    QTimeZone(Qt::TimeSpec) Q_DECL_EQ_DELETE_X(
85
        "Would be treated as int offsetSeconds. "
86
        "Use QTimeZone::UTC or QTimeZone::LocalTime instead.");
87
88
public:
89
    // Sane UTC offsets range from -16 to +16 hours:
90
    static constexpr int MinUtcOffsetSecs = -16 * 3600;
91
    // No known modern zone > 12 hrs West of Greenwich.
92
    // Until 1844, Asia/Manila (in The Philippines) was at 15:56 West.
93
    static constexpr int MaxUtcOffsetSecs = +16 * 3600;
94
    // No known modern zone > 14 hrs East of Greenwich.
95
    // Until 1867, America/Metlakatla (in Alaska) was at 15:13:42 East.
96
97
    enum Initialization { LocalTime, UTC };
98
99
    QTimeZone() noexcept;
100
    Q_IMPLICIT QTimeZone(Initialization spec) noexcept
101
        : d(ShortData(spec == UTC ? Qt::UTC : Qt::LocalTime)) {}
102
103
#if QT_CONFIG(timezone)
104
    explicit QTimeZone(int offsetSeconds);
105
    explicit QTimeZone(const QByteArray &ianaId);
106
    QTimeZone(const QByteArray &zoneId, int offsetSeconds, const QString &name,
107
              const QString &abbreviation, QLocale::Territory territory = QLocale::AnyTerritory,
108
              const QString &comment = QString());
109
#endif // timezone backends
110
111
    QTimeZone(const QTimeZone &other) noexcept;
112
0
    QTimeZone(QTimeZone &&other) noexcept : d(std::move(other.d)) {}
113
    ~QTimeZone();
114
115
    QTimeZone &operator=(const QTimeZone &other);
116
    QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QTimeZone)
117
118
    void swap(QTimeZone &other) noexcept
119
    { d.swap(other.d); }
120
121
#if QT_CORE_REMOVED_SINCE(6, 7)
122
    bool operator==(const QTimeZone &other) const;
123
    bool operator!=(const QTimeZone &other) const;
124
#endif
125
126
    bool isValid() const;
127
128
    static QTimeZone fromDurationAheadOfUtc(std::chrono::seconds offset)
129
1.27k
    {
130
1.27k
        return QTimeZone((offset.count() >= MinUtcOffsetSecs && offset.count() <= MaxUtcOffsetSecs)
131
1.27k
                         ? ShortData(offset.count() ? Qt::OffsetFromUTC : Qt::UTC,
132
1.16k
                                     int(offset.count()))
133
1.27k
                         : ShortData(Qt::TimeZone));
134
1.27k
    }
135
    static QTimeZone fromSecondsAheadOfUtc(int offset)
136
1.27k
    {
137
1.27k
        return fromDurationAheadOfUtc(std::chrono::seconds{offset});
138
1.27k
    }
139
    constexpr Qt::TimeSpec timeSpec() const noexcept { return d.s.spec(); }
140
    constexpr int fixedSecondsAheadOfUtc() const noexcept
141
    { return timeSpec() == Qt::OffsetFromUTC ? int(d.s.offset) : 0; }
142
143
    static constexpr bool isUtcOrFixedOffset(Qt::TimeSpec spec) noexcept
144
    { return spec == Qt::UTC || spec == Qt::OffsetFromUTC; }
145
    constexpr bool isUtcOrFixedOffset() const noexcept { return isUtcOrFixedOffset(timeSpec()); }
146
147
#if QT_CONFIG(timezone)
148
    QTimeZone asBackendZone() const;
149
150
    enum TimeType {
151
        StandardTime = 0,
152
        DaylightTime = 1,
153
        GenericTime = 2
154
    };
155
156
    enum NameType {
157
        DefaultName = 0,
158
        LongName = 1,
159
        ShortName = 2,
160
        OffsetName = 3
161
    };
162
163
    struct OffsetData {
164
        QString abbreviation;
165
        QDateTime atUtc;
166
        int offsetFromUtc;
167
        int standardTimeOffset;
168
        int daylightTimeOffset;
169
    };
170
    typedef QList<OffsetData> OffsetDataList;
171
172
    bool hasAlternativeName(QByteArrayView alias) const;
173
    QByteArray id() const;
174
    QLocale::Territory territory() const;
175
#  if QT_DEPRECATED_SINCE(6, 6)
176
    QT_DEPRECATED_VERSION_X_6_6("Use territory() instead")
177
    QLocale::Country country() const;
178
#  endif
179
    QString comment() const;
180
181
    QString displayName(const QDateTime &atDateTime, NameType nameType = DefaultName,
182
                        const QLocale &locale = QLocale()) const;
183
    QString displayName(TimeType timeType, NameType nameType = DefaultName,
184
                        const QLocale &locale = QLocale()) const;
185
    QString abbreviation(const QDateTime &atDateTime) const;
186
187
    int offsetFromUtc(const QDateTime &atDateTime) const;
188
    int standardTimeOffset(const QDateTime &atDateTime) const;
189
    int daylightTimeOffset(const QDateTime &atDateTime) const;
190
191
    bool hasDaylightTime() const;
192
    bool isDaylightTime(const QDateTime &atDateTime) const;
193
194
    OffsetData offsetData(const QDateTime &forDateTime) const;
195
196
    bool hasTransitions() const;
197
    OffsetData nextTransition(const QDateTime &afterDateTime) const;
198
    OffsetData previousTransition(const QDateTime &beforeDateTime) const;
199
    OffsetDataList transitions(const QDateTime &fromDateTime, const QDateTime &toDateTime) const;
200
201
    static QByteArray systemTimeZoneId();
202
    static QTimeZone systemTimeZone();
203
    static QTimeZone utc();
204
205
    static bool isTimeZoneIdAvailable(const QByteArray &ianaId);
206
207
    static QList<QByteArray> availableTimeZoneIds();
208
    static QList<QByteArray> availableTimeZoneIds(QLocale::Territory territory);
209
    static QList<QByteArray> availableTimeZoneIds(int offsetSeconds);
210
211
    static QByteArray ianaIdToWindowsId(const QByteArray &ianaId);
212
    static QByteArray windowsIdToDefaultIanaId(const QByteArray &windowsId);
213
    static QByteArray windowsIdToDefaultIanaId(const QByteArray &windowsId,
214
                                               QLocale::Territory territory);
215
    static QList<QByteArray> windowsIdToIanaIds(const QByteArray &windowsId);
216
    static QList<QByteArray> windowsIdToIanaIds(const QByteArray &windowsId,
217
                                                QLocale::Territory territory);
218
219
#  if defined(Q_OS_DARWIN) || defined(Q_QDOC)
220
    static QTimeZone fromCFTimeZone(CFTimeZoneRef timeZone);
221
    CFTimeZoneRef toCFTimeZone() const Q_DECL_CF_RETURNS_RETAINED;
222
    static QTimeZone fromNSTimeZone(const NSTimeZone *timeZone);
223
    NSTimeZone *toNSTimeZone() const Q_DECL_NS_RETURNS_AUTORELEASED;
224
#  endif
225
226
#  if __cpp_lib_chrono >= 201907L || defined(Q_QDOC)
227
    QT_POST_CXX17_API_IN_EXPORTED_CLASS
228
    static QTimeZone fromStdTimeZonePtr(const std::chrono::time_zone *timeZone)
229
    {
230
        if (!timeZone)
231
            return QTimeZone();
232
        const std::string_view timeZoneName = timeZone->name();
233
        return QTimeZone(QByteArrayView(timeZoneName).toByteArray());
234
    }
235
#  endif
236
#endif // feature timezone
237
private:
238
    friend Q_CORE_EXPORT bool comparesEqual(const QTimeZone &lhs, const QTimeZone &rhs) noexcept;
239
    Q_DECLARE_EQUALITY_COMPARABLE(QTimeZone)
240
241
#ifndef QT_NO_DATASTREAM
242
    friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &ds, const QTimeZone &tz);
243
#endif
244
#ifndef QT_NO_DEBUG_STREAM
245
    friend Q_CORE_EXPORT QDebug operator<<(QDebug dbg, const QTimeZone &tz);
246
#endif
247
    QTimeZone(QTimeZonePrivate &dd);
248
    friend class QTimeZonePrivate;
249
    friend class QDateTime;
250
    friend class QDateTimePrivate;
251
    Data d;
252
};
253
254
#if QT_CONFIG(timezone)
255
Q_DECLARE_TYPEINFO(QTimeZone::OffsetData, Q_RELOCATABLE_TYPE);
256
#endif
257
Q_DECLARE_SHARED(QTimeZone)
258
259
#ifndef QT_NO_DATASTREAM
260
Q_CORE_EXPORT QDataStream &operator<<(QDataStream &ds, const QTimeZone &tz);
261
Q_CORE_EXPORT QDataStream &operator>>(QDataStream &ds, QTimeZone &tz);
262
#endif
263
264
#ifndef QT_NO_DEBUG_STREAM
265
Q_CORE_EXPORT QDebug operator<<(QDebug dbg, const QTimeZone &tz);
266
#endif
267
268
#if QT_CONFIG(timezone) && __cpp_lib_chrono >= 201907L
269
// zoned_time
270
template <typename> // QT_POST_CXX17_API_IN_EXPORTED_CLASS
271
inline QDateTime QDateTime::fromStdZonedTime(const std::chrono::zoned_time<
272
                                                std::chrono::milliseconds,
273
                                                const std::chrono::time_zone *
274
                                             > &time)
275
{
276
    const auto sysTime = time.get_sys_time();
277
    const QTimeZone timeZone = QTimeZone::fromStdTimeZonePtr(time.get_time_zone());
278
    return fromMSecsSinceEpoch(sysTime.time_since_epoch().count(), timeZone);
279
}
280
#endif
281
282
QT_END_NAMESPACE
283
284
#endif // QTIMEZONE_H