1 /****************************************************************************
2 **
3 ** Copyright (C) 2013 John Layt <jlayt@kde.org>
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 
41 #ifndef QTIMEZONE_H
42 #define QTIMEZONE_H
43 
44 #include <QtCore/qshareddata.h>
45 #include <QtCore/qlocale.h>
46 #include <QtCore/qdatetime.h>
47 
48 QT_REQUIRE_CONFIG(timezone);
49 
50 #if (defined(Q_OS_DARWIN) || defined(Q_QDOC)) && !defined(QT_NO_SYSTEMLOCALE)
51 Q_FORWARD_DECLARE_CF_TYPE(CFTimeZone);
52 Q_FORWARD_DECLARE_OBJC_CLASS(NSTimeZone);
53 #endif
54 
55 QT_BEGIN_NAMESPACE
56 
57 class QTimeZonePrivate;
58 
59 class Q_CORE_EXPORT QTimeZone
60 {
61 public:
62     // Sane UTC offsets range from -14 to +14 hours:
63     enum {
64         // No known zone > 12 hrs West of Greenwich (Baker Island, USA)
65         MinUtcOffsetSecs = -14 * 3600,
66         // No known zone > 14 hrs East of Greenwich (Kiritimati, Christmas Island, Kiribati)
67         MaxUtcOffsetSecs = +14 * 3600
68     };
69 
70     enum TimeType {
71         StandardTime = 0,
72         DaylightTime = 1,
73         GenericTime = 2
74     };
75 
76     enum NameType {
77         DefaultName = 0,
78         LongName = 1,
79         ShortName = 2,
80         OffsetName = 3
81     };
82 
83     struct OffsetData {
84         QString abbreviation;
85         QDateTime atUtc;
86         int offsetFromUtc;
87         int standardTimeOffset;
88         int daylightTimeOffset;
89     };
90     typedef QVector<OffsetData> OffsetDataList;
91 
92     QTimeZone() noexcept;
93     explicit QTimeZone(const QByteArray &ianaId);
94     explicit QTimeZone(int offsetSeconds);
95     /*implicit*/ QTimeZone(const QByteArray &zoneId, int offsetSeconds, const QString &name,
96               const QString &abbreviation, QLocale::Country country = QLocale::AnyCountry,
97               const QString &comment = QString());
98     QTimeZone(const QTimeZone &other);
99     ~QTimeZone();
100 
101     QTimeZone &operator=(const QTimeZone &other);
102     QTimeZone &operator=(QTimeZone &&other) noexcept { swap(other); return *this; }
103 
swap(QTimeZone & other)104     void swap(QTimeZone &other) noexcept
105     { d.swap(other.d); }
106 
107     bool operator==(const QTimeZone &other) const;
108     bool operator!=(const QTimeZone &other) const;
109 
110     bool isValid() const;
111 
112     QByteArray id() const;
113     QLocale::Country country() const;
114     QString comment() const;
115 
116     QString displayName(const QDateTime &atDateTime,
117                         QTimeZone::NameType nameType = QTimeZone::DefaultName,
118                         const QLocale &locale = QLocale()) const;
119     QString displayName(QTimeZone::TimeType timeType,
120                         QTimeZone::NameType nameType = QTimeZone::DefaultName,
121                         const QLocale &locale = QLocale()) const;
122     QString abbreviation(const QDateTime &atDateTime) const;
123 
124     int offsetFromUtc(const QDateTime &atDateTime) const;
125     int standardTimeOffset(const QDateTime &atDateTime) const;
126     int daylightTimeOffset(const QDateTime &atDateTime) const;
127 
128     bool hasDaylightTime() const;
129     bool isDaylightTime(const QDateTime &atDateTime) const;
130 
131     OffsetData offsetData(const QDateTime &forDateTime) const;
132 
133     bool hasTransitions() const;
134     OffsetData nextTransition(const QDateTime &afterDateTime) const;
135     OffsetData previousTransition(const QDateTime &beforeDateTime) const;
136     OffsetDataList transitions(const QDateTime &fromDateTime, const QDateTime &toDateTime) const;
137 
138     static QByteArray systemTimeZoneId();
139     static QTimeZone systemTimeZone();
140     static QTimeZone utc();
141 
142     static bool isTimeZoneIdAvailable(const QByteArray &ianaId);
143 
144     static QList<QByteArray> availableTimeZoneIds();
145     static QList<QByteArray> availableTimeZoneIds(QLocale::Country country);
146     static QList<QByteArray> availableTimeZoneIds(int offsetSeconds);
147 
148     static QByteArray ianaIdToWindowsId(const QByteArray &ianaId);
149     static QByteArray windowsIdToDefaultIanaId(const QByteArray &windowsId);
150     static QByteArray windowsIdToDefaultIanaId(const QByteArray &windowsId,
151                                                 QLocale::Country country);
152     static QList<QByteArray> windowsIdToIanaIds(const QByteArray &windowsId);
153     static QList<QByteArray> windowsIdToIanaIds(const QByteArray &windowsId,
154                                                  QLocale::Country country);
155 
156 #if (defined(Q_OS_DARWIN) || defined(Q_QDOC)) && !defined(QT_NO_SYSTEMLOCALE)
157     static QTimeZone fromCFTimeZone(CFTimeZoneRef timeZone);
158     CFTimeZoneRef toCFTimeZone() const Q_DECL_CF_RETURNS_RETAINED;
159     static QTimeZone fromNSTimeZone(const NSTimeZone *timeZone);
160     NSTimeZone *toNSTimeZone() const Q_DECL_NS_RETURNS_AUTORELEASED;
161 #endif
162 
163 private:
164     QTimeZone(QTimeZonePrivate &dd);
165 #ifndef QT_NO_DATASTREAM
166     friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &ds, const QTimeZone &tz);
167 #endif
168     friend class QTimeZonePrivate;
169     friend class QDateTime;
170     friend class QDateTimePrivate;
171     QSharedDataPointer<QTimeZonePrivate> d;
172 };
173 
174 Q_DECLARE_TYPEINFO(QTimeZone::OffsetData, Q_MOVABLE_TYPE);
175 Q_DECLARE_SHARED(QTimeZone)
176 
177 #ifndef QT_NO_DATASTREAM
178 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &ds, const QTimeZone &tz);
179 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &ds, QTimeZone &tz);
180 #endif
181 
182 #ifndef QT_NO_DEBUG_STREAM
183 Q_CORE_EXPORT QDebug operator<<(QDebug dbg, const QTimeZone &tz);
184 #endif
185 
186 QT_END_NAMESPACE
187 
188 #endif // QTIMEZONE_H
189