Qt 4.8
qdatetime.h
Go to the documentation of this file.
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 
50 
52 
53 QT_MODULE(Core)
54 
56 {
57 public:
60  StandaloneFormat
61  };
62 public:
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
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 
136 private:
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 };
147 
149 {
150 public:
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
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;
197 private:
198  enum TimeFlag { NullTime = -1 };
199  inline int ds() const { return mds == -1 ? 0 : mds; }
200  int mds;
201 #if defined(Q_OS_WINCE)
203 #endif
204 
205  friend class QDateTime;
206  friend class QDateTimePrivate;
207 #ifndef QT_NO_DATASTREAM
210 #endif
211 };
213 
214 class QDateTimePrivate;
215 
217 {
218 public:
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
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 
291 private:
292  friend class QDateTimePrivate;
293  void detach();
295 
296 #ifndef QT_NO_DATASTREAM
299 #endif
300 };
302 
303 #ifdef QT3_SUPPORT
305 {
306  if (spec == Qt::LocalTime)
307  return currentDate();
308  else
310 }
311 
313 {
314  if (spec == Qt::LocalTime)
315  return currentTime();
316  else
318 }
319 #endif
320 
321 #ifndef QT_NO_DATASTREAM
328 #endif // QT_NO_DATASTREAM
329 
330 #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_NO_DATESTRING)
334 #endif
335 
337 
339 
340 #endif // QDATETIME_H
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
double d
Definition: qnumeric_p.h:62
bool operator<(const QTime &other) const
Returns true if this time is earlier than t; otherwise returns false.
Definition: qdatetime.h:178
TimeFlag
Definition: qdatetime.h:198
bool operator<=(const QTime &other) const
Returns true if this time is earlier than or equal to t; otherwise returns false. ...
Definition: qdatetime.h:179
int type
Definition: qmetatype.cpp:239
bool operator<=(const QDateTime &other) const
Returns true if this datetime is earlier than or equal to the other datetime; otherwise returns false...
Definition: qdatetime.h:259
static double elapsed(qint64 after, qint64 before)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
bool isNull() const
Returns true if the date is null; otherwise returns false.
Definition: qdatetime.h:66
#define QT_MODULE(x)
Definition: qglobal.h:2783
bool operator!=(const QDateTime &other) const
Returns true if this datetime is different from the other datetime; otherwise returns false...
Definition: qdatetime.h:257
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
bool isNull() const
Returns true if the time is null (i.
Definition: qdatetime.h:158
bool operator==(const QTime &other) const
Returns true if this time is equal to t; otherwise returns false.
Definition: qdatetime.h:176
bool operator>=(const QDate &other) const
Returns true if this date is later than or equal to d; otherwise returns false.
Definition: qdatetime.h:112
bool operator!=(const QDate &other) const
Returns true if this date is different from d; otherwise returns false.
Definition: qdatetime.h:108
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &, QDate &)
The QDate class provides date functions.
Definition: qdatetime.h:55
bool operator>=(const QDateTime &other) const
Returns true if this datetime is later than or equal to the other datetime; otherwise returns false...
Definition: qdatetime.h:261
QDate()
Constructs a null date.
Definition: qdatetime.h:63
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QDate fromJulianDay(int jd)
Converts the Julian day jd to a QDate.
Definition: qdatetime.h:133
bool operator<(int priority, const QPair< QRunnable *, int > &p)
Definition: qthreadpool.cpp:50
MonthNameType
This enum describes the types of the string representation used for the month name.
Definition: qdatetime.h:58
bool operator>=(const QTime &other) const
Returns true if this time is later than or equal to t; otherwise returns false.
Definition: qdatetime.h:181
TimeSpec
Definition: qnamespace.h:1404
static QString toString(Register *reg, int type, bool *ok=0)
int ds() const
Definition: qdatetime.h:199
The QTime class provides clock time functions.
Definition: qdatetime.h:148
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int startTick
Definition: qdatetime.h:202
static QDate currentDate()
Returns the current date, as reported by the system clock.
Definition: qdatetime.cpp:3115
QDateTime toLocalTime() const
Returns a datetime containing the date and time information in this datetime, but specified using the...
Definition: qdatetime.h:250
QTime()
Constructs a null time object.
Definition: qdatetime.h:151
bool operator>(const QTime &other) const
Returns true if this time is later than t; otherwise returns false.
Definition: qdatetime.h:180
Q_DECLARE_TYPEINFO(QDate, Q_MOVABLE_TYPE)
unsigned int uint
Definition: qglobal.h:996
__int64 qint64
Definition: qglobal.h:942
bool operator>(const QDateTime &other) const
Returns true if this datetime is later than the other datetime; otherwise returns false...
Definition: qdatetime.h:260
QDateTime toUTC() const
Returns a datetime containing the date and time information in this datetime, but specified using the...
Definition: qdatetime.h:251
bool operator!=(const QTime &other) const
Returns true if this time is different from t; otherwise returns false.
Definition: qdatetime.h:177
bool operator<=(const QDate &other) const
Returns true if this date is earlier than or equal to d; otherwise returns false. ...
Definition: qdatetime.h:110
QDate date() const
Returns the date part of the datetime.
Definition: qdatetime.cpp:2357
int toJulianDay() const
Converts the date to a Julian day.
Definition: qdatetime.h:134
Q_CORE_EXPORT QDataStream & operator<<(QDataStream &, const QDate &)
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
int mds
Definition: qdatetime.h:200
static QTime currentTime()
Returns the current time as reported by the system clock.
Definition: qdatetime.cpp:3125
bool operator<(const QDate &other) const
Returns true if this date is earlier than d; otherwise returns false.
Definition: qdatetime.h:109
if(void) toggleToolbarShown
static QDateTime currentDateTime()
Returns the current datetime, as reported by the system clock, in the local time zone.
Definition: qdatetime.cpp:3138
bool operator==(const QDate &other) const
Returns true if this date is equal to d; otherwise returns false.
Definition: qdatetime.h:107
static const char months[]
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294
QTime time() const
Returns the time part of the datetime.
Definition: qdatetime.cpp:2368
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
uint jd
Definition: qdatetime.h:137
#define class
#define QT_END_HEADER
Definition: qglobal.h:137
QDataStream & operator<<(QDataStream &out, const QUrl &url)
Writes url url to the stream out and returns a reference to the stream.
Definition: qurl.cpp:6757
bool operator==(QBool b1, bool b2)
Definition: qglobal.h:2023
bool operator>(const QDate &other) const
Returns true if this date is later than d; otherwise returns false.
Definition: qdatetime.h:111
static bool isNull(const QVariant::Private *d)
Definition: qvariant.cpp:300
QDataStream & operator>>(QDataStream &in, QUrl &url)
Reads a url into url from the stream in and returns a reference to the stream.
Definition: qurl.cpp:6774
DateFormat
Definition: qnamespace.h:1392