1/****************************************************************************
2**
3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QDATETIME_H
43#define QDATETIME_H
44
45#include <QtCore/qstring.h>
46#include <QtCore/qnamespace.h>
47#include <QtCore/qsharedpointer.h>
48
49QT_BEGIN_HEADER
50
51QT_BEGIN_NAMESPACE
52
53QT_MODULE(Core)
54
55class Q_CORE_EXPORT QDate
56{
57public:
58 enum MonthNameType {
59 DateFormat = 0,
60 StandaloneFormat
61 };
62public:
63 QDate() { jd = 0; }
64 QDate(int y, int m, int d);
65
66 bool isNull() const { return jd == 0; }
67 bool isValid() const;
68
69 int year() const;
70 int month() const;
71 int day() const;
72 int dayOfWeek() const;
73 int dayOfYear() const;
74 int daysInMonth() const;
75 int daysInYear() const;
76 int weekNumber(int *yearNum = 0) const;
77
78#ifndef QT_NO_TEXTDATE
79#ifdef QT3_SUPPORT
80 static QT3_SUPPORT QString monthName(int month) { return shortMonthName(month); }
81 static QT3_SUPPORT QString dayName(int weekday) { return shortDayName(weekday); }
82#endif
83 // ### Qt 5: merge these functions.
84 static QString shortMonthName(int month);
85 static QString shortMonthName(int month, MonthNameType type);
86 static QString shortDayName(int weekday);
87 static QString shortDayName(int weekday, MonthNameType type);
88 static QString longMonthName(int month);
89 static QString longMonthName(int month, MonthNameType type);
90 static QString longDayName(int weekday);
91 static QString longDayName(int weekday, MonthNameType type);
92#endif // QT_NO_TEXTDATE
93#ifndef QT_NO_DATESTRING
94 QString toString(Qt::DateFormat f = Qt::TextDate) const;
95 QString toString(const QString &format) const;
96#endif
97 bool setYMD(int y, int m, int d);
98 bool setDate(int year, int month, int day);
99
100 void getDate(int *year, int *month, int *day);
101
102 QDate addDays(int days) const;
103 QDate addMonths(int months) const;
104 QDate addYears(int years) const;
105 int daysTo(const QDate &) const;
106
107 bool operator==(const QDate &other) const { return jd == other.jd; }
108 bool operator!=(const QDate &other) const { return jd != other.jd; }
109 bool operator<(const QDate &other) const { return jd < other.jd; }
110 bool operator<=(const QDate &other) const { return jd <= other.jd; }
111 bool operator>(const QDate &other) const { return jd > other.jd; }
112 bool operator>=(const QDate &other) const { return jd >= other.jd; }
113
114 static QDate currentDate();
115#ifndef QT_NO_DATESTRING
116 static QDate fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
117 static QDate fromString(const QString &s, const QString &format);
118#endif
119 static bool isValid(int y, int m, int d);
120 static bool isLeapYear(int year);
121#ifdef QT3_SUPPORT
122 inline static QT3_SUPPORT bool leapYear(int year) { return isLeapYear(year); }
123#endif
124
125 // ### Qt 5: remove these two functions
126 static uint gregorianToJulian(int y, int m, int d);
127 static void julianToGregorian(uint jd, int &y, int &m, int &d);
128
129#ifdef QT3_SUPPORT
130 static QT3_SUPPORT QDate currentDate(Qt::TimeSpec spec);
131#endif
132
133 static inline QDate fromJulianDay(int jd) { QDate d; d.jd = jd; return d; }
134 inline int toJulianDay() const { return jd; }
135
136private:
137 uint jd;
138
139 friend class QDateTime;
140 friend class QDateTimePrivate;
141#ifndef QT_NO_DATASTREAM
142 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDate &);
143 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &);
144#endif
145};
146Q_DECLARE_TYPEINFO(QDate, Q_MOVABLE_TYPE);
147
148class Q_CORE_EXPORT QTime
149{
150public:
151 QTime(): mds(NullTime)
152#if defined(Q_OS_WINCE)
153 , startTick(NullTime)
154#endif
155 {}
156 QTime(int h, int m, int s = 0, int ms = 0);
157
158 bool isNull() const { return mds == NullTime; }
159 bool isValid() const;
160
161 int hour() const;
162 int minute() const;
163 int second() const;
164 int msec() const;
165#ifndef QT_NO_DATESTRING
166 QString toString(Qt::DateFormat f = Qt::TextDate) const;
167 QString toString(const QString &format) const;
168#endif
169 bool setHMS(int h, int m, int s, int ms = 0);
170
171 QTime addSecs(int secs) const;
172 int secsTo(const QTime &) const;
173 QTime addMSecs(int ms) const;
174 int msecsTo(const QTime &) const;
175
176 bool operator==(const QTime &other) const { return mds == other.mds; }
177 bool operator!=(const QTime &other) const { return mds != other.mds; }
178 bool operator<(const QTime &other) const { return mds < other.mds; }
179 bool operator<=(const QTime &other) const { return mds <= other.mds; }
180 bool operator>(const QTime &other) const { return mds > other.mds; }
181 bool operator>=(const QTime &other) const { return mds >= other.mds; }
182
183 static QTime currentTime();
184#ifndef QT_NO_DATESTRING
185 static QTime fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
186 static QTime fromString(const QString &s, const QString &format);
187#endif
188 static bool isValid(int h, int m, int s, int ms = 0);
189
190#ifdef QT3_SUPPORT
191 static QT3_SUPPORT QTime currentTime(Qt::TimeSpec spec);
192#endif
193
194 void start();
195 int restart();
196 int elapsed() const;
197private:
198 enum TimeFlag { NullTime = -1 };
199 inline int ds() const { return mds == -1 ? 0 : mds; }
200 int mds;
201#if defined(Q_OS_WINCE)
202 int startTick;
203#endif
204
205 friend class QDateTime;
206 friend class QDateTimePrivate;
207#ifndef QT_NO_DATASTREAM
208 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QTime &);
209 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &);
210#endif
211};
212Q_DECLARE_TYPEINFO(QTime, Q_MOVABLE_TYPE);
213
214class QDateTimePrivate;
215
216class Q_CORE_EXPORT QDateTime
217{
218public:
219 QDateTime();
220 explicit QDateTime(const QDate &);
221 QDateTime(const QDate &, const QTime &, Qt::TimeSpec spec = Qt::LocalTime);
222 QDateTime(const QDateTime &other);
223 ~QDateTime();
224
225 QDateTime &operator=(const QDateTime &other);
226
227 bool isNull() const;
228 bool isValid() const;
229
230 QDate date() const;
231 QTime time() const;
232 Qt::TimeSpec timeSpec() const;
233 qint64 toMSecsSinceEpoch() const;
234 uint toTime_t() const;
235 void setDate(const QDate &date);
236 void setTime(const QTime &time);
237 void setTimeSpec(Qt::TimeSpec spec);
238 void setMSecsSinceEpoch(qint64 msecs);
239 void setTime_t(uint secsSince1Jan1970UTC);
240#ifndef QT_NO_DATESTRING
241 QString toString(Qt::DateFormat f = Qt::TextDate) const;
242 QString toString(const QString &format) const;
243#endif
244 QDateTime addDays(int days) const;
245 QDateTime addMonths(int months) const;
246 QDateTime addYears(int years) const;
247 QDateTime addSecs(int secs) const;
248 QDateTime addMSecs(qint64 msecs) const;
249 QDateTime toTimeSpec(Qt::TimeSpec spec) const;
250 inline QDateTime toLocalTime() const { return toTimeSpec(Qt::LocalTime); }
251 inline QDateTime toUTC() const { return toTimeSpec(Qt::UTC); }
252 int daysTo(const QDateTime &) const;
253 int secsTo(const QDateTime &) const;
254 qint64 msecsTo(const QDateTime &) const;
255
256 bool operator==(const QDateTime &other) const;
257 inline bool operator!=(const QDateTime &other) const { return !(*this == other); }
258 bool operator<(const QDateTime &other) const;
259 inline bool operator<=(const QDateTime &other) const { return !(other < *this); }
260 inline bool operator>(const QDateTime &other) const { return other < *this; }
261 inline bool operator>=(const QDateTime &other) const { return !(*this < other); }
262
263 void setUtcOffset(int seconds);
264 int utcOffset() const;
265
266 static QDateTime currentDateTime();
267 static QDateTime currentDateTimeUtc();
268#ifndef QT_NO_DATESTRING
269 static QDateTime fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
270 static QDateTime fromString(const QString &s, const QString &format);
271#endif
272 static QDateTime fromTime_t(uint secsSince1Jan1970UTC);
273 static QDateTime fromMSecsSinceEpoch(qint64 msecs);
274 static qint64 currentMSecsSinceEpoch();
275
276#ifdef QT3_SUPPORT
277 inline QT3_SUPPORT void setTime_t(uint secsSince1Jan1970UTC, Qt::TimeSpec spec) {
278 setTime_t(secsSince1Jan1970UTC);
279 if (spec == Qt::UTC)
280 *this = toUTC();
281 }
282 static inline QT3_SUPPORT QDateTime currentDateTime(Qt::TimeSpec spec) {
283 if (spec == Qt::LocalTime)
284 return currentDateTime();
285 else
286 return currentDateTime().toUTC();
287 }
288
289#endif
290
291private:
292 friend class QDateTimePrivate;
293 void detach();
294 QExplicitlySharedDataPointer<QDateTimePrivate> d;
295
296#ifndef QT_NO_DATASTREAM
297 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);
298 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &);
299#endif
300};
301Q_DECLARE_TYPEINFO(QDateTime, Q_MOVABLE_TYPE);
302
303#ifdef QT3_SUPPORT
304inline QDate QDate::currentDate(Qt::TimeSpec spec)
305{
306 if (spec == Qt::LocalTime)
307 return currentDate();
308 else
309 return QDateTime::currentDateTime().toUTC().date();
310}
311
312inline QTime QTime::currentTime(Qt::TimeSpec spec)
313{
314 if (spec == Qt::LocalTime)
315 return currentTime();
316 else
317 return QDateTime::currentDateTime().toUTC().time();
318}
319#endif
320
321#ifndef QT_NO_DATASTREAM
322Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDate &);
323Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &);
324Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QTime &);
325Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &);
326Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);
327Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &);
328#endif // QT_NO_DATASTREAM
329
330#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_NO_DATESTRING)
331Q_CORE_EXPORT QDebug operator<<(QDebug, const QDate &);
332Q_CORE_EXPORT QDebug operator<<(QDebug, const QTime &);
333Q_CORE_EXPORT QDebug operator<<(QDebug, const QDateTime &);
334#endif
335
336QT_END_NAMESPACE
337
338QT_END_HEADER
339
340#endif // QDATETIME_H
341