Merge remote-tracking branch 'origin/master' into api_changes
[profile/ivi/qtbase.git] / src / corelib / tools / qdatetime.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
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
49 // windows.h defines these identifiers, so undefine it
50 // ### figure out where in Qt we include it too soon
51 #ifdef max
52 # undef max
53 #endif
54 #ifdef min
55 # undef min
56 #endif
57
58 #include <limits>
59
60 QT_BEGIN_HEADER
61
62 QT_BEGIN_NAMESPACE
63
64
65 class Q_CORE_EXPORT QDate
66 {
67 public:
68     enum MonthNameType {
69         DateFormat = 0,
70         StandaloneFormat
71     };
72 public:
73     QDate() { jd = nullJd(); }
74     QDate(int y, int m, int d);
75
76     bool isNull() const { return !isValid(); }
77     bool isValid() const { return jd >= minJd() && jd <= maxJd(); }
78
79     int year() const;
80     int month() const;
81     int day() const;
82     int dayOfWeek() const;
83     int dayOfYear() const;
84     int daysInMonth() const;
85     int daysInYear() const;
86     int weekNumber(int *yearNum = 0) const;
87
88 #ifndef QT_NO_TEXTDATE
89     static QString shortMonthName(int month, MonthNameType type = DateFormat);
90     static QString shortDayName(int weekday, MonthNameType type = DateFormat);
91     static QString longMonthName(int month, MonthNameType type = DateFormat);
92     static QString longDayName(int weekday, MonthNameType type = DateFormat);
93 #endif // QT_NO_TEXTDATE
94 #ifndef QT_NO_DATESTRING
95     QString toString(Qt::DateFormat f = Qt::TextDate) const;
96     QString toString(const QString &format) const;
97 #endif
98 #if QT_DEPRECATED_SINCE(5,0)
99 QT_DEPRECATED inline bool setYMD(int y, int m, int d)
100 { if (uint(y) <= 99) y += 1900; return setDate(y, m, d); }
101 #endif
102
103     bool setDate(int year, int month, int day);
104
105     void getDate(int *year, int *month, int *day);
106
107     QDate addDays(qint64 days) const;
108     QDate addMonths(int months) const;
109     QDate addYears(int years) const;
110     qint64 daysTo(const QDate &) const;
111
112     bool operator==(const QDate &other) const { return jd == other.jd; }
113     bool operator!=(const QDate &other) const { return jd != other.jd; }
114     bool operator<(const QDate &other) const { return jd < other.jd; }
115     bool operator<=(const QDate &other) const { return jd <= other.jd; }
116     bool operator>(const QDate &other) const { return jd > other.jd; }
117     bool operator>=(const QDate &other) const { return jd >= other.jd; }
118
119     static QDate currentDate();
120 #ifndef QT_NO_DATESTRING
121     static QDate fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
122     static QDate fromString(const QString &s, const QString &format);
123 #endif
124     static bool isValid(int y, int m, int d);
125     static bool isLeapYear(int year);
126
127     static inline QDate fromJulianDay(qint64 jd)
128     { QDate d; if (jd >= minJd() && jd <= maxJd()) d.jd = jd; return d; }
129     inline qint64 toJulianDay() const { return jd; }
130
131 private:
132     static inline qint64 nullJd() { return std::numeric_limits<qint64>::min(); }
133     static inline qint64 minJd() { return std::numeric_limits<qint64>::min() / 2; }
134     static inline qint64 maxJd() { return (std::numeric_limits<qint64>::max()) / 2; }
135
136     qint64 jd;
137
138     friend class QDateTime;
139     friend class QDateTimePrivate;
140 #ifndef QT_NO_DATASTREAM
141     friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDate &);
142     friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &);
143 #endif
144 };
145 Q_DECLARE_TYPEINFO(QDate, Q_MOVABLE_TYPE);
146
147 class Q_CORE_EXPORT QTime
148 {
149 public:
150     QTime(): mds(NullTime)
151 #if defined(Q_OS_WINCE)
152         , startTick(NullTime)
153 #endif
154     {}
155     QTime(int h, int m, int s = 0, int ms = 0);
156
157     bool isNull() const { return mds == NullTime; }
158     bool isValid() const;
159
160     int hour() const;
161     int minute() const;
162     int second() const;
163     int msec() const;
164 #ifndef QT_NO_DATESTRING
165     QString toString(Qt::DateFormat f = Qt::TextDate) const;
166     QString toString(const QString &format) const;
167 #endif
168     bool setHMS(int h, int m, int s, int ms = 0);
169
170     QTime addSecs(int secs) const;
171     int secsTo(const QTime &) const;
172     QTime addMSecs(int ms) const;
173     int msecsTo(const QTime &) const;
174
175     bool operator==(const QTime &other) const { return mds == other.mds; }
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
182     static QTime currentTime();
183 #ifndef QT_NO_DATESTRING
184     static QTime fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
185     static QTime fromString(const QString &s, const QString &format);
186 #endif
187     static bool isValid(int h, int m, int s, int ms = 0);
188
189     void start();
190     int restart();
191     int elapsed() const;
192 private:
193     enum TimeFlag { NullTime = -1 };
194     inline int ds() const { return mds == -1 ? 0 : mds; }
195     int mds;
196 #if defined(Q_OS_WINCE)
197     int startTick;
198 #endif
199
200     friend class QDateTime;
201     friend class QDateTimePrivate;
202 #ifndef QT_NO_DATASTREAM
203     friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QTime &);
204     friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &);
205 #endif
206 };
207 Q_DECLARE_TYPEINFO(QTime, Q_MOVABLE_TYPE);
208
209 class QDateTimePrivate;
210
211 class Q_CORE_EXPORT QDateTime
212 {
213 public:
214     QDateTime();
215     explicit QDateTime(const QDate &);
216     QDateTime(const QDate &, const QTime &, Qt::TimeSpec spec = Qt::LocalTime);
217     QDateTime(const QDateTime &other);
218     ~QDateTime();
219
220     QDateTime &operator=(const QDateTime &other);
221
222     bool isNull() const;
223     bool isValid() const;
224
225     QDate date() const;
226     QTime time() const;
227     Qt::TimeSpec timeSpec() const;
228     qint64 toMSecsSinceEpoch() const;
229     uint toTime_t() const;
230     void setDate(const QDate &date);
231     void setTime(const QTime &time);
232     void setTimeSpec(Qt::TimeSpec spec);
233     void setMSecsSinceEpoch(qint64 msecs);
234     void setTime_t(uint secsSince1Jan1970UTC);
235 #ifndef QT_NO_DATESTRING
236     QString toString(Qt::DateFormat f = Qt::TextDate) const;
237     QString toString(const QString &format) const;
238 #endif
239     QDateTime addDays(qint64 days) const;
240     QDateTime addMonths(int months) const;
241     QDateTime addYears(int years) const;
242     QDateTime addSecs(int secs) const;
243     QDateTime addMSecs(qint64 msecs) const;
244     QDateTime toTimeSpec(Qt::TimeSpec spec) const;
245     inline QDateTime toLocalTime() const { return toTimeSpec(Qt::LocalTime); }
246     inline QDateTime toUTC() const { return toTimeSpec(Qt::UTC); }
247     qint64 daysTo(const QDateTime &) const;
248     int secsTo(const QDateTime &) const;
249     qint64 msecsTo(const QDateTime &) const;
250
251     bool operator==(const QDateTime &other) const;
252     inline bool operator!=(const QDateTime &other) const { return !(*this == other); }
253     bool operator<(const QDateTime &other) const;
254     inline bool operator<=(const QDateTime &other) const { return !(other < *this); }
255     inline bool operator>(const QDateTime &other) const { return other < *this; }
256     inline bool operator>=(const QDateTime &other) const { return !(*this < other); }
257
258     void setUtcOffset(int seconds);
259     int utcOffset() const;
260
261     static QDateTime currentDateTime();
262     static QDateTime currentDateTimeUtc();
263 #ifndef QT_NO_DATESTRING
264     static QDateTime fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
265     static QDateTime fromString(const QString &s, const QString &format);
266 #endif
267     static QDateTime fromTime_t(uint secsSince1Jan1970UTC);
268     static QDateTime fromMSecsSinceEpoch(qint64 msecs);
269     static qint64 currentMSecsSinceEpoch();
270
271 private:
272     friend class QDateTimePrivate;
273     void detach();
274     QExplicitlySharedDataPointer<QDateTimePrivate> d;
275
276 #ifndef QT_NO_DATASTREAM
277     friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);
278     friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &);
279 #endif
280 };
281 Q_DECLARE_TYPEINFO(QDateTime, Q_MOVABLE_TYPE);
282
283 #ifndef QT_NO_DATASTREAM
284 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDate &);
285 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &);
286 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QTime &);
287 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &);
288 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);
289 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &);
290 #endif // QT_NO_DATASTREAM
291
292 #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_NO_DATESTRING)
293 Q_CORE_EXPORT QDebug operator<<(QDebug, const QDate &);
294 Q_CORE_EXPORT QDebug operator<<(QDebug, const QTime &);
295 Q_CORE_EXPORT QDebug operator<<(QDebug, const QDateTime &);
296 #endif
297
298 QT_END_NAMESPACE
299
300 QT_END_HEADER
301
302 #endif // QDATETIME_H