source: trunk/src/corelib/tools/qdatetime.h@ 135

Last change on this file since 135 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 11.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
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
48QT_BEGIN_HEADER
49
50QT_BEGIN_NAMESPACE
51
52QT_MODULE(Core)
53
54class Q_CORE_EXPORT QDate
55{
56public:
57 enum MonthNameType {
58 DateFormat = 0,
59 StandaloneFormat
60 };
61public:
62 QDate() { jd = 0; }
63 QDate(int y, int m, int d);
64
65 bool isNull() const { return jd == 0; }
66 bool isValid() const;
67
68 int year() const;
69 int month() const;
70 int day() const;
71 int dayOfWeek() const;
72 int dayOfYear() const;
73 int daysInMonth() const;
74 int daysInYear() const;
75 int weekNumber(int *yearNum = 0) const;
76
77#ifndef QT_NO_TEXTDATE
78#ifdef QT3_SUPPORT
79 static QT3_SUPPORT QString monthName(int month) { return shortMonthName(month); }
80 static QT3_SUPPORT QString dayName(int weekday) { return shortDayName(weekday); }
81#endif
82 // ### Qt 5: merge these functions.
83 static QString shortMonthName(int month);
84 static QString shortMonthName(int month, MonthNameType type);
85 static QString shortDayName(int weekday);
86 static QString shortDayName(int weekday, MonthNameType type);
87 static QString longMonthName(int month);
88 static QString longMonthName(int month, MonthNameType type);
89 static QString longDayName(int weekday);
90 static QString longDayName(int weekday, MonthNameType type);
91#endif // QT_NO_TEXTDATE
92#ifndef QT_NO_DATESTRING
93 QString toString(Qt::DateFormat f = Qt::TextDate) const;
94 QString toString(const QString &format) const;
95#endif
96 bool setYMD(int y, int m, int d);
97 bool setDate(int year, int month, int day);
98
99 void getDate(int *year, int *month, int *day);
100
101 QDate addDays(int days) const;
102 QDate addMonths(int months) const;
103 QDate addYears(int years) const;
104 int daysTo(const QDate &) const;
105
106 bool operator==(const QDate &other) const { return jd == other.jd; }
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
113 static QDate currentDate();
114#ifndef QT_NO_DATESTRING
115 static QDate fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
116 static QDate fromString(const QString &s, const QString &format);
117#endif
118 static bool isValid(int y, int m, int d);
119 static bool isLeapYear(int year);
120#ifdef QT3_SUPPORT
121 inline static QT3_SUPPORT bool leapYear(int year) { return isLeapYear(year); }
122#endif
123
124 // ### Qt 5: remove these two functions
125 static uint gregorianToJulian(int y, int m, int d);
126 static void julianToGregorian(uint jd, int &y, int &m, int &d);
127
128#ifdef QT3_SUPPORT
129 static QT3_SUPPORT QDate currentDate(Qt::TimeSpec spec);
130#endif
131
132 static inline QDate fromJulianDay(int jd) { QDate d; d.jd = jd; return d; }
133 inline int toJulianDay() const { return jd; }
134
135private:
136 uint 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};
145Q_DECLARE_TYPEINFO(QDate, Q_MOVABLE_TYPE);
146
147class Q_CORE_EXPORT QTime
148{
149public:
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#ifdef QT3_SUPPORT
190 static QT3_SUPPORT QTime currentTime(Qt::TimeSpec spec);
191#endif
192
193 void start();
194 int restart();
195 int elapsed() const;
196private:
197 enum TimeFlag { NullTime = -1 };
198 inline int ds() const { return mds == -1 ? 0 : mds; }
199 int mds;
200#if defined(Q_OS_WINCE)
201 int startTick;
202#endif
203
204 friend class QDateTime;
205 friend class QDateTimePrivate;
206#ifndef QT_NO_DATASTREAM
207 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QTime &);
208 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &);
209#endif
210};
211Q_DECLARE_TYPEINFO(QTime, Q_MOVABLE_TYPE);
212
213class QDateTimePrivate;
214
215class Q_CORE_EXPORT QDateTime
216{
217public:
218 QDateTime();
219 explicit QDateTime(const QDate &);
220 QDateTime(const QDate &, const QTime &, Qt::TimeSpec spec = Qt::LocalTime);
221 QDateTime(const QDateTime &other);
222 ~QDateTime();
223
224 QDateTime &operator=(const QDateTime &other);
225
226 bool isNull() const;
227 bool isValid() const;
228
229 QDate date() const;
230 QTime time() const;
231 Qt::TimeSpec timeSpec() const;
232 uint toTime_t() const;
233 void setDate(const QDate &date);
234 void setTime(const QTime &time);
235 void setTimeSpec(Qt::TimeSpec spec);
236 void setTime_t(uint secsSince1Jan1970UTC);
237#ifndef QT_NO_DATESTRING
238 QString toString(Qt::DateFormat f = Qt::TextDate) const;
239 QString toString(const QString &format) const;
240#endif
241 QDateTime addDays(int days) const;
242 QDateTime addMonths(int months) const;
243 QDateTime addYears(int years) const;
244 QDateTime addSecs(int secs) const;
245 QDateTime addMSecs(qint64 msecs) const;
246 QDateTime toTimeSpec(Qt::TimeSpec spec) const;
247 inline QDateTime toLocalTime() const { return toTimeSpec(Qt::LocalTime); }
248 inline QDateTime toUTC() const { return toTimeSpec(Qt::UTC); }
249 int daysTo(const QDateTime &) const;
250 int secsTo(const QDateTime &) const;
251
252 bool operator==(const QDateTime &other) const;
253 inline bool operator!=(const QDateTime &other) const { return !(*this == other); }
254 bool operator<(const QDateTime &other) const;
255 inline bool operator<=(const QDateTime &other) const { return !(other < *this); }
256 inline bool operator>(const QDateTime &other) const { return other < *this; }
257 inline bool operator>=(const QDateTime &other) const { return !(*this < other); }
258
259 void setUtcOffset(int seconds);
260 int utcOffset() const;
261
262 static QDateTime currentDateTime();
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
269#ifdef QT3_SUPPORT
270 inline QT3_SUPPORT void setTime_t(uint secsSince1Jan1970UTC, Qt::TimeSpec spec) {
271 setTime_t(secsSince1Jan1970UTC);
272 if (spec == Qt::UTC)
273 *this = toUTC();
274 }
275 static inline QT3_SUPPORT QDateTime currentDateTime(Qt::TimeSpec spec) {
276 if (spec == Qt::LocalTime)
277 return currentDateTime();
278 else
279 return currentDateTime().toUTC();
280 }
281
282#endif
283
284private:
285 friend class QDateTimePrivate;
286 void detach();
287 QDateTimePrivate *d;
288
289#ifndef QT_NO_DATASTREAM
290 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);
291 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &);
292#endif
293};
294Q_DECLARE_TYPEINFO(QDateTime, Q_MOVABLE_TYPE);
295
296#ifdef QT3_SUPPORT
297inline QDate QDate::currentDate(Qt::TimeSpec spec)
298{
299 if (spec == Qt::LocalTime)
300 return currentDate();
301 else
302 return QDateTime::currentDateTime().toUTC().date();
303}
304
305inline QTime QTime::currentTime(Qt::TimeSpec spec)
306{
307 if (spec == Qt::LocalTime)
308 return currentTime();
309 else
310 return QDateTime::currentDateTime().toUTC().time();
311}
312#endif
313
314#ifndef QT_NO_DATASTREAM
315Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDate &);
316Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &);
317Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QTime &);
318Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &);
319Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);
320Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &);
321#endif // QT_NO_DATASTREAM
322
323#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_NO_DATESTRING)
324Q_CORE_EXPORT QDebug operator<<(QDebug, const QDate &);
325Q_CORE_EXPORT QDebug operator<<(QDebug, const QTime &);
326Q_CORE_EXPORT QDebug operator<<(QDebug, const QDateTime &);
327#endif
328
329QT_END_NAMESPACE
330
331QT_END_HEADER
332
333#endif // QDATETIME_H
Note: See TracBrowser for help on using the repository browser.