1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
45 #include <QtCore/qstring.h>
46 #include <QtCore/qnamespace.h>
47 #include <QtCore/qsharedpointer.h>
49 // windows.h defines these identifiers, so undefine it
50 // ### figure out where in Qt we include it too soon
65 class Q_CORE_EXPORT QDate
73 QDate() { jd = nullJd(); }
74 QDate(int y, int m, int d);
76 bool isNull() const { return !isValid(); }
77 bool isValid() const { return jd >= minJd() && jd <= maxJd(); }
82 int dayOfWeek() const;
83 int dayOfYear() const;
84 int daysInMonth() const;
85 int daysInYear() const;
86 int weekNumber(int *yearNum = 0) const;
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;
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); }
103 bool setDate(int year, int month, int day);
105 void getDate(int *year, int *month, int *day);
107 QDate addDays(qint64 days) const;
108 QDate addMonths(int months) const;
109 QDate addYears(int years) const;
110 qint64 daysTo(const QDate &) const;
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; }
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);
124 static bool isValid(int y, int m, int d);
125 static bool isLeapYear(int year);
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; }
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; }
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 &);
145 Q_DECLARE_TYPEINFO(QDate, Q_MOVABLE_TYPE);
147 class Q_CORE_EXPORT QTime
150 QTime(): mds(NullTime)
151 #if defined(Q_OS_WINCE)
152 , startTick(NullTime)
155 QTime(int h, int m, int s = 0, int ms = 0);
157 bool isNull() const { return mds == NullTime; }
158 bool isValid() const;
164 #ifndef QT_NO_DATESTRING
165 QString toString(Qt::DateFormat f = Qt::TextDate) const;
166 QString toString(const QString &format) const;
168 bool setHMS(int h, int m, int s, int ms = 0);
170 QTime addSecs(int secs) const;
171 int secsTo(const QTime &) const;
172 QTime addMSecs(int ms) const;
173 int msecsTo(const QTime &) const;
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; }
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);
187 static bool isValid(int h, int m, int s, int ms = 0);
193 enum TimeFlag { NullTime = -1 };
194 inline int ds() const { return mds == -1 ? 0 : mds; }
196 #if defined(Q_OS_WINCE)
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 &);
207 Q_DECLARE_TYPEINFO(QTime, Q_MOVABLE_TYPE);
209 class QDateTimePrivate;
211 class Q_CORE_EXPORT QDateTime
215 explicit QDateTime(const QDate &);
216 QDateTime(const QDate &, const QTime &, Qt::TimeSpec spec = Qt::LocalTime);
217 QDateTime(const QDateTime &other);
220 QDateTime &operator=(const QDateTime &other);
223 bool isValid() 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;
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;
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); }
258 void setUtcOffset(int seconds);
259 int utcOffset() const;
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);
267 static QDateTime fromTime_t(uint secsSince1Jan1970UTC);
268 static QDateTime fromMSecsSinceEpoch(qint64 msecs);
269 static qint64 currentMSecsSinceEpoch();
272 friend class QDateTimePrivate;
274 QExplicitlySharedDataPointer<QDateTimePrivate> d;
276 #ifndef QT_NO_DATASTREAM
277 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);
278 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &);
281 Q_DECLARE_TYPEINFO(QDateTime, Q_MOVABLE_TYPE);
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
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 &);
302 #endif // QDATETIME_H