Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/tools/qdatetime.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Copyright (C) 2016 Intel Corporation. | - | ||||||||||||||||||
5 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||||||||
6 | ** | - | ||||||||||||||||||
7 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||||||||
8 | ** | - | ||||||||||||||||||
9 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||||||||
10 | ** Commercial License Usage | - | ||||||||||||||||||
11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
12 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
15 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
16 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||||||||
17 | ** | - | ||||||||||||||||||
18 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
20 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||||||||
21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||||||||
22 | ** packaging of this file. Please review the following information to | - | ||||||||||||||||||
23 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||||||||
24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||||||||
25 | ** | - | ||||||||||||||||||
26 | ** GNU General Public License Usage | - | ||||||||||||||||||
27 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||||||||
28 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||||||||
29 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||||||||
30 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||||||||
31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||||||||
32 | ** included in the packaging of this file. Please review the following | - | ||||||||||||||||||
33 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||||||||
34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||||||||
35 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||||||||
36 | ** | - | ||||||||||||||||||
37 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
38 | ** | - | ||||||||||||||||||
39 | ****************************************************************************/ | - | ||||||||||||||||||
40 | - | |||||||||||||||||||
41 | #include "qplatformdefs.h" | - | ||||||||||||||||||
42 | #include "private/qdatetime_p.h" | - | ||||||||||||||||||
43 | #include "private/qdatetimeparser_p.h" | - | ||||||||||||||||||
44 | - | |||||||||||||||||||
45 | #include "qdatastream.h" | - | ||||||||||||||||||
46 | #include "qset.h" | - | ||||||||||||||||||
47 | #include "qlocale.h" | - | ||||||||||||||||||
48 | #include "qdatetime.h" | - | ||||||||||||||||||
49 | #include "qtimezoneprivate_p.h" | - | ||||||||||||||||||
50 | #include "qregexp.h" | - | ||||||||||||||||||
51 | #include "qdebug.h" | - | ||||||||||||||||||
52 | #ifndef Q_OS_WIN | - | ||||||||||||||||||
53 | #include <locale.h> | - | ||||||||||||||||||
54 | #endif | - | ||||||||||||||||||
55 | - | |||||||||||||||||||
56 | #include <cmath> | - | ||||||||||||||||||
57 | #include <time.h> | - | ||||||||||||||||||
58 | #ifdef Q_OS_WIN | - | ||||||||||||||||||
59 | # include <qt_windows.h> | - | ||||||||||||||||||
60 | # ifdef Q_OS_WINCE | - | ||||||||||||||||||
61 | # include "qfunctions_wince.h" | - | ||||||||||||||||||
62 | # endif | - | ||||||||||||||||||
63 | # ifdef Q_OS_WINRT | - | ||||||||||||||||||
64 | # include "qfunctions_winrt.h" | - | ||||||||||||||||||
65 | # endif | - | ||||||||||||||||||
66 | #endif | - | ||||||||||||||||||
67 | - | |||||||||||||||||||
68 | #if defined(Q_OS_MAC) | - | ||||||||||||||||||
69 | #include <private/qcore_mac_p.h> | - | ||||||||||||||||||
70 | #endif | - | ||||||||||||||||||
71 | - | |||||||||||||||||||
72 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
73 | - | |||||||||||||||||||
74 | Q_GLOBAL_STATIC_WITH_ARGS(QSharedDataPointer<QDateTimePrivate>, defaultDateTimePrivate, (new QDateTimePrivate())) executed 326 times by 128 tests: end of block Executed by:
executed 326 times by 128 tests: guard.store(QtGlobalStatic::Destroyed); Executed by:
executed 7100330 times by 127 tests: return &holder.value; Executed by:
| 0-7100330 | ||||||||||||||||||
75 | - | |||||||||||||||||||
76 | /***************************************************************************** | - | ||||||||||||||||||
77 | Date/Time Constants | - | ||||||||||||||||||
78 | *****************************************************************************/ | - | ||||||||||||||||||
79 | - | |||||||||||||||||||
80 | enum { | - | ||||||||||||||||||
81 | SECS_PER_DAY = 86400, | - | ||||||||||||||||||
82 | MSECS_PER_DAY = 86400000, | - | ||||||||||||||||||
83 | SECS_PER_HOUR = 3600, | - | ||||||||||||||||||
84 | MSECS_PER_HOUR = 3600000, | - | ||||||||||||||||||
85 | SECS_PER_MIN = 60, | - | ||||||||||||||||||
86 | MSECS_PER_MIN = 60000, | - | ||||||||||||||||||
87 | TIME_T_MAX = 2145916799, // int maximum 2037-12-31T23:59:59 UTC | - | ||||||||||||||||||
88 | JULIAN_DAY_FOR_EPOCH = 2440588 // result of julianDayFromDate(1970, 1, 1) | - | ||||||||||||||||||
89 | }; | - | ||||||||||||||||||
90 | - | |||||||||||||||||||
91 | /***************************************************************************** | - | ||||||||||||||||||
92 | QDate static helper functions | - | ||||||||||||||||||
93 | *****************************************************************************/ | - | ||||||||||||||||||
94 | - | |||||||||||||||||||
95 | static inline QDate fixedDate(int y, int m, int d) | - | ||||||||||||||||||
96 | { | - | ||||||||||||||||||
97 | QDate result(y, m, 1); | - | ||||||||||||||||||
98 | result.setDate(y, m, qMin(d, result.daysInMonth())); | - | ||||||||||||||||||
99 | return result; | - | ||||||||||||||||||
100 | } | - | ||||||||||||||||||
101 | - | |||||||||||||||||||
102 | /* | - | ||||||||||||||||||
103 | Division, rounding down (rather than towards zero). | - | ||||||||||||||||||
104 | - | |||||||||||||||||||
105 | From C++11 onwards, integer division is defined to round towards zero, so we | - | ||||||||||||||||||
106 | can rely on that when implementing this. This is only used with denominator b | - | ||||||||||||||||||
107 | > 0, so we only have to treat negative numerator, a, specially. | - | ||||||||||||||||||
108 | */ | - | ||||||||||||||||||
109 | static inline qint64 floordiv(qint64 a, int b) | - | ||||||||||||||||||
110 | { | - | ||||||||||||||||||
111 | return (a - (a % b< 0 ? b - 1 : 0)) / b; executed 96372952 times by 106 tests: return (a - (a < 0 ? b - 1 : 0)) / b; Executed by:
| 96372952 | ||||||||||||||||||
112 | } | - | ||||||||||||||||||
113 | - | |||||||||||||||||||
114 | static inline int floordiv(int a, int b) | - | ||||||||||||||||||
115 | { | - | ||||||||||||||||||
116 | return (a - (a % b< 0 ? b - 1 : 0)) / b; executed 138065546 times by 106 tests: return (a - (a < 0 ? b - 1 : 0)) / b; Executed by:
| 138065546 | ||||||||||||||||||
117 | } | - | ||||||||||||||||||
118 | - | |||||||||||||||||||
119 | static inline qint64 julianDayFromDate(int year, int month, int day) | - | ||||||||||||||||||
120 | { | - | ||||||||||||||||||
121 | // Adjust for no year 0 | - | ||||||||||||||||||
122 | if (year < 0) | - | ||||||||||||||||||
123 | ++year; | - | ||||||||||||||||||
124 | - | |||||||||||||||||||
125 | /* | - | ||||||||||||||||||
126 | * Math from The Calendar FAQ at http://www.tondering.dk/claus/cal/julperiod.php | - | ||||||||||||||||||
127 | * This formula is correct for all julian days, when using mathematical integer | - | ||||||||||||||||||
128 | * division (round to negative infinity), not c++11 integer division (round to zero) | - | ||||||||||||||||||
129 | */ | - | ||||||||||||||||||
130 | int a = floordiv(14 - month, 12); | - | ||||||||||||||||||
131 | qint64 y = (qint64)year + 4800 - a; | - | ||||||||||||||||||
132 | int m = month + 12 * a - 3; | - | ||||||||||||||||||
133 | return day + floordiv(153 * m + 2, 5) + 365 * y + floordiv(y, 4) - floordiv(y, 100) + floordiv(y, 400) - 32045; | - | ||||||||||||||||||
134 | } | - | ||||||||||||||||||
135 | - | |||||||||||||||||||
136 | struct ParsedDate | - | ||||||||||||||||||
137 | { | - | ||||||||||||||||||
138 | int year, month, day; | - | ||||||||||||||||||
139 | }; | - | ||||||||||||||||||
140 | - | |||||||||||||||||||
141 | // prevent this function from being inlined into all 10 users | - | ||||||||||||||||||
142 | Q_NEVER_INLINE | - | ||||||||||||||||||
143 | static ParsedDate getDateFromJulianDay(qint64 julianDay) | - | ||||||||||||||||||
144 | { | - | ||||||||||||||||||
145 | /* | - | ||||||||||||||||||
146 | * Math from The Calendar FAQ at http://www.tondering.dk/claus/cal/julperiod.php | - | ||||||||||||||||||
147 | * This formula is correct for all julian days, when using mathematical integer | - | ||||||||||||||||||
148 | * division (round to negative infinity), not c++11 integer division (round to zero) | - | ||||||||||||||||||
149 | */ | - | ||||||||||||||||||
150 | qint64 a = julianDay + 32044; | - | ||||||||||||||||||
151 | qint64 b = floordiv(4 * a + 3, 146097); | - | ||||||||||||||||||
152 | int c = a - floordiv(146097 * b, 4); | - | ||||||||||||||||||
153 | - | |||||||||||||||||||
154 | int d = floordiv(4 * c + 3, 1461); | - | ||||||||||||||||||
155 | int e = c - floordiv(1461 * d, 4); | - | ||||||||||||||||||
156 | int m = floordiv(5 * e + 2, 153); | - | ||||||||||||||||||
157 | - | |||||||||||||||||||
158 | int day = e - floordiv(153 * m + 2, 5) + 1; | - | ||||||||||||||||||
159 | int month = m + 3 - 12 * floordiv(m, 10); | - | ||||||||||||||||||
160 | int year = 100 * b + d - 4800 + floordiv(m, 10); | - | ||||||||||||||||||
161 | - | |||||||||||||||||||
162 | // Adjust for no year 0 | - | ||||||||||||||||||
163 | if (year <= 0) | - | ||||||||||||||||||
164 | --year ; | - | ||||||||||||||||||
165 | - | |||||||||||||||||||
166 | const ParsedDate result = { year, month, day }; | - | ||||||||||||||||||
167 | return result; | - | ||||||||||||||||||
168 | } | - | ||||||||||||||||||
169 | - | |||||||||||||||||||
170 | /***************************************************************************** | - | ||||||||||||||||||
171 | Date/Time formatting helper functions | - | ||||||||||||||||||
172 | *****************************************************************************/ | - | ||||||||||||||||||
173 | - | |||||||||||||||||||
174 | static const char monthDays[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; | - | ||||||||||||||||||
175 | - | |||||||||||||||||||
176 | #ifndef QT_NO_TEXTDATE | - | ||||||||||||||||||
177 | static const char qt_shortMonthNames[][4] = { | - | ||||||||||||||||||
178 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", | - | ||||||||||||||||||
179 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; | - | ||||||||||||||||||
180 | - | |||||||||||||||||||
181 | static int qt_monthNumberFromShortName(QStringRef shortName) | - | ||||||||||||||||||
182 | { | - | ||||||||||||||||||
183 | for (unsigned int i = 0; i < sizeof(qt_shortMonthNames) / sizeof(qt_shortMonthNames[0]); ++i) { | - | ||||||||||||||||||
184 | if (shortName == QLatin1String(qt_shortMonthNames[i], 3)) | - | ||||||||||||||||||
185 | return i + 1; | - | ||||||||||||||||||
186 | } | - | ||||||||||||||||||
187 | return -1; | - | ||||||||||||||||||
188 | } | - | ||||||||||||||||||
189 | static int qt_monthNumberFromShortName(const QString &shortName) | - | ||||||||||||||||||
190 | { return qt_monthNumberFromShortName(QStringRef(&shortName)); } | - | ||||||||||||||||||
191 | - | |||||||||||||||||||
192 | static int fromShortMonthName(const QStringRef &monthName) | - | ||||||||||||||||||
193 | { | - | ||||||||||||||||||
194 | // Assume that English monthnames are the default | - | ||||||||||||||||||
195 | int month = qt_monthNumberFromShortName(monthName); | - | ||||||||||||||||||
196 | if (month != -1) | - | ||||||||||||||||||
197 | return month; | - | ||||||||||||||||||
198 | // If English names can't be found, search the localized ones | - | ||||||||||||||||||
199 | for (int i = 1; i <= 12; ++i) { | - | ||||||||||||||||||
200 | if (monthName == QDate::shortMonthName(i)) | - | ||||||||||||||||||
201 | return i; | - | ||||||||||||||||||
202 | } | - | ||||||||||||||||||
203 | return -1; | - | ||||||||||||||||||
204 | } | - | ||||||||||||||||||
205 | #endif // QT_NO_TEXTDATE | - | ||||||||||||||||||
206 | - | |||||||||||||||||||
207 | #ifndef QT_NO_DATESTRING | - | ||||||||||||||||||
208 | struct ParsedRfcDateTime { | - | ||||||||||||||||||
209 | QDate date; | - | ||||||||||||||||||
210 | QTime time; | - | ||||||||||||||||||
211 | int utcOffset; | - | ||||||||||||||||||
212 | }; | - | ||||||||||||||||||
213 | - | |||||||||||||||||||
214 | static ParsedRfcDateTime rfcDateImpl(const QString &s) | - | ||||||||||||||||||
215 | { | - | ||||||||||||||||||
216 | ParsedRfcDateTime result; | - | ||||||||||||||||||
217 | - | |||||||||||||||||||
218 | // Matches "Wdy, DD Mon YYYY HH:mm:ss ±hhmm" (Wdy, being optional) | - | ||||||||||||||||||
219 | QRegExp rex(QStringLiteral("^(?:[A-Z][a-z]+,)?[ \\t]*(\\d{1,2})[ \\t]+([A-Z][a-z]+)[ \\t]+(\\d\\d\\d\\d)(?:[ \\t]+(\\d\\d):(\\d\\d)(?::(\\d\\d))?)?[ \\t]*(?:([+-])(\\d\\d)(\\d\\d))?")); | - | ||||||||||||||||||
220 | if (s.indexOf(rex) == 0) { | - | ||||||||||||||||||
221 | const QStringList cap = rex.capturedTexts(); | - | ||||||||||||||||||
222 | result.date = QDate(cap[3].toInt(), qt_monthNumberFromShortName(cap[2]), cap[1].toInt()); | - | ||||||||||||||||||
223 | if (!cap[4].isEmpty()) | - | ||||||||||||||||||
224 | result.time = QTime(cap[4].toInt(), cap[5].toInt(), cap[6].toInt()); | - | ||||||||||||||||||
225 | const bool positiveOffset = (cap[7] == QLatin1String("+")); | - | ||||||||||||||||||
226 | const int hourOffset = cap[8].toInt(); | - | ||||||||||||||||||
227 | const int minOffset = cap[9].toInt(); | - | ||||||||||||||||||
228 | result.utcOffset = ((hourOffset * 60 + minOffset) * (positiveOffset ? 60 : -60)); | - | ||||||||||||||||||
229 | } else { | - | ||||||||||||||||||
230 | // Matches "Wdy Mon DD HH:mm:ss YYYY" | - | ||||||||||||||||||
231 | QRegExp rex(QStringLiteral("^[A-Z][a-z]+[ \\t]+([A-Z][a-z]+)[ \\t]+(\\d\\d)(?:[ \\t]+(\\d\\d):(\\d\\d):(\\d\\d))?[ \\t]+(\\d\\d\\d\\d)[ \\t]*(?:([+-])(\\d\\d)(\\d\\d))?")); | - | ||||||||||||||||||
232 | if (s.indexOf(rex) == 0) { | - | ||||||||||||||||||
233 | const QStringList cap = rex.capturedTexts(); | - | ||||||||||||||||||
234 | result.date = QDate(cap[6].toInt(), qt_monthNumberFromShortName(cap[1]), cap[2].toInt()); | - | ||||||||||||||||||
235 | if (!cap[3].isEmpty()) | - | ||||||||||||||||||
236 | result.time = QTime(cap[3].toInt(), cap[4].toInt(), cap[5].toInt()); | - | ||||||||||||||||||
237 | const bool positiveOffset = (cap[7] == QLatin1String("+")); | - | ||||||||||||||||||
238 | const int hourOffset = cap[8].toInt(); | - | ||||||||||||||||||
239 | const int minOffset = cap[9].toInt(); | - | ||||||||||||||||||
240 | result.utcOffset = ((hourOffset * 60 + minOffset) * (positiveOffset ? 60 : -60)); | - | ||||||||||||||||||
241 | } | - | ||||||||||||||||||
242 | } | - | ||||||||||||||||||
243 | - | |||||||||||||||||||
244 | return result; | - | ||||||||||||||||||
245 | } | - | ||||||||||||||||||
246 | #endif // QT_NO_DATESTRING | - | ||||||||||||||||||
247 | - | |||||||||||||||||||
248 | // Return offset in [+-]HH:mm format | - | ||||||||||||||||||
249 | static QString toOffsetString(Qt::DateFormat format, int offset) | - | ||||||||||||||||||
250 | { | - | ||||||||||||||||||
251 | return QString::asprintf("%c%02d%s%02d", | - | ||||||||||||||||||
252 | offset >= 0 ? '+' : '-', | - | ||||||||||||||||||
253 | qAbs(offset) / SECS_PER_HOUR, | - | ||||||||||||||||||
254 | // Qt::ISODate puts : between the hours and minutes, but Qt:TextDate does not: | - | ||||||||||||||||||
255 | format == Qt::TextDate ? "" : ":", | - | ||||||||||||||||||
256 | (qAbs(offset) / 60) % 60); | - | ||||||||||||||||||
257 | } | - | ||||||||||||||||||
258 | - | |||||||||||||||||||
259 | // Parse offset in [+-]HH[[:]mm] format | - | ||||||||||||||||||
260 | static int fromOffsetString(const QStringRef &offsetString, bool *valid) Q_DECL_NOTHROW | - | ||||||||||||||||||
261 | { | - | ||||||||||||||||||
262 | *valid = false; | - | ||||||||||||||||||
263 | - | |||||||||||||||||||
264 | const int size = offsetString.size(); | - | ||||||||||||||||||
265 | if (size < 2 || size > 6)
| 0-22 | ||||||||||||||||||
266 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
267 | - | |||||||||||||||||||
268 | // sign will be +1 for a positive and -1 for a negative offset | - | ||||||||||||||||||
269 | int sign; | - | ||||||||||||||||||
270 | - | |||||||||||||||||||
271 | // First char must be + or - | - | ||||||||||||||||||
272 | const QChar signChar = offsetString.at(0); | - | ||||||||||||||||||
273 | if (signChar == QLatin1Char('+'))
| 11 | ||||||||||||||||||
274 | sign = 1; executed 11 times by 1 test: sign = 1; Executed by:
| 11 | ||||||||||||||||||
275 | else if (signChar == QLatin1Char('-'))
| 1-10 | ||||||||||||||||||
276 | sign = -1; executed 10 times by 1 test: sign = -1; Executed by:
| 10 | ||||||||||||||||||
277 | else | - | ||||||||||||||||||
278 | return 0; executed 1 time by 1 test: return 0; Executed by:
| 1 | ||||||||||||||||||
279 | - | |||||||||||||||||||
280 | // Split the hour and minute parts | - | ||||||||||||||||||
281 | QVector<const QStringRef > partstime = offsetString.mid(1).split); | - | ||||||||||||||||||
282 | int hhLen = time.indexOf(QLatin1Char(':')); | - | ||||||||||||||||||
283 | int mmIndex; | - | ||||||||||||||||||
284 | if (parts.count()hhLen == -1)
| 8-13 | ||||||||||||||||||
285 | { executed 13 times by 1 test: mmIndex = hhLen = 2; Executed by:
| 13 | ||||||||||||||||||
parts.append(parts.first().mid( executed 13 times by 1 test: mmIndex = hhLen = 2));mmIndex = hhLen = 2; Executed by:
executed 13 times by 1 test: mmIndex = hhLen = 2; Executed by:
| ||||||||||||||||||||
parts[0] executed 13 times by 1 test: ; // [+-]HHmm or [+-]HH formatmmIndex = hhLen = 2; Executed by:
executed 13 times by 1 test: mmIndex = hhLen = 2; Executed by:
| ||||||||||||||||||||
286 | else | - | ||||||||||||||||||
287 | mmIndex executed 8 times by 1 test: = partshhLen + 1;mmIndex = hhLen + 1; Executed by:
executed 8 times by 1 test: mmIndex = hhLen + 1; Executed by:
| 8 | ||||||||||||||||||
288 | - | |||||||||||||||||||
289 | const QStringRef hhRef = time.first().left(2hhLen); | - | ||||||||||||||||||
290 | }bool ok = false; | - | ||||||||||||||||||
291 | const int hour = partshhRef.first().toInt(&ok); | - | ||||||||||||||||||
292 | if (!ok)
| 2-19 | ||||||||||||||||||
293 | return 0; executed 2 times by 1 test: return 0; Executed by:
| 2 | ||||||||||||||||||
294 | - | |||||||||||||||||||
295 | const QStringRef mmRef = time.mid(mmIndex); | - | ||||||||||||||||||
296 | const int minute = (partsmmRef.at(1).isEmpty())() ? 0 : partsmmRef.at(1).toInt(&ok);
| 4-15 | ||||||||||||||||||
297 | if (!ok || minute < 0 || minute > 59)
| 0-18 | ||||||||||||||||||
298 | return 0; executed 1 time by 1 test: return 0; Executed by:
| 1 | ||||||||||||||||||
299 | - | |||||||||||||||||||
300 | *valid = true; | - | ||||||||||||||||||
301 | return sign * ((hour * 60) + minute) * 60; executed 18 times by 1 test: return sign * ((hour * 60) + minute) * 60; Executed by:
| 18 | ||||||||||||||||||
302 | } | - | ||||||||||||||||||
303 | - | |||||||||||||||||||
304 | /***************************************************************************** | - | ||||||||||||||||||
305 | QDate member functions | - | ||||||||||||||||||
306 | *****************************************************************************/ | - | ||||||||||||||||||
307 | - | |||||||||||||||||||
308 | /*! | - | ||||||||||||||||||
309 | \since 4.5 | - | ||||||||||||||||||
310 | - | |||||||||||||||||||
311 | \enum QDate::MonthNameType | - | ||||||||||||||||||
312 | - | |||||||||||||||||||
313 | This enum describes the types of the string representation used | - | ||||||||||||||||||
314 | for the month name. | - | ||||||||||||||||||
315 | - | |||||||||||||||||||
316 | \value DateFormat This type of name can be used for date-to-string formatting. | - | ||||||||||||||||||
317 | \value StandaloneFormat This type is used when you need to enumerate months or weekdays. | - | ||||||||||||||||||
318 | Usually standalone names are represented in singular forms with | - | ||||||||||||||||||
319 | capitalized first letter. | - | ||||||||||||||||||
320 | */ | - | ||||||||||||||||||
321 | - | |||||||||||||||||||
322 | /*! | - | ||||||||||||||||||
323 | \class QDate | - | ||||||||||||||||||
324 | \inmodule QtCore | - | ||||||||||||||||||
325 | \reentrant | - | ||||||||||||||||||
326 | \brief The QDate class provides date functions. | - | ||||||||||||||||||
327 | - | |||||||||||||||||||
328 | - | |||||||||||||||||||
329 | A QDate object contains a calendar date, i.e. year, month, and day | - | ||||||||||||||||||
330 | numbers, in the Gregorian calendar. It can read the current date | - | ||||||||||||||||||
331 | from the system clock. It provides functions for comparing dates, | - | ||||||||||||||||||
332 | and for manipulating dates. For example, it is possible to add | - | ||||||||||||||||||
333 | and subtract days, months, and years to dates. | - | ||||||||||||||||||
334 | - | |||||||||||||||||||
335 | A QDate object is typically created by giving the year, | - | ||||||||||||||||||
336 | month, and day numbers explicitly. Note that QDate interprets two | - | ||||||||||||||||||
337 | digit years as is, i.e., years 0 - 99. A QDate can also be | - | ||||||||||||||||||
338 | constructed with the static function currentDate(), which creates | - | ||||||||||||||||||
339 | a QDate object containing the system clock's date. An explicit | - | ||||||||||||||||||
340 | date can also be set using setDate(). The fromString() function | - | ||||||||||||||||||
341 | returns a QDate given a string and a date format which is used to | - | ||||||||||||||||||
342 | interpret the date within the string. | - | ||||||||||||||||||
343 | - | |||||||||||||||||||
344 | The year(), month(), and day() functions provide access to the | - | ||||||||||||||||||
345 | year, month, and day numbers. Also, dayOfWeek() and dayOfYear() | - | ||||||||||||||||||
346 | functions are provided. The same information is provided in | - | ||||||||||||||||||
347 | textual format by the toString(), shortDayName(), longDayName(), | - | ||||||||||||||||||
348 | shortMonthName(), and longMonthName() functions. | - | ||||||||||||||||||
349 | - | |||||||||||||||||||
350 | QDate provides a full set of operators to compare two QDate | - | ||||||||||||||||||
351 | objects where smaller means earlier, and larger means later. | - | ||||||||||||||||||
352 | - | |||||||||||||||||||
353 | You can increment (or decrement) a date by a given number of days | - | ||||||||||||||||||
354 | using addDays(). Similarly you can use addMonths() and addYears(). | - | ||||||||||||||||||
355 | The daysTo() function returns the number of days between two | - | ||||||||||||||||||
356 | dates. | - | ||||||||||||||||||
357 | - | |||||||||||||||||||
358 | The daysInMonth() and daysInYear() functions return how many days | - | ||||||||||||||||||
359 | there are in this date's month and year, respectively. The | - | ||||||||||||||||||
360 | isLeapYear() function indicates whether a date is in a leap year. | - | ||||||||||||||||||
361 | - | |||||||||||||||||||
362 | \section1 | - | ||||||||||||||||||
363 | - | |||||||||||||||||||
364 | \section2 No Year 0 | - | ||||||||||||||||||
365 | - | |||||||||||||||||||
366 | There is no year 0. Dates in that year are considered invalid. The | - | ||||||||||||||||||
367 | year -1 is the year "1 before Christ" or "1 before current era." | - | ||||||||||||||||||
368 | The day before 1 January 1 CE is 31 December 1 BCE. | - | ||||||||||||||||||
369 | - | |||||||||||||||||||
370 | \section2 Range of Valid Dates | - | ||||||||||||||||||
371 | - | |||||||||||||||||||
372 | Dates are stored internally as a Julian Day number, an integer count of | - | ||||||||||||||||||
373 | every day in a contiguous range, with 24 November 4714 BCE in the Gregorian | - | ||||||||||||||||||
374 | calendar being Julian Day 0 (1 January 4713 BCE in the Julian calendar). | - | ||||||||||||||||||
375 | As well as being an efficient and accurate way of storing an absolute date, | - | ||||||||||||||||||
376 | it is suitable for converting a Date into other calendar systems such as | - | ||||||||||||||||||
377 | Hebrew, Islamic or Chinese. The Julian Day number can be obtained using | - | ||||||||||||||||||
378 | QDate::toJulianDay() and can be set using QDate::fromJulianDay(). | - | ||||||||||||||||||
379 | - | |||||||||||||||||||
380 | The range of dates able to be stored by QDate as a Julian Day number is | - | ||||||||||||||||||
381 | for technical reasons limited to between -784350574879 and 784354017364, | - | ||||||||||||||||||
382 | which means from before 2 billion BCE to after 2 billion CE. | - | ||||||||||||||||||
383 | - | |||||||||||||||||||
384 | \sa QTime, QDateTime, QDateEdit, QDateTimeEdit, QCalendarWidget | - | ||||||||||||||||||
385 | */ | - | ||||||||||||||||||
386 | - | |||||||||||||||||||
387 | /*! | - | ||||||||||||||||||
388 | \fn QDate::QDate() | - | ||||||||||||||||||
389 | - | |||||||||||||||||||
390 | Constructs a null date. Null dates are invalid. | - | ||||||||||||||||||
391 | - | |||||||||||||||||||
392 | \sa isNull(), isValid() | - | ||||||||||||||||||
393 | */ | - | ||||||||||||||||||
394 | - | |||||||||||||||||||
395 | /*! | - | ||||||||||||||||||
396 | Constructs a date with year \a y, month \a m and day \a d. | - | ||||||||||||||||||
397 | - | |||||||||||||||||||
398 | If the specified date is invalid, the date is not set and | - | ||||||||||||||||||
399 | isValid() returns \c false. | - | ||||||||||||||||||
400 | - | |||||||||||||||||||
401 | \warning Years 1 to 99 are interpreted as is. Year 0 is invalid. | - | ||||||||||||||||||
402 | - | |||||||||||||||||||
403 | \sa isValid() | - | ||||||||||||||||||
404 | */ | - | ||||||||||||||||||
405 | - | |||||||||||||||||||
406 | QDate::QDate(int y, int m, int d) | - | ||||||||||||||||||
407 | { | - | ||||||||||||||||||
408 | setDate(y, m, d); | - | ||||||||||||||||||
409 | } | - | ||||||||||||||||||
410 | - | |||||||||||||||||||
411 | - | |||||||||||||||||||
412 | /*! | - | ||||||||||||||||||
413 | \fn bool QDate::isNull() const | - | ||||||||||||||||||
414 | - | |||||||||||||||||||
415 | Returns \c true if the date is null; otherwise returns \c false. A null | - | ||||||||||||||||||
416 | date is invalid. | - | ||||||||||||||||||
417 | - | |||||||||||||||||||
418 | \note The behavior of this function is equivalent to isValid(). | - | ||||||||||||||||||
419 | - | |||||||||||||||||||
420 | \sa isValid() | - | ||||||||||||||||||
421 | */ | - | ||||||||||||||||||
422 | - | |||||||||||||||||||
423 | - | |||||||||||||||||||
424 | /*! | - | ||||||||||||||||||
425 | \fn bool QDate::isValid() const | - | ||||||||||||||||||
426 | - | |||||||||||||||||||
427 | Returns \c true if this date is valid; otherwise returns \c false. | - | ||||||||||||||||||
428 | - | |||||||||||||||||||
429 | \sa isNull() | - | ||||||||||||||||||
430 | */ | - | ||||||||||||||||||
431 | - | |||||||||||||||||||
432 | - | |||||||||||||||||||
433 | /*! | - | ||||||||||||||||||
434 | Returns the year of this date. Negative numbers indicate years | - | ||||||||||||||||||
435 | before 1 CE, such that year -44 is 44 BCE. | - | ||||||||||||||||||
436 | - | |||||||||||||||||||
437 | Returns 0 if the date is invalid. | - | ||||||||||||||||||
438 | - | |||||||||||||||||||
439 | \sa month(), day() | - | ||||||||||||||||||
440 | */ | - | ||||||||||||||||||
441 | - | |||||||||||||||||||
442 | int QDate::year() const | - | ||||||||||||||||||
443 | { | - | ||||||||||||||||||
444 | if (isNull()) | - | ||||||||||||||||||
445 | return 0; | - | ||||||||||||||||||
446 | - | |||||||||||||||||||
447 | return getDateFromJulianDay(jd).year; | - | ||||||||||||||||||
448 | } | - | ||||||||||||||||||
449 | - | |||||||||||||||||||
450 | /*! | - | ||||||||||||||||||
451 | Returns the number corresponding to the month of this date, using | - | ||||||||||||||||||
452 | the following convention: | - | ||||||||||||||||||
453 | - | |||||||||||||||||||
454 | \list | - | ||||||||||||||||||
455 | \li 1 = "January" | - | ||||||||||||||||||
456 | \li 2 = "February" | - | ||||||||||||||||||
457 | \li 3 = "March" | - | ||||||||||||||||||
458 | \li 4 = "April" | - | ||||||||||||||||||
459 | \li 5 = "May" | - | ||||||||||||||||||
460 | \li 6 = "June" | - | ||||||||||||||||||
461 | \li 7 = "July" | - | ||||||||||||||||||
462 | \li 8 = "August" | - | ||||||||||||||||||
463 | \li 9 = "September" | - | ||||||||||||||||||
464 | \li 10 = "October" | - | ||||||||||||||||||
465 | \li 11 = "November" | - | ||||||||||||||||||
466 | \li 12 = "December" | - | ||||||||||||||||||
467 | \endlist | - | ||||||||||||||||||
468 | - | |||||||||||||||||||
469 | Returns 0 if the date is invalid. | - | ||||||||||||||||||
470 | - | |||||||||||||||||||
471 | \sa year(), day() | - | ||||||||||||||||||
472 | */ | - | ||||||||||||||||||
473 | - | |||||||||||||||||||
474 | int QDate::month() const | - | ||||||||||||||||||
475 | { | - | ||||||||||||||||||
476 | if (isNull()) | - | ||||||||||||||||||
477 | return 0; | - | ||||||||||||||||||
478 | - | |||||||||||||||||||
479 | return getDateFromJulianDay(jd).month; | - | ||||||||||||||||||
480 | } | - | ||||||||||||||||||
481 | - | |||||||||||||||||||
482 | /*! | - | ||||||||||||||||||
483 | Returns the day of the month (1 to 31) of this date. | - | ||||||||||||||||||
484 | - | |||||||||||||||||||
485 | Returns 0 if the date is invalid. | - | ||||||||||||||||||
486 | - | |||||||||||||||||||
487 | \sa year(), month(), dayOfWeek() | - | ||||||||||||||||||
488 | */ | - | ||||||||||||||||||
489 | - | |||||||||||||||||||
490 | int QDate::day() const | - | ||||||||||||||||||
491 | { | - | ||||||||||||||||||
492 | if (isNull()) | - | ||||||||||||||||||
493 | return 0; | - | ||||||||||||||||||
494 | - | |||||||||||||||||||
495 | return getDateFromJulianDay(jd).day; | - | ||||||||||||||||||
496 | } | - | ||||||||||||||||||
497 | - | |||||||||||||||||||
498 | /*! | - | ||||||||||||||||||
499 | Returns the weekday (1 = Monday to 7 = Sunday) for this date. | - | ||||||||||||||||||
500 | - | |||||||||||||||||||
501 | Returns 0 if the date is invalid. | - | ||||||||||||||||||
502 | - | |||||||||||||||||||
503 | \sa day(), dayOfYear(), Qt::DayOfWeek | - | ||||||||||||||||||
504 | */ | - | ||||||||||||||||||
505 | - | |||||||||||||||||||
506 | int QDate::dayOfWeek() const | - | ||||||||||||||||||
507 | { | - | ||||||||||||||||||
508 | if (isNull()) | - | ||||||||||||||||||
509 | return 0; | - | ||||||||||||||||||
510 | - | |||||||||||||||||||
511 | if (jd >= 0) | - | ||||||||||||||||||
512 | return (jd % 7) + 1; | - | ||||||||||||||||||
513 | else | - | ||||||||||||||||||
514 | return ((jd + 1) % 7) + 7; | - | ||||||||||||||||||
515 | } | - | ||||||||||||||||||
516 | - | |||||||||||||||||||
517 | /*! | - | ||||||||||||||||||
518 | Returns the day of the year (1 to 365 or 366 on leap years) for | - | ||||||||||||||||||
519 | this date. | - | ||||||||||||||||||
520 | - | |||||||||||||||||||
521 | Returns 0 if the date is invalid. | - | ||||||||||||||||||
522 | - | |||||||||||||||||||
523 | \sa day(), dayOfWeek() | - | ||||||||||||||||||
524 | */ | - | ||||||||||||||||||
525 | - | |||||||||||||||||||
526 | int QDate::dayOfYear() const | - | ||||||||||||||||||
527 | { | - | ||||||||||||||||||
528 | if (isNull()) | - | ||||||||||||||||||
529 | return 0; | - | ||||||||||||||||||
530 | - | |||||||||||||||||||
531 | return jd - julianDayFromDate(year(), 1, 1) + 1; | - | ||||||||||||||||||
532 | } | - | ||||||||||||||||||
533 | - | |||||||||||||||||||
534 | /*! | - | ||||||||||||||||||
535 | Returns the number of days in the month (28 to 31) for this date. | - | ||||||||||||||||||
536 | - | |||||||||||||||||||
537 | Returns 0 if the date is invalid. | - | ||||||||||||||||||
538 | - | |||||||||||||||||||
539 | \sa day(), daysInYear() | - | ||||||||||||||||||
540 | */ | - | ||||||||||||||||||
541 | - | |||||||||||||||||||
542 | int QDate::daysInMonth() const | - | ||||||||||||||||||
543 | { | - | ||||||||||||||||||
544 | if (isNull()) | - | ||||||||||||||||||
545 | return 0; | - | ||||||||||||||||||
546 | - | |||||||||||||||||||
547 | const ParsedDate pd = getDateFromJulianDay(jd); | - | ||||||||||||||||||
548 | if (pd.month == 2 && isLeapYear(pd.year)) | - | ||||||||||||||||||
549 | return 29; | - | ||||||||||||||||||
550 | else | - | ||||||||||||||||||
551 | return monthDays[pd.month]; | - | ||||||||||||||||||
552 | } | - | ||||||||||||||||||
553 | - | |||||||||||||||||||
554 | /*! | - | ||||||||||||||||||
555 | Returns the number of days in the year (365 or 366) for this date. | - | ||||||||||||||||||
556 | - | |||||||||||||||||||
557 | Returns 0 if the date is invalid. | - | ||||||||||||||||||
558 | - | |||||||||||||||||||
559 | \sa day(), daysInMonth() | - | ||||||||||||||||||
560 | */ | - | ||||||||||||||||||
561 | - | |||||||||||||||||||
562 | int QDate::daysInYear() const | - | ||||||||||||||||||
563 | { | - | ||||||||||||||||||
564 | if (isNull()) | - | ||||||||||||||||||
565 | return 0; | - | ||||||||||||||||||
566 | - | |||||||||||||||||||
567 | return isLeapYear(getDateFromJulianDay(jd).year) ? 366 : 365; | - | ||||||||||||||||||
568 | } | - | ||||||||||||||||||
569 | - | |||||||||||||||||||
570 | /*! | - | ||||||||||||||||||
571 | Returns the week number (1 to 53), and stores the year in | - | ||||||||||||||||||
572 | *\a{yearNumber} unless \a yearNumber is null (the default). | - | ||||||||||||||||||
573 | - | |||||||||||||||||||
574 | Returns 0 if the date is invalid. | - | ||||||||||||||||||
575 | - | |||||||||||||||||||
576 | In accordance with ISO 8601, weeks start on Monday and the first | - | ||||||||||||||||||
577 | Thursday of a year is always in week 1 of that year. Most years | - | ||||||||||||||||||
578 | have 52 weeks, but some have 53. | - | ||||||||||||||||||
579 | - | |||||||||||||||||||
580 | *\a{yearNumber} is not always the same as year(). For example, 1 | - | ||||||||||||||||||
581 | January 2000 has week number 52 in the year 1999, and 31 December | - | ||||||||||||||||||
582 | 2002 has week number 1 in the year 2003. | - | ||||||||||||||||||
583 | - | |||||||||||||||||||
584 | \sa isValid() | - | ||||||||||||||||||
585 | */ | - | ||||||||||||||||||
586 | - | |||||||||||||||||||
587 | int QDate::weekNumber(int *yearNumber) const | - | ||||||||||||||||||
588 | { | - | ||||||||||||||||||
589 | if (!isValid()) | - | ||||||||||||||||||
590 | return 0; | - | ||||||||||||||||||
591 | - | |||||||||||||||||||
592 | int year = QDate::year(); | - | ||||||||||||||||||
593 | int yday = dayOfYear(); | - | ||||||||||||||||||
594 | int wday = dayOfWeek(); | - | ||||||||||||||||||
595 | - | |||||||||||||||||||
596 | int week = (yday - wday + 10) / 7; | - | ||||||||||||||||||
597 | - | |||||||||||||||||||
598 | if (week == 0) { | - | ||||||||||||||||||
599 | // last week of previous year | - | ||||||||||||||||||
600 | --year; | - | ||||||||||||||||||
601 | week = (yday + 365 + (QDate::isLeapYear(year) ? 1 : 0) - wday + 10) / 7; | - | ||||||||||||||||||
602 | Q_ASSERT(week == 52 || week == 53); | - | ||||||||||||||||||
603 | } else if (week == 53) { | - | ||||||||||||||||||
604 | // maybe first week of next year | - | ||||||||||||||||||
605 | int w = (yday - 365 - (QDate::isLeapYear(year) ? 1 : 0) - wday + 10) / 7; | - | ||||||||||||||||||
606 | if (w > 0) { | - | ||||||||||||||||||
607 | ++year; | - | ||||||||||||||||||
608 | week = w; | - | ||||||||||||||||||
609 | } | - | ||||||||||||||||||
610 | Q_ASSERT(week == 53 || week == 1); | - | ||||||||||||||||||
611 | } | - | ||||||||||||||||||
612 | - | |||||||||||||||||||
613 | if (yearNumber != 0) | - | ||||||||||||||||||
614 | *yearNumber = year; | - | ||||||||||||||||||
615 | return week; | - | ||||||||||||||||||
616 | } | - | ||||||||||||||||||
617 | - | |||||||||||||||||||
618 | #ifndef QT_NO_TEXTDATE | - | ||||||||||||||||||
619 | /*! | - | ||||||||||||||||||
620 | \since 4.5 | - | ||||||||||||||||||
621 | - | |||||||||||||||||||
622 | Returns the short name of the \a month for the representation specified | - | ||||||||||||||||||
623 | by \a type. | - | ||||||||||||||||||
624 | - | |||||||||||||||||||
625 | The months are enumerated using the following convention: | - | ||||||||||||||||||
626 | - | |||||||||||||||||||
627 | \list | - | ||||||||||||||||||
628 | \li 1 = "Jan" | - | ||||||||||||||||||
629 | \li 2 = "Feb" | - | ||||||||||||||||||
630 | \li 3 = "Mar" | - | ||||||||||||||||||
631 | \li 4 = "Apr" | - | ||||||||||||||||||
632 | \li 5 = "May" | - | ||||||||||||||||||
633 | \li 6 = "Jun" | - | ||||||||||||||||||
634 | \li 7 = "Jul" | - | ||||||||||||||||||
635 | \li 8 = "Aug" | - | ||||||||||||||||||
636 | \li 9 = "Sep" | - | ||||||||||||||||||
637 | \li 10 = "Oct" | - | ||||||||||||||||||
638 | \li 11 = "Nov" | - | ||||||||||||||||||
639 | \li 12 = "Dec" | - | ||||||||||||||||||
640 | \endlist | - | ||||||||||||||||||
641 | - | |||||||||||||||||||
642 | The month names will be localized according to the system's | - | ||||||||||||||||||
643 | locale settings, i.e. using QLocale::system(). | - | ||||||||||||||||||
644 | - | |||||||||||||||||||
645 | Returns an empty string if the date is invalid. | - | ||||||||||||||||||
646 | - | |||||||||||||||||||
647 | \sa toString(), longMonthName(), shortDayName(), longDayName() | - | ||||||||||||||||||
648 | */ | - | ||||||||||||||||||
649 | - | |||||||||||||||||||
650 | QString QDate::shortMonthName(int month, QDate::MonthNameType type) | - | ||||||||||||||||||
651 | { | - | ||||||||||||||||||
652 | if (month >= 1 || month <= 12) { | - | ||||||||||||||||||
653 | switch (type) { | - | ||||||||||||||||||
654 | case QDate::DateFormat: | - | ||||||||||||||||||
655 | return QLocale::system().monthName(month, QLocale::ShortFormat); | - | ||||||||||||||||||
656 | case QDate::StandaloneFormat: | - | ||||||||||||||||||
657 | return QLocale::system().standaloneMonthName(month, QLocale::ShortFormat); | - | ||||||||||||||||||
658 | } | - | ||||||||||||||||||
659 | } | - | ||||||||||||||||||
660 | return QString(); | - | ||||||||||||||||||
661 | } | - | ||||||||||||||||||
662 | - | |||||||||||||||||||
663 | /*! | - | ||||||||||||||||||
664 | \since 4.5 | - | ||||||||||||||||||
665 | - | |||||||||||||||||||
666 | Returns the long name of the \a month for the representation specified | - | ||||||||||||||||||
667 | by \a type. | - | ||||||||||||||||||
668 | - | |||||||||||||||||||
669 | The months are enumerated using the following convention: | - | ||||||||||||||||||
670 | - | |||||||||||||||||||
671 | \list | - | ||||||||||||||||||
672 | \li 1 = "January" | - | ||||||||||||||||||
673 | \li 2 = "February" | - | ||||||||||||||||||
674 | \li 3 = "March" | - | ||||||||||||||||||
675 | \li 4 = "April" | - | ||||||||||||||||||
676 | \li 5 = "May" | - | ||||||||||||||||||
677 | \li 6 = "June" | - | ||||||||||||||||||
678 | \li 7 = "July" | - | ||||||||||||||||||
679 | \li 8 = "August" | - | ||||||||||||||||||
680 | \li 9 = "September" | - | ||||||||||||||||||
681 | \li 10 = "October" | - | ||||||||||||||||||
682 | \li 11 = "November" | - | ||||||||||||||||||
683 | \li 12 = "December" | - | ||||||||||||||||||
684 | \endlist | - | ||||||||||||||||||
685 | - | |||||||||||||||||||
686 | The month names will be localized according to the system's | - | ||||||||||||||||||
687 | locale settings, i.e. using QLocale::system(). | - | ||||||||||||||||||
688 | - | |||||||||||||||||||
689 | Returns an empty string if the date is invalid. | - | ||||||||||||||||||
690 | - | |||||||||||||||||||
691 | \sa toString(), shortMonthName(), shortDayName(), longDayName() | - | ||||||||||||||||||
692 | */ | - | ||||||||||||||||||
693 | - | |||||||||||||||||||
694 | QString QDate::longMonthName(int month, MonthNameType type) | - | ||||||||||||||||||
695 | { | - | ||||||||||||||||||
696 | if (month >= 1 && month <= 12) { | - | ||||||||||||||||||
697 | switch (type) { | - | ||||||||||||||||||
698 | case QDate::DateFormat: | - | ||||||||||||||||||
699 | return QLocale::system().monthName(month, QLocale::LongFormat); | - | ||||||||||||||||||
700 | case QDate::StandaloneFormat: | - | ||||||||||||||||||
701 | return QLocale::system().standaloneMonthName(month, QLocale::LongFormat); | - | ||||||||||||||||||
702 | } | - | ||||||||||||||||||
703 | } | - | ||||||||||||||||||
704 | return QString(); | - | ||||||||||||||||||
705 | } | - | ||||||||||||||||||
706 | - | |||||||||||||||||||
707 | /*! | - | ||||||||||||||||||
708 | \since 4.5 | - | ||||||||||||||||||
709 | - | |||||||||||||||||||
710 | Returns the short name of the \a weekday for the representation specified | - | ||||||||||||||||||
711 | by \a type. | - | ||||||||||||||||||
712 | - | |||||||||||||||||||
713 | The days are enumerated using the following convention: | - | ||||||||||||||||||
714 | - | |||||||||||||||||||
715 | \list | - | ||||||||||||||||||
716 | \li 1 = "Mon" | - | ||||||||||||||||||
717 | \li 2 = "Tue" | - | ||||||||||||||||||
718 | \li 3 = "Wed" | - | ||||||||||||||||||
719 | \li 4 = "Thu" | - | ||||||||||||||||||
720 | \li 5 = "Fri" | - | ||||||||||||||||||
721 | \li 6 = "Sat" | - | ||||||||||||||||||
722 | \li 7 = "Sun" | - | ||||||||||||||||||
723 | \endlist | - | ||||||||||||||||||
724 | - | |||||||||||||||||||
725 | The day names will be localized according to the system's | - | ||||||||||||||||||
726 | locale settings, i.e. using QLocale::system(). | - | ||||||||||||||||||
727 | - | |||||||||||||||||||
728 | Returns an empty string if the date is invalid. | - | ||||||||||||||||||
729 | - | |||||||||||||||||||
730 | \sa toString(), shortMonthName(), longMonthName(), longDayName() | - | ||||||||||||||||||
731 | */ | - | ||||||||||||||||||
732 | - | |||||||||||||||||||
733 | QString QDate::shortDayName(int weekday, MonthNameType type) | - | ||||||||||||||||||
734 | { | - | ||||||||||||||||||
735 | if (weekday >= 1 && weekday <= 7) { | - | ||||||||||||||||||
736 | switch (type) { | - | ||||||||||||||||||
737 | case QDate::DateFormat: | - | ||||||||||||||||||
738 | return QLocale::system().dayName(weekday, QLocale::ShortFormat); | - | ||||||||||||||||||
739 | case QDate::StandaloneFormat: | - | ||||||||||||||||||
740 | return QLocale::system().standaloneDayName(weekday, QLocale::ShortFormat); | - | ||||||||||||||||||
741 | } | - | ||||||||||||||||||
742 | } | - | ||||||||||||||||||
743 | return QString(); | - | ||||||||||||||||||
744 | } | - | ||||||||||||||||||
745 | - | |||||||||||||||||||
746 | /*! | - | ||||||||||||||||||
747 | \since 4.5 | - | ||||||||||||||||||
748 | - | |||||||||||||||||||
749 | Returns the long name of the \a weekday for the representation specified | - | ||||||||||||||||||
750 | by \a type. | - | ||||||||||||||||||
751 | - | |||||||||||||||||||
752 | The days are enumerated using the following convention: | - | ||||||||||||||||||
753 | - | |||||||||||||||||||
754 | \list | - | ||||||||||||||||||
755 | \li 1 = "Monday" | - | ||||||||||||||||||
756 | \li 2 = "Tuesday" | - | ||||||||||||||||||
757 | \li 3 = "Wednesday" | - | ||||||||||||||||||
758 | \li 4 = "Thursday" | - | ||||||||||||||||||
759 | \li 5 = "Friday" | - | ||||||||||||||||||
760 | \li 6 = "Saturday" | - | ||||||||||||||||||
761 | \li 7 = "Sunday" | - | ||||||||||||||||||
762 | \endlist | - | ||||||||||||||||||
763 | - | |||||||||||||||||||
764 | The day names will be localized according to the system's | - | ||||||||||||||||||
765 | locale settings, i.e. using QLocale::system(). | - | ||||||||||||||||||
766 | - | |||||||||||||||||||
767 | Returns an empty string if the date is invalid. | - | ||||||||||||||||||
768 | - | |||||||||||||||||||
769 | \sa toString(), shortDayName(), shortMonthName(), longMonthName() | - | ||||||||||||||||||
770 | */ | - | ||||||||||||||||||
771 | - | |||||||||||||||||||
772 | QString QDate::longDayName(int weekday, MonthNameType type) | - | ||||||||||||||||||
773 | { | - | ||||||||||||||||||
774 | if (weekday >= 1 && weekday <= 7) { | - | ||||||||||||||||||
775 | switch (type) { | - | ||||||||||||||||||
776 | case QDate::DateFormat: | - | ||||||||||||||||||
777 | return QLocale::system().dayName(weekday, QLocale::LongFormat); | - | ||||||||||||||||||
778 | case QDate::StandaloneFormat: | - | ||||||||||||||||||
779 | return QLocale::system().standaloneDayName(weekday, QLocale::LongFormat); | - | ||||||||||||||||||
780 | } | - | ||||||||||||||||||
781 | } | - | ||||||||||||||||||
782 | return QString(); | - | ||||||||||||||||||
783 | } | - | ||||||||||||||||||
784 | #endif //QT_NO_TEXTDATE | - | ||||||||||||||||||
785 | - | |||||||||||||||||||
786 | #ifndef QT_NO_DATESTRING | - | ||||||||||||||||||
787 | - | |||||||||||||||||||
788 | #ifndef QT_NO_TEXTDATE | - | ||||||||||||||||||
789 | static QString toStringTextDate(QDate date) | - | ||||||||||||||||||
790 | { | - | ||||||||||||||||||
791 | const ParsedDate pd = getDateFromJulianDay(date.toJulianDay()); | - | ||||||||||||||||||
792 | static const QLatin1Char sp(' '); | - | ||||||||||||||||||
793 | return date.shortDayName(date.dayOfWeek()) + sp | - | ||||||||||||||||||
794 | + date.shortMonthName(pd.month) + sp | - | ||||||||||||||||||
795 | + QString::number(pd.day) + sp | - | ||||||||||||||||||
796 | + QString::number(pd.year); | - | ||||||||||||||||||
797 | } | - | ||||||||||||||||||
798 | #endif // QT_NO_TEXTDATE | - | ||||||||||||||||||
799 | - | |||||||||||||||||||
800 | static QString toStringIsoDate(qint64 jd) | - | ||||||||||||||||||
801 | { | - | ||||||||||||||||||
802 | const ParsedDate pd = getDateFromJulianDay(jd); | - | ||||||||||||||||||
803 | if (pd.year >= 0 && pd.year <= 9999) | - | ||||||||||||||||||
804 | return QString::asprintf("%04d-%02d-%02d", pd.year, pd.month, pd.day); | - | ||||||||||||||||||
805 | else | - | ||||||||||||||||||
806 | return QString(); | - | ||||||||||||||||||
807 | } | - | ||||||||||||||||||
808 | - | |||||||||||||||||||
809 | /*! | - | ||||||||||||||||||
810 | \fn QString QDate::toString(Qt::DateFormat format) const | - | ||||||||||||||||||
811 | - | |||||||||||||||||||
812 | \overload | - | ||||||||||||||||||
813 | - | |||||||||||||||||||
814 | Returns the date as a string. The \a format parameter determines | - | ||||||||||||||||||
815 | the format of the string. | - | ||||||||||||||||||
816 | - | |||||||||||||||||||
817 | If the \a format is Qt::TextDate, the string is formatted in | - | ||||||||||||||||||
818 | the default way. QDate::shortDayName() and QDate::shortMonthName() | - | ||||||||||||||||||
819 | are used to generate the string, so the day and month names will | - | ||||||||||||||||||
820 | be localized names using the system locale, i.e. QLocale::system(). An | - | ||||||||||||||||||
821 | example of this formatting is "Sat May 20 1995". | - | ||||||||||||||||||
822 | - | |||||||||||||||||||
823 | If the \a format is Qt::ISODate, the string format corresponds | - | ||||||||||||||||||
824 | to the ISO 8601 extended specification for representations of | - | ||||||||||||||||||
825 | dates and times, taking the form YYYY-MM-DD, where YYYY is the | - | ||||||||||||||||||
826 | year, MM is the month of the year (between 01 and 12), and DD is | - | ||||||||||||||||||
827 | the day of the month between 01 and 31. | - | ||||||||||||||||||
828 | - | |||||||||||||||||||
829 | If the \a format is Qt::SystemLocaleShortDate or | - | ||||||||||||||||||
830 | Qt::SystemLocaleLongDate, the string format depends on the locale | - | ||||||||||||||||||
831 | settings of the system. Identical to calling | - | ||||||||||||||||||
832 | QLocale::system().toString(date, QLocale::ShortFormat) or | - | ||||||||||||||||||
833 | QLocale::system().toString(date, QLocale::LongFormat). | - | ||||||||||||||||||
834 | - | |||||||||||||||||||
835 | If the \a format is Qt::DefaultLocaleShortDate or | - | ||||||||||||||||||
836 | Qt::DefaultLocaleLongDate, the string format depends on the | - | ||||||||||||||||||
837 | default application locale. This is the locale set with | - | ||||||||||||||||||
838 | QLocale::setDefault(), or the system locale if no default locale | - | ||||||||||||||||||
839 | has been set. Identical to calling | - | ||||||||||||||||||
840 | \l {QLocale::toString()}{QLocale().toString(date, QLocale::ShortFormat) } or | - | ||||||||||||||||||
841 | \l {QLocale::toString()}{QLocale().toString(date, QLocale::LongFormat)}. | - | ||||||||||||||||||
842 | - | |||||||||||||||||||
843 | If the \a format is Qt::RFC2822Date, the string is formatted in | - | ||||||||||||||||||
844 | an \l{RFC 2822} compatible way. An example of this formatting is | - | ||||||||||||||||||
845 | "20 May 1995". | - | ||||||||||||||||||
846 | - | |||||||||||||||||||
847 | If the date is invalid, an empty string will be returned. | - | ||||||||||||||||||
848 | - | |||||||||||||||||||
849 | \warning The Qt::ISODate format is only valid for years in the | - | ||||||||||||||||||
850 | range 0 to 9999. This restriction may apply to locale-aware | - | ||||||||||||||||||
851 | formats as well, depending on the locale settings. | - | ||||||||||||||||||
852 | - | |||||||||||||||||||
853 | \sa fromString(), shortDayName(), shortMonthName(), QLocale::toString() | - | ||||||||||||||||||
854 | */ | - | ||||||||||||||||||
855 | QString QDate::toString(Qt::DateFormat format) const | - | ||||||||||||||||||
856 | { | - | ||||||||||||||||||
857 | if (!isValid()) | - | ||||||||||||||||||
858 | return QString(); | - | ||||||||||||||||||
859 | - | |||||||||||||||||||
860 | switch (format) { | - | ||||||||||||||||||
861 | case Qt::SystemLocaleDate: | - | ||||||||||||||||||
862 | case Qt::SystemLocaleShortDate: | - | ||||||||||||||||||
863 | return QLocale::system().toString(*this, QLocale::ShortFormat); | - | ||||||||||||||||||
864 | case Qt::SystemLocaleLongDate: | - | ||||||||||||||||||
865 | return QLocale::system().toString(*this, QLocale::LongFormat); | - | ||||||||||||||||||
866 | case Qt::LocaleDate: | - | ||||||||||||||||||
867 | case Qt::DefaultLocaleShortDate: | - | ||||||||||||||||||
868 | return QLocale().toString(*this, QLocale::ShortFormat); | - | ||||||||||||||||||
869 | case Qt::DefaultLocaleLongDate: | - | ||||||||||||||||||
870 | return QLocale().toString(*this, QLocale::LongFormat); | - | ||||||||||||||||||
871 | case Qt::RFC2822Date: | - | ||||||||||||||||||
872 | return QLocale::c().toString(*this, QStringLiteral("dd MMM yyyy")); | - | ||||||||||||||||||
873 | default: | - | ||||||||||||||||||
874 | #ifndef QT_NO_TEXTDATE | - | ||||||||||||||||||
875 | case Qt::TextDate: | - | ||||||||||||||||||
876 | return toStringTextDate(*this); | - | ||||||||||||||||||
877 | #endif | - | ||||||||||||||||||
878 | case Qt::ISODate: | - | ||||||||||||||||||
879 | return toStringIsoDate(jd); | - | ||||||||||||||||||
880 | } | - | ||||||||||||||||||
881 | } | - | ||||||||||||||||||
882 | - | |||||||||||||||||||
883 | /*! | - | ||||||||||||||||||
884 | Returns the date as a string. The \a format parameter determines | - | ||||||||||||||||||
885 | the format of the result string. | - | ||||||||||||||||||
886 | - | |||||||||||||||||||
887 | These expressions may be used: | - | ||||||||||||||||||
888 | - | |||||||||||||||||||
889 | \table | - | ||||||||||||||||||
890 | \header \li Expression \li Output | - | ||||||||||||||||||
891 | \row \li d \li the day as number without a leading zero (1 to 31) | - | ||||||||||||||||||
892 | \row \li dd \li the day as number with a leading zero (01 to 31) | - | ||||||||||||||||||
893 | \row \li ddd | - | ||||||||||||||||||
894 | \li the abbreviated localized day name (e.g. 'Mon' to 'Sun'). | - | ||||||||||||||||||
895 | Uses the system locale to localize the name, i.e. QLocale::system(). | - | ||||||||||||||||||
896 | \row \li dddd | - | ||||||||||||||||||
897 | \li the long localized day name (e.g. 'Monday' to 'Sunday'). | - | ||||||||||||||||||
898 | Uses the system locale to localize the name, i.e. QLocale::system(). | - | ||||||||||||||||||
899 | \row \li M \li the month as number without a leading zero (1 to 12) | - | ||||||||||||||||||
900 | \row \li MM \li the month as number with a leading zero (01 to 12) | - | ||||||||||||||||||
901 | \row \li MMM | - | ||||||||||||||||||
902 | \li the abbreviated localized month name (e.g. 'Jan' to 'Dec'). | - | ||||||||||||||||||
903 | Uses the system locale to localize the name, i.e. QLocale::system(). | - | ||||||||||||||||||
904 | \row \li MMMM | - | ||||||||||||||||||
905 | \li the long localized month name (e.g. 'January' to 'December'). | - | ||||||||||||||||||
906 | Uses the system locale to localize the name, i.e. QLocale::system(). | - | ||||||||||||||||||
907 | \row \li yy \li the year as two digit number (00 to 99) | - | ||||||||||||||||||
908 | \row \li yyyy \li the year as four digit number. If the year is negative, | - | ||||||||||||||||||
909 | a minus sign is prepended in addition. | - | ||||||||||||||||||
910 | \endtable | - | ||||||||||||||||||
911 | - | |||||||||||||||||||
912 | All other input characters will be ignored. Any sequence of characters that | - | ||||||||||||||||||
913 | are enclosed in single quotes will be treated as text and not be used as an | - | ||||||||||||||||||
914 | expression. Two consecutive single quotes ("''") are replaced by a singlequote | - | ||||||||||||||||||
915 | in the output. Formats without separators (e.g. "ddMM") are currently not supported. | - | ||||||||||||||||||
916 | - | |||||||||||||||||||
917 | Example format strings (assuming that the QDate is the 20 July | - | ||||||||||||||||||
918 | 1969): | - | ||||||||||||||||||
919 | - | |||||||||||||||||||
920 | \table | - | ||||||||||||||||||
921 | \header \li Format \li Result | - | ||||||||||||||||||
922 | \row \li dd.MM.yyyy \li 20.07.1969 | - | ||||||||||||||||||
923 | \row \li ddd MMMM d yy \li Sun July 20 69 | - | ||||||||||||||||||
924 | \row \li 'The day is' dddd \li The day is Sunday | - | ||||||||||||||||||
925 | \endtable | - | ||||||||||||||||||
926 | - | |||||||||||||||||||
927 | If the datetime is invalid, an empty string will be returned. | - | ||||||||||||||||||
928 | - | |||||||||||||||||||
929 | \sa fromString(), QDateTime::toString(), QTime::toString(), QLocale::toString() | - | ||||||||||||||||||
930 | - | |||||||||||||||||||
931 | */ | - | ||||||||||||||||||
932 | QString QDate::toString(const QString& format) const | - | ||||||||||||||||||
933 | { | - | ||||||||||||||||||
934 | return QLocale::system().toString(*this, format); | - | ||||||||||||||||||
935 | } | - | ||||||||||||||||||
936 | #endif //QT_NO_DATESTRING | - | ||||||||||||||||||
937 | - | |||||||||||||||||||
938 | /*! | - | ||||||||||||||||||
939 | \fn bool QDate::setYMD(int y, int m, int d) | - | ||||||||||||||||||
940 | - | |||||||||||||||||||
941 | \deprecated in 5.0, use setDate() instead. | - | ||||||||||||||||||
942 | - | |||||||||||||||||||
943 | Sets the date's year \a y, month \a m, and day \a d. | - | ||||||||||||||||||
944 | - | |||||||||||||||||||
945 | If \a y is in the range 0 to 99, it is interpreted as 1900 to | - | ||||||||||||||||||
946 | 1999. | - | ||||||||||||||||||
947 | Returns \c false if the date is invalid. | - | ||||||||||||||||||
948 | - | |||||||||||||||||||
949 | Use setDate() instead. | - | ||||||||||||||||||
950 | */ | - | ||||||||||||||||||
951 | - | |||||||||||||||||||
952 | /*! | - | ||||||||||||||||||
953 | \since 4.2 | - | ||||||||||||||||||
954 | - | |||||||||||||||||||
955 | Sets the date's \a year, \a month, and \a day. Returns \c true if | - | ||||||||||||||||||
956 | the date is valid; otherwise returns \c false. | - | ||||||||||||||||||
957 | - | |||||||||||||||||||
958 | If the specified date is invalid, the QDate object is set to be | - | ||||||||||||||||||
959 | invalid. | - | ||||||||||||||||||
960 | - | |||||||||||||||||||
961 | \sa isValid() | - | ||||||||||||||||||
962 | */ | - | ||||||||||||||||||
963 | bool QDate::setDate(int year, int month, int day) | - | ||||||||||||||||||
964 | { | - | ||||||||||||||||||
965 | if (isValid(year, month, day)) | - | ||||||||||||||||||
966 | jd = julianDayFromDate(year, month, day); | - | ||||||||||||||||||
967 | else | - | ||||||||||||||||||
968 | jd = nullJd(); | - | ||||||||||||||||||
969 | - | |||||||||||||||||||
970 | return isValid(); | - | ||||||||||||||||||
971 | } | - | ||||||||||||||||||
972 | - | |||||||||||||||||||
973 | /*! | - | ||||||||||||||||||
974 | \since 4.5 | - | ||||||||||||||||||
975 | - | |||||||||||||||||||
976 | Extracts the date's year, month, and day, and assigns them to | - | ||||||||||||||||||
977 | *\a year, *\a month, and *\a day. The pointers may be null. | - | ||||||||||||||||||
978 | - | |||||||||||||||||||
979 | Returns 0 if the date is invalid. | - | ||||||||||||||||||
980 | - | |||||||||||||||||||
981 | \note In Qt versions prior to 5.7, this function is marked as non-\c{const}. | - | ||||||||||||||||||
982 | - | |||||||||||||||||||
983 | \sa year(), month(), day(), isValid() | - | ||||||||||||||||||
984 | */ | - | ||||||||||||||||||
985 | void QDate::getDate(int *year, int *month, int *day) const | - | ||||||||||||||||||
986 | { | - | ||||||||||||||||||
987 | ParsedDate pd = { 0, 0, 0 }; | - | ||||||||||||||||||
988 | if (isValid())
| 3-13951235 | ||||||||||||||||||
989 | pd = getDateFromJulianDay(jd); executed 13956030 times by 101 tests: pd = getDateFromJulianDay(jd); Executed by:
| 13956030 | ||||||||||||||||||
990 | - | |||||||||||||||||||
991 | if (year)
| 3-13956030 | ||||||||||||||||||
992 | *year = pd.year; executed 13956030 times by 101 tests: *year = pd.year; Executed by:
| 13956030 | ||||||||||||||||||
993 | if (month)
| 3-13955044 | ||||||||||||||||||
994 | *month = pd.month; executed 13956030 times by 101 tests: *month = pd.month; Executed by:
| 13956030 | ||||||||||||||||||
995 | if (day)
| 3-13942777 | ||||||||||||||||||
996 | *day = pd.day; executed 13954660 times by 101 tests: *day = pd.day; Executed by:
| 13954660 | ||||||||||||||||||
997 | } executed 13941005 times by 101 tests: end of block Executed by:
| 13941005 | ||||||||||||||||||
998 | - | |||||||||||||||||||
999 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) | - | ||||||||||||||||||
1000 | /*! | - | ||||||||||||||||||
1001 | \overload | - | ||||||||||||||||||
1002 | \internal | - | ||||||||||||||||||
1003 | */ | - | ||||||||||||||||||
1004 | void QDate::getDate(int *year, int *month, int *day) | - | ||||||||||||||||||
1005 | { | - | ||||||||||||||||||
1006 | qAsConst(*this).getDate(year, month, day); | - | ||||||||||||||||||
1007 | } executed 13947892 times by 101 tests: end of block Executed by:
| 13947892 | ||||||||||||||||||
1008 | #endif // < Qt 6 | - | ||||||||||||||||||
1009 | - | |||||||||||||||||||
1010 | /*! | - | ||||||||||||||||||
1011 | Returns a QDate object containing a date \a ndays later than the | - | ||||||||||||||||||
1012 | date of this object (or earlier if \a ndays is negative). | - | ||||||||||||||||||
1013 | - | |||||||||||||||||||
1014 | Returns a null date if the current date is invalid or the new date is | - | ||||||||||||||||||
1015 | out of range. | - | ||||||||||||||||||
1016 | - | |||||||||||||||||||
1017 | \sa addMonths(), addYears(), daysTo() | - | ||||||||||||||||||
1018 | */ | - | ||||||||||||||||||
1019 | - | |||||||||||||||||||
1020 | QDate QDate::addDays(qint64 ndays) const | - | ||||||||||||||||||
1021 | { | - | ||||||||||||||||||
1022 | if (isNull()) | - | ||||||||||||||||||
1023 | return QDate(); | - | ||||||||||||||||||
1024 | - | |||||||||||||||||||
1025 | // Due to limits on minJd() and maxJd() we know that any overflow | - | ||||||||||||||||||
1026 | // will be invalid and caught by fromJulianDay(). | - | ||||||||||||||||||
1027 | return fromJulianDay(jd + ndays); | - | ||||||||||||||||||
1028 | } | - | ||||||||||||||||||
1029 | - | |||||||||||||||||||
1030 | /*! | - | ||||||||||||||||||
1031 | Returns a QDate object containing a date \a nmonths later than the | - | ||||||||||||||||||
1032 | date of this object (or earlier if \a nmonths is negative). | - | ||||||||||||||||||
1033 | - | |||||||||||||||||||
1034 | \note If the ending day/month combination does not exist in the | - | ||||||||||||||||||
1035 | resulting month/year, this function will return a date that is the | - | ||||||||||||||||||
1036 | latest valid date. | - | ||||||||||||||||||
1037 | - | |||||||||||||||||||
1038 | \sa addDays(), addYears() | - | ||||||||||||||||||
1039 | */ | - | ||||||||||||||||||
1040 | - | |||||||||||||||||||
1041 | QDate QDate::addMonths(int nmonths) const | - | ||||||||||||||||||
1042 | { | - | ||||||||||||||||||
1043 | if (!isValid()) | - | ||||||||||||||||||
1044 | return QDate(); | - | ||||||||||||||||||
1045 | if (!nmonths) | - | ||||||||||||||||||
1046 | return *this; | - | ||||||||||||||||||
1047 | - | |||||||||||||||||||
1048 | int old_y, y, m, d; | - | ||||||||||||||||||
1049 | { | - | ||||||||||||||||||
1050 | const ParsedDate pd = getDateFromJulianDay(jd); | - | ||||||||||||||||||
1051 | y = pd.year; | - | ||||||||||||||||||
1052 | m = pd.month; | - | ||||||||||||||||||
1053 | d = pd.day; | - | ||||||||||||||||||
1054 | } | - | ||||||||||||||||||
1055 | old_y = y; | - | ||||||||||||||||||
1056 | - | |||||||||||||||||||
1057 | bool increasing = nmonths > 0; | - | ||||||||||||||||||
1058 | - | |||||||||||||||||||
1059 | while (nmonths != 0) { | - | ||||||||||||||||||
1060 | if (nmonths < 0 && nmonths + 12 <= 0) { | - | ||||||||||||||||||
1061 | y--; | - | ||||||||||||||||||
1062 | nmonths+=12; | - | ||||||||||||||||||
1063 | } else if (nmonths < 0) { | - | ||||||||||||||||||
1064 | m+= nmonths; | - | ||||||||||||||||||
1065 | nmonths = 0; | - | ||||||||||||||||||
1066 | if (m <= 0) { | - | ||||||||||||||||||
1067 | --y; | - | ||||||||||||||||||
1068 | m += 12; | - | ||||||||||||||||||
1069 | } | - | ||||||||||||||||||
1070 | } else if (nmonths - 12 >= 0) { | - | ||||||||||||||||||
1071 | y++; | - | ||||||||||||||||||
1072 | nmonths -= 12; | - | ||||||||||||||||||
1073 | } else if (m == 12) { | - | ||||||||||||||||||
1074 | y++; | - | ||||||||||||||||||
1075 | m = 0; | - | ||||||||||||||||||
1076 | } else { | - | ||||||||||||||||||
1077 | m += nmonths; | - | ||||||||||||||||||
1078 | nmonths = 0; | - | ||||||||||||||||||
1079 | if (m > 12) { | - | ||||||||||||||||||
1080 | ++y; | - | ||||||||||||||||||
1081 | m -= 12; | - | ||||||||||||||||||
1082 | } | - | ||||||||||||||||||
1083 | } | - | ||||||||||||||||||
1084 | } | - | ||||||||||||||||||
1085 | - | |||||||||||||||||||
1086 | // was there a sign change? | - | ||||||||||||||||||
1087 | if ((old_y > 0 && y <= 0) || | - | ||||||||||||||||||
1088 | (old_y < 0 && y >= 0)) | - | ||||||||||||||||||
1089 | // yes, adjust the date by +1 or -1 years | - | ||||||||||||||||||
1090 | y += increasing ? +1 : -1; | - | ||||||||||||||||||
1091 | - | |||||||||||||||||||
1092 | return fixedDate(y, m, d); | - | ||||||||||||||||||
1093 | } | - | ||||||||||||||||||
1094 | - | |||||||||||||||||||
1095 | /*! | - | ||||||||||||||||||
1096 | Returns a QDate object containing a date \a nyears later than the | - | ||||||||||||||||||
1097 | date of this object (or earlier if \a nyears is negative). | - | ||||||||||||||||||
1098 | - | |||||||||||||||||||
1099 | \note If the ending day/month combination does not exist in the | - | ||||||||||||||||||
1100 | resulting year (i.e., if the date was Feb 29 and the final year is | - | ||||||||||||||||||
1101 | not a leap year), this function will return a date that is the | - | ||||||||||||||||||
1102 | latest valid date (that is, Feb 28). | - | ||||||||||||||||||
1103 | - | |||||||||||||||||||
1104 | \sa addDays(), addMonths() | - | ||||||||||||||||||
1105 | */ | - | ||||||||||||||||||
1106 | - | |||||||||||||||||||
1107 | QDate QDate::addYears(int nyears) const | - | ||||||||||||||||||
1108 | { | - | ||||||||||||||||||
1109 | if (!isValid()) | - | ||||||||||||||||||
1110 | return QDate(); | - | ||||||||||||||||||
1111 | - | |||||||||||||||||||
1112 | ParsedDate pd = getDateFromJulianDay(jd); | - | ||||||||||||||||||
1113 | - | |||||||||||||||||||
1114 | int old_y = pd.year; | - | ||||||||||||||||||
1115 | pd.year += nyears; | - | ||||||||||||||||||
1116 | - | |||||||||||||||||||
1117 | // was there a sign change? | - | ||||||||||||||||||
1118 | if ((old_y > 0 && pd.year <= 0) || | - | ||||||||||||||||||
1119 | (old_y < 0 && pd.year >= 0)) | - | ||||||||||||||||||
1120 | // yes, adjust the date by +1 or -1 years | - | ||||||||||||||||||
1121 | pd.year += nyears > 0 ? +1 : -1; | - | ||||||||||||||||||
1122 | - | |||||||||||||||||||
1123 | return fixedDate(pd.year, pd.month, pd.day); | - | ||||||||||||||||||
1124 | } | - | ||||||||||||||||||
1125 | - | |||||||||||||||||||
1126 | /*! | - | ||||||||||||||||||
1127 | Returns the number of days from this date to \a d (which is | - | ||||||||||||||||||
1128 | negative if \a d is earlier than this date). | - | ||||||||||||||||||
1129 | - | |||||||||||||||||||
1130 | Returns 0 if either date is invalid. | - | ||||||||||||||||||
1131 | - | |||||||||||||||||||
1132 | Example: | - | ||||||||||||||||||
1133 | \snippet code/src_corelib_tools_qdatetime.cpp 0 | - | ||||||||||||||||||
1134 | - | |||||||||||||||||||
1135 | \sa addDays() | - | ||||||||||||||||||
1136 | */ | - | ||||||||||||||||||
1137 | - | |||||||||||||||||||
1138 | qint64 QDate::daysTo(const QDate &d) const | - | ||||||||||||||||||
1139 | { | - | ||||||||||||||||||
1140 | if (isNull() || d.isNull()) | - | ||||||||||||||||||
1141 | return 0; | - | ||||||||||||||||||
1142 | - | |||||||||||||||||||
1143 | // Due to limits on minJd() and maxJd() we know this will never overflow | - | ||||||||||||||||||
1144 | return d.jd - jd; | - | ||||||||||||||||||
1145 | } | - | ||||||||||||||||||
1146 | - | |||||||||||||||||||
1147 | - | |||||||||||||||||||
1148 | /*! | - | ||||||||||||||||||
1149 | \fn bool QDate::operator==(const QDate &d) const | - | ||||||||||||||||||
1150 | - | |||||||||||||||||||
1151 | Returns \c true if this date is equal to \a d; otherwise returns | - | ||||||||||||||||||
1152 | false. | - | ||||||||||||||||||
1153 | - | |||||||||||||||||||
1154 | */ | - | ||||||||||||||||||
1155 | - | |||||||||||||||||||
1156 | /*! | - | ||||||||||||||||||
1157 | \fn bool QDate::operator!=(const QDate &d) const | - | ||||||||||||||||||
1158 | - | |||||||||||||||||||
1159 | Returns \c true if this date is different from \a d; otherwise | - | ||||||||||||||||||
1160 | returns \c false. | - | ||||||||||||||||||
1161 | */ | - | ||||||||||||||||||
1162 | - | |||||||||||||||||||
1163 | /*! | - | ||||||||||||||||||
1164 | \fn bool QDate::operator<(const QDate &d) const | - | ||||||||||||||||||
1165 | - | |||||||||||||||||||
1166 | Returns \c true if this date is earlier than \a d; otherwise returns | - | ||||||||||||||||||
1167 | false. | - | ||||||||||||||||||
1168 | */ | - | ||||||||||||||||||
1169 | - | |||||||||||||||||||
1170 | /*! | - | ||||||||||||||||||
1171 | \fn bool QDate::operator<=(const QDate &d) const | - | ||||||||||||||||||
1172 | - | |||||||||||||||||||
1173 | Returns \c true if this date is earlier than or equal to \a d; | - | ||||||||||||||||||
1174 | otherwise returns \c false. | - | ||||||||||||||||||
1175 | */ | - | ||||||||||||||||||
1176 | - | |||||||||||||||||||
1177 | /*! | - | ||||||||||||||||||
1178 | \fn bool QDate::operator>(const QDate &d) const | - | ||||||||||||||||||
1179 | - | |||||||||||||||||||
1180 | Returns \c true if this date is later than \a d; otherwise returns | - | ||||||||||||||||||
1181 | false. | - | ||||||||||||||||||
1182 | */ | - | ||||||||||||||||||
1183 | - | |||||||||||||||||||
1184 | /*! | - | ||||||||||||||||||
1185 | \fn bool QDate::operator>=(const QDate &d) const | - | ||||||||||||||||||
1186 | - | |||||||||||||||||||
1187 | Returns \c true if this date is later than or equal to \a d; | - | ||||||||||||||||||
1188 | otherwise returns \c false. | - | ||||||||||||||||||
1189 | */ | - | ||||||||||||||||||
1190 | - | |||||||||||||||||||
1191 | /*! | - | ||||||||||||||||||
1192 | \fn QDate::currentDate() | - | ||||||||||||||||||
1193 | Returns the current date, as reported by the system clock. | - | ||||||||||||||||||
1194 | - | |||||||||||||||||||
1195 | \sa QTime::currentTime(), QDateTime::currentDateTime() | - | ||||||||||||||||||
1196 | */ | - | ||||||||||||||||||
1197 | - | |||||||||||||||||||
1198 | #ifndef QT_NO_DATESTRING | - | ||||||||||||||||||
1199 | /*! | - | ||||||||||||||||||
1200 | \fn QDate QDate::fromString(const QString &string, Qt::DateFormat format) | - | ||||||||||||||||||
1201 | - | |||||||||||||||||||
1202 | Returns the QDate represented by the \a string, using the | - | ||||||||||||||||||
1203 | \a format given, or an invalid date if the string cannot be | - | ||||||||||||||||||
1204 | parsed. | - | ||||||||||||||||||
1205 | - | |||||||||||||||||||
1206 | Note for Qt::TextDate: It is recommended that you use the | - | ||||||||||||||||||
1207 | English short month names (e.g. "Jan"). Although localized month | - | ||||||||||||||||||
1208 | names can also be used, they depend on the user's locale settings. | - | ||||||||||||||||||
1209 | - | |||||||||||||||||||
1210 | \sa toString(), QLocale::toDate() | - | ||||||||||||||||||
1211 | */ | - | ||||||||||||||||||
1212 | QDate QDate::fromString(const QString& string, Qt::DateFormat format) | - | ||||||||||||||||||
1213 | { | - | ||||||||||||||||||
1214 | if (string.isEmpty()) | - | ||||||||||||||||||
1215 | return QDate(); | - | ||||||||||||||||||
1216 | - | |||||||||||||||||||
1217 | switch (format) { | - | ||||||||||||||||||
1218 | case Qt::SystemLocaleDate: | - | ||||||||||||||||||
1219 | case Qt::SystemLocaleShortDate: | - | ||||||||||||||||||
1220 | return QLocale::system().toDate(string, QLocale::ShortFormat); | - | ||||||||||||||||||
1221 | case Qt::SystemLocaleLongDate: | - | ||||||||||||||||||
1222 | return QLocale::system().toDate(string, QLocale::LongFormat); | - | ||||||||||||||||||
1223 | case Qt::LocaleDate: | - | ||||||||||||||||||
1224 | case Qt::DefaultLocaleShortDate: | - | ||||||||||||||||||
1225 | return QLocale().toDate(string, QLocale::ShortFormat); | - | ||||||||||||||||||
1226 | case Qt::DefaultLocaleLongDate: | - | ||||||||||||||||||
1227 | return QLocale().toDate(string, QLocale::LongFormat); | - | ||||||||||||||||||
1228 | case Qt::RFC2822Date: | - | ||||||||||||||||||
1229 | return rfcDateImpl(string).date; | - | ||||||||||||||||||
1230 | default: | - | ||||||||||||||||||
1231 | #ifndef QT_NO_TEXTDATE | - | ||||||||||||||||||
1232 | case Qt::TextDate: { | - | ||||||||||||||||||
1233 | QVector<QStringRef> parts = string.splitRef(QLatin1Char(' '), QString::SkipEmptyParts); | - | ||||||||||||||||||
1234 | - | |||||||||||||||||||
1235 | if (parts.count() != 4) | - | ||||||||||||||||||
1236 | return QDate(); | - | ||||||||||||||||||
1237 | - | |||||||||||||||||||
1238 | QStringRef monthName = parts.at(1); | - | ||||||||||||||||||
1239 | const int month = fromShortMonthName(monthName); | - | ||||||||||||||||||
1240 | if (month == -1) { | - | ||||||||||||||||||
1241 | // Month name matches neither English nor other localised name. | - | ||||||||||||||||||
1242 | return QDate(); | - | ||||||||||||||||||
1243 | } | - | ||||||||||||||||||
1244 | - | |||||||||||||||||||
1245 | bool ok = false; | - | ||||||||||||||||||
1246 | int year = parts.at(3).toInt(&ok); | - | ||||||||||||||||||
1247 | if (!ok) | - | ||||||||||||||||||
1248 | return QDate(); | - | ||||||||||||||||||
1249 | - | |||||||||||||||||||
1250 | return QDate(year, month, parts.at(2).toInt()); | - | ||||||||||||||||||
1251 | } | - | ||||||||||||||||||
1252 | #endif // QT_NO_TEXTDATE | - | ||||||||||||||||||
1253 | case Qt::ISODate: { | - | ||||||||||||||||||
1254 | // Semi-strict parsing, must be long enough and have non-numeric separators | - | ||||||||||||||||||
1255 | if (string.size() < 10 || string.at(4).isDigit() || string.at(7).isDigit() | - | ||||||||||||||||||
1256 | || (string.size() > 10 && string.at(10).isDigit())) { | - | ||||||||||||||||||
1257 | return QDate(); | - | ||||||||||||||||||
1258 | } | - | ||||||||||||||||||
1259 | const int year = string.midRef(0, 4).toInt(); | - | ||||||||||||||||||
1260 | if (year <= 0 || year > 9999) | - | ||||||||||||||||||
1261 | return QDate(); | - | ||||||||||||||||||
1262 | return QDate(year, string.midRef(5, 2).toInt(), string.midRef(8, 2).toInt()); | - | ||||||||||||||||||
1263 | } | - | ||||||||||||||||||
1264 | } | - | ||||||||||||||||||
1265 | return QDate(); | - | ||||||||||||||||||
1266 | } | - | ||||||||||||||||||
1267 | - | |||||||||||||||||||
1268 | /*! | - | ||||||||||||||||||
1269 | \fn QDate::fromString(const QString &string, const QString &format) | - | ||||||||||||||||||
1270 | - | |||||||||||||||||||
1271 | Returns the QDate represented by the \a string, using the \a | - | ||||||||||||||||||
1272 | format given, or an invalid date if the string cannot be parsed. | - | ||||||||||||||||||
1273 | - | |||||||||||||||||||
1274 | These expressions may be used for the format: | - | ||||||||||||||||||
1275 | - | |||||||||||||||||||
1276 | \table | - | ||||||||||||||||||
1277 | \header \li Expression \li Output | - | ||||||||||||||||||
1278 | \row \li d \li The day as a number without a leading zero (1 to 31) | - | ||||||||||||||||||
1279 | \row \li dd \li The day as a number with a leading zero (01 to 31) | - | ||||||||||||||||||
1280 | \row \li ddd | - | ||||||||||||||||||
1281 | \li The abbreviated localized day name (e.g. 'Mon' to 'Sun'). | - | ||||||||||||||||||
1282 | Uses the system locale to localize the name, i.e. QLocale::system(). | - | ||||||||||||||||||
1283 | \row \li dddd | - | ||||||||||||||||||
1284 | \li The long localized day name (e.g. 'Monday' to 'Sunday'). | - | ||||||||||||||||||
1285 | Uses the system locale to localize the name, i.e. QLocale::system(). | - | ||||||||||||||||||
1286 | \row \li M \li The month as a number without a leading zero (1 to 12) | - | ||||||||||||||||||
1287 | \row \li MM \li The month as a number with a leading zero (01 to 12) | - | ||||||||||||||||||
1288 | \row \li MMM | - | ||||||||||||||||||
1289 | \li The abbreviated localized month name (e.g. 'Jan' to 'Dec'). | - | ||||||||||||||||||
1290 | Uses the system locale to localize the name, i.e. QLocale::system(). | - | ||||||||||||||||||
1291 | \row \li MMMM | - | ||||||||||||||||||
1292 | \li The long localized month name (e.g. 'January' to 'December'). | - | ||||||||||||||||||
1293 | Uses the system locale to localize the name, i.e. QLocale::system(). | - | ||||||||||||||||||
1294 | \row \li yy \li The year as two digit number (00 to 99) | - | ||||||||||||||||||
1295 | \row \li yyyy \li The year as four digit number. If the year is negative, | - | ||||||||||||||||||
1296 | a minus sign is prepended in addition. | - | ||||||||||||||||||
1297 | \endtable | - | ||||||||||||||||||
1298 | - | |||||||||||||||||||
1299 | All other input characters will be treated as text. Any sequence | - | ||||||||||||||||||
1300 | of characters that are enclosed in single quotes will also be | - | ||||||||||||||||||
1301 | treated as text and will not be used as an expression. For example: | - | ||||||||||||||||||
1302 | - | |||||||||||||||||||
1303 | \snippet code/src_corelib_tools_qdatetime.cpp 1 | - | ||||||||||||||||||
1304 | - | |||||||||||||||||||
1305 | If the format is not satisfied, an invalid QDate is returned. The | - | ||||||||||||||||||
1306 | expressions that don't expect leading zeroes (d, M) will be | - | ||||||||||||||||||
1307 | greedy. This means that they will use two digits even if this | - | ||||||||||||||||||
1308 | will put them outside the accepted range of values and leaves too | - | ||||||||||||||||||
1309 | few digits for other sections. For example, the following format | - | ||||||||||||||||||
1310 | string could have meant January 30 but the M will grab two | - | ||||||||||||||||||
1311 | digits, resulting in an invalid date: | - | ||||||||||||||||||
1312 | - | |||||||||||||||||||
1313 | \snippet code/src_corelib_tools_qdatetime.cpp 2 | - | ||||||||||||||||||
1314 | - | |||||||||||||||||||
1315 | For any field that is not represented in the format the following | - | ||||||||||||||||||
1316 | defaults are used: | - | ||||||||||||||||||
1317 | - | |||||||||||||||||||
1318 | \table | - | ||||||||||||||||||
1319 | \header \li Field \li Default value | - | ||||||||||||||||||
1320 | \row \li Year \li 1900 | - | ||||||||||||||||||
1321 | \row \li Month \li 1 | - | ||||||||||||||||||
1322 | \row \li Day \li 1 | - | ||||||||||||||||||
1323 | \endtable | - | ||||||||||||||||||
1324 | - | |||||||||||||||||||
1325 | The following examples demonstrate the default values: | - | ||||||||||||||||||
1326 | - | |||||||||||||||||||
1327 | \snippet code/src_corelib_tools_qdatetime.cpp 3 | - | ||||||||||||||||||
1328 | - | |||||||||||||||||||
1329 | \sa toString(), QDateTime::fromString(), QTime::fromString(), | - | ||||||||||||||||||
1330 | QLocale::toDate() | - | ||||||||||||||||||
1331 | */ | - | ||||||||||||||||||
1332 | - | |||||||||||||||||||
1333 | QDate QDate::fromString(const QString &string, const QString &format) | - | ||||||||||||||||||
1334 | { | - | ||||||||||||||||||
1335 | QDate date; | - | ||||||||||||||||||
1336 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
1337 | QDateTimeParser dt(QVariant::Date, QDateTimeParser::FromString); | - | ||||||||||||||||||
1338 | if (dt.parseFormat(format)) | - | ||||||||||||||||||
1339 | dt.fromString(string, &date, 0); | - | ||||||||||||||||||
1340 | #else | - | ||||||||||||||||||
1341 | Q_UNUSED(string); | - | ||||||||||||||||||
1342 | Q_UNUSED(format); | - | ||||||||||||||||||
1343 | #endif | - | ||||||||||||||||||
1344 | return date; | - | ||||||||||||||||||
1345 | } | - | ||||||||||||||||||
1346 | #endif // QT_NO_DATESTRING | - | ||||||||||||||||||
1347 | - | |||||||||||||||||||
1348 | /*! | - | ||||||||||||||||||
1349 | \overload | - | ||||||||||||||||||
1350 | - | |||||||||||||||||||
1351 | Returns \c true if the specified date (\a year, \a month, and \a | - | ||||||||||||||||||
1352 | day) is valid; otherwise returns \c false. | - | ||||||||||||||||||
1353 | - | |||||||||||||||||||
1354 | Example: | - | ||||||||||||||||||
1355 | \snippet code/src_corelib_tools_qdatetime.cpp 4 | - | ||||||||||||||||||
1356 | - | |||||||||||||||||||
1357 | \sa isNull(), setDate() | - | ||||||||||||||||||
1358 | */ | - | ||||||||||||||||||
1359 | - | |||||||||||||||||||
1360 | bool QDate::isValid(int year, int month, int day) | - | ||||||||||||||||||
1361 | { | - | ||||||||||||||||||
1362 | // there is no year 0 in the Gregorian calendar | - | ||||||||||||||||||
1363 | if (year == 0) | - | ||||||||||||||||||
1364 | return false; | - | ||||||||||||||||||
1365 | - | |||||||||||||||||||
1366 | return (day > 0 && month > 0 && month <= 12) && | - | ||||||||||||||||||
1367 | (day <= monthDays[month] || (day == 29 && month == 2 && isLeapYear(year))); | - | ||||||||||||||||||
1368 | } | - | ||||||||||||||||||
1369 | - | |||||||||||||||||||
1370 | /*! | - | ||||||||||||||||||
1371 | \fn bool QDate::isLeapYear(int year) | - | ||||||||||||||||||
1372 | - | |||||||||||||||||||
1373 | Returns \c true if the specified \a year is a leap year; otherwise | - | ||||||||||||||||||
1374 | returns \c false. | - | ||||||||||||||||||
1375 | */ | - | ||||||||||||||||||
1376 | - | |||||||||||||||||||
1377 | bool QDate::isLeapYear(int y) | - | ||||||||||||||||||
1378 | { | - | ||||||||||||||||||
1379 | // No year 0 in Gregorian calendar, so -1, -5, -9 etc are leap years | - | ||||||||||||||||||
1380 | if ( y < 1) | - | ||||||||||||||||||
1381 | ++y; | - | ||||||||||||||||||
1382 | - | |||||||||||||||||||
1383 | return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0; | - | ||||||||||||||||||
1384 | } | - | ||||||||||||||||||
1385 | - | |||||||||||||||||||
1386 | /*! \fn static QDate QDate::fromJulianDay(qint64 jd) | - | ||||||||||||||||||
1387 | - | |||||||||||||||||||
1388 | Converts the Julian day \a jd to a QDate. | - | ||||||||||||||||||
1389 | - | |||||||||||||||||||
1390 | \sa toJulianDay() | - | ||||||||||||||||||
1391 | */ | - | ||||||||||||||||||
1392 | - | |||||||||||||||||||
1393 | /*! \fn int QDate::toJulianDay() const | - | ||||||||||||||||||
1394 | - | |||||||||||||||||||
1395 | Converts the date to a Julian day. | - | ||||||||||||||||||
1396 | - | |||||||||||||||||||
1397 | \sa fromJulianDay() | - | ||||||||||||||||||
1398 | */ | - | ||||||||||||||||||
1399 | - | |||||||||||||||||||
1400 | /***************************************************************************** | - | ||||||||||||||||||
1401 | QTime member functions | - | ||||||||||||||||||
1402 | *****************************************************************************/ | - | ||||||||||||||||||
1403 | - | |||||||||||||||||||
1404 | /*! | - | ||||||||||||||||||
1405 | \class QTime | - | ||||||||||||||||||
1406 | \inmodule QtCore | - | ||||||||||||||||||
1407 | \reentrant | - | ||||||||||||||||||
1408 | - | |||||||||||||||||||
1409 | \brief The QTime class provides clock time functions. | - | ||||||||||||||||||
1410 | - | |||||||||||||||||||
1411 | - | |||||||||||||||||||
1412 | A QTime object contains a clock time, i.e. the number of hours, | - | ||||||||||||||||||
1413 | minutes, seconds, and milliseconds since midnight. It can read the | - | ||||||||||||||||||
1414 | current time from the system clock and measure a span of elapsed | - | ||||||||||||||||||
1415 | time. It provides functions for comparing times and for | - | ||||||||||||||||||
1416 | manipulating a time by adding a number of milliseconds. | - | ||||||||||||||||||
1417 | - | |||||||||||||||||||
1418 | QTime uses the 24-hour clock format; it has no concept of AM/PM. | - | ||||||||||||||||||
1419 | Unlike QDateTime, QTime knows nothing about time zones or | - | ||||||||||||||||||
1420 | daylight-saving time (DST). | - | ||||||||||||||||||
1421 | - | |||||||||||||||||||
1422 | A QTime object is typically created either by giving the number | - | ||||||||||||||||||
1423 | of hours, minutes, seconds, and milliseconds explicitly, or by | - | ||||||||||||||||||
1424 | using the static function currentTime(), which creates a QTime | - | ||||||||||||||||||
1425 | object that contains the system's local time. Note that the | - | ||||||||||||||||||
1426 | accuracy depends on the accuracy of the underlying operating | - | ||||||||||||||||||
1427 | system; not all systems provide 1-millisecond accuracy. | - | ||||||||||||||||||
1428 | - | |||||||||||||||||||
1429 | The hour(), minute(), second(), and msec() functions provide | - | ||||||||||||||||||
1430 | access to the number of hours, minutes, seconds, and milliseconds | - | ||||||||||||||||||
1431 | of the time. The same information is provided in textual format by | - | ||||||||||||||||||
1432 | the toString() function. | - | ||||||||||||||||||
1433 | - | |||||||||||||||||||
1434 | QTime provides a full set of operators to compare two QTime | - | ||||||||||||||||||
1435 | objects. QTime A is considered smaller than QTime B if A is | - | ||||||||||||||||||
1436 | earlier than B. | - | ||||||||||||||||||
1437 | - | |||||||||||||||||||
1438 | The addSecs() and addMSecs() functions provide the time a given | - | ||||||||||||||||||
1439 | number of seconds or milliseconds later than a given time. | - | ||||||||||||||||||
1440 | Correspondingly, the number of seconds or milliseconds | - | ||||||||||||||||||
1441 | between two times can be found using secsTo() or msecsTo(). | - | ||||||||||||||||||
1442 | - | |||||||||||||||||||
1443 | QTime can be used to measure a span of elapsed time using the | - | ||||||||||||||||||
1444 | start(), restart(), and elapsed() functions. | - | ||||||||||||||||||
1445 | - | |||||||||||||||||||
1446 | \sa QDate, QDateTime | - | ||||||||||||||||||
1447 | */ | - | ||||||||||||||||||
1448 | - | |||||||||||||||||||
1449 | /*! | - | ||||||||||||||||||
1450 | \fn QTime::QTime() | - | ||||||||||||||||||
1451 | - | |||||||||||||||||||
1452 | Constructs a null time object. A null time can be a QTime(0, 0, 0, 0) | - | ||||||||||||||||||
1453 | (i.e., midnight) object, except that isNull() returns \c true and isValid() | - | ||||||||||||||||||
1454 | returns \c false. | - | ||||||||||||||||||
1455 | - | |||||||||||||||||||
1456 | \sa isNull(), isValid() | - | ||||||||||||||||||
1457 | */ | - | ||||||||||||||||||
1458 | - | |||||||||||||||||||
1459 | /*! | - | ||||||||||||||||||
1460 | Constructs a time with hour \a h, minute \a m, seconds \a s and | - | ||||||||||||||||||
1461 | milliseconds \a ms. | - | ||||||||||||||||||
1462 | - | |||||||||||||||||||
1463 | \a h must be in the range 0 to 23, \a m and \a s must be in the | - | ||||||||||||||||||
1464 | range 0 to 59, and \a ms must be in the range 0 to 999. | - | ||||||||||||||||||
1465 | - | |||||||||||||||||||
1466 | \sa isValid() | - | ||||||||||||||||||
1467 | */ | - | ||||||||||||||||||
1468 | - | |||||||||||||||||||
1469 | QTime::QTime(int h, int m, int s, int ms) | - | ||||||||||||||||||
1470 | { | - | ||||||||||||||||||
1471 | setHMS(h, m, s, ms); | - | ||||||||||||||||||
1472 | } | - | ||||||||||||||||||
1473 | - | |||||||||||||||||||
1474 | - | |||||||||||||||||||
1475 | /*! | - | ||||||||||||||||||
1476 | \fn bool QTime::isNull() const | - | ||||||||||||||||||
1477 | - | |||||||||||||||||||
1478 | Returns \c true if the time is null (i.e., the QTime object was | - | ||||||||||||||||||
1479 | constructed using the default constructor); otherwise returns | - | ||||||||||||||||||
1480 | false. A null time is also an invalid time. | - | ||||||||||||||||||
1481 | - | |||||||||||||||||||
1482 | \sa isValid() | - | ||||||||||||||||||
1483 | */ | - | ||||||||||||||||||
1484 | - | |||||||||||||||||||
1485 | /*! | - | ||||||||||||||||||
1486 | Returns \c true if the time is valid; otherwise returns \c false. For example, | - | ||||||||||||||||||
1487 | the time 23:30:55.746 is valid, but 24:12:30 is invalid. | - | ||||||||||||||||||
1488 | - | |||||||||||||||||||
1489 | \sa isNull() | - | ||||||||||||||||||
1490 | */ | - | ||||||||||||||||||
1491 | - | |||||||||||||||||||
1492 | bool QTime::isValid() const | - | ||||||||||||||||||
1493 | { | - | ||||||||||||||||||
1494 | return mds > NullTime && mds < MSECS_PER_DAY; | - | ||||||||||||||||||
1495 | } | - | ||||||||||||||||||
1496 | - | |||||||||||||||||||
1497 | - | |||||||||||||||||||
1498 | /*! | - | ||||||||||||||||||
1499 | Returns the hour part (0 to 23) of the time. | - | ||||||||||||||||||
1500 | - | |||||||||||||||||||
1501 | Returns -1 if the time is invalid. | - | ||||||||||||||||||
1502 | - | |||||||||||||||||||
1503 | \sa minute(), second(), msec() | - | ||||||||||||||||||
1504 | */ | - | ||||||||||||||||||
1505 | - | |||||||||||||||||||
1506 | int QTime::hour() const | - | ||||||||||||||||||
1507 | { | - | ||||||||||||||||||
1508 | if (!isValid()) | - | ||||||||||||||||||
1509 | return -1; | - | ||||||||||||||||||
1510 | - | |||||||||||||||||||
1511 | return ds() / MSECS_PER_HOUR; | - | ||||||||||||||||||
1512 | } | - | ||||||||||||||||||
1513 | - | |||||||||||||||||||
1514 | /*! | - | ||||||||||||||||||
1515 | Returns the minute part (0 to 59) of the time. | - | ||||||||||||||||||
1516 | - | |||||||||||||||||||
1517 | Returns -1 if the time is invalid. | - | ||||||||||||||||||
1518 | - | |||||||||||||||||||
1519 | \sa hour(), second(), msec() | - | ||||||||||||||||||
1520 | */ | - | ||||||||||||||||||
1521 | - | |||||||||||||||||||
1522 | int QTime::minute() const | - | ||||||||||||||||||
1523 | { | - | ||||||||||||||||||
1524 | if (!isValid()) | - | ||||||||||||||||||
1525 | return -1; | - | ||||||||||||||||||
1526 | - | |||||||||||||||||||
1527 | return (ds() % MSECS_PER_HOUR) / MSECS_PER_MIN; | - | ||||||||||||||||||
1528 | } | - | ||||||||||||||||||
1529 | - | |||||||||||||||||||
1530 | /*! | - | ||||||||||||||||||
1531 | Returns the second part (0 to 59) of the time. | - | ||||||||||||||||||
1532 | - | |||||||||||||||||||
1533 | Returns -1 if the time is invalid. | - | ||||||||||||||||||
1534 | - | |||||||||||||||||||
1535 | \sa hour(), minute(), msec() | - | ||||||||||||||||||
1536 | */ | - | ||||||||||||||||||
1537 | - | |||||||||||||||||||
1538 | int QTime::second() const | - | ||||||||||||||||||
1539 | { | - | ||||||||||||||||||
1540 | if (!isValid()) | - | ||||||||||||||||||
1541 | return -1; | - | ||||||||||||||||||
1542 | - | |||||||||||||||||||
1543 | return (ds() / 1000)%SECS_PER_MIN; | - | ||||||||||||||||||
1544 | } | - | ||||||||||||||||||
1545 | - | |||||||||||||||||||
1546 | /*! | - | ||||||||||||||||||
1547 | Returns the millisecond part (0 to 999) of the time. | - | ||||||||||||||||||
1548 | - | |||||||||||||||||||
1549 | Returns -1 if the time is invalid. | - | ||||||||||||||||||
1550 | - | |||||||||||||||||||
1551 | \sa hour(), minute(), second() | - | ||||||||||||||||||
1552 | */ | - | ||||||||||||||||||
1553 | - | |||||||||||||||||||
1554 | int QTime::msec() const | - | ||||||||||||||||||
1555 | { | - | ||||||||||||||||||
1556 | if (!isValid()) | - | ||||||||||||||||||
1557 | return -1; | - | ||||||||||||||||||
1558 | - | |||||||||||||||||||
1559 | return ds() % 1000; | - | ||||||||||||||||||
1560 | } | - | ||||||||||||||||||
1561 | - | |||||||||||||||||||
1562 | #ifndef QT_NO_DATESTRING | - | ||||||||||||||||||
1563 | /*! | - | ||||||||||||||||||
1564 | \overload | - | ||||||||||||||||||
1565 | - | |||||||||||||||||||
1566 | Returns the time as a string. The \a format parameter determines | - | ||||||||||||||||||
1567 | the format of the string. | - | ||||||||||||||||||
1568 | - | |||||||||||||||||||
1569 | If \a format is Qt::TextDate, the string format is HH:mm:ss; | - | ||||||||||||||||||
1570 | e.g. 1 second before midnight would be "23:59:59". | - | ||||||||||||||||||
1571 | - | |||||||||||||||||||
1572 | If \a format is Qt::ISODate, the string format corresponds to the | - | ||||||||||||||||||
1573 | ISO 8601 extended specification for representations of dates, | - | ||||||||||||||||||
1574 | which is also HH:mm:ss. | - | ||||||||||||||||||
1575 | - | |||||||||||||||||||
1576 | If the \a format is Qt::SystemLocaleShortDate or | - | ||||||||||||||||||
1577 | Qt::SystemLocaleLongDate, the string format depends on the locale | - | ||||||||||||||||||
1578 | settings of the system. Identical to calling | - | ||||||||||||||||||
1579 | QLocale::system().toString(time, QLocale::ShortFormat) or | - | ||||||||||||||||||
1580 | QLocale::system().toString(time, QLocale::LongFormat). | - | ||||||||||||||||||
1581 | - | |||||||||||||||||||
1582 | If the \a format is Qt::DefaultLocaleShortDate or | - | ||||||||||||||||||
1583 | Qt::DefaultLocaleLongDate, the string format depends on the | - | ||||||||||||||||||
1584 | default application locale. This is the locale set with | - | ||||||||||||||||||
1585 | QLocale::setDefault(), or the system locale if no default locale | - | ||||||||||||||||||
1586 | has been set. Identical to calling | - | ||||||||||||||||||
1587 | - | |||||||||||||||||||
1588 | \l {QLocale::toString()}{QLocale().toString(time, QLocale::ShortFormat)} or | - | ||||||||||||||||||
1589 | \l {QLocale::toString()}{QLocale().toString(time, QLocale::LongFormat)}. | - | ||||||||||||||||||
1590 | - | |||||||||||||||||||
1591 | If the \a format is Qt::RFC2822Date, the string is formatted in | - | ||||||||||||||||||
1592 | an \l{RFC 2822} compatible way. An example of this formatting is | - | ||||||||||||||||||
1593 | "23:59:20". | - | ||||||||||||||||||
1594 | - | |||||||||||||||||||
1595 | If the time is invalid, an empty string will be returned. | - | ||||||||||||||||||
1596 | - | |||||||||||||||||||
1597 | \sa fromString(), QDate::toString(), QDateTime::toString(), QLocale::toString() | - | ||||||||||||||||||
1598 | */ | - | ||||||||||||||||||
1599 | - | |||||||||||||||||||
1600 | QString QTime::toString(Qt::DateFormat format) const | - | ||||||||||||||||||
1601 | { | - | ||||||||||||||||||
1602 | if (!isValid()) | - | ||||||||||||||||||
1603 | return QString(); | - | ||||||||||||||||||
1604 | - | |||||||||||||||||||
1605 | switch (format) { | - | ||||||||||||||||||
1606 | case Qt::SystemLocaleDate: | - | ||||||||||||||||||
1607 | case Qt::SystemLocaleShortDate: | - | ||||||||||||||||||
1608 | return QLocale::system().toString(*this, QLocale::ShortFormat); | - | ||||||||||||||||||
1609 | case Qt::SystemLocaleLongDate: | - | ||||||||||||||||||
1610 | return QLocale::system().toString(*this, QLocale::LongFormat); | - | ||||||||||||||||||
1611 | case Qt::LocaleDate: | - | ||||||||||||||||||
1612 | case Qt::DefaultLocaleShortDate: | - | ||||||||||||||||||
1613 | return QLocale().toString(*this, QLocale::ShortFormat); | - | ||||||||||||||||||
1614 | case Qt::DefaultLocaleLongDate: | - | ||||||||||||||||||
1615 | return QLocale().toString(*this, QLocale::LongFormat); | - | ||||||||||||||||||
1616 | case Qt::RFC2822Date: | - | ||||||||||||||||||
1617 | case Qt::ISODate: | - | ||||||||||||||||||
1618 | case Qt::TextDate: | - | ||||||||||||||||||
1619 | default: | - | ||||||||||||||||||
1620 | return QString::asprintf("%02d:%02d:%02d", hour(), minute(), second()); | - | ||||||||||||||||||
1621 | } | - | ||||||||||||||||||
1622 | } | - | ||||||||||||||||||
1623 | - | |||||||||||||||||||
1624 | /*! | - | ||||||||||||||||||
1625 | Returns the time as a string. The \a format parameter determines | - | ||||||||||||||||||
1626 | the format of the result string. | - | ||||||||||||||||||
1627 | - | |||||||||||||||||||
1628 | These expressions may be used: | - | ||||||||||||||||||
1629 | - | |||||||||||||||||||
1630 | \table | - | ||||||||||||||||||
1631 | \header \li Expression \li Output | - | ||||||||||||||||||
1632 | \row \li h | - | ||||||||||||||||||
1633 | \li the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) | - | ||||||||||||||||||
1634 | \row \li hh | - | ||||||||||||||||||
1635 | \li the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) | - | ||||||||||||||||||
1636 | \row \li H | - | ||||||||||||||||||
1637 | \li the hour without a leading zero (0 to 23, even with AM/PM display) | - | ||||||||||||||||||
1638 | \row \li HH | - | ||||||||||||||||||
1639 | \li the hour with a leading zero (00 to 23, even with AM/PM display) | - | ||||||||||||||||||
1640 | \row \li m \li the minute without a leading zero (0 to 59) | - | ||||||||||||||||||
1641 | \row \li mm \li the minute with a leading zero (00 to 59) | - | ||||||||||||||||||
1642 | \row \li s \li the second without a leading zero (0 to 59) | - | ||||||||||||||||||
1643 | \row \li ss \li the second with a leading zero (00 to 59) | - | ||||||||||||||||||
1644 | \row \li z \li the milliseconds without leading zeroes (0 to 999) | - | ||||||||||||||||||
1645 | \row \li zzz \li the milliseconds with leading zeroes (000 to 999) | - | ||||||||||||||||||
1646 | \row \li AP or A | - | ||||||||||||||||||
1647 | \li use AM/PM display. \e A/AP will be replaced by either | - | ||||||||||||||||||
1648 | QLocale::amText() or QLocale::pmText(). | - | ||||||||||||||||||
1649 | \row \li ap or a | - | ||||||||||||||||||
1650 | \li use am/pm display. \e a/ap will be replaced by a lower-case version of | - | ||||||||||||||||||
1651 | QLocale::amText() or QLocale::pmText(). | - | ||||||||||||||||||
1652 | \row \li t \li the timezone (for example "CEST") | - | ||||||||||||||||||
1653 | \endtable | - | ||||||||||||||||||
1654 | - | |||||||||||||||||||
1655 | All other input characters will be ignored. Any sequence of characters that | - | ||||||||||||||||||
1656 | are enclosed in single quotes will be treated as text and not be used as an | - | ||||||||||||||||||
1657 | expression. Two consecutive single quotes ("''") are replaced by a singlequote | - | ||||||||||||||||||
1658 | in the output. Formats without separators (e.g. "HHmm") are currently not supported. | - | ||||||||||||||||||
1659 | - | |||||||||||||||||||
1660 | Example format strings (assuming that the QTime is 14:13:09.042 and the system | - | ||||||||||||||||||
1661 | locale is \c{en_US}) | - | ||||||||||||||||||
1662 | - | |||||||||||||||||||
1663 | \table | - | ||||||||||||||||||
1664 | \header \li Format \li Result | - | ||||||||||||||||||
1665 | \row \li hh:mm:ss.zzz \li 14:13:09.042 | - | ||||||||||||||||||
1666 | \row \li h:m:s ap \li 2:13:9 pm | - | ||||||||||||||||||
1667 | \row \li H:m:s a \li 14:13:9 pm | - | ||||||||||||||||||
1668 | \endtable | - | ||||||||||||||||||
1669 | - | |||||||||||||||||||
1670 | If the time is invalid, an empty string will be returned. | - | ||||||||||||||||||
1671 | If \a format is empty, the default format "hh:mm:ss" is used. | - | ||||||||||||||||||
1672 | - | |||||||||||||||||||
1673 | \sa fromString(), QDate::toString(), QDateTime::toString(), QLocale::toString() | - | ||||||||||||||||||
1674 | */ | - | ||||||||||||||||||
1675 | QString QTime::toString(const QString& format) const | - | ||||||||||||||||||
1676 | { | - | ||||||||||||||||||
1677 | return QLocale::system().toString(*this, format); | - | ||||||||||||||||||
1678 | } | - | ||||||||||||||||||
1679 | #endif //QT_NO_DATESTRING | - | ||||||||||||||||||
1680 | /*! | - | ||||||||||||||||||
1681 | Sets the time to hour \a h, minute \a m, seconds \a s and | - | ||||||||||||||||||
1682 | milliseconds \a ms. | - | ||||||||||||||||||
1683 | - | |||||||||||||||||||
1684 | \a h must be in the range 0 to 23, \a m and \a s must be in the | - | ||||||||||||||||||
1685 | range 0 to 59, and \a ms must be in the range 0 to 999. | - | ||||||||||||||||||
1686 | Returns \c true if the set time is valid; otherwise returns \c false. | - | ||||||||||||||||||
1687 | - | |||||||||||||||||||
1688 | \sa isValid() | - | ||||||||||||||||||
1689 | */ | - | ||||||||||||||||||
1690 | - | |||||||||||||||||||
1691 | bool QTime::setHMS(int h, int m, int s, int ms) | - | ||||||||||||||||||
1692 | { | - | ||||||||||||||||||
1693 | #if defined(Q_OS_WINCE) | - | ||||||||||||||||||
1694 | startTick = NullTime; | - | ||||||||||||||||||
1695 | #endif | - | ||||||||||||||||||
1696 | if (!isValid(h,m,s,ms)) { | - | ||||||||||||||||||
1697 | mds = NullTime; // make this invalid | - | ||||||||||||||||||
1698 | return false; | - | ||||||||||||||||||
1699 | } | - | ||||||||||||||||||
1700 | mds = (h*SECS_PER_HOUR + m*SECS_PER_MIN + s)*1000 + ms; | - | ||||||||||||||||||
1701 | return true; | - | ||||||||||||||||||
1702 | } | - | ||||||||||||||||||
1703 | - | |||||||||||||||||||
1704 | /*! | - | ||||||||||||||||||
1705 | Returns a QTime object containing a time \a s seconds later | - | ||||||||||||||||||
1706 | than the time of this object (or earlier if \a s is negative). | - | ||||||||||||||||||
1707 | - | |||||||||||||||||||
1708 | Note that the time will wrap if it passes midnight. | - | ||||||||||||||||||
1709 | - | |||||||||||||||||||
1710 | Returns a null time if this time is invalid. | - | ||||||||||||||||||
1711 | - | |||||||||||||||||||
1712 | Example: | - | ||||||||||||||||||
1713 | - | |||||||||||||||||||
1714 | \snippet code/src_corelib_tools_qdatetime.cpp 5 | - | ||||||||||||||||||
1715 | - | |||||||||||||||||||
1716 | \sa addMSecs(), secsTo(), QDateTime::addSecs() | - | ||||||||||||||||||
1717 | */ | - | ||||||||||||||||||
1718 | - | |||||||||||||||||||
1719 | QTime QTime::addSecs(int s) const | - | ||||||||||||||||||
1720 | { | - | ||||||||||||||||||
1721 | s %= SECS_PER_DAY; | - | ||||||||||||||||||
1722 | return addMSecs(s * 1000); | - | ||||||||||||||||||
1723 | } | - | ||||||||||||||||||
1724 | - | |||||||||||||||||||
1725 | /*! | - | ||||||||||||||||||
1726 | Returns the number of seconds from this time to \a t. | - | ||||||||||||||||||
1727 | If \a t is earlier than this time, the number of seconds returned | - | ||||||||||||||||||
1728 | is negative. | - | ||||||||||||||||||
1729 | - | |||||||||||||||||||
1730 | Because QTime measures time within a day and there are 86400 | - | ||||||||||||||||||
1731 | seconds in a day, the result is always between -86400 and 86400. | - | ||||||||||||||||||
1732 | - | |||||||||||||||||||
1733 | secsTo() does not take into account any milliseconds. | - | ||||||||||||||||||
1734 | - | |||||||||||||||||||
1735 | Returns 0 if either time is invalid. | - | ||||||||||||||||||
1736 | - | |||||||||||||||||||
1737 | \sa addSecs(), QDateTime::secsTo() | - | ||||||||||||||||||
1738 | */ | - | ||||||||||||||||||
1739 | - | |||||||||||||||||||
1740 | int QTime::secsTo(const QTime &t) const | - | ||||||||||||||||||
1741 | { | - | ||||||||||||||||||
1742 | if (!isValid() || !t.isValid()) | - | ||||||||||||||||||
1743 | return 0; | - | ||||||||||||||||||
1744 | - | |||||||||||||||||||
1745 | // Truncate milliseconds as we do not want to consider them. | - | ||||||||||||||||||
1746 | int ourSeconds = ds() / 1000; | - | ||||||||||||||||||
1747 | int theirSeconds = t.ds() / 1000; | - | ||||||||||||||||||
1748 | return theirSeconds - ourSeconds; | - | ||||||||||||||||||
1749 | } | - | ||||||||||||||||||
1750 | - | |||||||||||||||||||
1751 | /*! | - | ||||||||||||||||||
1752 | Returns a QTime object containing a time \a ms milliseconds later | - | ||||||||||||||||||
1753 | than the time of this object (or earlier if \a ms is negative). | - | ||||||||||||||||||
1754 | - | |||||||||||||||||||
1755 | Note that the time will wrap if it passes midnight. See addSecs() | - | ||||||||||||||||||
1756 | for an example. | - | ||||||||||||||||||
1757 | - | |||||||||||||||||||
1758 | Returns a null time if this time is invalid. | - | ||||||||||||||||||
1759 | - | |||||||||||||||||||
1760 | \sa addSecs(), msecsTo(), QDateTime::addMSecs() | - | ||||||||||||||||||
1761 | */ | - | ||||||||||||||||||
1762 | - | |||||||||||||||||||
1763 | QTime QTime::addMSecs(int ms) const | - | ||||||||||||||||||
1764 | { | - | ||||||||||||||||||
1765 | QTime t; | - | ||||||||||||||||||
1766 | if (isValid()) { | - | ||||||||||||||||||
1767 | if (ms < 0) { | - | ||||||||||||||||||
1768 | // %,/ not well-defined for -ve, so always work with +ve. | - | ||||||||||||||||||
1769 | int negdays = (MSECS_PER_DAY - ms) / MSECS_PER_DAY; | - | ||||||||||||||||||
1770 | t.mds = (ds() + ms + negdays * MSECS_PER_DAY) % MSECS_PER_DAY; | - | ||||||||||||||||||
1771 | } else { | - | ||||||||||||||||||
1772 | t.mds = (ds() + ms) % MSECS_PER_DAY; | - | ||||||||||||||||||
1773 | } | - | ||||||||||||||||||
1774 | } | - | ||||||||||||||||||
1775 | #if defined(Q_OS_WINCE) | - | ||||||||||||||||||
1776 | if (startTick > NullTime) | - | ||||||||||||||||||
1777 | t.startTick = (startTick + ms) % MSECS_PER_DAY; | - | ||||||||||||||||||
1778 | #endif | - | ||||||||||||||||||
1779 | return t; | - | ||||||||||||||||||
1780 | } | - | ||||||||||||||||||
1781 | - | |||||||||||||||||||
1782 | /*! | - | ||||||||||||||||||
1783 | Returns the number of milliseconds from this time to \a t. | - | ||||||||||||||||||
1784 | If \a t is earlier than this time, the number of milliseconds returned | - | ||||||||||||||||||
1785 | is negative. | - | ||||||||||||||||||
1786 | - | |||||||||||||||||||
1787 | Because QTime measures time within a day and there are 86400 | - | ||||||||||||||||||
1788 | seconds in a day, the result is always between -86400000 and | - | ||||||||||||||||||
1789 | 86400000 ms. | - | ||||||||||||||||||
1790 | - | |||||||||||||||||||
1791 | Returns 0 if either time is invalid. | - | ||||||||||||||||||
1792 | - | |||||||||||||||||||
1793 | \sa secsTo(), addMSecs(), QDateTime::msecsTo() | - | ||||||||||||||||||
1794 | */ | - | ||||||||||||||||||
1795 | - | |||||||||||||||||||
1796 | int QTime::msecsTo(const QTime &t) const | - | ||||||||||||||||||
1797 | { | - | ||||||||||||||||||
1798 | if (!isValid() || !t.isValid()) | - | ||||||||||||||||||
1799 | return 0; | - | ||||||||||||||||||
1800 | #if defined(Q_OS_WINCE) | - | ||||||||||||||||||
1801 | // GetLocalTime() for Windows CE has no milliseconds resolution | - | ||||||||||||||||||
1802 | if (t.startTick > NullTime && startTick > NullTime) | - | ||||||||||||||||||
1803 | return t.startTick - startTick; | - | ||||||||||||||||||
1804 | else | - | ||||||||||||||||||
1805 | #endif | - | ||||||||||||||||||
1806 | return t.ds() - ds(); | - | ||||||||||||||||||
1807 | } | - | ||||||||||||||||||
1808 | - | |||||||||||||||||||
1809 | - | |||||||||||||||||||
1810 | /*! | - | ||||||||||||||||||
1811 | \fn bool QTime::operator==(const QTime &t) const | - | ||||||||||||||||||
1812 | - | |||||||||||||||||||
1813 | Returns \c true if this time is equal to \a t; otherwise returns \c false. | - | ||||||||||||||||||
1814 | */ | - | ||||||||||||||||||
1815 | - | |||||||||||||||||||
1816 | /*! | - | ||||||||||||||||||
1817 | \fn bool QTime::operator!=(const QTime &t) const | - | ||||||||||||||||||
1818 | - | |||||||||||||||||||
1819 | Returns \c true if this time is different from \a t; otherwise returns \c false. | - | ||||||||||||||||||
1820 | */ | - | ||||||||||||||||||
1821 | - | |||||||||||||||||||
1822 | /*! | - | ||||||||||||||||||
1823 | \fn bool QTime::operator<(const QTime &t) const | - | ||||||||||||||||||
1824 | - | |||||||||||||||||||
1825 | Returns \c true if this time is earlier than \a t; otherwise returns \c false. | - | ||||||||||||||||||
1826 | */ | - | ||||||||||||||||||
1827 | - | |||||||||||||||||||
1828 | /*! | - | ||||||||||||||||||
1829 | \fn bool QTime::operator<=(const QTime &t) const | - | ||||||||||||||||||
1830 | - | |||||||||||||||||||
1831 | Returns \c true if this time is earlier than or equal to \a t; | - | ||||||||||||||||||
1832 | otherwise returns \c false. | - | ||||||||||||||||||
1833 | */ | - | ||||||||||||||||||
1834 | - | |||||||||||||||||||
1835 | /*! | - | ||||||||||||||||||
1836 | \fn bool QTime::operator>(const QTime &t) const | - | ||||||||||||||||||
1837 | - | |||||||||||||||||||
1838 | Returns \c true if this time is later than \a t; otherwise returns \c false. | - | ||||||||||||||||||
1839 | */ | - | ||||||||||||||||||
1840 | - | |||||||||||||||||||
1841 | /*! | - | ||||||||||||||||||
1842 | \fn bool QTime::operator>=(const QTime &t) const | - | ||||||||||||||||||
1843 | - | |||||||||||||||||||
1844 | Returns \c true if this time is later than or equal to \a t; | - | ||||||||||||||||||
1845 | otherwise returns \c false. | - | ||||||||||||||||||
1846 | */ | - | ||||||||||||||||||
1847 | - | |||||||||||||||||||
1848 | /*! | - | ||||||||||||||||||
1849 | \fn QTime QTime::fromMSecsSinceStartOfDay(int msecs) | - | ||||||||||||||||||
1850 | - | |||||||||||||||||||
1851 | Returns a new QTime instance with the time set to the number of \a msecs | - | ||||||||||||||||||
1852 | since the start of the day, i.e. since 00:00:00. | - | ||||||||||||||||||
1853 | - | |||||||||||||||||||
1854 | If \a msecs falls outside the valid range an invalid QTime will be returned. | - | ||||||||||||||||||
1855 | - | |||||||||||||||||||
1856 | \sa msecsSinceStartOfDay() | - | ||||||||||||||||||
1857 | */ | - | ||||||||||||||||||
1858 | - | |||||||||||||||||||
1859 | /*! | - | ||||||||||||||||||
1860 | \fn int QTime::msecsSinceStartOfDay() const | - | ||||||||||||||||||
1861 | - | |||||||||||||||||||
1862 | Returns the number of msecs since the start of the day, i.e. since 00:00:00. | - | ||||||||||||||||||
1863 | - | |||||||||||||||||||
1864 | \sa fromMSecsSinceStartOfDay() | - | ||||||||||||||||||
1865 | */ | - | ||||||||||||||||||
1866 | - | |||||||||||||||||||
1867 | /*! | - | ||||||||||||||||||
1868 | \fn QTime::currentTime() | - | ||||||||||||||||||
1869 | - | |||||||||||||||||||
1870 | Returns the current time as reported by the system clock. | - | ||||||||||||||||||
1871 | - | |||||||||||||||||||
1872 | Note that the accuracy depends on the accuracy of the underlying | - | ||||||||||||||||||
1873 | operating system; not all systems provide 1-millisecond accuracy. | - | ||||||||||||||||||
1874 | */ | - | ||||||||||||||||||
1875 | - | |||||||||||||||||||
1876 | #ifndef QT_NO_DATESTRING | - | ||||||||||||||||||
1877 | - | |||||||||||||||||||
1878 | static QTime fromIsoTimeString(const QStringRef &string, Qt::DateFormat format, bool *isMidnight24) | - | ||||||||||||||||||
1879 | { | - | ||||||||||||||||||
1880 | if (isMidnight24) | - | ||||||||||||||||||
1881 | *isMidnight24 = false; | - | ||||||||||||||||||
1882 | - | |||||||||||||||||||
1883 | const int size = string.size(); | - | ||||||||||||||||||
1884 | if (size < 5) | - | ||||||||||||||||||
1885 | return QTime(); | - | ||||||||||||||||||
1886 | - | |||||||||||||||||||
1887 | bool ok = false; | - | ||||||||||||||||||
1888 | int hour = string.mid(0, 2).toInt(&ok); | - | ||||||||||||||||||
1889 | if (!ok) | - | ||||||||||||||||||
1890 | return QTime(); | - | ||||||||||||||||||
1891 | const int minute = string.mid(3, 2).toInt(&ok); | - | ||||||||||||||||||
1892 | if (!ok) | - | ||||||||||||||||||
1893 | return QTime(); | - | ||||||||||||||||||
1894 | int second = 0; | - | ||||||||||||||||||
1895 | int msec = 0; | - | ||||||||||||||||||
1896 | - | |||||||||||||||||||
1897 | if (size == 5) { | - | ||||||||||||||||||
1898 | // HH:mm format | - | ||||||||||||||||||
1899 | second = 0; | - | ||||||||||||||||||
1900 | msec = 0; | - | ||||||||||||||||||
1901 | } else if (string.at(5) == QLatin1Char(',') || string.at(5) == QLatin1Char('.')) { | - | ||||||||||||||||||
1902 | if (format == Qt::TextDate) | - | ||||||||||||||||||
1903 | return QTime(); | - | ||||||||||||||||||
1904 | // ISODate HH:mm.ssssss format | - | ||||||||||||||||||
1905 | // We only want 5 digits worth of fraction of minute. This follows the existing | - | ||||||||||||||||||
1906 | // behavior that determines how milliseconds are read; 4 millisecond digits are | - | ||||||||||||||||||
1907 | // read and then rounded to 3. If we read at most 5 digits for fraction of minute, | - | ||||||||||||||||||
1908 | // the maximum amount of millisecond digits it will expand to once converted to | - | ||||||||||||||||||
1909 | // seconds is 4. E.g. 12:34,99999 will expand to 12:34:59.9994. The milliseconds | - | ||||||||||||||||||
1910 | // will then be rounded up AND clamped to 999. | - | ||||||||||||||||||
1911 | - | |||||||||||||||||||
1912 | const QStringRef minuteFractionStr = string.mid(6, 5); | - | ||||||||||||||||||
1913 | const long minuteFractionInt = minuteFractionStr.toLong(&ok); | - | ||||||||||||||||||
1914 | if (!ok) | - | ||||||||||||||||||
1915 | return QTime(); | - | ||||||||||||||||||
1916 | const float minuteFraction = double(minuteFractionInt) / (std::pow(double(10), minuteFractionStr.count())); | - | ||||||||||||||||||
1917 | - | |||||||||||||||||||
1918 | const float secondWithMs = minuteFraction * 60; | - | ||||||||||||||||||
1919 | const float secondNoMs = std::floor(secondWithMs); | - | ||||||||||||||||||
1920 | const float secondFraction = secondWithMs - secondNoMs; | - | ||||||||||||||||||
1921 | second = secondNoMs; | - | ||||||||||||||||||
1922 | msec = qMin(qRound(secondFraction * 1000.0), 999); | - | ||||||||||||||||||
1923 | } else { | - | ||||||||||||||||||
1924 | // HH:mm:ss or HH:mm:ss.zzz | - | ||||||||||||||||||
1925 | second = string.mid(6, 2).toInt(&ok); | - | ||||||||||||||||||
1926 | if (!ok) | - | ||||||||||||||||||
1927 | return QTime(); | - | ||||||||||||||||||
1928 | if (size > 8 && (string.at(8) == QLatin1Char(',') || string.at(8) == QLatin1Char('.'))) { | - | ||||||||||||||||||
1929 | const QStringRef msecStr(string.mid(9, 4)); | - | ||||||||||||||||||
1930 | int msecInt = msecStr.isEmpty() ? 0 : msecStr.toInt(&ok); | - | ||||||||||||||||||
1931 | if (!ok) | - | ||||||||||||||||||
1932 | return QTime(); | - | ||||||||||||||||||
1933 | const double secondFraction(msecInt / (std::pow(double(10), msecStr.count()))); | - | ||||||||||||||||||
1934 | msec = qMin(qRound(secondFraction * 1000.0), 999); | - | ||||||||||||||||||
1935 | } | - | ||||||||||||||||||
1936 | } | - | ||||||||||||||||||
1937 | - | |||||||||||||||||||
1938 | if (format == Qt::ISODate && hour == 24 && minute == 0 && second == 0 && msec == 0) { | - | ||||||||||||||||||
1939 | if (isMidnight24) | - | ||||||||||||||||||
1940 | *isMidnight24 = true; | - | ||||||||||||||||||
1941 | hour = 0; | - | ||||||||||||||||||
1942 | } | - | ||||||||||||||||||
1943 | - | |||||||||||||||||||
1944 | return QTime(hour, minute, second, msec); | - | ||||||||||||||||||
1945 | } | - | ||||||||||||||||||
1946 | - | |||||||||||||||||||
1947 | /*! | - | ||||||||||||||||||
1948 | \fn QTime QTime::fromString(const QString &string, Qt::DateFormat format) | - | ||||||||||||||||||
1949 | - | |||||||||||||||||||
1950 | Returns the time represented in the \a string as a QTime using the | - | ||||||||||||||||||
1951 | \a format given, or an invalid time if this is not possible. | - | ||||||||||||||||||
1952 | - | |||||||||||||||||||
1953 | Note that fromString() uses a "C" locale encoded string to convert | - | ||||||||||||||||||
1954 | milliseconds to a float value. If the default locale is not "C", | - | ||||||||||||||||||
1955 | this may result in two conversion attempts (if the conversion | - | ||||||||||||||||||
1956 | fails for the default locale). This should be considered an | - | ||||||||||||||||||
1957 | implementation detail. | - | ||||||||||||||||||
1958 | - | |||||||||||||||||||
1959 | \sa toString(), QLocale::toTime() | - | ||||||||||||||||||
1960 | */ | - | ||||||||||||||||||
1961 | QTime QTime::fromString(const QString& string, Qt::DateFormat format) | - | ||||||||||||||||||
1962 | { | - | ||||||||||||||||||
1963 | if (string.isEmpty()) | - | ||||||||||||||||||
1964 | return QTime(); | - | ||||||||||||||||||
1965 | - | |||||||||||||||||||
1966 | switch (format) { | - | ||||||||||||||||||
1967 | case Qt::SystemLocaleDate: | - | ||||||||||||||||||
1968 | case Qt::SystemLocaleShortDate: | - | ||||||||||||||||||
1969 | return QLocale::system().toTime(string, QLocale::ShortFormat); | - | ||||||||||||||||||
1970 | case Qt::SystemLocaleLongDate: | - | ||||||||||||||||||
1971 | return QLocale::system().toTime(string, QLocale::LongFormat); | - | ||||||||||||||||||
1972 | case Qt::LocaleDate: | - | ||||||||||||||||||
1973 | case Qt::DefaultLocaleShortDate: | - | ||||||||||||||||||
1974 | return QLocale().toTime(string, QLocale::ShortFormat); | - | ||||||||||||||||||
1975 | case Qt::DefaultLocaleLongDate: | - | ||||||||||||||||||
1976 | return QLocale().toTime(string, QLocale::LongFormat); | - | ||||||||||||||||||
1977 | case Qt::RFC2822Date: | - | ||||||||||||||||||
1978 | return rfcDateImpl(string).time; | - | ||||||||||||||||||
1979 | case Qt::ISODate: | - | ||||||||||||||||||
1980 | case Qt::TextDate: | - | ||||||||||||||||||
1981 | default: | - | ||||||||||||||||||
1982 | return fromIsoTimeString(&string, format, 0); | - | ||||||||||||||||||
1983 | } | - | ||||||||||||||||||
1984 | } | - | ||||||||||||||||||
1985 | - | |||||||||||||||||||
1986 | /*! | - | ||||||||||||||||||
1987 | \fn QTime::fromString(const QString &string, const QString &format) | - | ||||||||||||||||||
1988 | - | |||||||||||||||||||
1989 | Returns the QTime represented by the \a string, using the \a | - | ||||||||||||||||||
1990 | format given, or an invalid time if the string cannot be parsed. | - | ||||||||||||||||||
1991 | - | |||||||||||||||||||
1992 | These expressions may be used for the format: | - | ||||||||||||||||||
1993 | - | |||||||||||||||||||
1994 | \table | - | ||||||||||||||||||
1995 | \header \li Expression \li Output | - | ||||||||||||||||||
1996 | \row \li h | - | ||||||||||||||||||
1997 | \li the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) | - | ||||||||||||||||||
1998 | \row \li hh | - | ||||||||||||||||||
1999 | \li the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) | - | ||||||||||||||||||
2000 | \row \li m \li the minute without a leading zero (0 to 59) | - | ||||||||||||||||||
2001 | \row \li mm \li the minute with a leading zero (00 to 59) | - | ||||||||||||||||||
2002 | \row \li s \li the second without a leading zero (0 to 59) | - | ||||||||||||||||||
2003 | \row \li ss \li the second with a leading zero (00 to 59) | - | ||||||||||||||||||
2004 | \row \li z \li the milliseconds without leading zeroes (0 to 999) | - | ||||||||||||||||||
2005 | \row \li zzz \li the milliseconds with leading zeroes (000 to 999) | - | ||||||||||||||||||
2006 | \row \li AP | - | ||||||||||||||||||
2007 | \li interpret as an AM/PM time. \e AP must be either "AM" or "PM". | - | ||||||||||||||||||
2008 | \row \li ap | - | ||||||||||||||||||
2009 | \li Interpret as an AM/PM time. \e ap must be either "am" or "pm". | - | ||||||||||||||||||
2010 | \endtable | - | ||||||||||||||||||
2011 | - | |||||||||||||||||||
2012 | All other input characters will be treated as text. Any sequence | - | ||||||||||||||||||
2013 | of characters that are enclosed in single quotes will also be | - | ||||||||||||||||||
2014 | treated as text and not be used as an expression. | - | ||||||||||||||||||
2015 | - | |||||||||||||||||||
2016 | \snippet code/src_corelib_tools_qdatetime.cpp 6 | - | ||||||||||||||||||
2017 | - | |||||||||||||||||||
2018 | If the format is not satisfied, an invalid QTime is returned. | - | ||||||||||||||||||
2019 | Expressions that do not expect leading zeroes to be given (h, m, s | - | ||||||||||||||||||
2020 | and z) are greedy. This means that they will use two digits even if | - | ||||||||||||||||||
2021 | this puts them outside the range of accepted values and leaves too | - | ||||||||||||||||||
2022 | few digits for other sections. For example, the following string | - | ||||||||||||||||||
2023 | could have meant 00:07:10, but the m will grab two digits, resulting | - | ||||||||||||||||||
2024 | in an invalid time: | - | ||||||||||||||||||
2025 | - | |||||||||||||||||||
2026 | \snippet code/src_corelib_tools_qdatetime.cpp 7 | - | ||||||||||||||||||
2027 | - | |||||||||||||||||||
2028 | Any field that is not represented in the format will be set to zero. | - | ||||||||||||||||||
2029 | For example: | - | ||||||||||||||||||
2030 | - | |||||||||||||||||||
2031 | \snippet code/src_corelib_tools_qdatetime.cpp 8 | - | ||||||||||||||||||
2032 | - | |||||||||||||||||||
2033 | \sa toString(), QDateTime::fromString(), QDate::fromString(), | - | ||||||||||||||||||
2034 | QLocale::toTime() | - | ||||||||||||||||||
2035 | */ | - | ||||||||||||||||||
2036 | - | |||||||||||||||||||
2037 | QTime QTime::fromString(const QString &string, const QString &format) | - | ||||||||||||||||||
2038 | { | - | ||||||||||||||||||
2039 | QTime time; | - | ||||||||||||||||||
2040 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
2041 | QDateTimeParser dt(QVariant::Time, QDateTimeParser::FromString); | - | ||||||||||||||||||
2042 | if (dt.parseFormat(format)) | - | ||||||||||||||||||
2043 | dt.fromString(string, 0, &time); | - | ||||||||||||||||||
2044 | #else | - | ||||||||||||||||||
2045 | Q_UNUSED(string); | - | ||||||||||||||||||
2046 | Q_UNUSED(format); | - | ||||||||||||||||||
2047 | #endif | - | ||||||||||||||||||
2048 | return time; | - | ||||||||||||||||||
2049 | } | - | ||||||||||||||||||
2050 | - | |||||||||||||||||||
2051 | #endif // QT_NO_DATESTRING | - | ||||||||||||||||||
2052 | - | |||||||||||||||||||
2053 | - | |||||||||||||||||||
2054 | /*! | - | ||||||||||||||||||
2055 | \overload | - | ||||||||||||||||||
2056 | - | |||||||||||||||||||
2057 | Returns \c true if the specified time is valid; otherwise returns | - | ||||||||||||||||||
2058 | false. | - | ||||||||||||||||||
2059 | - | |||||||||||||||||||
2060 | The time is valid if \a h is in the range 0 to 23, \a m and | - | ||||||||||||||||||
2061 | \a s are in the range 0 to 59, and \a ms is in the range 0 to 999. | - | ||||||||||||||||||
2062 | - | |||||||||||||||||||
2063 | Example: | - | ||||||||||||||||||
2064 | - | |||||||||||||||||||
2065 | \snippet code/src_corelib_tools_qdatetime.cpp 9 | - | ||||||||||||||||||
2066 | */ | - | ||||||||||||||||||
2067 | - | |||||||||||||||||||
2068 | bool QTime::isValid(int h, int m, int s, int ms) | - | ||||||||||||||||||
2069 | { | - | ||||||||||||||||||
2070 | return (uint)h < 24 && (uint)m < 60 && (uint)s < 60 && (uint)ms < 1000; | - | ||||||||||||||||||
2071 | } | - | ||||||||||||||||||
2072 | - | |||||||||||||||||||
2073 | - | |||||||||||||||||||
2074 | /*! | - | ||||||||||||||||||
2075 | Sets this time to the current time. This is practical for timing: | - | ||||||||||||||||||
2076 | - | |||||||||||||||||||
2077 | \snippet code/src_corelib_tools_qdatetime.cpp 10 | - | ||||||||||||||||||
2078 | - | |||||||||||||||||||
2079 | \sa restart(), elapsed(), currentTime() | - | ||||||||||||||||||
2080 | */ | - | ||||||||||||||||||
2081 | - | |||||||||||||||||||
2082 | void QTime::start() | - | ||||||||||||||||||
2083 | { | - | ||||||||||||||||||
2084 | *this = currentTime(); | - | ||||||||||||||||||
2085 | } | - | ||||||||||||||||||
2086 | - | |||||||||||||||||||
2087 | /*! | - | ||||||||||||||||||
2088 | Sets this time to the current time and returns the number of | - | ||||||||||||||||||
2089 | milliseconds that have elapsed since the last time start() or | - | ||||||||||||||||||
2090 | restart() was called. | - | ||||||||||||||||||
2091 | - | |||||||||||||||||||
2092 | This function is guaranteed to be atomic and is thus very handy | - | ||||||||||||||||||
2093 | for repeated measurements. Call start() to start the first | - | ||||||||||||||||||
2094 | measurement, and restart() for each later measurement. | - | ||||||||||||||||||
2095 | - | |||||||||||||||||||
2096 | Note that the counter wraps to zero 24 hours after the last call | - | ||||||||||||||||||
2097 | to start() or restart(). | - | ||||||||||||||||||
2098 | - | |||||||||||||||||||
2099 | \warning If the system's clock setting has been changed since the | - | ||||||||||||||||||
2100 | last time start() or restart() was called, the result is | - | ||||||||||||||||||
2101 | undefined. This can happen when daylight-saving time is turned on | - | ||||||||||||||||||
2102 | or off. | - | ||||||||||||||||||
2103 | - | |||||||||||||||||||
2104 | \sa start(), elapsed(), currentTime() | - | ||||||||||||||||||
2105 | */ | - | ||||||||||||||||||
2106 | - | |||||||||||||||||||
2107 | int QTime::restart() | - | ||||||||||||||||||
2108 | { | - | ||||||||||||||||||
2109 | QTime t = currentTime(); | - | ||||||||||||||||||
2110 | int n = msecsTo(t); | - | ||||||||||||||||||
2111 | if (n < 0) // passed midnight | - | ||||||||||||||||||
2112 | n += 86400*1000; | - | ||||||||||||||||||
2113 | *this = t; | - | ||||||||||||||||||
2114 | return n; | - | ||||||||||||||||||
2115 | } | - | ||||||||||||||||||
2116 | - | |||||||||||||||||||
2117 | /*! | - | ||||||||||||||||||
2118 | Returns the number of milliseconds that have elapsed since the | - | ||||||||||||||||||
2119 | last time start() or restart() was called. | - | ||||||||||||||||||
2120 | - | |||||||||||||||||||
2121 | Note that the counter wraps to zero 24 hours after the last call | - | ||||||||||||||||||
2122 | to start() or restart. | - | ||||||||||||||||||
2123 | - | |||||||||||||||||||
2124 | Note that the accuracy depends on the accuracy of the underlying | - | ||||||||||||||||||
2125 | operating system; not all systems provide 1-millisecond accuracy. | - | ||||||||||||||||||
2126 | - | |||||||||||||||||||
2127 | \warning If the system's clock setting has been changed since the | - | ||||||||||||||||||
2128 | last time start() or restart() was called, the result is | - | ||||||||||||||||||
2129 | undefined. This can happen when daylight-saving time is turned on | - | ||||||||||||||||||
2130 | or off. | - | ||||||||||||||||||
2131 | - | |||||||||||||||||||
2132 | \sa start(), restart() | - | ||||||||||||||||||
2133 | */ | - | ||||||||||||||||||
2134 | - | |||||||||||||||||||
2135 | int QTime::elapsed() const | - | ||||||||||||||||||
2136 | { | - | ||||||||||||||||||
2137 | int n = msecsTo(currentTime()); | - | ||||||||||||||||||
2138 | if (n < 0) // passed midnight | - | ||||||||||||||||||
2139 | n += 86400 * 1000; | - | ||||||||||||||||||
2140 | return n; | - | ||||||||||||||||||
2141 | } | - | ||||||||||||||||||
2142 | - | |||||||||||||||||||
2143 | /***************************************************************************** | - | ||||||||||||||||||
2144 | QDateTime static helper functions | - | ||||||||||||||||||
2145 | *****************************************************************************/ | - | ||||||||||||||||||
2146 | - | |||||||||||||||||||
2147 | // Calls the platform variant of tzset | - | ||||||||||||||||||
2148 | static void qt_tzset() | - | ||||||||||||||||||
2149 | { | - | ||||||||||||||||||
2150 | #if defined(Q_OS_WINCE) | - | ||||||||||||||||||
2151 | // WinCE doesn't use tzset | - | ||||||||||||||||||
2152 | return; | - | ||||||||||||||||||
2153 | #elif defined(Q_OS_WIN) | - | ||||||||||||||||||
2154 | _tzset(); | - | ||||||||||||||||||
2155 | #else | - | ||||||||||||||||||
2156 | tzset(); | - | ||||||||||||||||||
2157 | #endif // Q_OS_WIN | - | ||||||||||||||||||
2158 | } | - | ||||||||||||||||||
2159 | - | |||||||||||||||||||
2160 | // Returns the platform variant of timezone, i.e. the standard time offset | - | ||||||||||||||||||
2161 | // The timezone external variable is documented as always holding the | - | ||||||||||||||||||
2162 | // Standard Time offset as seconds west of Greenwich, i.e. UTC+01:00 is -3600 | - | ||||||||||||||||||
2163 | // Note this may not be historicaly accurate. | - | ||||||||||||||||||
2164 | // Relies on tzset, mktime, or localtime having been called to populate timezone | - | ||||||||||||||||||
2165 | static int qt_timezone() | - | ||||||||||||||||||
2166 | { | - | ||||||||||||||||||
2167 | #if defined(Q_OS_WINCE) | - | ||||||||||||||||||
2168 | TIME_ZONE_INFORMATION tzi; | - | ||||||||||||||||||
2169 | GetTimeZoneInformation(&tzi); | - | ||||||||||||||||||
2170 | // Expressed in minutes, convert to seconds | - | ||||||||||||||||||
2171 | return (tzi.Bias + tzi.StandardBias) * 60; | - | ||||||||||||||||||
2172 | #elif defined(_MSC_VER) && _MSC_VER >= 1400 | - | ||||||||||||||||||
2173 | long offset; | - | ||||||||||||||||||
2174 | _get_timezone(&offset); | - | ||||||||||||||||||
2175 | return offset; | - | ||||||||||||||||||
2176 | #elif defined(Q_OS_BSD4) && !defined(Q_OS_DARWIN) | - | ||||||||||||||||||
2177 | time_t clock = time(NULL); | - | ||||||||||||||||||
2178 | struct tm t; | - | ||||||||||||||||||
2179 | localtime_r(&clock, &t); | - | ||||||||||||||||||
2180 | // QTBUG-36080 Workaround for systems without the POSIX timezone | - | ||||||||||||||||||
2181 | // variable. This solution is not very efficient but fixing it is up to | - | ||||||||||||||||||
2182 | // the libc implementations. | - | ||||||||||||||||||
2183 | // | - | ||||||||||||||||||
2184 | // tm_gmtoff has some important differences compared to the timezone | - | ||||||||||||||||||
2185 | // variable: | - | ||||||||||||||||||
2186 | // - It returns the number of seconds east of UTC, and we want the | - | ||||||||||||||||||
2187 | // number of seconds west of UTC. | - | ||||||||||||||||||
2188 | // - It also takes DST into account, so we need to adjust it to always | - | ||||||||||||||||||
2189 | // get the Standard Time offset. | - | ||||||||||||||||||
2190 | return -t.tm_gmtoff + (t.tm_isdst ? (long)SECS_PER_HOUR : 0L); | - | ||||||||||||||||||
2191 | #elif defined(Q_OS_INTEGRITY) | - | ||||||||||||||||||
2192 | return 0; | - | ||||||||||||||||||
2193 | #else | - | ||||||||||||||||||
2194 | return timezone; executed 7004 times by 16 tests: return timezone; Executed by:
| 7004 | ||||||||||||||||||
2195 | #endif // Q_OS_WIN | - | ||||||||||||||||||
2196 | } | - | ||||||||||||||||||
2197 | - | |||||||||||||||||||
2198 | // Returns the tzname, assume tzset has been called already | - | ||||||||||||||||||
2199 | static QString qt_tzname(QDateTimePrivate::DaylightStatus daylightStatus) | - | ||||||||||||||||||
2200 | { | - | ||||||||||||||||||
2201 | #if defined(Q_OS_WINCE) | - | ||||||||||||||||||
2202 | TIME_ZONE_INFORMATION tzi; | - | ||||||||||||||||||
2203 | DWORD res = GetTimeZoneInformation(&tzi); | - | ||||||||||||||||||
2204 | if (res == TIME_ZONE_ID_UNKNOWN) | - | ||||||||||||||||||
2205 | return QString(); | - | ||||||||||||||||||
2206 | else if (daylightStatus == QDateTimePrivate::DaylightTime) | - | ||||||||||||||||||
2207 | return QString::fromWCharArray(tzi.DaylightName); | - | ||||||||||||||||||
2208 | else | - | ||||||||||||||||||
2209 | return QString::fromWCharArray(tzi.StandardName); | - | ||||||||||||||||||
2210 | #else | - | ||||||||||||||||||
2211 | int isDst = (daylightStatus == QDateTimePrivate::DaylightTime) ? 1 : 0; | - | ||||||||||||||||||
2212 | #if defined(_MSC_VER) && _MSC_VER >= 1400 | - | ||||||||||||||||||
2213 | size_t s = 0; | - | ||||||||||||||||||
2214 | char name[512]; | - | ||||||||||||||||||
2215 | if (_get_tzname(&s, name, 512, isDst)) | - | ||||||||||||||||||
2216 | return QString(); | - | ||||||||||||||||||
2217 | return QString::fromLocal8Bit(name); | - | ||||||||||||||||||
2218 | #else | - | ||||||||||||||||||
2219 | return QString::fromLocal8Bit(tzname[isDst]); | - | ||||||||||||||||||
2220 | #endif // Q_OS_WIN | - | ||||||||||||||||||
2221 | #endif // Q_OS_WINCE | - | ||||||||||||||||||
2222 | } | - | ||||||||||||||||||
2223 | - | |||||||||||||||||||
2224 | // Calls the platform variant of mktime for the given date, time and daylightStatus, | - | ||||||||||||||||||
2225 | // and updates the date, time, daylightStatus and abbreviation with the returned values | - | ||||||||||||||||||
2226 | // If the date falls outside the 1970 to 2037 range supported by mktime / time_t | - | ||||||||||||||||||
2227 | // then null date/time will be returned, you should adjust the date first if | - | ||||||||||||||||||
2228 | // you need a guaranteed result. | - | ||||||||||||||||||
2229 | static qint64 qt_mktime(QDate *date, QTime *time, QDateTimePrivate::DaylightStatus *daylightStatus, | - | ||||||||||||||||||
2230 | QString *abbreviation, bool *ok = 0) | - | ||||||||||||||||||
2231 | { | - | ||||||||||||||||||
2232 | const qint64 msec = time->msec(); | - | ||||||||||||||||||
2233 | int yy, mm, dd; | - | ||||||||||||||||||
2234 | date->getDate(&yy, &mm, &dd); | - | ||||||||||||||||||
2235 | - | |||||||||||||||||||
2236 | #if defined(Q_OS_WINCE) | - | ||||||||||||||||||
2237 | // WinCE doesn't provide standard C library time functions | - | ||||||||||||||||||
2238 | SYSTEMTIME st; | - | ||||||||||||||||||
2239 | memset(&st, 0, sizeof(SYSTEMTIME)); | - | ||||||||||||||||||
2240 | st.wSecond = time->second(); | - | ||||||||||||||||||
2241 | st.wMinute = time->minute(); | - | ||||||||||||||||||
2242 | st.wHour = time->hour(); | - | ||||||||||||||||||
2243 | st.wDay = dd; | - | ||||||||||||||||||
2244 | st.wMonth = mm; | - | ||||||||||||||||||
2245 | st.wYear = yy; | - | ||||||||||||||||||
2246 | FILETIME lft; | - | ||||||||||||||||||
2247 | bool valid = SystemTimeToFileTime(&st, &lft); | - | ||||||||||||||||||
2248 | FILETIME ft; | - | ||||||||||||||||||
2249 | if (valid) | - | ||||||||||||||||||
2250 | valid = LocalFileTimeToFileTime(&lft, &ft); | - | ||||||||||||||||||
2251 | const time_t secsSinceEpoch = ftToTime_t(ft); | - | ||||||||||||||||||
2252 | const time_t localSecs = ftToTime_t(lft); | - | ||||||||||||||||||
2253 | TIME_ZONE_INFORMATION tzi; | - | ||||||||||||||||||
2254 | GetTimeZoneInformation(&tzi); | - | ||||||||||||||||||
2255 | bool isDaylight = false; | - | ||||||||||||||||||
2256 | // Check for overflow | - | ||||||||||||||||||
2257 | qint64 localDiff = qAbs(localSecs - secsSinceEpoch); | - | ||||||||||||||||||
2258 | int daylightOffset = qAbs(tzi.Bias + tzi.DaylightBias) * 60; | - | ||||||||||||||||||
2259 | if (localDiff > daylightOffset) | - | ||||||||||||||||||
2260 | valid = false; | - | ||||||||||||||||||
2261 | else | - | ||||||||||||||||||
2262 | isDaylight = (localDiff == daylightOffset); | - | ||||||||||||||||||
2263 | if (daylightStatus) { | - | ||||||||||||||||||
2264 | if (isDaylight) | - | ||||||||||||||||||
2265 | *daylightStatus = QDateTimePrivate::DaylightTime; | - | ||||||||||||||||||
2266 | else | - | ||||||||||||||||||
2267 | *daylightStatus = QDateTimePrivate::StandardTime; | - | ||||||||||||||||||
2268 | } | - | ||||||||||||||||||
2269 | if (abbreviation) { | - | ||||||||||||||||||
2270 | if (isDaylight) | - | ||||||||||||||||||
2271 | *abbreviation = QString::fromWCharArray(tzi.DaylightName); | - | ||||||||||||||||||
2272 | else | - | ||||||||||||||||||
2273 | *abbreviation = QString::fromWCharArray(tzi.StandardName); | - | ||||||||||||||||||
2274 | } | - | ||||||||||||||||||
2275 | if (ok) | - | ||||||||||||||||||
2276 | *ok = valid; | - | ||||||||||||||||||
2277 | #else | - | ||||||||||||||||||
2278 | // All other platforms provide standard C library time functions | - | ||||||||||||||||||
2279 | tm local; | - | ||||||||||||||||||
2280 | memset(&local, 0, sizeof(local)); // tm_[wy]day plus any non-standard fields | - | ||||||||||||||||||
2281 | local.tm_sec = time->second(); | - | ||||||||||||||||||
2282 | local.tm_min = time->minute(); | - | ||||||||||||||||||
2283 | local.tm_hour = time->hour(); | - | ||||||||||||||||||
2284 | local.tm_mday = dd; | - | ||||||||||||||||||
2285 | local.tm_mon = mm - 1; | - | ||||||||||||||||||
2286 | local.tm_year = yy - 1900; | - | ||||||||||||||||||
2287 | if (daylightStatus) | - | ||||||||||||||||||
2288 | local.tm_isdst = int(*daylightStatus); | - | ||||||||||||||||||
2289 | else | - | ||||||||||||||||||
2290 | local.tm_isdst = -1; | - | ||||||||||||||||||
2291 | - | |||||||||||||||||||
2292 | #if defined(Q_OS_WIN) | - | ||||||||||||||||||
2293 | int hh = local.tm_hour; | - | ||||||||||||||||||
2294 | #endif // Q_OS_WIN | - | ||||||||||||||||||
2295 | time_t secsSinceEpoch = mktime(&local); | - | ||||||||||||||||||
2296 | if (secsSinceEpoch != time_t(-1)) { | - | ||||||||||||||||||
2297 | *date = QDate(local.tm_year + 1900, local.tm_mon + 1, local.tm_mday); | - | ||||||||||||||||||
2298 | *time = QTime(local.tm_hour, local.tm_min, local.tm_sec, msec); | - | ||||||||||||||||||
2299 | #if defined(Q_OS_WIN) | - | ||||||||||||||||||
2300 | // Windows mktime for the missing hour subtracts 1 hour from the time | - | ||||||||||||||||||
2301 | // instead of adding 1 hour. If time differs and is standard time then | - | ||||||||||||||||||
2302 | // this has happened, so add 2 hours to the time and 1 hour to the msecs | - | ||||||||||||||||||
2303 | if (local.tm_isdst == 0 && local.tm_hour != hh) { | - | ||||||||||||||||||
2304 | if (time->hour() >= 22) | - | ||||||||||||||||||
2305 | *date = date->addDays(1); | - | ||||||||||||||||||
2306 | *time = time->addSecs(2 * SECS_PER_HOUR); | - | ||||||||||||||||||
2307 | secsSinceEpoch += SECS_PER_HOUR; | - | ||||||||||||||||||
2308 | local.tm_isdst = 1; | - | ||||||||||||||||||
2309 | } | - | ||||||||||||||||||
2310 | #endif // Q_OS_WIN | - | ||||||||||||||||||
2311 | if (local.tm_isdst >= 1) { | - | ||||||||||||||||||
2312 | if (daylightStatus) | - | ||||||||||||||||||
2313 | *daylightStatus = QDateTimePrivate::DaylightTime; | - | ||||||||||||||||||
2314 | if (abbreviation) | - | ||||||||||||||||||
2315 | *abbreviation = qt_tzname(QDateTimePrivate::DaylightTime); | - | ||||||||||||||||||
2316 | } else if (local.tm_isdst == 0) { | - | ||||||||||||||||||
2317 | if (daylightStatus) | - | ||||||||||||||||||
2318 | *daylightStatus = QDateTimePrivate::StandardTime; | - | ||||||||||||||||||
2319 | if (abbreviation) | - | ||||||||||||||||||
2320 | *abbreviation = qt_tzname(QDateTimePrivate::StandardTime); | - | ||||||||||||||||||
2321 | } else { | - | ||||||||||||||||||
2322 | if (daylightStatus) | - | ||||||||||||||||||
2323 | *daylightStatus = QDateTimePrivate::UnknownDaylightTime; | - | ||||||||||||||||||
2324 | if (abbreviation) | - | ||||||||||||||||||
2325 | *abbreviation = qt_tzname(QDateTimePrivate::StandardTime); | - | ||||||||||||||||||
2326 | } | - | ||||||||||||||||||
2327 | if (ok) | - | ||||||||||||||||||
2328 | *ok = true; | - | ||||||||||||||||||
2329 | } else { | - | ||||||||||||||||||
2330 | *date = QDate(); | - | ||||||||||||||||||
2331 | *time = QTime(); | - | ||||||||||||||||||
2332 | if (daylightStatus) | - | ||||||||||||||||||
2333 | *daylightStatus = QDateTimePrivate::UnknownDaylightTime; | - | ||||||||||||||||||
2334 | if (abbreviation) | - | ||||||||||||||||||
2335 | *abbreviation = QString(); | - | ||||||||||||||||||
2336 | if (ok) | - | ||||||||||||||||||
2337 | *ok = false; | - | ||||||||||||||||||
2338 | } | - | ||||||||||||||||||
2339 | #endif // Q_OS_WINCE | - | ||||||||||||||||||
2340 | - | |||||||||||||||||||
2341 | return ((qint64)secsSinceEpoch * 1000) + msec; | - | ||||||||||||||||||
2342 | } | - | ||||||||||||||||||
2343 | - | |||||||||||||||||||
2344 | // Calls the platform variant of localtime for the given msecs, and updates | - | ||||||||||||||||||
2345 | // the date, time, and DST status with the returned values. | - | ||||||||||||||||||
2346 | static bool qt_localtime(qint64 msecsSinceEpoch, QDate *localDate, QTime *localTime, | - | ||||||||||||||||||
2347 | QDateTimePrivate::DaylightStatus *daylightStatus) | - | ||||||||||||||||||
2348 | { | - | ||||||||||||||||||
2349 | const time_t secsSinceEpoch = msecsSinceEpoch / 1000; | - | ||||||||||||||||||
2350 | const int msec = msecsSinceEpoch % 1000; | - | ||||||||||||||||||
2351 | - | |||||||||||||||||||
2352 | tm local; | - | ||||||||||||||||||
2353 | bool valid = false; | - | ||||||||||||||||||
2354 | - | |||||||||||||||||||
2355 | #if defined(Q_OS_WINCE) | - | ||||||||||||||||||
2356 | FILETIME utcTime = time_tToFt(secsSinceEpoch); | - | ||||||||||||||||||
2357 | FILETIME resultTime; | - | ||||||||||||||||||
2358 | valid = FileTimeToLocalFileTime(&utcTime , &resultTime); | - | ||||||||||||||||||
2359 | SYSTEMTIME sysTime; | - | ||||||||||||||||||
2360 | if (valid) | - | ||||||||||||||||||
2361 | valid = FileTimeToSystemTime(&resultTime , &sysTime); | - | ||||||||||||||||||
2362 | - | |||||||||||||||||||
2363 | if (valid) { | - | ||||||||||||||||||
2364 | local.tm_sec = sysTime.wSecond; | - | ||||||||||||||||||
2365 | local.tm_min = sysTime.wMinute; | - | ||||||||||||||||||
2366 | local.tm_hour = sysTime.wHour; | - | ||||||||||||||||||
2367 | local.tm_mday = sysTime.wDay; | - | ||||||||||||||||||
2368 | local.tm_mon = sysTime.wMonth - 1; | - | ||||||||||||||||||
2369 | local.tm_year = sysTime.wYear - 1900; | - | ||||||||||||||||||
2370 | } | - | ||||||||||||||||||
2371 | #elif !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) | - | ||||||||||||||||||
2372 | // localtime() is required to work as if tzset() was called before it. | - | ||||||||||||||||||
2373 | // localtime_r() does not have this requirement, so make an explicit call. | - | ||||||||||||||||||
2374 | qt_tzset(); | - | ||||||||||||||||||
2375 | // Use the reentrant version of localtime() where available | - | ||||||||||||||||||
2376 | // as is thread-safe and doesn't use a shared static data area | - | ||||||||||||||||||
2377 | tm *res = 0; | - | ||||||||||||||||||
2378 | res = localtime_r(&secsSinceEpoch, &local); | - | ||||||||||||||||||
2379 | if (res) | - | ||||||||||||||||||
2380 | valid = true; | - | ||||||||||||||||||
2381 | #elif defined(_MSC_VER) && _MSC_VER >= 1400 | - | ||||||||||||||||||
2382 | if (!_localtime64_s(&local, &secsSinceEpoch)) | - | ||||||||||||||||||
2383 | valid = true; | - | ||||||||||||||||||
2384 | #else | - | ||||||||||||||||||
2385 | // Returns shared static data which may be overwritten at any time | - | ||||||||||||||||||
2386 | // So copy the result asap | - | ||||||||||||||||||
2387 | tm *res = 0; | - | ||||||||||||||||||
2388 | res = localtime(&secsSinceEpoch); | - | ||||||||||||||||||
2389 | if (res) { | - | ||||||||||||||||||
2390 | local = *res; | - | ||||||||||||||||||
2391 | valid = true; | - | ||||||||||||||||||
2392 | } | - | ||||||||||||||||||
2393 | #endif | - | ||||||||||||||||||
2394 | if (valid) { | - | ||||||||||||||||||
2395 | *localDate = QDate(local.tm_year + 1900, local.tm_mon + 1, local.tm_mday); | - | ||||||||||||||||||
2396 | *localTime = QTime(local.tm_hour, local.tm_min, local.tm_sec, msec); | - | ||||||||||||||||||
2397 | if (daylightStatus) { | - | ||||||||||||||||||
2398 | if (local.tm_isdst > 0) | - | ||||||||||||||||||
2399 | *daylightStatus = QDateTimePrivate::DaylightTime; | - | ||||||||||||||||||
2400 | else if (local.tm_isdst < 0) | - | ||||||||||||||||||
2401 | *daylightStatus = QDateTimePrivate::UnknownDaylightTime; | - | ||||||||||||||||||
2402 | else | - | ||||||||||||||||||
2403 | *daylightStatus = QDateTimePrivate::StandardTime; | - | ||||||||||||||||||
2404 | } | - | ||||||||||||||||||
2405 | return true; | - | ||||||||||||||||||
2406 | } else { | - | ||||||||||||||||||
2407 | *localDate = QDate(); | - | ||||||||||||||||||
2408 | *localTime = QTime(); | - | ||||||||||||||||||
2409 | if (daylightStatus) | - | ||||||||||||||||||
2410 | *daylightStatus = QDateTimePrivate::UnknownDaylightTime; | - | ||||||||||||||||||
2411 | return false; | - | ||||||||||||||||||
2412 | } | - | ||||||||||||||||||
2413 | } | - | ||||||||||||||||||
2414 | - | |||||||||||||||||||
2415 | // Converts an msecs value into a date and time | - | ||||||||||||||||||
2416 | static void msecsToTime(qint64 msecs, QDate *date, QTime *time) | - | ||||||||||||||||||
2417 | { | - | ||||||||||||||||||
2418 | qint64 jd = JULIAN_DAY_FOR_EPOCH; | - | ||||||||||||||||||
2419 | qint64 ds = 0; | - | ||||||||||||||||||
2420 | - | |||||||||||||||||||
2421 | if (qAbs(msecs) >= MSECS_PER_DAY) { | - | ||||||||||||||||||
2422 | jd += (msecs / MSECS_PER_DAY); | - | ||||||||||||||||||
2423 | msecs %= MSECS_PER_DAY; | - | ||||||||||||||||||
2424 | } | - | ||||||||||||||||||
2425 | - | |||||||||||||||||||
2426 | if (msecs < 0) { | - | ||||||||||||||||||
2427 | ds = MSECS_PER_DAY - msecs - 1; | - | ||||||||||||||||||
2428 | jd -= ds / MSECS_PER_DAY; | - | ||||||||||||||||||
2429 | ds = ds % MSECS_PER_DAY; | - | ||||||||||||||||||
2430 | ds = MSECS_PER_DAY - ds - 1; | - | ||||||||||||||||||
2431 | } else { | - | ||||||||||||||||||
2432 | ds = msecs; | - | ||||||||||||||||||
2433 | } | - | ||||||||||||||||||
2434 | - | |||||||||||||||||||
2435 | if (date) | - | ||||||||||||||||||
2436 | *date = QDate::fromJulianDay(jd); | - | ||||||||||||||||||
2437 | if (time) | - | ||||||||||||||||||
2438 | *time = QTime::fromMSecsSinceStartOfDay(ds); | - | ||||||||||||||||||
2439 | } | - | ||||||||||||||||||
2440 | - | |||||||||||||||||||
2441 | // Converts a date/time value into msecs | - | ||||||||||||||||||
2442 | static qint64 timeToMSecs(const QDate &date, const QTime &time) | - | ||||||||||||||||||
2443 | { | - | ||||||||||||||||||
2444 | return ((date.toJulianDay() - JULIAN_DAY_FOR_EPOCH) * MSECS_PER_DAY) | - | ||||||||||||||||||
2445 | + time.msecsSinceStartOfDay(); | - | ||||||||||||||||||
2446 | } | - | ||||||||||||||||||
2447 | - | |||||||||||||||||||
2448 | // Convert an MSecs Since Epoch into Local Time | - | ||||||||||||||||||
2449 | static bool epochMSecsToLocalTime(qint64 msecs, QDate *localDate, QTime *localTime, | - | ||||||||||||||||||
2450 | QDateTimePrivate::DaylightStatus *daylightStatus = 0) | - | ||||||||||||||||||
2451 | { | - | ||||||||||||||||||
2452 | if (msecs < 0) { | - | ||||||||||||||||||
2453 | // Docs state any LocalTime before 1970-01-01 will *not* have any Daylight Time applied | - | ||||||||||||||||||
2454 | // Instead just use the standard offset from UTC to convert to UTC time | - | ||||||||||||||||||
2455 | qt_tzset(); | - | ||||||||||||||||||
2456 | msecsToTime(msecs - qt_timezone() * 1000, localDate, localTime); | - | ||||||||||||||||||
2457 | if (daylightStatus) | - | ||||||||||||||||||
2458 | *daylightStatus = QDateTimePrivate::StandardTime; | - | ||||||||||||||||||
2459 | return true; | - | ||||||||||||||||||
2460 | } else if (msecs > (qint64(TIME_T_MAX) * 1000)) { | - | ||||||||||||||||||
2461 | // Docs state any LocalTime after 2037-12-31 *will* have any DST applied | - | ||||||||||||||||||
2462 | // but this may fall outside the supported time_t range, so need to fake it. | - | ||||||||||||||||||
2463 | // Use existing method to fake the conversion, but this is deeply flawed as it may | - | ||||||||||||||||||
2464 | // apply the conversion from the wrong day number, e.g. if rule is last Sunday of month | - | ||||||||||||||||||
2465 | // TODO Use QTimeZone when available to apply the future rule correctly | - | ||||||||||||||||||
2466 | QDate utcDate; | - | ||||||||||||||||||
2467 | QTime utcTime; | - | ||||||||||||||||||
2468 | msecsToTime(msecs, &utcDate, &utcTime); | - | ||||||||||||||||||
2469 | int year, month, day; | - | ||||||||||||||||||
2470 | utcDate.getDate(&year, &month, &day); | - | ||||||||||||||||||
2471 | // 2037 is not a leap year, so make sure date isn't Feb 29 | - | ||||||||||||||||||
2472 | if (month == 2 && day == 29) | - | ||||||||||||||||||
2473 | --day; | - | ||||||||||||||||||
2474 | QDate fakeDate(2037, month, day); | - | ||||||||||||||||||
2475 | qint64 fakeMsecs = QDateTime(fakeDate, utcTime, Qt::UTC).toMSecsSinceEpoch(); | - | ||||||||||||||||||
2476 | bool res = qt_localtime(fakeMsecs, localDate, localTime, daylightStatus); | - | ||||||||||||||||||
2477 | *localDate = localDate->addDays(fakeDate.daysTo(utcDate)); | - | ||||||||||||||||||
2478 | return res; | - | ||||||||||||||||||
2479 | } else { | - | ||||||||||||||||||
2480 | // Falls inside time_t suported range so can use localtime | - | ||||||||||||||||||
2481 | return qt_localtime(msecs, localDate, localTime, daylightStatus); | - | ||||||||||||||||||
2482 | } | - | ||||||||||||||||||
2483 | } | - | ||||||||||||||||||
2484 | - | |||||||||||||||||||
2485 | // Convert a LocalTime expressed in local msecs encoding and the corresponding | - | ||||||||||||||||||
2486 | // DST status into a UTC epoch msecs. Optionally populate the returned | - | ||||||||||||||||||
2487 | // values from mktime for the adjusted local date and time. | - | ||||||||||||||||||
2488 | static qint64 localMSecsToEpochMSecs(qint64 localMsecs, | - | ||||||||||||||||||
2489 | QDateTimePrivate::DaylightStatus *daylightStatus, | - | ||||||||||||||||||
2490 | QDate *localDate = 0, QTime *localTime = 0, | - | ||||||||||||||||||
2491 | QString *abbreviation = 0) | - | ||||||||||||||||||
2492 | { | - | ||||||||||||||||||
2493 | QDate dt; | - | ||||||||||||||||||
2494 | QTime tm; | - | ||||||||||||||||||
2495 | msecsToTime(localMsecs, &dt, &tm); | - | ||||||||||||||||||
2496 | - | |||||||||||||||||||
2497 | const qint64 msecsMax = qint64(TIME_T_MAX) * 1000; | - | ||||||||||||||||||
2498 | - | |||||||||||||||||||
2499 | if (localMsecs <= qint64(MSECS_PER_DAY)) { | - | ||||||||||||||||||
2500 | - | |||||||||||||||||||
2501 | // Docs state any LocalTime before 1970-01-01 will *not* have any DST applied | - | ||||||||||||||||||
2502 | - | |||||||||||||||||||
2503 | // First, if localMsecs is within +/- 1 day of minimum time_t try mktime in case it does | - | ||||||||||||||||||
2504 | // fall after minimum and needs proper DST conversion | - | ||||||||||||||||||
2505 | if (localMsecs >= -qint64(MSECS_PER_DAY)) { | - | ||||||||||||||||||
2506 | bool valid; | - | ||||||||||||||||||
2507 | qint64 utcMsecs = qt_mktime(&dt, &tm, daylightStatus, abbreviation, &valid); | - | ||||||||||||||||||
2508 | if (valid && utcMsecs >= 0) { | - | ||||||||||||||||||
2509 | // mktime worked and falls in valid range, so use it | - | ||||||||||||||||||
2510 | if (localDate) | - | ||||||||||||||||||
2511 | *localDate = dt; | - | ||||||||||||||||||
2512 | if (localTime) | - | ||||||||||||||||||
2513 | *localTime = tm; | - | ||||||||||||||||||
2514 | return utcMsecs; | - | ||||||||||||||||||
2515 | } | - | ||||||||||||||||||
2516 | } else { | - | ||||||||||||||||||
2517 | // If we don't call mktime then need to call tzset to get offset | - | ||||||||||||||||||
2518 | qt_tzset(); | - | ||||||||||||||||||
2519 | } | - | ||||||||||||||||||
2520 | // Time is clearly before 1970-01-01 so just use standard offset to convert | - | ||||||||||||||||||
2521 | qint64 utcMsecs = localMsecs + qt_timezone() * 1000; | - | ||||||||||||||||||
2522 | if (localDate || localTime) | - | ||||||||||||||||||
2523 | msecsToTime(localMsecs, localDate, localTime); | - | ||||||||||||||||||
2524 | if (daylightStatus) | - | ||||||||||||||||||
2525 | *daylightStatus = QDateTimePrivate::StandardTime; | - | ||||||||||||||||||
2526 | if (abbreviation) | - | ||||||||||||||||||
2527 | *abbreviation = qt_tzname(QDateTimePrivate::StandardTime); | - | ||||||||||||||||||
2528 | return utcMsecs; | - | ||||||||||||||||||
2529 | - | |||||||||||||||||||
2530 | } else if (localMsecs >= msecsMax - MSECS_PER_DAY) { | - | ||||||||||||||||||
2531 | - | |||||||||||||||||||
2532 | // Docs state any LocalTime after 2037-12-31 *will* have any DST applied | - | ||||||||||||||||||
2533 | // but this may fall outside the supported time_t range, so need to fake it. | - | ||||||||||||||||||
2534 | - | |||||||||||||||||||
2535 | // First, if localMsecs is within +/- 1 day of maximum time_t try mktime in case it does | - | ||||||||||||||||||
2536 | // fall before maximum and can use proper DST conversion | - | ||||||||||||||||||
2537 | if (localMsecs <= msecsMax + MSECS_PER_DAY) { | - | ||||||||||||||||||
2538 | bool valid; | - | ||||||||||||||||||
2539 | qint64 utcMsecs = qt_mktime(&dt, &tm, daylightStatus, abbreviation, &valid); | - | ||||||||||||||||||
2540 | if (valid && utcMsecs <= msecsMax) { | - | ||||||||||||||||||
2541 | // mktime worked and falls in valid range, so use it | - | ||||||||||||||||||
2542 | if (localDate) | - | ||||||||||||||||||
2543 | *localDate = dt; | - | ||||||||||||||||||
2544 | if (localTime) | - | ||||||||||||||||||
2545 | *localTime = tm; | - | ||||||||||||||||||
2546 | return utcMsecs; | - | ||||||||||||||||||
2547 | } | - | ||||||||||||||||||
2548 | } | - | ||||||||||||||||||
2549 | // Use existing method to fake the conversion, but this is deeply flawed as it may | - | ||||||||||||||||||
2550 | // apply the conversion from the wrong day number, e.g. if rule is last Sunday of month | - | ||||||||||||||||||
2551 | // TODO Use QTimeZone when available to apply the future rule correctly | - | ||||||||||||||||||
2552 | int year, month, day; | - | ||||||||||||||||||
2553 | dt.getDate(&year, &month, &day); | - | ||||||||||||||||||
2554 | // 2037 is not a leap year, so make sure date isn't Feb 29 | - | ||||||||||||||||||
2555 | if (month == 2 && day == 29) | - | ||||||||||||||||||
2556 | --day; | - | ||||||||||||||||||
2557 | QDate fakeDate(2037, month, day); | - | ||||||||||||||||||
2558 | qint64 fakeDiff = fakeDate.daysTo(dt); | - | ||||||||||||||||||
2559 | qint64 utcMsecs = qt_mktime(&fakeDate, &tm, daylightStatus, abbreviation); | - | ||||||||||||||||||
2560 | if (localDate) | - | ||||||||||||||||||
2561 | *localDate = fakeDate.addDays(fakeDiff); | - | ||||||||||||||||||
2562 | if (localTime) | - | ||||||||||||||||||
2563 | *localTime = tm; | - | ||||||||||||||||||
2564 | QDate utcDate; | - | ||||||||||||||||||
2565 | QTime utcTime; | - | ||||||||||||||||||
2566 | msecsToTime(utcMsecs, &utcDate, &utcTime); | - | ||||||||||||||||||
2567 | utcDate = utcDate.addDays(fakeDiff); | - | ||||||||||||||||||
2568 | utcMsecs = timeToMSecs(utcDate, utcTime); | - | ||||||||||||||||||
2569 | return utcMsecs; | - | ||||||||||||||||||
2570 | - | |||||||||||||||||||
2571 | } else { | - | ||||||||||||||||||
2572 | - | |||||||||||||||||||
2573 | // Clearly falls inside 1970-2037 suported range so can use mktime | - | ||||||||||||||||||
2574 | qint64 utcMsecs = qt_mktime(&dt, &tm, daylightStatus, abbreviation); | - | ||||||||||||||||||
2575 | if (localDate) | - | ||||||||||||||||||
2576 | *localDate = dt; | - | ||||||||||||||||||
2577 | if (localTime) | - | ||||||||||||||||||
2578 | *localTime = tm; | - | ||||||||||||||||||
2579 | return utcMsecs; | - | ||||||||||||||||||
2580 | - | |||||||||||||||||||
2581 | } | - | ||||||||||||||||||
2582 | } | - | ||||||||||||||||||
2583 | - | |||||||||||||||||||
2584 | /***************************************************************************** | - | ||||||||||||||||||
2585 | QDateTimePrivate member functions | - | ||||||||||||||||||
2586 | *****************************************************************************/ | - | ||||||||||||||||||
2587 | - | |||||||||||||||||||
2588 | QDateTimePrivate::QDateTimePrivate(const QDate &toDate, const QTime &toTime, Qt::TimeSpec toSpec, | - | ||||||||||||||||||
2589 | int offsetSeconds) | - | ||||||||||||||||||
2590 | : m_msecs(0), | - | ||||||||||||||||||
2591 | m_spec(Qt::LocalTime), | - | ||||||||||||||||||
2592 | m_offsetFromUtc(0), | - | ||||||||||||||||||
2593 | m_status(0) | - | ||||||||||||||||||
2594 | { | - | ||||||||||||||||||
2595 | setTimeSpec(toSpec, offsetSeconds); | - | ||||||||||||||||||
2596 | setDateTime(toDate, toTime); | - | ||||||||||||||||||
2597 | } | - | ||||||||||||||||||
2598 | - | |||||||||||||||||||
2599 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
2600 | QDateTimePrivate::QDateTimePrivate(const QDate &toDate, const QTime &toTime, | - | ||||||||||||||||||
2601 | const QTimeZone &toTimeZone) | - | ||||||||||||||||||
2602 | : m_spec(Qt::TimeZone), | - | ||||||||||||||||||
2603 | m_offsetFromUtc(0), | - | ||||||||||||||||||
2604 | m_timeZone(toTimeZone), | - | ||||||||||||||||||
2605 | m_status(0) | - | ||||||||||||||||||
2606 | { | - | ||||||||||||||||||
2607 | setDateTime(toDate, toTime); | - | ||||||||||||||||||
2608 | } | - | ||||||||||||||||||
2609 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
2610 | - | |||||||||||||||||||
2611 | void QDateTimePrivate::setTimeSpec(Qt::TimeSpec spec, int offsetSeconds) | - | ||||||||||||||||||
2612 | { | - | ||||||||||||||||||
2613 | clearValidDateTime(); | - | ||||||||||||||||||
2614 | clearSetToDaylightStatus(); | - | ||||||||||||||||||
2615 | - | |||||||||||||||||||
2616 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
2617 | m_timeZone = QTimeZone(); | - | ||||||||||||||||||
2618 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
2619 | - | |||||||||||||||||||
2620 | switch (spec) { | - | ||||||||||||||||||
2621 | case Qt::OffsetFromUTC: | - | ||||||||||||||||||
2622 | if (offsetSeconds == 0) { | - | ||||||||||||||||||
2623 | m_spec = Qt::UTC; | - | ||||||||||||||||||
2624 | m_offsetFromUtc = 0; | - | ||||||||||||||||||
2625 | } else { | - | ||||||||||||||||||
2626 | m_spec = Qt::OffsetFromUTC; | - | ||||||||||||||||||
2627 | m_offsetFromUtc = offsetSeconds; | - | ||||||||||||||||||
2628 | } | - | ||||||||||||||||||
2629 | break; | - | ||||||||||||||||||
2630 | case Qt::TimeZone: | - | ||||||||||||||||||
2631 | // Use system time zone instead | - | ||||||||||||||||||
2632 | m_spec = Qt::LocalTime; | - | ||||||||||||||||||
2633 | m_offsetFromUtc = 0; | - | ||||||||||||||||||
2634 | break; | - | ||||||||||||||||||
2635 | case Qt::UTC: | - | ||||||||||||||||||
2636 | case Qt::LocalTime: | - | ||||||||||||||||||
2637 | m_spec = spec; | - | ||||||||||||||||||
2638 | m_offsetFromUtc = 0; | - | ||||||||||||||||||
2639 | break; | - | ||||||||||||||||||
2640 | } | - | ||||||||||||||||||
2641 | } | - | ||||||||||||||||||
2642 | - | |||||||||||||||||||
2643 | void QDateTimePrivate::setDateTime(const QDate &date, const QTime &time) | - | ||||||||||||||||||
2644 | { | - | ||||||||||||||||||
2645 | // If the date is valid and the time is not we set time to 00:00:00 | - | ||||||||||||||||||
2646 | QTime useTime = time; | - | ||||||||||||||||||
2647 | if (!useTime.isValid() && date.isValid()) | - | ||||||||||||||||||
2648 | useTime = QTime::fromMSecsSinceStartOfDay(0); | - | ||||||||||||||||||
2649 | - | |||||||||||||||||||
2650 | StatusFlags newStatus; | - | ||||||||||||||||||
2651 | - | |||||||||||||||||||
2652 | // Set date value and status | - | ||||||||||||||||||
2653 | qint64 days = 0; | - | ||||||||||||||||||
2654 | if (date.isValid()) { | - | ||||||||||||||||||
2655 | days = date.toJulianDay() - JULIAN_DAY_FOR_EPOCH; | - | ||||||||||||||||||
2656 | newStatus = ValidDate; | - | ||||||||||||||||||
2657 | } else if (date.isNull()) { | - | ||||||||||||||||||
2658 | newStatus = NullDate; | - | ||||||||||||||||||
2659 | } | - | ||||||||||||||||||
2660 | - | |||||||||||||||||||
2661 | // Set time value and status | - | ||||||||||||||||||
2662 | int ds = 0; | - | ||||||||||||||||||
2663 | if (useTime.isValid()) { | - | ||||||||||||||||||
2664 | ds = useTime.msecsSinceStartOfDay(); | - | ||||||||||||||||||
2665 | newStatus |= ValidTime; | - | ||||||||||||||||||
2666 | } else if (time.isNull()) { | - | ||||||||||||||||||
2667 | newStatus |= NullTime; | - | ||||||||||||||||||
2668 | } | - | ||||||||||||||||||
2669 | - | |||||||||||||||||||
2670 | // Set msecs serial value | - | ||||||||||||||||||
2671 | m_msecs = (days * MSECS_PER_DAY) + ds; | - | ||||||||||||||||||
2672 | m_status = newStatus; | - | ||||||||||||||||||
2673 | - | |||||||||||||||||||
2674 | // Set if date and time are valid | - | ||||||||||||||||||
2675 | checkValidDateTime(); | - | ||||||||||||||||||
2676 | } | - | ||||||||||||||||||
2677 | - | |||||||||||||||||||
2678 | QPair<QDate, QTime> QDateTimePrivate::getDateTime() const | - | ||||||||||||||||||
2679 | { | - | ||||||||||||||||||
2680 | QPair<QDate, QTime> result; | - | ||||||||||||||||||
2681 | msecsToTime(m_msecs, &result.first, &result.second); | - | ||||||||||||||||||
2682 | - | |||||||||||||||||||
2683 | if (isNullDate()) | - | ||||||||||||||||||
2684 | result.first = QDate(); | - | ||||||||||||||||||
2685 | - | |||||||||||||||||||
2686 | if (isNullTime()) | - | ||||||||||||||||||
2687 | result.second = QTime(); | - | ||||||||||||||||||
2688 | - | |||||||||||||||||||
2689 | return result; | - | ||||||||||||||||||
2690 | } | - | ||||||||||||||||||
2691 | - | |||||||||||||||||||
2692 | // Set the Daylight Status if LocalTime set via msecs | - | ||||||||||||||||||
2693 | void QDateTimePrivate::setDaylightStatus(QDateTimePrivate::DaylightStatus status) | - | ||||||||||||||||||
2694 | { | - | ||||||||||||||||||
2695 | if (status == DaylightTime) { | - | ||||||||||||||||||
2696 | m_status = m_status & ~SetToStandardTime; | - | ||||||||||||||||||
2697 | m_status = m_status | SetToDaylightTime; | - | ||||||||||||||||||
2698 | } else if (status == StandardTime) { | - | ||||||||||||||||||
2699 | m_status = m_status & ~SetToDaylightTime; | - | ||||||||||||||||||
2700 | m_status = m_status | SetToStandardTime; | - | ||||||||||||||||||
2701 | } else { | - | ||||||||||||||||||
2702 | clearSetToDaylightStatus(); | - | ||||||||||||||||||
2703 | } | - | ||||||||||||||||||
2704 | } | - | ||||||||||||||||||
2705 | - | |||||||||||||||||||
2706 | // Get the DST Status if LocalTime set via msecs | - | ||||||||||||||||||
2707 | QDateTimePrivate::DaylightStatus QDateTimePrivate::daylightStatus() const | - | ||||||||||||||||||
2708 | { | - | ||||||||||||||||||
2709 | if ((m_status & SetToDaylightTime) == SetToDaylightTime) | - | ||||||||||||||||||
2710 | return DaylightTime; | - | ||||||||||||||||||
2711 | if ((m_status & SetToStandardTime) == SetToStandardTime) | - | ||||||||||||||||||
2712 | return StandardTime; | - | ||||||||||||||||||
2713 | return UnknownDaylightTime; | - | ||||||||||||||||||
2714 | } | - | ||||||||||||||||||
2715 | - | |||||||||||||||||||
2716 | qint64 QDateTimePrivate::toMSecsSinceEpoch() const | - | ||||||||||||||||||
2717 | { | - | ||||||||||||||||||
2718 | switch (m_spec) { | - | ||||||||||||||||||
2719 | case Qt::OffsetFromUTC: | - | ||||||||||||||||||
2720 | case Qt::UTC: | - | ||||||||||||||||||
2721 | return (m_msecs - (m_offsetFromUtc * 1000)); | - | ||||||||||||||||||
2722 | - | |||||||||||||||||||
2723 | case Qt::LocalTime: { | - | ||||||||||||||||||
2724 | // recalculate the local timezone | - | ||||||||||||||||||
2725 | DaylightStatus status = daylightStatus(); | - | ||||||||||||||||||
2726 | return localMSecsToEpochMSecs(m_msecs, &status); | - | ||||||||||||||||||
2727 | } | - | ||||||||||||||||||
2728 | - | |||||||||||||||||||
2729 | case Qt::TimeZone: | - | ||||||||||||||||||
2730 | #ifdef QT_BOOTSTRAPPED | - | ||||||||||||||||||
2731 | return 0; | - | ||||||||||||||||||
2732 | #else | - | ||||||||||||||||||
2733 | return zoneMSecsToEpochMSecs(m_msecs, m_timeZone); | - | ||||||||||||||||||
2734 | #endif | - | ||||||||||||||||||
2735 | } | - | ||||||||||||||||||
2736 | Q_UNREACHABLE(); | - | ||||||||||||||||||
2737 | return 0; | - | ||||||||||||||||||
2738 | } | - | ||||||||||||||||||
2739 | - | |||||||||||||||||||
2740 | // Check the UTC / offsetFromUTC validity | - | ||||||||||||||||||
2741 | void QDateTimePrivate::checkValidDateTime() | - | ||||||||||||||||||
2742 | { | - | ||||||||||||||||||
2743 | switch (m_spec) { | - | ||||||||||||||||||
2744 | case Qt::OffsetFromUTC: | - | ||||||||||||||||||
2745 | case Qt::UTC: | - | ||||||||||||||||||
2746 | // for these, a valid date and a valid time imply a valid QDateTime | - | ||||||||||||||||||
2747 | if (isValidDate() && isValidTime()) | - | ||||||||||||||||||
2748 | setValidDateTime(); | - | ||||||||||||||||||
2749 | else | - | ||||||||||||||||||
2750 | clearValidDateTime(); | - | ||||||||||||||||||
2751 | break; | - | ||||||||||||||||||
2752 | case Qt::TimeZone: | - | ||||||||||||||||||
2753 | case Qt::LocalTime: | - | ||||||||||||||||||
2754 | // for these, we need to check whether the timezone is valid and whether | - | ||||||||||||||||||
2755 | // the time is valid in that timezone. Expensive, but no other option. | - | ||||||||||||||||||
2756 | refreshDateTime(); | - | ||||||||||||||||||
2757 | break; | - | ||||||||||||||||||
2758 | } | - | ||||||||||||||||||
2759 | } | - | ||||||||||||||||||
2760 | - | |||||||||||||||||||
2761 | // Refresh the LocalTime validity and offset | - | ||||||||||||||||||
2762 | void QDateTimePrivate::refreshDateTime() | - | ||||||||||||||||||
2763 | { | - | ||||||||||||||||||
2764 | switch (m_spec) { | - | ||||||||||||||||||
2765 | case Qt::OffsetFromUTC: | - | ||||||||||||||||||
2766 | case Qt::UTC: | - | ||||||||||||||||||
2767 | // Always set by setDateTime so just return | - | ||||||||||||||||||
2768 | return; | - | ||||||||||||||||||
2769 | case Qt::TimeZone: | - | ||||||||||||||||||
2770 | case Qt::LocalTime: | - | ||||||||||||||||||
2771 | break; | - | ||||||||||||||||||
2772 | } | - | ||||||||||||||||||
2773 | - | |||||||||||||||||||
2774 | // If not valid date and time then is invalid | - | ||||||||||||||||||
2775 | if (!isValidDate() || !isValidTime()) { | - | ||||||||||||||||||
2776 | clearValidDateTime(); | - | ||||||||||||||||||
2777 | m_offsetFromUtc = 0; | - | ||||||||||||||||||
2778 | return; | - | ||||||||||||||||||
2779 | } | - | ||||||||||||||||||
2780 | - | |||||||||||||||||||
2781 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
2782 | // If not valid time zone then is invalid | - | ||||||||||||||||||
2783 | if (m_spec == Qt::TimeZone && !m_timeZone.isValid()) { | - | ||||||||||||||||||
2784 | clearValidDateTime(); | - | ||||||||||||||||||
2785 | m_offsetFromUtc = 0; | - | ||||||||||||||||||
2786 | return; | - | ||||||||||||||||||
2787 | } | - | ||||||||||||||||||
2788 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
2789 | - | |||||||||||||||||||
2790 | // We have a valid date and time and a Qt::LocalTime or Qt::TimeZone that needs calculating | - | ||||||||||||||||||
2791 | // LocalTime and TimeZone might fall into a "missing" DST transition hour | - | ||||||||||||||||||
2792 | // Calling toEpochMSecs will adjust the returned date/time if it does | - | ||||||||||||||||||
2793 | QDate testDate; | - | ||||||||||||||||||
2794 | QTime testTime; | - | ||||||||||||||||||
2795 | qint64 epochMSecs = 0; | - | ||||||||||||||||||
2796 | if (m_spec == Qt::LocalTime) { | - | ||||||||||||||||||
2797 | DaylightStatus status = daylightStatus(); | - | ||||||||||||||||||
2798 | epochMSecs = localMSecsToEpochMSecs(m_msecs, &status, &testDate, &testTime); | - | ||||||||||||||||||
2799 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
2800 | } else { | - | ||||||||||||||||||
2801 | epochMSecs = zoneMSecsToEpochMSecs(m_msecs, m_timeZone, &testDate, &testTime); | - | ||||||||||||||||||
2802 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
2803 | } | - | ||||||||||||||||||
2804 | if (timeToMSecs(testDate, testTime) == m_msecs) { | - | ||||||||||||||||||
2805 | setValidDateTime(); | - | ||||||||||||||||||
2806 | // Cache the offset to use in toMSecsSinceEpoch() | - | ||||||||||||||||||
2807 | m_offsetFromUtc = (m_msecs - epochMSecs) / 1000; | - | ||||||||||||||||||
2808 | } else { | - | ||||||||||||||||||
2809 | clearValidDateTime(); | - | ||||||||||||||||||
2810 | m_offsetFromUtc = 0; | - | ||||||||||||||||||
2811 | } | - | ||||||||||||||||||
2812 | } | - | ||||||||||||||||||
2813 | - | |||||||||||||||||||
2814 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
2815 | // Convert a TimeZone time expressed in zone msecs encoding into a UTC epoch msecs | - | ||||||||||||||||||
2816 | qint64 QDateTimePrivate::zoneMSecsToEpochMSecs(qint64 zoneMSecs, const QTimeZone &zone, | - | ||||||||||||||||||
2817 | QDate *localDate, QTime *localTime) | - | ||||||||||||||||||
2818 | { | - | ||||||||||||||||||
2819 | // Get the effective data from QTimeZone | - | ||||||||||||||||||
2820 | QTimeZonePrivate::Data data = zone.d->dataForLocalTime(zoneMSecs); | - | ||||||||||||||||||
2821 | // Docs state any LocalTime before 1970-01-01 will *not* have any DST applied | - | ||||||||||||||||||
2822 | // but all affected times afterwards will have DST applied. | - | ||||||||||||||||||
2823 | if (data.atMSecsSinceEpoch >= 0) { | - | ||||||||||||||||||
2824 | msecsToTime(data.atMSecsSinceEpoch + (data.offsetFromUtc * 1000), localDate, localTime); | - | ||||||||||||||||||
2825 | return data.atMSecsSinceEpoch; | - | ||||||||||||||||||
2826 | } else { | - | ||||||||||||||||||
2827 | msecsToTime(zoneMSecs, localDate, localTime); | - | ||||||||||||||||||
2828 | return zoneMSecs - (data.standardTimeOffset * 1000); | - | ||||||||||||||||||
2829 | } | - | ||||||||||||||||||
2830 | } | - | ||||||||||||||||||
2831 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
2832 | - | |||||||||||||||||||
2833 | /***************************************************************************** | - | ||||||||||||||||||
2834 | QDateTime member functions | - | ||||||||||||||||||
2835 | *****************************************************************************/ | - | ||||||||||||||||||
2836 | - | |||||||||||||||||||
2837 | /*! | - | ||||||||||||||||||
2838 | \class QDateTime | - | ||||||||||||||||||
2839 | \inmodule QtCore | - | ||||||||||||||||||
2840 | \ingroup shared | - | ||||||||||||||||||
2841 | \reentrant | - | ||||||||||||||||||
2842 | \brief The QDateTime class provides date and time functions. | - | ||||||||||||||||||
2843 | - | |||||||||||||||||||
2844 | - | |||||||||||||||||||
2845 | A QDateTime object contains a calendar date and a clock time (a | - | ||||||||||||||||||
2846 | "datetime"). It is a combination of the QDate and QTime classes. | - | ||||||||||||||||||
2847 | It can read the current datetime from the system clock. It | - | ||||||||||||||||||
2848 | provides functions for comparing datetimes and for manipulating a | - | ||||||||||||||||||
2849 | datetime by adding a number of seconds, days, months, or years. | - | ||||||||||||||||||
2850 | - | |||||||||||||||||||
2851 | A QDateTime object is typically created either by giving a date | - | ||||||||||||||||||
2852 | and time explicitly in the constructor, or by using the static | - | ||||||||||||||||||
2853 | function currentDateTime() that returns a QDateTime object set | - | ||||||||||||||||||
2854 | to the system clock's time. The date and time can be changed with | - | ||||||||||||||||||
2855 | setDate() and setTime(). A datetime can also be set using the | - | ||||||||||||||||||
2856 | setTime_t() function that takes a POSIX-standard "number of | - | ||||||||||||||||||
2857 | seconds since 00:00:00 on January 1, 1970" value. The fromString() | - | ||||||||||||||||||
2858 | function returns a QDateTime, given a string and a date format | - | ||||||||||||||||||
2859 | used to interpret the date within the string. | - | ||||||||||||||||||
2860 | - | |||||||||||||||||||
2861 | The date() and time() functions provide access to the date and | - | ||||||||||||||||||
2862 | time parts of the datetime. The same information is provided in | - | ||||||||||||||||||
2863 | textual format by the toString() function. | - | ||||||||||||||||||
2864 | - | |||||||||||||||||||
2865 | QDateTime provides a full set of operators to compare two | - | ||||||||||||||||||
2866 | QDateTime objects, where smaller means earlier and larger means | - | ||||||||||||||||||
2867 | later. | - | ||||||||||||||||||
2868 | - | |||||||||||||||||||
2869 | You can increment (or decrement) a datetime by a given number of | - | ||||||||||||||||||
2870 | milliseconds using addMSecs(), seconds using addSecs(), or days | - | ||||||||||||||||||
2871 | using addDays(). Similarly, you can use addMonths() and addYears(). | - | ||||||||||||||||||
2872 | The daysTo() function returns the number of days between two datetimes, | - | ||||||||||||||||||
2873 | secsTo() returns the number of seconds between two datetimes, and | - | ||||||||||||||||||
2874 | msecsTo() returns the number of milliseconds between two datetimes. | - | ||||||||||||||||||
2875 | - | |||||||||||||||||||
2876 | QDateTime can store datetimes as \l{Qt::LocalTime}{local time} or | - | ||||||||||||||||||
2877 | as \l{Qt::UTC}{UTC}. QDateTime::currentDateTime() returns a | - | ||||||||||||||||||
2878 | QDateTime expressed as local time; use toUTC() to convert it to | - | ||||||||||||||||||
2879 | UTC. You can also use timeSpec() to find out if a QDateTime | - | ||||||||||||||||||
2880 | object stores a UTC time or a local time. Operations such as | - | ||||||||||||||||||
2881 | addSecs() and secsTo() are aware of daylight-saving time (DST). | - | ||||||||||||||||||
2882 | - | |||||||||||||||||||
2883 | \note QDateTime does not account for leap seconds. | - | ||||||||||||||||||
2884 | - | |||||||||||||||||||
2885 | \section1 | - | ||||||||||||||||||
2886 | - | |||||||||||||||||||
2887 | \section2 No Year 0 | - | ||||||||||||||||||
2888 | - | |||||||||||||||||||
2889 | There is no year 0. Dates in that year are considered invalid. The | - | ||||||||||||||||||
2890 | year -1 is the year "1 before Christ" or "1 before current era." | - | ||||||||||||||||||
2891 | The day before 1 January 1 CE is 31 December 1 BCE. | - | ||||||||||||||||||
2892 | - | |||||||||||||||||||
2893 | \section2 Range of Valid Dates | - | ||||||||||||||||||
2894 | - | |||||||||||||||||||
2895 | The range of valid values able to be stored in QDateTime is dependent on | - | ||||||||||||||||||
2896 | the internal storage implementation. QDateTime is currently stored in a | - | ||||||||||||||||||
2897 | qint64 as a serial msecs value encoding the date and time. This restricts | - | ||||||||||||||||||
2898 | the date range to about +/- 292 million years, compared to the QDate range | - | ||||||||||||||||||
2899 | of +/- 2 billion years. Care must be taken when creating a QDateTime with | - | ||||||||||||||||||
2900 | extreme values that you do not overflow the storage. The exact range of | - | ||||||||||||||||||
2901 | supported values varies depending on the Qt::TimeSpec and time zone. | - | ||||||||||||||||||
2902 | - | |||||||||||||||||||
2903 | \section2 | - | ||||||||||||||||||
2904 | Use of System Timezone | - | ||||||||||||||||||
2905 | - | |||||||||||||||||||
2906 | QDateTime uses the system's time zone information to determine the | - | ||||||||||||||||||
2907 | offset of local time from UTC. If the system is not configured | - | ||||||||||||||||||
2908 | correctly or not up-to-date, QDateTime will give wrong results as | - | ||||||||||||||||||
2909 | well. | - | ||||||||||||||||||
2910 | - | |||||||||||||||||||
2911 | \section2 Daylight-Saving Time (DST) | - | ||||||||||||||||||
2912 | - | |||||||||||||||||||
2913 | QDateTime takes into account the system's time zone information | - | ||||||||||||||||||
2914 | when dealing with DST. On modern Unix systems, this means it | - | ||||||||||||||||||
2915 | applies the correct historical DST data whenever possible. On | - | ||||||||||||||||||
2916 | Windows, where the system doesn't support historical DST data, | - | ||||||||||||||||||
2917 | historical accuracy is not maintained with respect to DST. | - | ||||||||||||||||||
2918 | - | |||||||||||||||||||
2919 | The range of valid dates taking DST into account is 1970-01-01 to | - | ||||||||||||||||||
2920 | the present, and rules are in place for handling DST correctly | - | ||||||||||||||||||
2921 | until 2037-12-31, but these could change. For dates falling | - | ||||||||||||||||||
2922 | outside that range, QDateTime makes a \e{best guess} using the | - | ||||||||||||||||||
2923 | rules for year 1970 or 2037, but we can't guarantee accuracy. This | - | ||||||||||||||||||
2924 | means QDateTime doesn't take into account changes in a locale's | - | ||||||||||||||||||
2925 | time zone before 1970, even if the system's time zone database | - | ||||||||||||||||||
2926 | supports that information. | - | ||||||||||||||||||
2927 | - | |||||||||||||||||||
2928 | QDateTime takes into consideration the Standard Time to Daylight-Saving Time | - | ||||||||||||||||||
2929 | transition. For example if the transition is at 2am and the clock goes | - | ||||||||||||||||||
2930 | forward to 3am, then there is a "missing" hour from 02:00:00 to 02:59:59.999 | - | ||||||||||||||||||
2931 | which QDateTime considers to be invalid. Any date maths performed | - | ||||||||||||||||||
2932 | will take this missing hour into account and return a valid result. | - | ||||||||||||||||||
2933 | - | |||||||||||||||||||
2934 | \section2 Offset From UTC | - | ||||||||||||||||||
2935 | - | |||||||||||||||||||
2936 | A Qt::TimeSpec of Qt::OffsetFromUTC is also supported. This allows you | - | ||||||||||||||||||
2937 | to define a QDateTime relative to UTC at a fixed offset of a given number | - | ||||||||||||||||||
2938 | of seconds from UTC. For example, an offset of +3600 seconds is one hour | - | ||||||||||||||||||
2939 | ahead of UTC and is usually written in ISO standard notation as | - | ||||||||||||||||||
2940 | "UTC+01:00". Daylight-Saving Time never applies with this TimeSpec. | - | ||||||||||||||||||
2941 | - | |||||||||||||||||||
2942 | There is no explicit size restriction to the offset seconds, but there is | - | ||||||||||||||||||
2943 | an implicit limit imposed when using the toString() and fromString() | - | ||||||||||||||||||
2944 | methods which use a format of [+|-]hh:mm, effectively limiting the range | - | ||||||||||||||||||
2945 | to +/- 99 hours and 59 minutes and whole minutes only. Note that currently | - | ||||||||||||||||||
2946 | no time zone lies outside the range of +/- 14 hours. | - | ||||||||||||||||||
2947 | - | |||||||||||||||||||
2948 | \section2 Time Zone Support | - | ||||||||||||||||||
2949 | - | |||||||||||||||||||
2950 | A Qt::TimeSpec of Qt::TimeZone is also supported in conjunction with the | - | ||||||||||||||||||
2951 | QTimeZone class. This allows you to define a datetime in a named time zone | - | ||||||||||||||||||
2952 | adhering to a consistent set of daylight-saving transition rules. For | - | ||||||||||||||||||
2953 | example a time zone of "Europe/Berlin" will apply the daylight-saving | - | ||||||||||||||||||
2954 | rules as used in Germany since 1970. Note that the transition rules | - | ||||||||||||||||||
2955 | applied depend on the platform support. See the QTimeZone documentation | - | ||||||||||||||||||
2956 | for more details. | - | ||||||||||||||||||
2957 | - | |||||||||||||||||||
2958 | \sa QDate, QTime, QDateTimeEdit, QTimeZone | - | ||||||||||||||||||
2959 | */ | - | ||||||||||||||||||
2960 | - | |||||||||||||||||||
2961 | /*! | - | ||||||||||||||||||
2962 | Constructs a null datetime (i.e. null date and null time). A null | - | ||||||||||||||||||
2963 | datetime is invalid, since the date is invalid. | - | ||||||||||||||||||
2964 | - | |||||||||||||||||||
2965 | \sa isValid() | - | ||||||||||||||||||
2966 | */ | - | ||||||||||||||||||
2967 | QDateTime::QDateTime() | - | ||||||||||||||||||
2968 | : d(new QDateTimePrivate)(*defaultDateTimePrivate()) | - | ||||||||||||||||||
2969 | { | - | ||||||||||||||||||
2970 | } executed 7103614 times by 127 tests: end of block Executed by:
| 7103614 | ||||||||||||||||||
2971 | - | |||||||||||||||||||
2972 | - | |||||||||||||||||||
2973 | /*! | - | ||||||||||||||||||
2974 | Constructs a datetime with the given \a date, a valid | - | ||||||||||||||||||
2975 | time(00:00:00.000), and sets the timeSpec() to Qt::LocalTime. | - | ||||||||||||||||||
2976 | */ | - | ||||||||||||||||||
2977 | - | |||||||||||||||||||
2978 | QDateTime::QDateTime(const QDate &date) | - | ||||||||||||||||||
2979 | : d(new QDateTimePrivate(date, QTime(0, 0, 0), Qt::LocalTime, 0)) | - | ||||||||||||||||||
2980 | { | - | ||||||||||||||||||
2981 | } | - | ||||||||||||||||||
2982 | - | |||||||||||||||||||
2983 | /*! | - | ||||||||||||||||||
2984 | Constructs a datetime with the given \a date and \a time, using | - | ||||||||||||||||||
2985 | the time specification defined by \a spec. | - | ||||||||||||||||||
2986 | - | |||||||||||||||||||
2987 | If \a date is valid and \a time is not, the time will be set to midnight. | - | ||||||||||||||||||
2988 | - | |||||||||||||||||||
2989 | If \a spec is Qt::OffsetFromUTC then it will be set to Qt::UTC, i.e. an | - | ||||||||||||||||||
2990 | offset of 0 seconds. To create a Qt::OffsetFromUTC datetime use the | - | ||||||||||||||||||
2991 | correct constructor. | - | ||||||||||||||||||
2992 | - | |||||||||||||||||||
2993 | If \a spec is Qt::TimeZone then the spec will be set to Qt::LocalTime, | - | ||||||||||||||||||
2994 | i.e. the current system time zone. To create a Qt::TimeZone datetime | - | ||||||||||||||||||
2995 | use the correct constructor. | - | ||||||||||||||||||
2996 | */ | - | ||||||||||||||||||
2997 | - | |||||||||||||||||||
2998 | QDateTime::QDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec) | - | ||||||||||||||||||
2999 | : d(new QDateTimePrivate(date, time, spec, 0)) | - | ||||||||||||||||||
3000 | { | - | ||||||||||||||||||
3001 | } | - | ||||||||||||||||||
3002 | - | |||||||||||||||||||
3003 | /*! | - | ||||||||||||||||||
3004 | \since 5.2 | - | ||||||||||||||||||
3005 | - | |||||||||||||||||||
3006 | Constructs a datetime with the given \a date and \a time, using | - | ||||||||||||||||||
3007 | the time specification defined by \a spec and \a offsetSeconds seconds. | - | ||||||||||||||||||
3008 | - | |||||||||||||||||||
3009 | If \a date is valid and \a time is not, the time will be set to midnight. | - | ||||||||||||||||||
3010 | - | |||||||||||||||||||
3011 | If the \a spec is not Qt::OffsetFromUTC then \a offsetSeconds will be ignored. | - | ||||||||||||||||||
3012 | - | |||||||||||||||||||
3013 | If the \a spec is Qt::OffsetFromUTC and \a offsetSeconds is 0 then the | - | ||||||||||||||||||
3014 | timeSpec() will be set to Qt::UTC, i.e. an offset of 0 seconds. | - | ||||||||||||||||||
3015 | - | |||||||||||||||||||
3016 | If \a spec is Qt::TimeZone then the spec will be set to Qt::LocalTime, | - | ||||||||||||||||||
3017 | i.e. the current system time zone. To create a Qt::TimeZone datetime | - | ||||||||||||||||||
3018 | use the correct constructor. | - | ||||||||||||||||||
3019 | */ | - | ||||||||||||||||||
3020 | - | |||||||||||||||||||
3021 | QDateTime::QDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec, int offsetSeconds) | - | ||||||||||||||||||
3022 | : d(new QDateTimePrivate(date, time, spec, offsetSeconds)) | - | ||||||||||||||||||
3023 | { | - | ||||||||||||||||||
3024 | } | - | ||||||||||||||||||
3025 | - | |||||||||||||||||||
3026 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
3027 | /*! | - | ||||||||||||||||||
3028 | \since 5.2 | - | ||||||||||||||||||
3029 | - | |||||||||||||||||||
3030 | Constructs a datetime with the given \a date and \a time, using | - | ||||||||||||||||||
3031 | the Time Zone specified by \a timeZone. | - | ||||||||||||||||||
3032 | - | |||||||||||||||||||
3033 | If \a date is valid and \a time is not, the time will be set to 00:00:00. | - | ||||||||||||||||||
3034 | - | |||||||||||||||||||
3035 | If \a timeZone is invalid then the datetime will be invalid. | - | ||||||||||||||||||
3036 | */ | - | ||||||||||||||||||
3037 | - | |||||||||||||||||||
3038 | QDateTime::QDateTime(const QDate &date, const QTime &time, const QTimeZone &timeZone) | - | ||||||||||||||||||
3039 | : d(new QDateTimePrivate(date, time, timeZone)) | - | ||||||||||||||||||
3040 | { | - | ||||||||||||||||||
3041 | } | - | ||||||||||||||||||
3042 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
3043 | - | |||||||||||||||||||
3044 | /*! | - | ||||||||||||||||||
3045 | Constructs a copy of the \a other datetime. | - | ||||||||||||||||||
3046 | */ | - | ||||||||||||||||||
3047 | - | |||||||||||||||||||
3048 | QDateTime::QDateTime(const QDateTime &other) | - | ||||||||||||||||||
3049 | : d(other.d) | - | ||||||||||||||||||
3050 | { | - | ||||||||||||||||||
3051 | } | - | ||||||||||||||||||
3052 | - | |||||||||||||||||||
3053 | /*! | - | ||||||||||||||||||
3054 | Destroys the datetime. | - | ||||||||||||||||||
3055 | */ | - | ||||||||||||||||||
3056 | QDateTime::~QDateTime() | - | ||||||||||||||||||
3057 | { | - | ||||||||||||||||||
3058 | } | - | ||||||||||||||||||
3059 | - | |||||||||||||||||||
3060 | /*! | - | ||||||||||||||||||
3061 | Makes a copy of the \a other datetime and returns a reference to the | - | ||||||||||||||||||
3062 | copy. | - | ||||||||||||||||||
3063 | */ | - | ||||||||||||||||||
3064 | - | |||||||||||||||||||
3065 | QDateTime &QDateTime::operator=(const QDateTime &other) | - | ||||||||||||||||||
3066 | { | - | ||||||||||||||||||
3067 | d = other.d; | - | ||||||||||||||||||
3068 | return *this; | - | ||||||||||||||||||
3069 | } | - | ||||||||||||||||||
3070 | /*! | - | ||||||||||||||||||
3071 | \fn void QDateTime::swap(QDateTime &other) | - | ||||||||||||||||||
3072 | \since 5.0 | - | ||||||||||||||||||
3073 | - | |||||||||||||||||||
3074 | Swaps this datetime with \a other. This operation is very fast | - | ||||||||||||||||||
3075 | and never fails. | - | ||||||||||||||||||
3076 | */ | - | ||||||||||||||||||
3077 | - | |||||||||||||||||||
3078 | /*! | - | ||||||||||||||||||
3079 | Returns \c true if both the date and the time are null; otherwise | - | ||||||||||||||||||
3080 | returns \c false. A null datetime is invalid. | - | ||||||||||||||||||
3081 | - | |||||||||||||||||||
3082 | \sa QDate::isNull(), QTime::isNull(), isValid() | - | ||||||||||||||||||
3083 | */ | - | ||||||||||||||||||
3084 | - | |||||||||||||||||||
3085 | bool QDateTime::isNull() const | - | ||||||||||||||||||
3086 | { | - | ||||||||||||||||||
3087 | return d->isNullDate() && d->isNullTime(); | - | ||||||||||||||||||
3088 | } | - | ||||||||||||||||||
3089 | - | |||||||||||||||||||
3090 | /*! | - | ||||||||||||||||||
3091 | Returns \c true if both the date and the time are valid and they are valid in | - | ||||||||||||||||||
3092 | the current Qt::TimeSpec, otherwise returns \c false. | - | ||||||||||||||||||
3093 | - | |||||||||||||||||||
3094 | If the timeSpec() is Qt::LocalTime or Qt::TimeZone then the date and time are | - | ||||||||||||||||||
3095 | checked to see if they fall in the Standard Time to Daylight-Saving Time transition | - | ||||||||||||||||||
3096 | hour, i.e. if the transition is at 2am and the clock goes forward to 3am | - | ||||||||||||||||||
3097 | then the time from 02:00:00 to 02:59:59.999 is considered to be invalid. | - | ||||||||||||||||||
3098 | - | |||||||||||||||||||
3099 | \sa QDate::isValid(), QTime::isValid() | - | ||||||||||||||||||
3100 | */ | - | ||||||||||||||||||
3101 | - | |||||||||||||||||||
3102 | bool QDateTime::isValid() const | - | ||||||||||||||||||
3103 | { | - | ||||||||||||||||||
3104 | return (d->isValidDateTime()); | - | ||||||||||||||||||
3105 | } | - | ||||||||||||||||||
3106 | - | |||||||||||||||||||
3107 | /*! | - | ||||||||||||||||||
3108 | Returns the date part of the datetime. | - | ||||||||||||||||||
3109 | - | |||||||||||||||||||
3110 | \sa setDate(), time(), timeSpec() | - | ||||||||||||||||||
3111 | */ | - | ||||||||||||||||||
3112 | - | |||||||||||||||||||
3113 | QDate QDateTime::date() const | - | ||||||||||||||||||
3114 | { | - | ||||||||||||||||||
3115 | if (d->isNullDate()) | - | ||||||||||||||||||
3116 | return QDate(); | - | ||||||||||||||||||
3117 | QDate dt; | - | ||||||||||||||||||
3118 | msecsToTime(d->m_msecs, &dt, 0); | - | ||||||||||||||||||
3119 | return dt; | - | ||||||||||||||||||
3120 | } | - | ||||||||||||||||||
3121 | - | |||||||||||||||||||
3122 | /*! | - | ||||||||||||||||||
3123 | Returns the time part of the datetime. | - | ||||||||||||||||||
3124 | - | |||||||||||||||||||
3125 | \sa setTime(), date(), timeSpec() | - | ||||||||||||||||||
3126 | */ | - | ||||||||||||||||||
3127 | - | |||||||||||||||||||
3128 | QTime QDateTime::time() const | - | ||||||||||||||||||
3129 | { | - | ||||||||||||||||||
3130 | if (d->isNullTime()) | - | ||||||||||||||||||
3131 | return QTime(); | - | ||||||||||||||||||
3132 | QTime tm; | - | ||||||||||||||||||
3133 | msecsToTime(d->m_msecs, 0, &tm); | - | ||||||||||||||||||
3134 | return tm; | - | ||||||||||||||||||
3135 | } | - | ||||||||||||||||||
3136 | - | |||||||||||||||||||
3137 | /*! | - | ||||||||||||||||||
3138 | Returns the time specification of the datetime. | - | ||||||||||||||||||
3139 | - | |||||||||||||||||||
3140 | \sa setTimeSpec(), date(), time(), Qt::TimeSpec | - | ||||||||||||||||||
3141 | */ | - | ||||||||||||||||||
3142 | - | |||||||||||||||||||
3143 | Qt::TimeSpec QDateTime::timeSpec() const | - | ||||||||||||||||||
3144 | { | - | ||||||||||||||||||
3145 | return d->m_spec; | - | ||||||||||||||||||
3146 | } | - | ||||||||||||||||||
3147 | - | |||||||||||||||||||
3148 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
3149 | /*! | - | ||||||||||||||||||
3150 | \since 5.2 | - | ||||||||||||||||||
3151 | - | |||||||||||||||||||
3152 | Returns the time zone of the datetime. | - | ||||||||||||||||||
3153 | - | |||||||||||||||||||
3154 | If the timeSpec() is Qt::LocalTime then an instance of the current system | - | ||||||||||||||||||
3155 | time zone will be returned. Note however that if you copy this time zone | - | ||||||||||||||||||
3156 | the instance will not remain in sync if the system time zone changes. | - | ||||||||||||||||||
3157 | - | |||||||||||||||||||
3158 | \sa setTimeZone(), Qt::TimeSpec | - | ||||||||||||||||||
3159 | */ | - | ||||||||||||||||||
3160 | - | |||||||||||||||||||
3161 | QTimeZone QDateTime::timeZone() const | - | ||||||||||||||||||
3162 | { | - | ||||||||||||||||||
3163 | switch (d->m_spec) { | - | ||||||||||||||||||
3164 | case Qt::UTC: | - | ||||||||||||||||||
3165 | return QTimeZone::utc(); | - | ||||||||||||||||||
3166 | case Qt::OffsetFromUTC: | - | ||||||||||||||||||
3167 | return QTimeZone(d->m_offsetFromUtc); | - | ||||||||||||||||||
3168 | case Qt::TimeZone: | - | ||||||||||||||||||
3169 | Q_ASSERT(d->m_timeZone.isValid()); | - | ||||||||||||||||||
3170 | return d->m_timeZone; | - | ||||||||||||||||||
3171 | case Qt::LocalTime: | - | ||||||||||||||||||
3172 | return QTimeZone::systemTimeZone(); | - | ||||||||||||||||||
3173 | } | - | ||||||||||||||||||
3174 | return QTimeZone(); | - | ||||||||||||||||||
3175 | } | - | ||||||||||||||||||
3176 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
3177 | - | |||||||||||||||||||
3178 | /*! | - | ||||||||||||||||||
3179 | \since 5.2 | - | ||||||||||||||||||
3180 | - | |||||||||||||||||||
3181 | Returns the current Offset From UTC in seconds. | - | ||||||||||||||||||
3182 | - | |||||||||||||||||||
3183 | If the timeSpec() is Qt::OffsetFromUTC this will be the value originally set. | - | ||||||||||||||||||
3184 | - | |||||||||||||||||||
3185 | If the timeSpec() is Qt::TimeZone this will be the offset effective in the | - | ||||||||||||||||||
3186 | Time Zone including any Daylight-Saving Offset. | - | ||||||||||||||||||
3187 | - | |||||||||||||||||||
3188 | If the timeSpec() is Qt::LocalTime this will be the difference between the | - | ||||||||||||||||||
3189 | Local Time and UTC including any Daylight-Saving Offset. | - | ||||||||||||||||||
3190 | - | |||||||||||||||||||
3191 | If the timeSpec() is Qt::UTC this will be 0. | - | ||||||||||||||||||
3192 | - | |||||||||||||||||||
3193 | \sa setOffsetFromUtc() | - | ||||||||||||||||||
3194 | */ | - | ||||||||||||||||||
3195 | - | |||||||||||||||||||
3196 | int QDateTime::offsetFromUtc() const | - | ||||||||||||||||||
3197 | { | - | ||||||||||||||||||
3198 | return d->m_offsetFromUtc; | - | ||||||||||||||||||
3199 | } | - | ||||||||||||||||||
3200 | - | |||||||||||||||||||
3201 | /*! | - | ||||||||||||||||||
3202 | \since 5.2 | - | ||||||||||||||||||
3203 | - | |||||||||||||||||||
3204 | Returns the Time Zone Abbreviation for the datetime. | - | ||||||||||||||||||
3205 | - | |||||||||||||||||||
3206 | If the timeSpec() is Qt::UTC this will be "UTC". | - | ||||||||||||||||||
3207 | - | |||||||||||||||||||
3208 | If the timeSpec() is Qt::OffsetFromUTC this will be in the format | - | ||||||||||||||||||
3209 | "UTC[+-]00:00". | - | ||||||||||||||||||
3210 | - | |||||||||||||||||||
3211 | If the timeSpec() is Qt::LocalTime then the host system is queried for the | - | ||||||||||||||||||
3212 | correct abbreviation. | - | ||||||||||||||||||
3213 | - | |||||||||||||||||||
3214 | Note that abbreviations may or may not be localized. | - | ||||||||||||||||||
3215 | - | |||||||||||||||||||
3216 | Note too that the abbreviation is not guaranteed to be a unique value, | - | ||||||||||||||||||
3217 | i.e. different time zones may have the same abbreviation. | - | ||||||||||||||||||
3218 | - | |||||||||||||||||||
3219 | \sa timeSpec() | - | ||||||||||||||||||
3220 | */ | - | ||||||||||||||||||
3221 | - | |||||||||||||||||||
3222 | QString QDateTime::timeZoneAbbreviation() const | - | ||||||||||||||||||
3223 | { | - | ||||||||||||||||||
3224 | switch (d->m_spec) { | - | ||||||||||||||||||
3225 | case Qt::UTC: | - | ||||||||||||||||||
3226 | return QTimeZonePrivate::utcQString(); | - | ||||||||||||||||||
3227 | case Qt::OffsetFromUTC: | - | ||||||||||||||||||
3228 | return QTimeZonePrivate::utcQString() + toOffsetString(Qt::ISODate, d->m_offsetFromUtc); | - | ||||||||||||||||||
3229 | case Qt::TimeZone: | - | ||||||||||||||||||
3230 | #ifdef QT_BOOTSTRAPPED | - | ||||||||||||||||||
3231 | break; | - | ||||||||||||||||||
3232 | #else | - | ||||||||||||||||||
3233 | return d->m_timeZone.d->abbreviation(d->toMSecsSinceEpoch()); | - | ||||||||||||||||||
3234 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
3235 | case Qt::LocalTime: { | - | ||||||||||||||||||
3236 | QString abbrev; | - | ||||||||||||||||||
3237 | QDateTimePrivate::DaylightStatus status = d->daylightStatus(); | - | ||||||||||||||||||
3238 | localMSecsToEpochMSecs(d->m_msecs, &status, 0, 0, &abbrev); | - | ||||||||||||||||||
3239 | return abbrev; | - | ||||||||||||||||||
3240 | } | - | ||||||||||||||||||
3241 | } | - | ||||||||||||||||||
3242 | return QString(); | - | ||||||||||||||||||
3243 | } | - | ||||||||||||||||||
3244 | - | |||||||||||||||||||
3245 | /*! | - | ||||||||||||||||||
3246 | \since 5.2 | - | ||||||||||||||||||
3247 | - | |||||||||||||||||||
3248 | Returns if this datetime falls in Daylight-Saving Time. | - | ||||||||||||||||||
3249 | - | |||||||||||||||||||
3250 | If the Qt::TimeSpec is not Qt::LocalTime or Qt::TimeZone then will always | - | ||||||||||||||||||
3251 | return false. | - | ||||||||||||||||||
3252 | - | |||||||||||||||||||
3253 | \sa timeSpec() | - | ||||||||||||||||||
3254 | */ | - | ||||||||||||||||||
3255 | - | |||||||||||||||||||
3256 | bool QDateTime::isDaylightTime() const | - | ||||||||||||||||||
3257 | { | - | ||||||||||||||||||
3258 | switch (d->m_spec) { | - | ||||||||||||||||||
3259 | case Qt::UTC: | - | ||||||||||||||||||
3260 | case Qt::OffsetFromUTC: | - | ||||||||||||||||||
3261 | return false; | - | ||||||||||||||||||
3262 | case Qt::TimeZone: | - | ||||||||||||||||||
3263 | #ifdef QT_BOOTSTRAPPED | - | ||||||||||||||||||
3264 | break; | - | ||||||||||||||||||
3265 | #else | - | ||||||||||||||||||
3266 | return d->m_timeZone.d->isDaylightTime(toMSecsSinceEpoch()); | - | ||||||||||||||||||
3267 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
3268 | case Qt::LocalTime: { | - | ||||||||||||||||||
3269 | QDateTimePrivate::DaylightStatus status = d->daylightStatus(); | - | ||||||||||||||||||
3270 | if (status == QDateTimePrivate::UnknownDaylightTime) | - | ||||||||||||||||||
3271 | localMSecsToEpochMSecs(d->m_msecs, &status); | - | ||||||||||||||||||
3272 | return (status == QDateTimePrivate::DaylightTime); | - | ||||||||||||||||||
3273 | } | - | ||||||||||||||||||
3274 | } | - | ||||||||||||||||||
3275 | return false; | - | ||||||||||||||||||
3276 | } | - | ||||||||||||||||||
3277 | - | |||||||||||||||||||
3278 | /*! | - | ||||||||||||||||||
3279 | Sets the date part of this datetime to \a date. If no time is set yet, it | - | ||||||||||||||||||
3280 | is set to midnight. If \a date is invalid, this QDateTime becomes invalid. | - | ||||||||||||||||||
3281 | - | |||||||||||||||||||
3282 | \sa date(), setTime(), setTimeSpec() | - | ||||||||||||||||||
3283 | */ | - | ||||||||||||||||||
3284 | - | |||||||||||||||||||
3285 | void QDateTime::setDate(const QDate &date) | - | ||||||||||||||||||
3286 | { | - | ||||||||||||||||||
3287 | d->setDateTime(date, time()); | - | ||||||||||||||||||
3288 | } | - | ||||||||||||||||||
3289 | - | |||||||||||||||||||
3290 | /*! | - | ||||||||||||||||||
3291 | Sets the time part of this datetime to \a time. If \a time is not valid, | - | ||||||||||||||||||
3292 | this function sets it to midnight. Therefore, it's possible to clear any | - | ||||||||||||||||||
3293 | set time in a QDateTime by setting it to a default QTime: | - | ||||||||||||||||||
3294 | - | |||||||||||||||||||
3295 | \code | - | ||||||||||||||||||
3296 | QDateTime dt = QDateTime::currentDateTime(); | - | ||||||||||||||||||
3297 | dt.setTime(QTime()); | - | ||||||||||||||||||
3298 | \endcode | - | ||||||||||||||||||
3299 | - | |||||||||||||||||||
3300 | \sa time(), setDate(), setTimeSpec() | - | ||||||||||||||||||
3301 | */ | - | ||||||||||||||||||
3302 | - | |||||||||||||||||||
3303 | void QDateTime::setTime(const QTime &time) | - | ||||||||||||||||||
3304 | { | - | ||||||||||||||||||
3305 | d->setDateTime(date(), time); | - | ||||||||||||||||||
3306 | } | - | ||||||||||||||||||
3307 | - | |||||||||||||||||||
3308 | /*! | - | ||||||||||||||||||
3309 | Sets the time specification used in this datetime to \a spec. | - | ||||||||||||||||||
3310 | The datetime will refer to a different point in time. | - | ||||||||||||||||||
3311 | - | |||||||||||||||||||
3312 | If \a spec is Qt::OffsetFromUTC then the timeSpec() will be set | - | ||||||||||||||||||
3313 | to Qt::UTC, i.e. an effective offset of 0. | - | ||||||||||||||||||
3314 | - | |||||||||||||||||||
3315 | If \a spec is Qt::TimeZone then the spec will be set to Qt::LocalTime, | - | ||||||||||||||||||
3316 | i.e. the current system time zone. | - | ||||||||||||||||||
3317 | - | |||||||||||||||||||
3318 | Example: | - | ||||||||||||||||||
3319 | \snippet code/src_corelib_tools_qdatetime.cpp 19 | - | ||||||||||||||||||
3320 | - | |||||||||||||||||||
3321 | \sa timeSpec(), setDate(), setTime(), setTimeZone(), Qt::TimeSpec | - | ||||||||||||||||||
3322 | */ | - | ||||||||||||||||||
3323 | - | |||||||||||||||||||
3324 | void QDateTime::setTimeSpec(Qt::TimeSpec spec) | - | ||||||||||||||||||
3325 | { | - | ||||||||||||||||||
3326 | QDateTimePrivate *d = this->d.data(); // detaches (and shadows d) | - | ||||||||||||||||||
3327 | d->setTimeSpec(spec, 0); | - | ||||||||||||||||||
3328 | d->checkValidDateTime(); | - | ||||||||||||||||||
3329 | } | - | ||||||||||||||||||
3330 | - | |||||||||||||||||||
3331 | /*! | - | ||||||||||||||||||
3332 | \since 5.2 | - | ||||||||||||||||||
3333 | - | |||||||||||||||||||
3334 | Sets the timeSpec() to Qt::OffsetFromUTC and the offset to \a offsetSeconds. | - | ||||||||||||||||||
3335 | The datetime will refer to a different point in time. | - | ||||||||||||||||||
3336 | - | |||||||||||||||||||
3337 | The maximum and minimum offset is 14 positive or negative hours. If | - | ||||||||||||||||||
3338 | \a offsetSeconds is larger or smaller than that, then the result is | - | ||||||||||||||||||
3339 | undefined. | - | ||||||||||||||||||
3340 | - | |||||||||||||||||||
3341 | If \a offsetSeconds is 0 then the timeSpec() will be set to Qt::UTC. | - | ||||||||||||||||||
3342 | - | |||||||||||||||||||
3343 | \sa isValid(), offsetFromUtc() | - | ||||||||||||||||||
3344 | */ | - | ||||||||||||||||||
3345 | - | |||||||||||||||||||
3346 | void QDateTime::setOffsetFromUtc(int offsetSeconds) | - | ||||||||||||||||||
3347 | { | - | ||||||||||||||||||
3348 | QDateTimePrivate *d = this->d.data(); // detaches (and shadows d) | - | ||||||||||||||||||
3349 | d->setTimeSpec(Qt::OffsetFromUTC, offsetSeconds); | - | ||||||||||||||||||
3350 | d->checkValidDateTime(); | - | ||||||||||||||||||
3351 | } | - | ||||||||||||||||||
3352 | - | |||||||||||||||||||
3353 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
3354 | /*! | - | ||||||||||||||||||
3355 | \since 5.2 | - | ||||||||||||||||||
3356 | - | |||||||||||||||||||
3357 | Sets the time zone used in this datetime to \a toZone. | - | ||||||||||||||||||
3358 | The datetime will refer to a different point in time. | - | ||||||||||||||||||
3359 | - | |||||||||||||||||||
3360 | If \a toZone is invalid then the datetime will be invalid. | - | ||||||||||||||||||
3361 | - | |||||||||||||||||||
3362 | \sa timeZone(), Qt::TimeSpec | - | ||||||||||||||||||
3363 | */ | - | ||||||||||||||||||
3364 | - | |||||||||||||||||||
3365 | void QDateTime::setTimeZone(const QTimeZone &toZone) | - | ||||||||||||||||||
3366 | { | - | ||||||||||||||||||
3367 | QDateTimePrivate *d = this->d.data(); // detaches (and shadows d) | - | ||||||||||||||||||
3368 | d->m_spec = Qt::TimeZone; | - | ||||||||||||||||||
3369 | d->m_offsetFromUtc = 0; | - | ||||||||||||||||||
3370 | d->m_timeZone = toZone; | - | ||||||||||||||||||
3371 | d->refreshDateTime(); | - | ||||||||||||||||||
3372 | } | - | ||||||||||||||||||
3373 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
3374 | - | |||||||||||||||||||
3375 | /*! | - | ||||||||||||||||||
3376 | \since 4.7 | - | ||||||||||||||||||
3377 | - | |||||||||||||||||||
3378 | Returns the datetime as the number of milliseconds that have passed | - | ||||||||||||||||||
3379 | since 1970-01-01T00:00:00.000, Coordinated Universal Time (Qt::UTC). | - | ||||||||||||||||||
3380 | - | |||||||||||||||||||
3381 | On systems that do not support time zones, this function will | - | ||||||||||||||||||
3382 | behave as if local time were Qt::UTC. | - | ||||||||||||||||||
3383 | - | |||||||||||||||||||
3384 | The behavior for this function is undefined if the datetime stored in | - | ||||||||||||||||||
3385 | this object is not valid. However, for all valid dates, this function | - | ||||||||||||||||||
3386 | returns a unique value. | - | ||||||||||||||||||
3387 | - | |||||||||||||||||||
3388 | \sa toTime_t(), setMSecsSinceEpoch() | - | ||||||||||||||||||
3389 | */ | - | ||||||||||||||||||
3390 | qint64 QDateTime::toMSecsSinceEpoch() const | - | ||||||||||||||||||
3391 | { | - | ||||||||||||||||||
3392 | return d->toMSecsSinceEpoch(); | - | ||||||||||||||||||
3393 | } | - | ||||||||||||||||||
3394 | - | |||||||||||||||||||
3395 | /*! | - | ||||||||||||||||||
3396 | Returns the datetime as the number of seconds that have passed | - | ||||||||||||||||||
3397 | since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC). | - | ||||||||||||||||||
3398 | - | |||||||||||||||||||
3399 | On systems that do not support time zones, this function will | - | ||||||||||||||||||
3400 | behave as if local time were Qt::UTC. | - | ||||||||||||||||||
3401 | - | |||||||||||||||||||
3402 | \note This function returns a 32-bit unsigned integer, so it does not | - | ||||||||||||||||||
3403 | support dates before 1970, but it does support dates after | - | ||||||||||||||||||
3404 | 2038-01-19T03:14:06, which may not be valid time_t values. Be careful | - | ||||||||||||||||||
3405 | when passing those time_t values to system functions, which could | - | ||||||||||||||||||
3406 | interpret them as negative dates. | - | ||||||||||||||||||
3407 | - | |||||||||||||||||||
3408 | If the date is outside the range 1970-01-01T00:00:00 to | - | ||||||||||||||||||
3409 | 2106-02-07T06:28:14, this function returns -1 cast to an unsigned integer | - | ||||||||||||||||||
3410 | (i.e., 0xFFFFFFFF). | - | ||||||||||||||||||
3411 | - | |||||||||||||||||||
3412 | To get an extended range, use toMSecsSinceEpoch(). | - | ||||||||||||||||||
3413 | - | |||||||||||||||||||
3414 | \sa toMSecsSinceEpoch(), setTime_t() | - | ||||||||||||||||||
3415 | */ | - | ||||||||||||||||||
3416 | - | |||||||||||||||||||
3417 | uint QDateTime::toTime_t() const | - | ||||||||||||||||||
3418 | { | - | ||||||||||||||||||
3419 | if (!isValid()) | - | ||||||||||||||||||
3420 | return uint(-1); | - | ||||||||||||||||||
3421 | qint64 retval = d->toMSecsSinceEpoch() / 1000; | - | ||||||||||||||||||
3422 | if (quint64(retval) >= Q_UINT64_C(0xFFFFFFFF)) | - | ||||||||||||||||||
3423 | return uint(-1); | - | ||||||||||||||||||
3424 | return uint(retval); | - | ||||||||||||||||||
3425 | } | - | ||||||||||||||||||
3426 | - | |||||||||||||||||||
3427 | /*! | - | ||||||||||||||||||
3428 | \since 4.7 | - | ||||||||||||||||||
3429 | - | |||||||||||||||||||
3430 | Sets the date and time given the number of milliseconds \a msecs that have | - | ||||||||||||||||||
3431 | passed since 1970-01-01T00:00:00.000, Coordinated Universal Time | - | ||||||||||||||||||
3432 | (Qt::UTC). On systems that do not support time zones this function | - | ||||||||||||||||||
3433 | will behave as if local time were Qt::UTC. | - | ||||||||||||||||||
3434 | - | |||||||||||||||||||
3435 | Note that passing the minimum of \c qint64 | - | ||||||||||||||||||
3436 | (\c{std::numeric_limits<qint64>::min()}) to \a msecs will result in | - | ||||||||||||||||||
3437 | undefined behavior. | - | ||||||||||||||||||
3438 | - | |||||||||||||||||||
3439 | \sa toMSecsSinceEpoch(), setTime_t() | - | ||||||||||||||||||
3440 | */ | - | ||||||||||||||||||
3441 | void QDateTime::setMSecsSinceEpoch(qint64 msecs) | - | ||||||||||||||||||
3442 | { | - | ||||||||||||||||||
3443 | QDateTimePrivate *d = this->d.data(); // detaches (and shadows d) | - | ||||||||||||||||||
3444 | - | |||||||||||||||||||
3445 | d->m_status = 0; | - | ||||||||||||||||||
3446 | switch (d->m_spec) { | - | ||||||||||||||||||
3447 | case Qt::UTC: | - | ||||||||||||||||||
3448 | d->m_msecs = msecs; | - | ||||||||||||||||||
3449 | d->m_status = d->m_status | - | ||||||||||||||||||
3450 | | QDateTimePrivate::ValidDate | - | ||||||||||||||||||
3451 | | QDateTimePrivate::ValidTime | - | ||||||||||||||||||
3452 | | QDateTimePrivate::ValidDateTime; | - | ||||||||||||||||||
3453 | break; | - | ||||||||||||||||||
3454 | case Qt::OffsetFromUTC: | - | ||||||||||||||||||
3455 | d->m_msecs = msecs + (d->m_offsetFromUtc * 1000); | - | ||||||||||||||||||
3456 | d->m_status = d->m_status | - | ||||||||||||||||||
3457 | | QDateTimePrivate::ValidDate | - | ||||||||||||||||||
3458 | | QDateTimePrivate::ValidTime | - | ||||||||||||||||||
3459 | | QDateTimePrivate::ValidDateTime; | - | ||||||||||||||||||
3460 | break; | - | ||||||||||||||||||
3461 | case Qt::TimeZone: | - | ||||||||||||||||||
3462 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
3463 | // Docs state any LocalTime before 1970-01-01 will *not* have any DST applied | - | ||||||||||||||||||
3464 | // but all affected times afterwards will have DST applied. | - | ||||||||||||||||||
3465 | if (msecs >= 0) | - | ||||||||||||||||||
3466 | d->m_offsetFromUtc = d->m_timeZone.d->offsetFromUtc(msecs); | - | ||||||||||||||||||
3467 | else | - | ||||||||||||||||||
3468 | d->m_offsetFromUtc = d->m_timeZone.d->standardTimeOffset(msecs); | - | ||||||||||||||||||
3469 | d->m_msecs = msecs + (d->m_offsetFromUtc * 1000); | - | ||||||||||||||||||
3470 | d->m_status = d->m_status | - | ||||||||||||||||||
3471 | | QDateTimePrivate::ValidDate | - | ||||||||||||||||||
3472 | | QDateTimePrivate::ValidTime | - | ||||||||||||||||||
3473 | | QDateTimePrivate::ValidDateTime; | - | ||||||||||||||||||
3474 | d->refreshDateTime(); | - | ||||||||||||||||||
3475 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
3476 | break; | - | ||||||||||||||||||
3477 | case Qt::LocalTime: { | - | ||||||||||||||||||
3478 | QDate dt; | - | ||||||||||||||||||
3479 | QTime tm; | - | ||||||||||||||||||
3480 | QDateTimePrivate::DaylightStatus status; | - | ||||||||||||||||||
3481 | epochMSecsToLocalTime(msecs, &dt, &tm, &status); | - | ||||||||||||||||||
3482 | d->setDateTime(dt, tm); | - | ||||||||||||||||||
3483 | d->setDaylightStatus(status); | - | ||||||||||||||||||
3484 | d->refreshDateTime(); | - | ||||||||||||||||||
3485 | break; | - | ||||||||||||||||||
3486 | } | - | ||||||||||||||||||
3487 | } | - | ||||||||||||||||||
3488 | } | - | ||||||||||||||||||
3489 | - | |||||||||||||||||||
3490 | /*! | - | ||||||||||||||||||
3491 | \fn void QDateTime::setTime_t(uint seconds) | - | ||||||||||||||||||
3492 | - | |||||||||||||||||||
3493 | Sets the date and time given the number of \a seconds that have | - | ||||||||||||||||||
3494 | passed since 1970-01-01T00:00:00, Coordinated Universal Time | - | ||||||||||||||||||
3495 | (Qt::UTC). On systems that do not support time zones this function | - | ||||||||||||||||||
3496 | will behave as if local time were Qt::UTC. | - | ||||||||||||||||||
3497 | - | |||||||||||||||||||
3498 | \sa toTime_t() | - | ||||||||||||||||||
3499 | */ | - | ||||||||||||||||||
3500 | - | |||||||||||||||||||
3501 | void QDateTime::setTime_t(uint secsSince1Jan1970UTC) | - | ||||||||||||||||||
3502 | { | - | ||||||||||||||||||
3503 | setMSecsSinceEpoch((qint64)secsSince1Jan1970UTC * 1000); | - | ||||||||||||||||||
3504 | } | - | ||||||||||||||||||
3505 | - | |||||||||||||||||||
3506 | #ifndef QT_NO_DATESTRING | - | ||||||||||||||||||
3507 | /*! | - | ||||||||||||||||||
3508 | \fn QString QDateTime::toString(Qt::DateFormat format) const | - | ||||||||||||||||||
3509 | - | |||||||||||||||||||
3510 | \overload | - | ||||||||||||||||||
3511 | - | |||||||||||||||||||
3512 | Returns the datetime as a string in the \a format given. | - | ||||||||||||||||||
3513 | - | |||||||||||||||||||
3514 | If the \a format is Qt::TextDate, the string is formatted in | - | ||||||||||||||||||
3515 | the default way. QDate::shortDayName(), QDate::shortMonthName(), | - | ||||||||||||||||||
3516 | and QTime::toString() are used to generate the string, so the | - | ||||||||||||||||||
3517 | day and month names will be localized names using the system locale, | - | ||||||||||||||||||
3518 | i.e. QLocale::system(). An example of this formatting is | - | ||||||||||||||||||
3519 | "Wed May 20 03:40:13 1998". | - | ||||||||||||||||||
3520 | - | |||||||||||||||||||
3521 | If the \a format is Qt::ISODate, the string format corresponds | - | ||||||||||||||||||
3522 | to the ISO 8601 extended specification for representations of | - | ||||||||||||||||||
3523 | dates and times, taking the form YYYY-MM-DDTHH:mm:ss[Z|[+|-]HH:mm], | - | ||||||||||||||||||
3524 | depending on the timeSpec() of the QDateTime. If the timeSpec() | - | ||||||||||||||||||
3525 | is Qt::UTC, Z will be appended to the string; if the timeSpec() is | - | ||||||||||||||||||
3526 | Qt::OffsetFromUTC, the offset in hours and minutes from UTC will | - | ||||||||||||||||||
3527 | be appended to the string. | - | ||||||||||||||||||
3528 | - | |||||||||||||||||||
3529 | If the \a format is Qt::SystemLocaleShortDate or | - | ||||||||||||||||||
3530 | Qt::SystemLocaleLongDate, the string format depends on the locale | - | ||||||||||||||||||
3531 | settings of the system. Identical to calling | - | ||||||||||||||||||
3532 | QLocale::system().toString(datetime, QLocale::ShortFormat) or | - | ||||||||||||||||||
3533 | QLocale::system().toString(datetime, QLocale::LongFormat). | - | ||||||||||||||||||
3534 | - | |||||||||||||||||||
3535 | If the \a format is Qt::DefaultLocaleShortDate or | - | ||||||||||||||||||
3536 | Qt::DefaultLocaleLongDate, the string format depends on the | - | ||||||||||||||||||
3537 | default application locale. This is the locale set with | - | ||||||||||||||||||
3538 | QLocale::setDefault(), or the system locale if no default locale | - | ||||||||||||||||||
3539 | has been set. Identical to calling QLocale().toString(datetime, | - | ||||||||||||||||||
3540 | QLocale::ShortFormat) or QLocale().toString(datetime, | - | ||||||||||||||||||
3541 | QLocale::LongFormat). | - | ||||||||||||||||||
3542 | - | |||||||||||||||||||
3543 | If the \a format is Qt::RFC2822Date, the string is formatted | - | ||||||||||||||||||
3544 | following \l{RFC 2822}. | - | ||||||||||||||||||
3545 | - | |||||||||||||||||||
3546 | If the datetime is invalid, an empty string will be returned. | - | ||||||||||||||||||
3547 | - | |||||||||||||||||||
3548 | \warning The Qt::ISODate format is only valid for years in the | - | ||||||||||||||||||
3549 | range 0 to 9999. This restriction may apply to locale-aware | - | ||||||||||||||||||
3550 | formats as well, depending on the locale settings. | - | ||||||||||||||||||
3551 | - | |||||||||||||||||||
3552 | \sa fromString(), QDate::toString(), QTime::toString(), | - | ||||||||||||||||||
3553 | QLocale::toString() | - | ||||||||||||||||||
3554 | */ | - | ||||||||||||||||||
3555 | - | |||||||||||||||||||
3556 | QString QDateTime::toString(Qt::DateFormat format) const | - | ||||||||||||||||||
3557 | { | - | ||||||||||||||||||
3558 | QString buf; | - | ||||||||||||||||||
3559 | if (!isValid())
| 2-3156 | ||||||||||||||||||
3560 | return buf; executed 2 times by 1 test: return buf; Executed by:
| 2 | ||||||||||||||||||
3561 | - | |||||||||||||||||||
3562 | switch (format) { | - | ||||||||||||||||||
3563 | case Qt::SystemLocaleDate: executed 2721 times by 8 tests: case Qt::SystemLocaleDate: Executed by:
| 2721 | ||||||||||||||||||
3564 | case Qt::SystemLocaleShortDate: executed 1 time by 1 test: case Qt::SystemLocaleShortDate: Executed by:
| 1 | ||||||||||||||||||
3565 | return QLocale::system().toString(*this, QLocale::ShortFormat); executed 2722 times by 8 tests: return QLocale::system().toString(*this, QLocale::ShortFormat); Executed by:
| 2722 | ||||||||||||||||||
3566 | case Qt::SystemLocaleLongDate: executed 1 time by 1 test: case Qt::SystemLocaleLongDate: Executed by:
| 1 | ||||||||||||||||||
3567 | return QLocale::system().toString(*this, QLocale::LongFormat); executed 1 time by 1 test: return QLocale::system().toString(*this, QLocale::LongFormat); Executed by:
| 1 | ||||||||||||||||||
3568 | case Qt::LocaleDate: executed 1 time by 1 test: case Qt::LocaleDate: Executed by:
| 1 | ||||||||||||||||||
3569 | case Qt::DefaultLocaleShortDate: executed 1 time by 1 test: case Qt::DefaultLocaleShortDate: Executed by:
| 1 | ||||||||||||||||||
3570 | return QLocale().toString(*this, QLocale::ShortFormat); executed 2 times by 1 test: return QLocale().toString(*this, QLocale::ShortFormat); Executed by:
| 2 | ||||||||||||||||||
3571 | case Qt::DefaultLocaleLongDate: executed 1 time by 1 test: case Qt::DefaultLocaleLongDate: Executed by:
| 1 | ||||||||||||||||||
3572 | return QLocale().toString(*this, QLocale::LongFormat); executed 1 time by 1 test: return QLocale().toString(*this, QLocale::LongFormat); Executed by:
| 1 | ||||||||||||||||||
3573 | case Qt::RFC2822Date: { executed 5 times by 1 test: case Qt::RFC2822Date: Executed by:
| 5 | ||||||||||||||||||
3574 | buf = QLocale::c().toString(*this, QStringLiteral("dd MMM yyyy hh:mm:ss ")); | - | ||||||||||||||||||
3575 | buf += toOffsetString(Qt::TextDate, d->m_offsetFromUtc); | - | ||||||||||||||||||
3576 | return buf; executed 5 times by 1 test: return buf; Executed by:
| 5 | ||||||||||||||||||
3577 | } | - | ||||||||||||||||||
3578 | default: never executed: default: | 0 | ||||||||||||||||||
3579 | #ifndef QT_NO_TEXTDATE | - | ||||||||||||||||||
3580 | case Qt::TextDate: { executed 399 times by 3 tests: case Qt::TextDate: Executed by:
| 399 | ||||||||||||||||||
3581 | const QPair<QDate, QTime> p = d->getDateTime(); | - | ||||||||||||||||||
3582 | const QDate &dt = p.first; | - | ||||||||||||||||||
3583 | const QTime &tm = p.second; | - | ||||||||||||||||||
3584 | //We cant use date.toString(Qt::TextDate) as we need to insert the time before the year | - | ||||||||||||||||||
3585 | buf = QString::fromLatin1("%1 %2 %3 %4 %5").arg(dt.shortDayName(dt.dayOfWeek())) | - | ||||||||||||||||||
3586 | .arg(dt.shortMonthName(dt.month())) | - | ||||||||||||||||||
3587 | .arg(dt.day()) | - | ||||||||||||||||||
3588 | .arg(tm.toString(Qt::TextDate)) | - | ||||||||||||||||||
3589 | .arg(dt.year()); | - | ||||||||||||||||||
3590 | if (timeSpec() != Qt::LocalTime) {
| 3-396 | ||||||||||||||||||
3591 | buf += QStringLiteralQLatin1String(" GMT"); | - | ||||||||||||||||||
3592 | if (d->m_spec == Qt::OffsetFromUTC)
| 1-2 | ||||||||||||||||||
3593 | buf += toOffsetString(Qt::TextDate, d->m_offsetFromUtc); executed 2 times by 1 test: buf += toOffsetString(Qt::TextDate, d->m_offsetFromUtc); Executed by:
| 2 | ||||||||||||||||||
3594 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||||||||
3595 | return buf; executed 399 times by 3 tests: return buf; Executed by:
| 399 | ||||||||||||||||||
3596 | } | - | ||||||||||||||||||
3597 | #endif | - | ||||||||||||||||||
3598 | case Qt::ISODate: { executed 26 times by 4 tests: case Qt::ISODate: Executed by:
| 26 | ||||||||||||||||||
3599 | const QPair<QDate, QTime> p = d->getDateTime(); | - | ||||||||||||||||||
3600 | const QDate &dt = p.first; | - | ||||||||||||||||||
3601 | const QTime &tm = p.second; | - | ||||||||||||||||||
3602 | buf = dt.toString(Qt::ISODate); | - | ||||||||||||||||||
3603 | if (buf.isEmpty())
| 1-25 | ||||||||||||||||||
3604 | return QString(); // failed to convert executed 1 time by 1 test: return QString(); Executed by:
| 1 | ||||||||||||||||||
3605 | buf += QLatin1Char('T'); | - | ||||||||||||||||||
3606 | buf += tm.toString(Qt::ISODate); | - | ||||||||||||||||||
3607 | switch (d->m_spec) { | - | ||||||||||||||||||
3608 | case Qt::UTC: executed 9 times by 2 tests: case Qt::UTC: Executed by:
| 9 | ||||||||||||||||||
3609 | buf += QLatin1Char('Z'); | - | ||||||||||||||||||
3610 | break; executed 9 times by 2 tests: break; Executed by:
| 9 | ||||||||||||||||||
3611 | case Qt::OffsetFromUTC: executed 3 times by 1 test: case Qt::OffsetFromUTC: Executed by:
| 3 | ||||||||||||||||||
3612 | buf += toOffsetString(Qt::ISODate, d->m_offsetFromUtc); | - | ||||||||||||||||||
3613 | break; executed 3 times by 1 test: break; Executed by:
| 3 | ||||||||||||||||||
3614 | default: executed 13 times by 3 tests: default: Executed by:
| 13 | ||||||||||||||||||
3615 | break; executed 13 times by 3 tests: break; Executed by:
| 13 | ||||||||||||||||||
3616 | } | - | ||||||||||||||||||
3617 | return buf; executed 25 times by 4 tests: return buf; Executed by:
| 25 | ||||||||||||||||||
3618 | } | - | ||||||||||||||||||
3619 | } | - | ||||||||||||||||||
3620 | } | - | ||||||||||||||||||
3621 | - | |||||||||||||||||||
3622 | /*! | - | ||||||||||||||||||
3623 | Returns the datetime as a string. The \a format parameter | - | ||||||||||||||||||
3624 | determines the format of the result string. | - | ||||||||||||||||||
3625 | - | |||||||||||||||||||
3626 | These expressions may be used for the date: | - | ||||||||||||||||||
3627 | - | |||||||||||||||||||
3628 | \table | - | ||||||||||||||||||
3629 | \header \li Expression \li Output | - | ||||||||||||||||||
3630 | \row \li d \li the day as number without a leading zero (1 to 31) | - | ||||||||||||||||||
3631 | \row \li dd \li the day as number with a leading zero (01 to 31) | - | ||||||||||||||||||
3632 | \row \li ddd | - | ||||||||||||||||||
3633 | \li the abbreviated localized day name (e.g. 'Mon' to 'Sun'). | - | ||||||||||||||||||
3634 | Uses the system locale to localize the name, i.e. QLocale::system(). | - | ||||||||||||||||||
3635 | \row \li dddd | - | ||||||||||||||||||
3636 | \li the long localized day name (e.g. 'Monday' to 'Sunday'). | - | ||||||||||||||||||
3637 | Uses the system locale to localize the name, i.e. QLocale::system(). | - | ||||||||||||||||||
3638 | \row \li M \li the month as number without a leading zero (1-12) | - | ||||||||||||||||||
3639 | \row \li MM \li the month as number with a leading zero (01-12) | - | ||||||||||||||||||
3640 | \row \li MMM | - | ||||||||||||||||||
3641 | \li the abbreviated localized month name (e.g. 'Jan' to 'Dec'). | - | ||||||||||||||||||
3642 | Uses the system locale to localize the name, i.e. QLocale::system(). | - | ||||||||||||||||||
3643 | \row \li MMMM | - | ||||||||||||||||||
3644 | \li the long localized month name (e.g. 'January' to 'December'). | - | ||||||||||||||||||
3645 | Uses the system locale to localize the name, i.e. QLocale::system(). | - | ||||||||||||||||||
3646 | \row \li yy \li the year as two digit number (00-99) | - | ||||||||||||||||||
3647 | \row \li yyyy \li the year as four digit number | - | ||||||||||||||||||
3648 | \endtable | - | ||||||||||||||||||
3649 | - | |||||||||||||||||||
3650 | These expressions may be used for the time: | - | ||||||||||||||||||
3651 | - | |||||||||||||||||||
3652 | \table | - | ||||||||||||||||||
3653 | \header \li Expression \li Output | - | ||||||||||||||||||
3654 | \row \li h | - | ||||||||||||||||||
3655 | \li the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) | - | ||||||||||||||||||
3656 | \row \li hh | - | ||||||||||||||||||
3657 | \li the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) | - | ||||||||||||||||||
3658 | \row \li H | - | ||||||||||||||||||
3659 | \li the hour without a leading zero (0 to 23, even with AM/PM display) | - | ||||||||||||||||||
3660 | \row \li HH | - | ||||||||||||||||||
3661 | \li the hour with a leading zero (00 to 23, even with AM/PM display) | - | ||||||||||||||||||
3662 | \row \li m \li the minute without a leading zero (0 to 59) | - | ||||||||||||||||||
3663 | \row \li mm \li the minute with a leading zero (00 to 59) | - | ||||||||||||||||||
3664 | \row \li s \li the second without a leading zero (0 to 59) | - | ||||||||||||||||||
3665 | \row \li ss \li the second with a leading zero (00 to 59) | - | ||||||||||||||||||
3666 | \row \li z \li the milliseconds without leading zeroes (0 to 999) | - | ||||||||||||||||||
3667 | \row \li zzz \li the milliseconds with leading zeroes (000 to 999) | - | ||||||||||||||||||
3668 | \row \li AP or A | - | ||||||||||||||||||
3669 | \li use AM/PM display. \e A/AP will be replaced by either "AM" or "PM". | - | ||||||||||||||||||
3670 | \row \li ap or a | - | ||||||||||||||||||
3671 | \li use am/pm display. \e a/ap will be replaced by either "am" or "pm". | - | ||||||||||||||||||
3672 | \row \li t \li the timezone (for example "CEST") | - | ||||||||||||||||||
3673 | \endtable | - | ||||||||||||||||||
3674 | - | |||||||||||||||||||
3675 | All other input characters will be ignored. Any sequence of characters that | - | ||||||||||||||||||
3676 | are enclosed in single quotes will be treated as text and not be used as an | - | ||||||||||||||||||
3677 | expression. Two consecutive single quotes ("''") are replaced by a singlequote | - | ||||||||||||||||||
3678 | in the output. Formats without separators (e.g. "HHmm") are currently not supported. | - | ||||||||||||||||||
3679 | - | |||||||||||||||||||
3680 | Example format strings (assumed that the QDateTime is 21 May 2001 | - | ||||||||||||||||||
3681 | 14:13:09): | - | ||||||||||||||||||
3682 | - | |||||||||||||||||||
3683 | \table | - | ||||||||||||||||||
3684 | \header \li Format \li Result | - | ||||||||||||||||||
3685 | \row \li dd.MM.yyyy \li 21.05.2001 | - | ||||||||||||||||||
3686 | \row \li ddd MMMM d yy \li Tue May 21 01 | - | ||||||||||||||||||
3687 | \row \li hh:mm:ss.zzz \li 14:13:09.042 | - | ||||||||||||||||||
3688 | \row \li h:m:s ap \li 2:13:9 pm | - | ||||||||||||||||||
3689 | \endtable | - | ||||||||||||||||||
3690 | - | |||||||||||||||||||
3691 | If the datetime is invalid, an empty string will be returned. | - | ||||||||||||||||||
3692 | - | |||||||||||||||||||
3693 | \sa fromString(), QDate::toString(), QTime::toString(), QLocale::toString() | - | ||||||||||||||||||
3694 | */ | - | ||||||||||||||||||
3695 | QString QDateTime::toString(const QString& format) const | - | ||||||||||||||||||
3696 | { | - | ||||||||||||||||||
3697 | return QLocale::system().toString(*this, format); | - | ||||||||||||||||||
3698 | } | - | ||||||||||||||||||
3699 | #endif //QT_NO_DATESTRING | - | ||||||||||||||||||
3700 | - | |||||||||||||||||||
3701 | static void massageAdjustedDateTime(Qt::TimeSpec spec, | - | ||||||||||||||||||
3702 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
3703 | const QTimeZone &zone, | - | ||||||||||||||||||
3704 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
3705 | QDate *date, | - | ||||||||||||||||||
3706 | QTime *time) | - | ||||||||||||||||||
3707 | { | - | ||||||||||||||||||
3708 | /* | - | ||||||||||||||||||
3709 | If we have just adjusted to a day with a DST transition, our given time | - | ||||||||||||||||||
3710 | may lie in the transition hour (either missing or duplicated). For any | - | ||||||||||||||||||
3711 | other time, telling mktime (deep in the bowels of localMSecsToEpochMSecs) | - | ||||||||||||||||||
3712 | we don't know its DST-ness will produce no adjustment (just a decision as | - | ||||||||||||||||||
3713 | to its DST-ness); but for a time in spring's missing hour it'll adjust the | - | ||||||||||||||||||
3714 | time while picking a DST-ness. (Handling of autumn is trickier, as either | - | ||||||||||||||||||
3715 | DST-ness is valid, without adjusting the time. We might want to propagate | - | ||||||||||||||||||
3716 | d->daylightStatus() in that case, but it's hard to do so without breaking | - | ||||||||||||||||||
3717 | (far more common) other cases; and it makes little difference, as the two | - | ||||||||||||||||||
3718 | answers do then differ only in DST-ness.) | - | ||||||||||||||||||
3719 | */ | - | ||||||||||||||||||
3720 | if (spec == Qt::LocalTime) { | - | ||||||||||||||||||
3721 | QDateTimePrivate::DaylightStatus status = QDateTimePrivate::UnknownDaylightTime; | - | ||||||||||||||||||
3722 | localMSecsToEpochMSecs(timeToMSecs(*date, *time), &status, date, time); | - | ||||||||||||||||||
3723 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
3724 | } else if (spec == Qt::TimeZone) { | - | ||||||||||||||||||
3725 | QDateTimePrivate::zoneMSecsToEpochMSecs(timeToMSecs(*date, *time), zone, date, time); | - | ||||||||||||||||||
3726 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
3727 | } | - | ||||||||||||||||||
3728 | } | - | ||||||||||||||||||
3729 | #ifdef QT_BOOTSTRAPPED // Avoid duplicate #if-ery in uses. | - | ||||||||||||||||||
3730 | #define MASSAGEADJUSTEDDATETIME(s, z, d, t) massageAdjustedDateTime(s, d, t) | - | ||||||||||||||||||
3731 | #else | - | ||||||||||||||||||
3732 | #define MASSAGEADJUSTEDDATETIME(s, z, d, t) massageAdjustedDateTime(s, z, d, t) | - | ||||||||||||||||||
3733 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
3734 | - | |||||||||||||||||||
3735 | /*! | - | ||||||||||||||||||
3736 | Returns a QDateTime object containing a datetime \a ndays days | - | ||||||||||||||||||
3737 | later than the datetime of this object (or earlier if \a ndays is | - | ||||||||||||||||||
3738 | negative). | - | ||||||||||||||||||
3739 | - | |||||||||||||||||||
3740 | If the timeSpec() is Qt::LocalTime and the resulting | - | ||||||||||||||||||
3741 | date and time fall in the Standard Time to Daylight-Saving Time transition | - | ||||||||||||||||||
3742 | hour then the result will be adjusted accordingly, i.e. if the transition | - | ||||||||||||||||||
3743 | is at 2am and the clock goes forward to 3am and the result falls between | - | ||||||||||||||||||
3744 | 2am and 3am then the result will be adjusted to fall after 3am. | - | ||||||||||||||||||
3745 | - | |||||||||||||||||||
3746 | \sa daysTo(), addMonths(), addYears(), addSecs() | - | ||||||||||||||||||
3747 | */ | - | ||||||||||||||||||
3748 | - | |||||||||||||||||||
3749 | QDateTime QDateTime::addDays(qint64 ndays) const | - | ||||||||||||||||||
3750 | { | - | ||||||||||||||||||
3751 | QDateTime dt(*this); | - | ||||||||||||||||||
3752 | QPair<QDate, QTime> p = d->getDateTime(); | - | ||||||||||||||||||
3753 | QDate &date = p.first; | - | ||||||||||||||||||
3754 | QTime &time = p.second; | - | ||||||||||||||||||
3755 | date = date.addDays(ndays); | - | ||||||||||||||||||
3756 | MASSAGEADJUSTEDDATETIME(d->m_spec, d->m_timeZone, &date, &time); | - | ||||||||||||||||||
3757 | dt.d->setDateTime(date, time); | - | ||||||||||||||||||
3758 | return dt; | - | ||||||||||||||||||
3759 | } | - | ||||||||||||||||||
3760 | - | |||||||||||||||||||
3761 | /*! | - | ||||||||||||||||||
3762 | Returns a QDateTime object containing a datetime \a nmonths months | - | ||||||||||||||||||
3763 | later than the datetime of this object (or earlier if \a nmonths | - | ||||||||||||||||||
3764 | is negative). | - | ||||||||||||||||||
3765 | - | |||||||||||||||||||
3766 | If the timeSpec() is Qt::LocalTime and the resulting | - | ||||||||||||||||||
3767 | date and time fall in the Standard Time to Daylight-Saving Time transition | - | ||||||||||||||||||
3768 | hour then the result will be adjusted accordingly, i.e. if the transition | - | ||||||||||||||||||
3769 | is at 2am and the clock goes forward to 3am and the result falls between | - | ||||||||||||||||||
3770 | 2am and 3am then the result will be adjusted to fall after 3am. | - | ||||||||||||||||||
3771 | - | |||||||||||||||||||
3772 | \sa daysTo(), addDays(), addYears(), addSecs() | - | ||||||||||||||||||
3773 | */ | - | ||||||||||||||||||
3774 | - | |||||||||||||||||||
3775 | QDateTime QDateTime::addMonths(int nmonths) const | - | ||||||||||||||||||
3776 | { | - | ||||||||||||||||||
3777 | QDateTime dt(*this); | - | ||||||||||||||||||
3778 | QPair<QDate, QTime> p = d->getDateTime(); | - | ||||||||||||||||||
3779 | QDate &date = p.first; | - | ||||||||||||||||||
3780 | QTime &time = p.second; | - | ||||||||||||||||||
3781 | date = date.addMonths(nmonths); | - | ||||||||||||||||||
3782 | MASSAGEADJUSTEDDATETIME(d->m_spec, d->m_timeZone, &date, &time); | - | ||||||||||||||||||
3783 | dt.d->setDateTime(date, time); | - | ||||||||||||||||||
3784 | return dt; | - | ||||||||||||||||||
3785 | } | - | ||||||||||||||||||
3786 | - | |||||||||||||||||||
3787 | /*! | - | ||||||||||||||||||
3788 | Returns a QDateTime object containing a datetime \a nyears years | - | ||||||||||||||||||
3789 | later than the datetime of this object (or earlier if \a nyears is | - | ||||||||||||||||||
3790 | negative). | - | ||||||||||||||||||
3791 | - | |||||||||||||||||||
3792 | If the timeSpec() is Qt::LocalTime and the resulting | - | ||||||||||||||||||
3793 | date and time fall in the Standard Time to Daylight-Saving Time transition | - | ||||||||||||||||||
3794 | hour then the result will be adjusted accordingly, i.e. if the transition | - | ||||||||||||||||||
3795 | is at 2am and the clock goes forward to 3am and the result falls between | - | ||||||||||||||||||
3796 | 2am and 3am then the result will be adjusted to fall after 3am. | - | ||||||||||||||||||
3797 | - | |||||||||||||||||||
3798 | \sa daysTo(), addDays(), addMonths(), addSecs() | - | ||||||||||||||||||
3799 | */ | - | ||||||||||||||||||
3800 | - | |||||||||||||||||||
3801 | QDateTime QDateTime::addYears(int nyears) const | - | ||||||||||||||||||
3802 | { | - | ||||||||||||||||||
3803 | QDateTime dt(*this); | - | ||||||||||||||||||
3804 | QPair<QDate, QTime> p = d->getDateTime(); | - | ||||||||||||||||||
3805 | QDate &date = p.first; | - | ||||||||||||||||||
3806 | QTime &time = p.second; | - | ||||||||||||||||||
3807 | date = date.addYears(nyears); | - | ||||||||||||||||||
3808 | MASSAGEADJUSTEDDATETIME(d->m_spec, d->m_timeZone, &date, &time); | - | ||||||||||||||||||
3809 | dt.d->setDateTime(date, time); | - | ||||||||||||||||||
3810 | return dt; | - | ||||||||||||||||||
3811 | } | - | ||||||||||||||||||
3812 | #undef MASSAGEADJUSTEDDATETIME | - | ||||||||||||||||||
3813 | - | |||||||||||||||||||
3814 | /*! | - | ||||||||||||||||||
3815 | Returns a QDateTime object containing a datetime \a s seconds | - | ||||||||||||||||||
3816 | later than the datetime of this object (or earlier if \a s is | - | ||||||||||||||||||
3817 | negative). | - | ||||||||||||||||||
3818 | - | |||||||||||||||||||
3819 | If this datetime is invalid, an invalid datetime will be returned. | - | ||||||||||||||||||
3820 | - | |||||||||||||||||||
3821 | \sa addMSecs(), secsTo(), addDays(), addMonths(), addYears() | - | ||||||||||||||||||
3822 | */ | - | ||||||||||||||||||
3823 | - | |||||||||||||||||||
3824 | QDateTime QDateTime::addSecs(qint64 s) const | - | ||||||||||||||||||
3825 | { | - | ||||||||||||||||||
3826 | return addMSecs(s * 1000); | - | ||||||||||||||||||
3827 | } | - | ||||||||||||||||||
3828 | - | |||||||||||||||||||
3829 | /*! | - | ||||||||||||||||||
3830 | Returns a QDateTime object containing a datetime \a msecs miliseconds | - | ||||||||||||||||||
3831 | later than the datetime of this object (or earlier if \a msecs is | - | ||||||||||||||||||
3832 | negative). | - | ||||||||||||||||||
3833 | - | |||||||||||||||||||
3834 | If this datetime is invalid, an invalid datetime will be returned. | - | ||||||||||||||||||
3835 | - | |||||||||||||||||||
3836 | \sa addSecs(), msecsTo(), addDays(), addMonths(), addYears() | - | ||||||||||||||||||
3837 | */ | - | ||||||||||||||||||
3838 | QDateTime QDateTime::addMSecs(qint64 msecs) const | - | ||||||||||||||||||
3839 | { | - | ||||||||||||||||||
3840 | if (!isValid()) | - | ||||||||||||||||||
3841 | return QDateTime(); | - | ||||||||||||||||||
3842 | - | |||||||||||||||||||
3843 | QDateTime dt(*this); | - | ||||||||||||||||||
3844 | if (d->m_spec == Qt::LocalTime || d->m_spec == Qt::TimeZone) | - | ||||||||||||||||||
3845 | // Convert to real UTC first in case crosses DST transition | - | ||||||||||||||||||
3846 | dt.setMSecsSinceEpoch(d->toMSecsSinceEpoch() + msecs); | - | ||||||||||||||||||
3847 | else | - | ||||||||||||||||||
3848 | // No need to convert, just add on | - | ||||||||||||||||||
3849 | dt.d->m_msecs = dt.d->m_msecs + msecs; | - | ||||||||||||||||||
3850 | return dt; | - | ||||||||||||||||||
3851 | } | - | ||||||||||||||||||
3852 | - | |||||||||||||||||||
3853 | /*! | - | ||||||||||||||||||
3854 | Returns the number of days from this datetime to the \a other | - | ||||||||||||||||||
3855 | datetime. The number of days is counted as the number of times | - | ||||||||||||||||||
3856 | midnight is reached between this datetime to the \a other | - | ||||||||||||||||||
3857 | datetime. This means that a 10 minute difference from 23:55 to | - | ||||||||||||||||||
3858 | 0:05 the next day counts as one day. | - | ||||||||||||||||||
3859 | - | |||||||||||||||||||
3860 | If the \a other datetime is earlier than this datetime, | - | ||||||||||||||||||
3861 | the value returned is negative. | - | ||||||||||||||||||
3862 | - | |||||||||||||||||||
3863 | Example: | - | ||||||||||||||||||
3864 | \snippet code/src_corelib_tools_qdatetime.cpp 15 | - | ||||||||||||||||||
3865 | - | |||||||||||||||||||
3866 | \sa addDays(), secsTo(), msecsTo() | - | ||||||||||||||||||
3867 | */ | - | ||||||||||||||||||
3868 | - | |||||||||||||||||||
3869 | qint64 QDateTime::daysTo(const QDateTime &other) const | - | ||||||||||||||||||
3870 | { | - | ||||||||||||||||||
3871 | return date().daysTo(other.date()); | - | ||||||||||||||||||
3872 | } | - | ||||||||||||||||||
3873 | - | |||||||||||||||||||
3874 | /*! | - | ||||||||||||||||||
3875 | Returns the number of seconds from this datetime to the \a other | - | ||||||||||||||||||
3876 | datetime. If the \a other datetime is earlier than this datetime, | - | ||||||||||||||||||
3877 | the value returned is negative. | - | ||||||||||||||||||
3878 | - | |||||||||||||||||||
3879 | Before performing the comparison, the two datetimes are converted | - | ||||||||||||||||||
3880 | to Qt::UTC to ensure that the result is correct if daylight-saving | - | ||||||||||||||||||
3881 | (DST) applies to one of the two datetimes but not the other. | - | ||||||||||||||||||
3882 | - | |||||||||||||||||||
3883 | Returns 0 if either datetime is invalid. | - | ||||||||||||||||||
3884 | - | |||||||||||||||||||
3885 | Example: | - | ||||||||||||||||||
3886 | \snippet code/src_corelib_tools_qdatetime.cpp 11 | - | ||||||||||||||||||
3887 | - | |||||||||||||||||||
3888 | \sa addSecs(), daysTo(), QTime::secsTo() | - | ||||||||||||||||||
3889 | */ | - | ||||||||||||||||||
3890 | - | |||||||||||||||||||
3891 | qint64 QDateTime::secsTo(const QDateTime &other) const | - | ||||||||||||||||||
3892 | { | - | ||||||||||||||||||
3893 | return (msecsTo(other) / 1000); | - | ||||||||||||||||||
3894 | } | - | ||||||||||||||||||
3895 | - | |||||||||||||||||||
3896 | /*! | - | ||||||||||||||||||
3897 | Returns the number of milliseconds from this datetime to the \a other | - | ||||||||||||||||||
3898 | datetime. If the \a other datetime is earlier than this datetime, | - | ||||||||||||||||||
3899 | the value returned is negative. | - | ||||||||||||||||||
3900 | - | |||||||||||||||||||
3901 | Before performing the comparison, the two datetimes are converted | - | ||||||||||||||||||
3902 | to Qt::UTC to ensure that the result is correct if daylight-saving | - | ||||||||||||||||||
3903 | (DST) applies to one of the two datetimes and but not the other. | - | ||||||||||||||||||
3904 | - | |||||||||||||||||||
3905 | Returns 0 if either datetime is invalid. | - | ||||||||||||||||||
3906 | - | |||||||||||||||||||
3907 | \sa addMSecs(), daysTo(), QTime::msecsTo() | - | ||||||||||||||||||
3908 | */ | - | ||||||||||||||||||
3909 | - | |||||||||||||||||||
3910 | qint64 QDateTime::msecsTo(const QDateTime &other) const | - | ||||||||||||||||||
3911 | { | - | ||||||||||||||||||
3912 | if (!isValid() || !other.isValid()) | - | ||||||||||||||||||
3913 | return 0; | - | ||||||||||||||||||
3914 | - | |||||||||||||||||||
3915 | return other.d->toMSecsSinceEpoch() - d->toMSecsSinceEpoch(); | - | ||||||||||||||||||
3916 | } | - | ||||||||||||||||||
3917 | - | |||||||||||||||||||
3918 | /*! | - | ||||||||||||||||||
3919 | \fn QDateTime QDateTime::toTimeSpec(Qt::TimeSpec spec) const | - | ||||||||||||||||||
3920 | - | |||||||||||||||||||
3921 | Returns a copy of this datetime converted to the given time | - | ||||||||||||||||||
3922 | \a spec. | - | ||||||||||||||||||
3923 | - | |||||||||||||||||||
3924 | If \a spec is Qt::OffsetFromUTC then it is set to Qt::UTC. To set to a | - | ||||||||||||||||||
3925 | spec of Qt::OffsetFromUTC use toOffsetFromUtc(). | - | ||||||||||||||||||
3926 | - | |||||||||||||||||||
3927 | If \a spec is Qt::TimeZone then it is set to Qt::LocalTime, | - | ||||||||||||||||||
3928 | i.e. the local Time Zone. | - | ||||||||||||||||||
3929 | - | |||||||||||||||||||
3930 | Example: | - | ||||||||||||||||||
3931 | \snippet code/src_corelib_tools_qdatetime.cpp 16 | - | ||||||||||||||||||
3932 | - | |||||||||||||||||||
3933 | \sa timeSpec(), toTimeZone(), toUTC(), toLocalTime() | - | ||||||||||||||||||
3934 | */ | - | ||||||||||||||||||
3935 | - | |||||||||||||||||||
3936 | QDateTime QDateTime::toTimeSpec(Qt::TimeSpec spec) const | - | ||||||||||||||||||
3937 | { | - | ||||||||||||||||||
3938 | if (d->m_spec == spec && (spec == Qt::UTC || spec == Qt::LocalTime)) | - | ||||||||||||||||||
3939 | return *this; | - | ||||||||||||||||||
3940 | - | |||||||||||||||||||
3941 | if (!isValid()) { | - | ||||||||||||||||||
3942 | QDateTime ret = *this; | - | ||||||||||||||||||
3943 | ret.setTimeSpec(spec); | - | ||||||||||||||||||
3944 | return ret; | - | ||||||||||||||||||
3945 | } | - | ||||||||||||||||||
3946 | - | |||||||||||||||||||
3947 | return fromMSecsSinceEpoch(d->toMSecsSinceEpoch(), spec, 0); | - | ||||||||||||||||||
3948 | } | - | ||||||||||||||||||
3949 | - | |||||||||||||||||||
3950 | /*! | - | ||||||||||||||||||
3951 | \since 5.2 | - | ||||||||||||||||||
3952 | - | |||||||||||||||||||
3953 | \fn QDateTime QDateTime::toOffsetFromUtc(int offsetSeconds) const | - | ||||||||||||||||||
3954 | - | |||||||||||||||||||
3955 | Returns a copy of this datetime converted to a spec of Qt::OffsetFromUTC | - | ||||||||||||||||||
3956 | with the given \a offsetSeconds. | - | ||||||||||||||||||
3957 | - | |||||||||||||||||||
3958 | If the \a offsetSeconds equals 0 then a UTC datetime will be returned | - | ||||||||||||||||||
3959 | - | |||||||||||||||||||
3960 | \sa setOffsetFromUtc(), offsetFromUtc(), toTimeSpec() | - | ||||||||||||||||||
3961 | */ | - | ||||||||||||||||||
3962 | - | |||||||||||||||||||
3963 | QDateTime QDateTime::toOffsetFromUtc(int offsetSeconds) const | - | ||||||||||||||||||
3964 | { | - | ||||||||||||||||||
3965 | if (d->m_spec == Qt::OffsetFromUTC && d->m_offsetFromUtc == offsetSeconds) | - | ||||||||||||||||||
3966 | return *this; | - | ||||||||||||||||||
3967 | - | |||||||||||||||||||
3968 | if (!isValid()) { | - | ||||||||||||||||||
3969 | QDateTime ret = *this; | - | ||||||||||||||||||
3970 | ret.setOffsetFromUtc(offsetSeconds); | - | ||||||||||||||||||
3971 | return ret; | - | ||||||||||||||||||
3972 | } | - | ||||||||||||||||||
3973 | - | |||||||||||||||||||
3974 | return fromMSecsSinceEpoch(d->toMSecsSinceEpoch(), Qt::OffsetFromUTC, offsetSeconds); | - | ||||||||||||||||||
3975 | } | - | ||||||||||||||||||
3976 | - | |||||||||||||||||||
3977 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
3978 | /*! | - | ||||||||||||||||||
3979 | \since 5.2 | - | ||||||||||||||||||
3980 | - | |||||||||||||||||||
3981 | Returns a copy of this datetime converted to the given \a timeZone | - | ||||||||||||||||||
3982 | - | |||||||||||||||||||
3983 | \sa timeZone(), toTimeSpec() | - | ||||||||||||||||||
3984 | */ | - | ||||||||||||||||||
3985 | - | |||||||||||||||||||
3986 | QDateTime QDateTime::toTimeZone(const QTimeZone &timeZone) const | - | ||||||||||||||||||
3987 | { | - | ||||||||||||||||||
3988 | if (d->m_spec == Qt::TimeZone && d->m_timeZone == timeZone) | - | ||||||||||||||||||
3989 | return *this; | - | ||||||||||||||||||
3990 | - | |||||||||||||||||||
3991 | if (!isValid()) { | - | ||||||||||||||||||
3992 | QDateTime ret = *this; | - | ||||||||||||||||||
3993 | ret.setTimeZone(timeZone); | - | ||||||||||||||||||
3994 | return ret; | - | ||||||||||||||||||
3995 | } | - | ||||||||||||||||||
3996 | - | |||||||||||||||||||
3997 | return fromMSecsSinceEpoch(d->toMSecsSinceEpoch(), timeZone); | - | ||||||||||||||||||
3998 | } | - | ||||||||||||||||||
3999 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
4000 | - | |||||||||||||||||||
4001 | /*! | - | ||||||||||||||||||
4002 | Returns \c true if this datetime is equal to the \a other datetime; | - | ||||||||||||||||||
4003 | otherwise returns \c false. | - | ||||||||||||||||||
4004 | - | |||||||||||||||||||
4005 | \sa operator!=() | - | ||||||||||||||||||
4006 | */ | - | ||||||||||||||||||
4007 | - | |||||||||||||||||||
4008 | bool QDateTime::operator==(const QDateTime &other) const | - | ||||||||||||||||||
4009 | { | - | ||||||||||||||||||
4010 | if (d->m_spec == Qt::LocalTime | - | ||||||||||||||||||
4011 | && other.d->m_spec == Qt::LocalTime | - | ||||||||||||||||||
4012 | && d->m_status == other.d->m_status) { | - | ||||||||||||||||||
4013 | return (d->m_msecs == other.d->m_msecs); | - | ||||||||||||||||||
4014 | } | - | ||||||||||||||||||
4015 | // Convert to UTC and compare | - | ||||||||||||||||||
4016 | return (toMSecsSinceEpoch() == other.toMSecsSinceEpoch()); | - | ||||||||||||||||||
4017 | } | - | ||||||||||||||||||
4018 | - | |||||||||||||||||||
4019 | /*! | - | ||||||||||||||||||
4020 | \fn bool QDateTime::operator!=(const QDateTime &other) const | - | ||||||||||||||||||
4021 | - | |||||||||||||||||||
4022 | Returns \c true if this datetime is different from the \a other | - | ||||||||||||||||||
4023 | datetime; otherwise returns \c false. | - | ||||||||||||||||||
4024 | - | |||||||||||||||||||
4025 | Two datetimes are different if either the date, the time, or the | - | ||||||||||||||||||
4026 | time zone components are different. | - | ||||||||||||||||||
4027 | - | |||||||||||||||||||
4028 | \sa operator==() | - | ||||||||||||||||||
4029 | */ | - | ||||||||||||||||||
4030 | - | |||||||||||||||||||
4031 | /*! | - | ||||||||||||||||||
4032 | Returns \c true if this datetime is earlier than the \a other | - | ||||||||||||||||||
4033 | datetime; otherwise returns \c false. | - | ||||||||||||||||||
4034 | */ | - | ||||||||||||||||||
4035 | - | |||||||||||||||||||
4036 | bool QDateTime::operator<(const QDateTime &other) const | - | ||||||||||||||||||
4037 | { | - | ||||||||||||||||||
4038 | if (d->m_spec == Qt::LocalTime | - | ||||||||||||||||||
4039 | && other.d->m_spec == Qt::LocalTime | - | ||||||||||||||||||
4040 | && d->m_status == other.d->m_status) { | - | ||||||||||||||||||
4041 | return (d->m_msecs < other.d->m_msecs); | - | ||||||||||||||||||
4042 | } | - | ||||||||||||||||||
4043 | // Convert to UTC and compare | - | ||||||||||||||||||
4044 | return (toMSecsSinceEpoch() < other.toMSecsSinceEpoch()); | - | ||||||||||||||||||
4045 | } | - | ||||||||||||||||||
4046 | - | |||||||||||||||||||
4047 | /*! | - | ||||||||||||||||||
4048 | \fn bool QDateTime::operator<=(const QDateTime &other) const | - | ||||||||||||||||||
4049 | - | |||||||||||||||||||
4050 | Returns \c true if this datetime is earlier than or equal to the | - | ||||||||||||||||||
4051 | \a other datetime; otherwise returns \c false. | - | ||||||||||||||||||
4052 | */ | - | ||||||||||||||||||
4053 | - | |||||||||||||||||||
4054 | /*! | - | ||||||||||||||||||
4055 | \fn bool QDateTime::operator>(const QDateTime &other) const | - | ||||||||||||||||||
4056 | - | |||||||||||||||||||
4057 | Returns \c true if this datetime is later than the \a other datetime; | - | ||||||||||||||||||
4058 | otherwise returns \c false. | - | ||||||||||||||||||
4059 | */ | - | ||||||||||||||||||
4060 | - | |||||||||||||||||||
4061 | /*! | - | ||||||||||||||||||
4062 | \fn bool QDateTime::operator>=(const QDateTime &other) const | - | ||||||||||||||||||
4063 | - | |||||||||||||||||||
4064 | Returns \c true if this datetime is later than or equal to the | - | ||||||||||||||||||
4065 | \a other datetime; otherwise returns \c false. | - | ||||||||||||||||||
4066 | */ | - | ||||||||||||||||||
4067 | - | |||||||||||||||||||
4068 | /*! | - | ||||||||||||||||||
4069 | \fn QDateTime QDateTime::currentDateTime() | - | ||||||||||||||||||
4070 | Returns the current datetime, as reported by the system clock, in | - | ||||||||||||||||||
4071 | the local time zone. | - | ||||||||||||||||||
4072 | - | |||||||||||||||||||
4073 | \sa currentDateTimeUtc(), QDate::currentDate(), QTime::currentTime(), toTimeSpec() | - | ||||||||||||||||||
4074 | */ | - | ||||||||||||||||||
4075 | - | |||||||||||||||||||
4076 | /*! | - | ||||||||||||||||||
4077 | \fn QDateTime QDateTime::currentDateTimeUtc() | - | ||||||||||||||||||
4078 | \since 4.7 | - | ||||||||||||||||||
4079 | Returns the current datetime, as reported by the system clock, in | - | ||||||||||||||||||
4080 | UTC. | - | ||||||||||||||||||
4081 | - | |||||||||||||||||||
4082 | \sa currentDateTime(), QDate::currentDate(), QTime::currentTime(), toTimeSpec() | - | ||||||||||||||||||
4083 | */ | - | ||||||||||||||||||
4084 | - | |||||||||||||||||||
4085 | /*! | - | ||||||||||||||||||
4086 | \fn qint64 QDateTime::currentMSecsSinceEpoch() | - | ||||||||||||||||||
4087 | \since 4.7 | - | ||||||||||||||||||
4088 | - | |||||||||||||||||||
4089 | Returns the number of milliseconds since 1970-01-01T00:00:00 Universal | - | ||||||||||||||||||
4090 | Coordinated Time. This number is like the POSIX time_t variable, but | - | ||||||||||||||||||
4091 | expressed in milliseconds instead. | - | ||||||||||||||||||
4092 | - | |||||||||||||||||||
4093 | \sa currentDateTime(), currentDateTimeUtc(), toTime_t(), toTimeSpec() | - | ||||||||||||||||||
4094 | */ | - | ||||||||||||||||||
4095 | - | |||||||||||||||||||
4096 | #if defined(Q_OS_WIN) | - | ||||||||||||||||||
4097 | static inline uint msecsFromDecomposed(int hour, int minute, int sec, int msec = 0) | - | ||||||||||||||||||
4098 | { | - | ||||||||||||||||||
4099 | return MSECS_PER_HOUR * hour + MSECS_PER_MIN * minute + 1000 * sec + msec; | - | ||||||||||||||||||
4100 | } | - | ||||||||||||||||||
4101 | - | |||||||||||||||||||
4102 | QDate QDate::currentDate() | - | ||||||||||||||||||
4103 | { | - | ||||||||||||||||||
4104 | QDate d; | - | ||||||||||||||||||
4105 | SYSTEMTIME st; | - | ||||||||||||||||||
4106 | memset(&st, 0, sizeof(SYSTEMTIME)); | - | ||||||||||||||||||
4107 | GetLocalTime(&st); | - | ||||||||||||||||||
4108 | d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay); | - | ||||||||||||||||||
4109 | return d; | - | ||||||||||||||||||
4110 | } | - | ||||||||||||||||||
4111 | - | |||||||||||||||||||
4112 | QTime QTime::currentTime() | - | ||||||||||||||||||
4113 | { | - | ||||||||||||||||||
4114 | QTime ct; | - | ||||||||||||||||||
4115 | SYSTEMTIME st; | - | ||||||||||||||||||
4116 | memset(&st, 0, sizeof(SYSTEMTIME)); | - | ||||||||||||||||||
4117 | GetLocalTime(&st); | - | ||||||||||||||||||
4118 | ct.setHMS(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); | - | ||||||||||||||||||
4119 | #if defined(Q_OS_WINCE) | - | ||||||||||||||||||
4120 | ct.startTick = GetTickCount() % MSECS_PER_DAY; | - | ||||||||||||||||||
4121 | #endif | - | ||||||||||||||||||
4122 | return ct; | - | ||||||||||||||||||
4123 | } | - | ||||||||||||||||||
4124 | - | |||||||||||||||||||
4125 | QDateTime QDateTime::currentDateTime() | - | ||||||||||||||||||
4126 | { | - | ||||||||||||||||||
4127 | QDate d; | - | ||||||||||||||||||
4128 | QTime t; | - | ||||||||||||||||||
4129 | SYSTEMTIME st; | - | ||||||||||||||||||
4130 | memset(&st, 0, sizeof(SYSTEMTIME)); | - | ||||||||||||||||||
4131 | GetLocalTime(&st); | - | ||||||||||||||||||
4132 | d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay); | - | ||||||||||||||||||
4133 | t.mds = msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); | - | ||||||||||||||||||
4134 | return QDateTime(d, t); | - | ||||||||||||||||||
4135 | } | - | ||||||||||||||||||
4136 | - | |||||||||||||||||||
4137 | QDateTime QDateTime::currentDateTimeUtc() | - | ||||||||||||||||||
4138 | { | - | ||||||||||||||||||
4139 | QDate d; | - | ||||||||||||||||||
4140 | QTime t; | - | ||||||||||||||||||
4141 | SYSTEMTIME st; | - | ||||||||||||||||||
4142 | memset(&st, 0, sizeof(SYSTEMTIME)); | - | ||||||||||||||||||
4143 | GetSystemTime(&st); | - | ||||||||||||||||||
4144 | d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay); | - | ||||||||||||||||||
4145 | t.mds = msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); | - | ||||||||||||||||||
4146 | return QDateTime(d, t, Qt::UTC); | - | ||||||||||||||||||
4147 | } | - | ||||||||||||||||||
4148 | - | |||||||||||||||||||
4149 | qint64 QDateTime::currentMSecsSinceEpoch() Q_DECL_NOTHROW | - | ||||||||||||||||||
4150 | { | - | ||||||||||||||||||
4151 | SYSTEMTIME st; | - | ||||||||||||||||||
4152 | memset(&st, 0, sizeof(SYSTEMTIME)); | - | ||||||||||||||||||
4153 | GetSystemTime(&st); | - | ||||||||||||||||||
4154 | - | |||||||||||||||||||
4155 | return msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds) + | - | ||||||||||||||||||
4156 | qint64(julianDayFromDate(st.wYear, st.wMonth, st.wDay) | - | ||||||||||||||||||
4157 | - julianDayFromDate(1970, 1, 1)) * Q_INT64_C(86400000); | - | ||||||||||||||||||
4158 | } | - | ||||||||||||||||||
4159 | - | |||||||||||||||||||
4160 | #elif defined(Q_OS_UNIX) | - | ||||||||||||||||||
4161 | QDate QDate::currentDate() | - | ||||||||||||||||||
4162 | { | - | ||||||||||||||||||
4163 | return QDateTime::currentDateTime().date(); | - | ||||||||||||||||||
4164 | } | - | ||||||||||||||||||
4165 | - | |||||||||||||||||||
4166 | QTime QTime::currentTime() | - | ||||||||||||||||||
4167 | { | - | ||||||||||||||||||
4168 | return QDateTime::currentDateTime().time(); | - | ||||||||||||||||||
4169 | } | - | ||||||||||||||||||
4170 | - | |||||||||||||||||||
4171 | QDateTime QDateTime::currentDateTime() | - | ||||||||||||||||||
4172 | { | - | ||||||||||||||||||
4173 | return fromMSecsSinceEpoch(currentMSecsSinceEpoch(), Qt::LocalTime); | - | ||||||||||||||||||
4174 | } | - | ||||||||||||||||||
4175 | - | |||||||||||||||||||
4176 | QDateTime QDateTime::currentDateTimeUtc() | - | ||||||||||||||||||
4177 | { | - | ||||||||||||||||||
4178 | return fromMSecsSinceEpoch(currentMSecsSinceEpoch(), Qt::UTC); | - | ||||||||||||||||||
4179 | } | - | ||||||||||||||||||
4180 | - | |||||||||||||||||||
4181 | qint64 QDateTime::currentMSecsSinceEpoch() Q_DECL_NOTHROW | - | ||||||||||||||||||
4182 | { | - | ||||||||||||||||||
4183 | // posix compliant system | - | ||||||||||||||||||
4184 | // we have milliseconds | - | ||||||||||||||||||
4185 | struct timeval tv; | - | ||||||||||||||||||
4186 | gettimeofday(&tv, 0); | - | ||||||||||||||||||
4187 | return qint64(tv.tv_sec) * Q_INT64_C(1000) + tv.tv_usec / 1000; executed 6948773 times by 70 tests: return qint64(tv.tv_sec) * static_cast<long long>(1000LL) + tv.tv_usec / 1000; Executed by:
| 6948773 | ||||||||||||||||||
4188 | } | - | ||||||||||||||||||
4189 | - | |||||||||||||||||||
4190 | #else | - | ||||||||||||||||||
4191 | #error "What system is this?" | - | ||||||||||||||||||
4192 | #endif | - | ||||||||||||||||||
4193 | - | |||||||||||||||||||
4194 | /*! \fn QDateTime QDateTime::fromCFDate(CFDateRef date) | - | ||||||||||||||||||
4195 | \since 5.5 | - | ||||||||||||||||||
4196 | - | |||||||||||||||||||
4197 | Constructs a new QDateTime containing a copy of the CFDate \a date. | - | ||||||||||||||||||
4198 | - | |||||||||||||||||||
4199 | \sa toCFDate() | - | ||||||||||||||||||
4200 | */ | - | ||||||||||||||||||
4201 | - | |||||||||||||||||||
4202 | /*! \fn CFDateRef QDateTime::toCFDate() const | - | ||||||||||||||||||
4203 | \since 5.5 | - | ||||||||||||||||||
4204 | - | |||||||||||||||||||
4205 | Creates a CFDate from a QDateTime. The caller owns the CFDate object | - | ||||||||||||||||||
4206 | and is responsible for releasing it. | - | ||||||||||||||||||
4207 | - | |||||||||||||||||||
4208 | \sa fromCFDate() | - | ||||||||||||||||||
4209 | */ | - | ||||||||||||||||||
4210 | - | |||||||||||||||||||
4211 | /*! \fn QDateTime QDateTime::fromNSDate(const NSDate *date) | - | ||||||||||||||||||
4212 | \since 5.5 | - | ||||||||||||||||||
4213 | - | |||||||||||||||||||
4214 | Constructs a new QDateTime containing a copy of the NSDate \a date. | - | ||||||||||||||||||
4215 | - | |||||||||||||||||||
4216 | \sa toNSDate() | - | ||||||||||||||||||
4217 | */ | - | ||||||||||||||||||
4218 | - | |||||||||||||||||||
4219 | /*! \fn NSDate QDateTime::toNSDate() const | - | ||||||||||||||||||
4220 | \since 5.5 | - | ||||||||||||||||||
4221 | - | |||||||||||||||||||
4222 | Creates an NSDate from a QDateTime. The NSDate object is autoreleased. | - | ||||||||||||||||||
4223 | - | |||||||||||||||||||
4224 | \sa fromNSDate() | - | ||||||||||||||||||
4225 | */ | - | ||||||||||||||||||
4226 | - | |||||||||||||||||||
4227 | /*! | - | ||||||||||||||||||
4228 | \since 4.2 | - | ||||||||||||||||||
4229 | - | |||||||||||||||||||
4230 | Returns a datetime whose date and time are the number of \a seconds | - | ||||||||||||||||||
4231 | that have passed since 1970-01-01T00:00:00, Coordinated Universal | - | ||||||||||||||||||
4232 | Time (Qt::UTC) and converted to Qt::LocalTime. On systems that do not | - | ||||||||||||||||||
4233 | support time zones, the time will be set as if local time were Qt::UTC. | - | ||||||||||||||||||
4234 | - | |||||||||||||||||||
4235 | \sa toTime_t(), setTime_t() | - | ||||||||||||||||||
4236 | */ | - | ||||||||||||||||||
4237 | QDateTime QDateTime::fromTime_t(uint seconds) | - | ||||||||||||||||||
4238 | { | - | ||||||||||||||||||
4239 | return fromMSecsSinceEpoch((qint64)seconds * 1000, Qt::LocalTime); | - | ||||||||||||||||||
4240 | } | - | ||||||||||||||||||
4241 | - | |||||||||||||||||||
4242 | /*! | - | ||||||||||||||||||
4243 | \since 5.2 | - | ||||||||||||||||||
4244 | - | |||||||||||||||||||
4245 | Returns a datetime whose date and time are the number of \a seconds | - | ||||||||||||||||||
4246 | that have passed since 1970-01-01T00:00:00, Coordinated Universal | - | ||||||||||||||||||
4247 | Time (Qt::UTC) and converted to the given \a spec. | - | ||||||||||||||||||
4248 | - | |||||||||||||||||||
4249 | If the \a spec is not Qt::OffsetFromUTC then the \a offsetSeconds will be | - | ||||||||||||||||||
4250 | ignored. If the \a spec is Qt::OffsetFromUTC and the \a offsetSeconds is 0 | - | ||||||||||||||||||
4251 | then the spec will be set to Qt::UTC, i.e. an offset of 0 seconds. | - | ||||||||||||||||||
4252 | - | |||||||||||||||||||
4253 | \sa toTime_t(), setTime_t() | - | ||||||||||||||||||
4254 | */ | - | ||||||||||||||||||
4255 | QDateTime QDateTime::fromTime_t(uint seconds, Qt::TimeSpec spec, int offsetSeconds) | - | ||||||||||||||||||
4256 | { | - | ||||||||||||||||||
4257 | return fromMSecsSinceEpoch((qint64)seconds * 1000, spec, offsetSeconds); | - | ||||||||||||||||||
4258 | } | - | ||||||||||||||||||
4259 | - | |||||||||||||||||||
4260 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
4261 | /*! | - | ||||||||||||||||||
4262 | \since 5.2 | - | ||||||||||||||||||
4263 | - | |||||||||||||||||||
4264 | Returns a datetime whose date and time are the number of \a seconds | - | ||||||||||||||||||
4265 | that have passed since 1970-01-01T00:00:00, Coordinated Universal | - | ||||||||||||||||||
4266 | Time (Qt::UTC) and with the given \a timeZone. | - | ||||||||||||||||||
4267 | - | |||||||||||||||||||
4268 | \sa toTime_t(), setTime_t() | - | ||||||||||||||||||
4269 | */ | - | ||||||||||||||||||
4270 | QDateTime QDateTime::fromTime_t(uint seconds, const QTimeZone &timeZone) | - | ||||||||||||||||||
4271 | { | - | ||||||||||||||||||
4272 | return fromMSecsSinceEpoch((qint64)seconds * 1000, timeZone); | - | ||||||||||||||||||
4273 | } | - | ||||||||||||||||||
4274 | #endif | - | ||||||||||||||||||
4275 | - | |||||||||||||||||||
4276 | /*! | - | ||||||||||||||||||
4277 | \since 4.7 | - | ||||||||||||||||||
4278 | - | |||||||||||||||||||
4279 | Returns a datetime whose date and time are the number of milliseconds, \a msecs, | - | ||||||||||||||||||
4280 | that have passed since 1970-01-01T00:00:00.000, Coordinated Universal | - | ||||||||||||||||||
4281 | Time (Qt::UTC), and converted to Qt::LocalTime. On systems that do not | - | ||||||||||||||||||
4282 | support time zones, the time will be set as if local time were Qt::UTC. | - | ||||||||||||||||||
4283 | - | |||||||||||||||||||
4284 | Note that there are possible values for \a msecs that lie outside the valid | - | ||||||||||||||||||
4285 | range of QDateTime, both negative and positive. The behavior of this | - | ||||||||||||||||||
4286 | function is undefined for those values. | - | ||||||||||||||||||
4287 | - | |||||||||||||||||||
4288 | \sa toTime_t(), setTime_t() | - | ||||||||||||||||||
4289 | */ | - | ||||||||||||||||||
4290 | QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs) | - | ||||||||||||||||||
4291 | { | - | ||||||||||||||||||
4292 | return fromMSecsSinceEpoch(msecs, Qt::LocalTime); | - | ||||||||||||||||||
4293 | } | - | ||||||||||||||||||
4294 | - | |||||||||||||||||||
4295 | /*! | - | ||||||||||||||||||
4296 | \since 5.2 | - | ||||||||||||||||||
4297 | - | |||||||||||||||||||
4298 | Returns a datetime whose date and time are the number of milliseconds \a msecs | - | ||||||||||||||||||
4299 | that have passed since 1970-01-01T00:00:00.000, Coordinated Universal | - | ||||||||||||||||||
4300 | Time (Qt::UTC) and converted to the given \a spec. | - | ||||||||||||||||||
4301 | - | |||||||||||||||||||
4302 | Note that there are possible values for \a msecs that lie outside the valid | - | ||||||||||||||||||
4303 | range of QDateTime, both negative and positive. The behavior of this | - | ||||||||||||||||||
4304 | function is undefined for those values. | - | ||||||||||||||||||
4305 | - | |||||||||||||||||||
4306 | If the \a spec is not Qt::OffsetFromUTC then the \a offsetSeconds will be | - | ||||||||||||||||||
4307 | ignored. If the \a spec is Qt::OffsetFromUTC and the \a offsetSeconds is 0 | - | ||||||||||||||||||
4308 | then the spec will be set to Qt::UTC, i.e. an offset of 0 seconds. | - | ||||||||||||||||||
4309 | - | |||||||||||||||||||
4310 | If \a spec is Qt::TimeZone then the spec will be set to Qt::LocalTime, | - | ||||||||||||||||||
4311 | i.e. the current system time zone. | - | ||||||||||||||||||
4312 | - | |||||||||||||||||||
4313 | \sa fromTime_t() | - | ||||||||||||||||||
4314 | */ | - | ||||||||||||||||||
4315 | QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int offsetSeconds) | - | ||||||||||||||||||
4316 | { | - | ||||||||||||||||||
4317 | QDateTime dt; | - | ||||||||||||||||||
4318 | dt.d->setTimeSpec(spec, offsetSeconds); | - | ||||||||||||||||||
4319 | dt.setMSecsSinceEpoch(msecs); | - | ||||||||||||||||||
4320 | return dt; | - | ||||||||||||||||||
4321 | } | - | ||||||||||||||||||
4322 | - | |||||||||||||||||||
4323 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
4324 | /*! | - | ||||||||||||||||||
4325 | \since 5.2 | - | ||||||||||||||||||
4326 | - | |||||||||||||||||||
4327 | Returns a datetime whose date and time are the number of milliseconds \a msecs | - | ||||||||||||||||||
4328 | that have passed since 1970-01-01T00:00:00.000, Coordinated Universal | - | ||||||||||||||||||
4329 | Time (Qt::UTC) and with the given \a timeZone. | - | ||||||||||||||||||
4330 | - | |||||||||||||||||||
4331 | \sa fromTime_t() | - | ||||||||||||||||||
4332 | */ | - | ||||||||||||||||||
4333 | QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, const QTimeZone &timeZone) | - | ||||||||||||||||||
4334 | { | - | ||||||||||||||||||
4335 | QDateTime dt; | - | ||||||||||||||||||
4336 | dt.setTimeZone(timeZone); | - | ||||||||||||||||||
4337 | dt.setMSecsSinceEpoch(msecs); | - | ||||||||||||||||||
4338 | return dt; | - | ||||||||||||||||||
4339 | } | - | ||||||||||||||||||
4340 | #endif | - | ||||||||||||||||||
4341 | - | |||||||||||||||||||
4342 | #if QT_DEPRECATED_SINCE(5, 2) | - | ||||||||||||||||||
4343 | /*! | - | ||||||||||||||||||
4344 | \since 4.4 | - | ||||||||||||||||||
4345 | \internal | - | ||||||||||||||||||
4346 | \obsolete | - | ||||||||||||||||||
4347 | - | |||||||||||||||||||
4348 | This method was added in 4.4 but never documented as public. It was replaced | - | ||||||||||||||||||
4349 | in 5.2 with public method setOffsetFromUtc() for consistency with QTimeZone. | - | ||||||||||||||||||
4350 | - | |||||||||||||||||||
4351 | This method should never be made public. | - | ||||||||||||||||||
4352 | - | |||||||||||||||||||
4353 | \sa setOffsetFromUtc() | - | ||||||||||||||||||
4354 | */ | - | ||||||||||||||||||
4355 | void QDateTime::setUtcOffset(int seconds) | - | ||||||||||||||||||
4356 | { | - | ||||||||||||||||||
4357 | setOffsetFromUtc(seconds); | - | ||||||||||||||||||
4358 | } | - | ||||||||||||||||||
4359 | - | |||||||||||||||||||
4360 | /*! | - | ||||||||||||||||||
4361 | \since 4.4 | - | ||||||||||||||||||
4362 | \internal | - | ||||||||||||||||||
4363 | \obsolete | - | ||||||||||||||||||
4364 | - | |||||||||||||||||||
4365 | This method was added in 4.4 but never documented as public. It was replaced | - | ||||||||||||||||||
4366 | in 5.1 with public method offsetFromUTC() for consistency with QTimeZone. | - | ||||||||||||||||||
4367 | - | |||||||||||||||||||
4368 | This method should never be made public. | - | ||||||||||||||||||
4369 | - | |||||||||||||||||||
4370 | \sa offsetFromUTC() | - | ||||||||||||||||||
4371 | */ | - | ||||||||||||||||||
4372 | int QDateTime::utcOffset() const | - | ||||||||||||||||||
4373 | { | - | ||||||||||||||||||
4374 | return offsetFromUtc(); | - | ||||||||||||||||||
4375 | } | - | ||||||||||||||||||
4376 | #endif // QT_DEPRECATED_SINCE | - | ||||||||||||||||||
4377 | - | |||||||||||||||||||
4378 | #ifndef QT_NO_DATESTRING | - | ||||||||||||||||||
4379 | - | |||||||||||||||||||
4380 | /*! | - | ||||||||||||||||||
4381 | \fn QDateTime QDateTime::fromString(const QString &string, Qt::DateFormat format) | - | ||||||||||||||||||
4382 | - | |||||||||||||||||||
4383 | Returns the QDateTime represented by the \a string, using the | - | ||||||||||||||||||
4384 | \a format given, or an invalid datetime if this is not possible. | - | ||||||||||||||||||
4385 | - | |||||||||||||||||||
4386 | Note for Qt::TextDate: It is recommended that you use the | - | ||||||||||||||||||
4387 | English short month names (e.g. "Jan"). Although localized month | - | ||||||||||||||||||
4388 | names can also be used, they depend on the user's locale settings. | - | ||||||||||||||||||
4389 | - | |||||||||||||||||||
4390 | \sa toString(), QLocale::toDateTime() | - | ||||||||||||||||||
4391 | */ | - | ||||||||||||||||||
4392 | QDateTime QDateTime::fromString(const QString& string, Qt::DateFormat format) | - | ||||||||||||||||||
4393 | { | - | ||||||||||||||||||
4394 | if (string.isEmpty())
| 5-144 | ||||||||||||||||||
4395 | return QDateTime(); executed 5 times by 1 test: return QDateTime(); Executed by:
| 5 | ||||||||||||||||||
4396 | - | |||||||||||||||||||
4397 | switch (format) { | - | ||||||||||||||||||
4398 | case Qt::SystemLocaleDate: executed 1 time by 1 test: case Qt::SystemLocaleDate: Executed by:
| 1 | ||||||||||||||||||
4399 | case Qt::SystemLocaleShortDate: executed 1 time by 1 test: case Qt::SystemLocaleShortDate: Executed by:
| 1 | ||||||||||||||||||
4400 | return QLocale::system().toDateTime(string, QLocale::ShortFormat); executed 2 times by 1 test: return QLocale::system().toDateTime(string, QLocale::ShortFormat); Executed by:
| 2 | ||||||||||||||||||
4401 | case Qt::SystemLocaleLongDate: executed 1 time by 1 test: case Qt::SystemLocaleLongDate: Executed by:
| 1 | ||||||||||||||||||
4402 | return QLocale::system().toDateTime(string, QLocale::LongFormat); executed 1 time by 1 test: return QLocale::system().toDateTime(string, QLocale::LongFormat); Executed by:
| 1 | ||||||||||||||||||
4403 | case Qt::LocaleDate: executed 1 time by 1 test: case Qt::LocaleDate: Executed by:
| 1 | ||||||||||||||||||
4404 | case Qt::DefaultLocaleShortDate: executed 1 time by 1 test: case Qt::DefaultLocaleShortDate: Executed by:
| 1 | ||||||||||||||||||
4405 | return QLocale().toDateTime(string, QLocale::ShortFormat); executed 2 times by 1 test: return QLocale().toDateTime(string, QLocale::ShortFormat); Executed by:
| 2 | ||||||||||||||||||
4406 | case Qt::DefaultLocaleLongDate: executed 1 time by 1 test: case Qt::DefaultLocaleLongDate: Executed by:
| 1 | ||||||||||||||||||
4407 | return QLocale().toDateTime(string, QLocale::LongFormat); executed 1 time by 1 test: return QLocale().toDateTime(string, QLocale::LongFormat); Executed by:
| 1 | ||||||||||||||||||
4408 | case Qt::RFC2822Date: { executed 30 times by 1 test: case Qt::RFC2822Date: Executed by:
| 30 | ||||||||||||||||||
4409 | const ParsedRfcDateTime rfc = rfcDateImpl(string); | - | ||||||||||||||||||
4410 | - | |||||||||||||||||||
4411 | if (!rfc.date.isValid() || !rfc.time.isValid())
| 3-19 | ||||||||||||||||||
4412 | return QDateTime(); executed 14 times by 1 test: return QDateTime(); Executed by:
| 14 | ||||||||||||||||||
4413 | - | |||||||||||||||||||
4414 | QDateTime dateTime(rfc.date, rfc.time, Qt::UTC); | - | ||||||||||||||||||
4415 | dateTime.setOffsetFromUtc(rfc.utcOffset); | - | ||||||||||||||||||
4416 | return dateTime; executed 16 times by 1 test: return dateTime; Executed by:
| 16 | ||||||||||||||||||
4417 | } | - | ||||||||||||||||||
4418 | case Qt::ISODate: { executed 66 times by 4 tests: case Qt::ISODate: Executed by:
| 66 | ||||||||||||||||||
4419 | const int size = string.size(); | - | ||||||||||||||||||
4420 | if (size < 10)
| 4-62 | ||||||||||||||||||
4421 | return QDateTime(); executed 4 times by 1 test: return QDateTime(); Executed by:
| 4 | ||||||||||||||||||
4422 | - | |||||||||||||||||||
4423 | QStringRef isoString(&string); | - | ||||||||||||||||||
4424 | Qt::TimeSpec spec = Qt::LocalTime; | - | ||||||||||||||||||
4425 | - | |||||||||||||||||||
4426 | QDate date = QDate::fromString(string.left(10), Qt::ISODate); | - | ||||||||||||||||||
4427 | if (!date.isValid())
| 7-55 | ||||||||||||||||||
4428 | return QDateTime(); executed 7 times by 2 tests: return QDateTime(); Executed by:
| 7 | ||||||||||||||||||
4429 | if (size == 10)
| 1-54 | ||||||||||||||||||
4430 | return QDateTime(date); executed 1 time by 1 test: return QDateTime(date); Executed by:
| 1 | ||||||||||||||||||
4431 | - | |||||||||||||||||||
4432 | isoString = isoString.right(isoString.length() - 11); | - | ||||||||||||||||||
4433 | int offset = 0; | - | ||||||||||||||||||
4434 | // Check end of string for Time Zone definition, either Z for UTC or [+-]HH:mm for Offset | - | ||||||||||||||||||
4435 | if (isoString.endsWith(QLatin1Char('Z'))) {
| 6-48 | ||||||||||||||||||
4436 | spec = Qt::UTC; | - | ||||||||||||||||||
4437 | isoString = isoString.left(isoString.size() - 1); | - | ||||||||||||||||||
4438 | } else { executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||||||||
4439 | // the loop below is faster but functionally equal to: | - | ||||||||||||||||||
4440 | // const int signIndex = isoString.indexOf(QRegExp(QStringLiteral("[+-]"))); | - | ||||||||||||||||||
4441 | int signIndex = isoString.size() - 1; | - | ||||||||||||||||||
4442 | bool found = false; | - | ||||||||||||||||||
4443 | { | - | ||||||||||||||||||
4444 | const QChar plus = QLatin1Char('+'); | - | ||||||||||||||||||
4445 | const QChar minus = QLatin1Char('-'); | - | ||||||||||||||||||
4446 | do { | - | ||||||||||||||||||
4447 | QChar character(isoString.at(signIndex)); | - | ||||||||||||||||||
4448 | found = character == plus || character == minus;
| 5-397 | ||||||||||||||||||
4449 | } while (--signIndex >= 0 && !found); executed 402 times by 4 tests: end of block Executed by:
| 13-402 | ||||||||||||||||||
4450 | ++signIndex; | - | ||||||||||||||||||
4451 | } | - | ||||||||||||||||||
4452 | - | |||||||||||||||||||
4453 | if (found) {
| 13-35 | ||||||||||||||||||
4454 | bool ok; | - | ||||||||||||||||||
4455 | offset = fromOffsetString(isoString.mid(signIndex), &ok); | - | ||||||||||||||||||
4456 | if (!ok)
| 0-13 | ||||||||||||||||||
4457 | return QDateTime(); never executed: return QDateTime(); | 0 | ||||||||||||||||||
4458 | isoString = isoString.left(signIndex); | - | ||||||||||||||||||
4459 | spec = Qt::OffsetFromUTC; | - | ||||||||||||||||||
4460 | } executed 13 times by 1 test: end of block Executed by:
| 13 | ||||||||||||||||||
4461 | } executed 48 times by 4 tests: end of block Executed by:
| 48 | ||||||||||||||||||
4462 | - | |||||||||||||||||||
4463 | // Might be end of day (24:00, including variants), which QTime considers invalid. | - | ||||||||||||||||||
4464 | // ISO 8601 (section 4.2.3) says that 24:00 is equivalent to 00:00 the next day. | - | ||||||||||||||||||
4465 | bool isMidnight24 = false; | - | ||||||||||||||||||
4466 | QTime time = fromIsoTimeString(isoString, Qt::ISODate, &isMidnight24); | - | ||||||||||||||||||
4467 | if (!time.isValid())
| 0-54 | ||||||||||||||||||
4468 | return QDateTime(); never executed: return QDateTime(); | 0 | ||||||||||||||||||
4469 | if (isMidnight24)
| 5-49 | ||||||||||||||||||
4470 | date = date.addDays(1); executed 5 times by 1 test: date = date.addDays(1); Executed by:
| 5 | ||||||||||||||||||
4471 | return QDateTime(date, time, spec, offset); executed 54 times by 4 tests: return QDateTime(date, time, spec, offset); Executed by:
| 54 | ||||||||||||||||||
4472 | } | - | ||||||||||||||||||
4473 | #if !defined(QT_NO_TEXTDATE) | - | ||||||||||||||||||
4474 | case Qt::TextDate: { executed 42 times by 3 tests: case Qt::TextDate: Executed by:
| 42 | ||||||||||||||||||
4475 | QVector<QStringRef> parts = string.splitRef(QLatin1Char(' '), QString::SkipEmptyParts); | - | ||||||||||||||||||
4476 | - | |||||||||||||||||||
4477 | if ((parts.count() < 5) || (parts.count() > 6))
| 1-41 | ||||||||||||||||||
4478 | return QDateTime(); executed 2 times by 1 test: return QDateTime(); Executed by:
| 2 | ||||||||||||||||||
4479 | - | |||||||||||||||||||
4480 | // Accept "Sun Dec 1 13:02:00 1974" and "Sun 1. Dec 13:02:00 1974" | - | ||||||||||||||||||
4481 | int month = 0; | - | ||||||||||||||||||
4482 | int day = 0; | - | ||||||||||||||||||
4483 | bool ok = false; | - | ||||||||||||||||||
4484 | - | |||||||||||||||||||
4485 | // First try month then day | - | ||||||||||||||||||
4486 | month = fromShortMonthName(parts.at(1)); | - | ||||||||||||||||||
4487 | if (month)
| 0-40 | ||||||||||||||||||
4488 | day = parts.at(2).toInt(); executed 40 times by 3 tests: day = parts.at(2).toInt(); Executed by:
| 40 | ||||||||||||||||||
4489 | - | |||||||||||||||||||
4490 | // If failed try day then month | - | ||||||||||||||||||
4491 | if (!month || !day) {
| 0-40 | ||||||||||||||||||
4492 | month = fromShortMonthName(parts.at(2)); | - | ||||||||||||||||||
4493 | if (month) {
| 0-14 | ||||||||||||||||||
4494 | QStringRef dayStr = parts.at(1); | - | ||||||||||||||||||
4495 | if (dayStr.endsWith(QLatin1Char('.'))) {
| 2-12 | ||||||||||||||||||
4496 | dayStr = dayStr.left(dayStr.size() - 1); | - | ||||||||||||||||||
4497 | day = dayStr.toInt(); | - | ||||||||||||||||||
4498 | } executed 12 times by 1 test: end of block Executed by:
| 12 | ||||||||||||||||||
4499 | } executed 14 times by 1 test: end of block Executed by:
| 14 | ||||||||||||||||||
4500 | } executed 14 times by 1 test: end of block Executed by:
| 14 | ||||||||||||||||||
4501 | - | |||||||||||||||||||
4502 | // If both failed, give up | - | ||||||||||||||||||
4503 | if (!month || !day)
| 0-40 | ||||||||||||||||||
4504 | return QDateTime(); executed 3 times by 1 test: return QDateTime(); Executed by:
| 3 | ||||||||||||||||||
4505 | - | |||||||||||||||||||
4506 | // Year can be before or after time, "Sun Dec 1 1974 13:02:00" or "Sun Dec 1 13:02:00 1974" | - | ||||||||||||||||||
4507 | // Guess which by looking for ':' in the time | - | ||||||||||||||||||
4508 | int year = 0; | - | ||||||||||||||||||
4509 | int yearPart = 0; | - | ||||||||||||||||||
4510 | int timePart = 0; | - | ||||||||||||||||||
4511 | if (parts.at(3).contains(QLatin1Char(':'))) {
| 14-23 | ||||||||||||||||||
4512 | yearPart = 4; | - | ||||||||||||||||||
4513 | timePart = 3; | - | ||||||||||||||||||
4514 | } else if (parts.at(4).contains(QLatin1Char(':'))) { executed 23 times by 3 tests: end of block Executed by:
| 1-23 | ||||||||||||||||||
4515 | yearPart = 3; | - | ||||||||||||||||||
4516 | timePart = 4; | - | ||||||||||||||||||
4517 | } else { executed 13 times by 1 test: end of block Executed by:
| 13 | ||||||||||||||||||
4518 | return QDateTime(); executed 1 time by 1 test: return QDateTime(); Executed by:
| 1 | ||||||||||||||||||
4519 | } | - | ||||||||||||||||||
4520 | - | |||||||||||||||||||
4521 | year = parts.at(yearPart).toInt(&ok); | - | ||||||||||||||||||
4522 | if (!ok)
| 2-34 | ||||||||||||||||||
4523 | return QDateTime(); executed 2 times by 1 test: return QDateTime(); Executed by:
| 2 | ||||||||||||||||||
4524 | - | |||||||||||||||||||
4525 | QDate date(year, month, day); | - | ||||||||||||||||||
4526 | if (!date.isValid())
| 2-32 | ||||||||||||||||||
4527 | return QDateTime(); executed 2 times by 1 test: return QDateTime(); Executed by:
| 2 | ||||||||||||||||||
4528 | - | |||||||||||||||||||
4529 | QVector<QStringRef> timeParts = parts.at(timePart).split(QLatin1Char(':')); | - | ||||||||||||||||||
4530 | if (timeParts.count() < 2 || timeParts.count() > 3)
| 0-32 | ||||||||||||||||||
4531 | return QDateTime(); executed 1 time by 1 test: return QDateTime(); Executed by:
| 1 | ||||||||||||||||||
4532 | - | |||||||||||||||||||
4533 | int hour = timeParts.at(0).toInt(&ok); | - | ||||||||||||||||||
4534 | if (!ok)
| 1-30 | ||||||||||||||||||
4535 | return QDateTime(); executed 1 time by 1 test: return QDateTime(); Executed by:
| 1 | ||||||||||||||||||
4536 | - | |||||||||||||||||||
4537 | int minute = timeParts.at(1).toInt(&ok); | - | ||||||||||||||||||
4538 | if (!ok)
| 1-29 | ||||||||||||||||||
4539 | return QDateTime(); executed 1 time by 1 test: return QDateTime(); Executed by:
| 1 | ||||||||||||||||||
4540 | - | |||||||||||||||||||
4541 | int second = 0; | - | ||||||||||||||||||
4542 | int millisecond = 0; | - | ||||||||||||||||||
4543 | if (timeParts.count() > 2) {
| 1-28 | ||||||||||||||||||
4544 | const QVector<QStringRef> secondParts = timeParts.at(2).split(QLatin1Char('.')); | - | ||||||||||||||||||
4545 | if (secondParts.size() > 2) {
| 0-28 | ||||||||||||||||||
4546 | return QDateTime(); never executed: return QDateTime(); | 0 | ||||||||||||||||||
4547 | } | - | ||||||||||||||||||
4548 | - | |||||||||||||||||||
4549 | second = secondParts.first().toInt(&ok); | - | ||||||||||||||||||
4550 | if (!ok) {
| 1-27 | ||||||||||||||||||
4551 | return QDateTime(); executed 1 time by 1 test: return QDateTime(); Executed by:
| 1 | ||||||||||||||||||
4552 | } | - | ||||||||||||||||||
4553 | - | |||||||||||||||||||
4554 | if (secondParts.size() > 1) {
| 1-26 | ||||||||||||||||||
4555 | millisecond = secondParts.last().toInt(&ok); | - | ||||||||||||||||||
4556 | if (!ok) {
| 0-1 | ||||||||||||||||||
4557 | return QDateTime(); never executed: return QDateTime(); | 0 | ||||||||||||||||||
4558 | } | - | ||||||||||||||||||
4559 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||
4560 | } executed 27 times by 3 tests: end of block Executed by:
| 27 | ||||||||||||||||||
4561 | - | |||||||||||||||||||
4562 | QTime time(hour, minute, second, millisecond); | - | ||||||||||||||||||
4563 | if (!time.isValid())
| 0-28 | ||||||||||||||||||
4564 | return QDateTime(); never executed: return QDateTime(); | 0 | ||||||||||||||||||
4565 | - | |||||||||||||||||||
4566 | if (parts.count() == 5)
| 13-15 | ||||||||||||||||||
4567 | return QDateTime(date, time, Qt::LocalTime); executed 15 times by 3 tests: return QDateTime(date, time, Qt::LocalTime); Executed by:
| 15 | ||||||||||||||||||
4568 | - | |||||||||||||||||||
4569 | QStringRef tz = parts.at(5); | - | ||||||||||||||||||
4570 | if (!tz.startsWith(QLatin1String("GMT"), Qt::CaseInsensitive))
| 1-12 | ||||||||||||||||||
4571 | return QDateTime(); executed 1 time by 1 test: return QDateTime(); Executed by:
| 1 | ||||||||||||||||||
4572 | tz = tz.mid(3); | - | ||||||||||||||||||
4573 | if (!tz.isEmpty()) {
| 3-9 | ||||||||||||||||||
4574 | int offset = fromOffsetString(tz, &ok); | - | ||||||||||||||||||
4575 | if (!ok)
| 4-5 | ||||||||||||||||||
4576 | return QDateTime(); executed 4 times by 1 test: return QDateTime(); Executed by:
| 4 | ||||||||||||||||||
4577 | return QDateTime(date, time, Qt::OffsetFromUTC, offset); executed 5 times by 1 test: return QDateTime(date, time, Qt::OffsetFromUTC, offset); Executed by:
| 5 | ||||||||||||||||||
4578 | } else { | - | ||||||||||||||||||
4579 | return QDateTime(date, time, Qt::UTC); executed 3 times by 1 test: return QDateTime(date, time, Qt::UTC); Executed by:
| 3 | ||||||||||||||||||
4580 | } | - | ||||||||||||||||||
4581 | } | - | ||||||||||||||||||
4582 | #endif //QT_NO_TEXTDATE | - | ||||||||||||||||||
4583 | } | - | ||||||||||||||||||
4584 | - | |||||||||||||||||||
4585 | return QDateTime(); never executed: return QDateTime(); | 0 | ||||||||||||||||||
4586 | } | - | ||||||||||||||||||
4587 | - | |||||||||||||||||||
4588 | /*! | - | ||||||||||||||||||
4589 | \fn QDateTime::fromString(const QString &string, const QString &format) | - | ||||||||||||||||||
4590 | - | |||||||||||||||||||
4591 | Returns the QDateTime represented by the \a string, using the \a | - | ||||||||||||||||||
4592 | format given, or an invalid datetime if the string cannot be parsed. | - | ||||||||||||||||||
4593 | - | |||||||||||||||||||
4594 | These expressions may be used for the date part of the format string: | - | ||||||||||||||||||
4595 | - | |||||||||||||||||||
4596 | \table | - | ||||||||||||||||||
4597 | \header \li Expression \li Output | - | ||||||||||||||||||
4598 | \row \li d \li the day as number without a leading zero (1 to 31) | - | ||||||||||||||||||
4599 | \row \li dd \li the day as number with a leading zero (01 to 31) | - | ||||||||||||||||||
4600 | \row \li ddd | - | ||||||||||||||||||
4601 | \li the abbreviated localized day name (e.g. 'Mon' to 'Sun'). | - | ||||||||||||||||||
4602 | Uses QDate::shortDayName(). | - | ||||||||||||||||||
4603 | \row \li dddd | - | ||||||||||||||||||
4604 | \li the long localized day name (e.g. 'Monday' to 'Sunday'). | - | ||||||||||||||||||
4605 | Uses QDate::longDayName(). | - | ||||||||||||||||||
4606 | \row \li M \li the month as number without a leading zero (1-12) | - | ||||||||||||||||||
4607 | \row \li MM \li the month as number with a leading zero (01-12) | - | ||||||||||||||||||
4608 | \row \li MMM | - | ||||||||||||||||||
4609 | \li the abbreviated localized month name (e.g. 'Jan' to 'Dec'). | - | ||||||||||||||||||
4610 | Uses QDate::shortMonthName(). | - | ||||||||||||||||||
4611 | \row \li MMMM | - | ||||||||||||||||||
4612 | \li the long localized month name (e.g. 'January' to 'December'). | - | ||||||||||||||||||
4613 | Uses QDate::longMonthName(). | - | ||||||||||||||||||
4614 | \row \li yy \li the year as two digit number (00-99) | - | ||||||||||||||||||
4615 | \row \li yyyy \li the year as four digit number | - | ||||||||||||||||||
4616 | \endtable | - | ||||||||||||||||||
4617 | - | |||||||||||||||||||
4618 | \note Unlike the other version of this function, day and month names must | - | ||||||||||||||||||
4619 | be given in the user's local language. It is only possible to use the English | - | ||||||||||||||||||
4620 | names if the user's language is English. | - | ||||||||||||||||||
4621 | - | |||||||||||||||||||
4622 | These expressions may be used for the time part of the format string: | - | ||||||||||||||||||
4623 | - | |||||||||||||||||||
4624 | \table | - | ||||||||||||||||||
4625 | \header \li Expression \li Output | - | ||||||||||||||||||
4626 | \row \li h | - | ||||||||||||||||||
4627 | \li the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) | - | ||||||||||||||||||
4628 | \row \li hh | - | ||||||||||||||||||
4629 | \li the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) | - | ||||||||||||||||||
4630 | \row \li H | - | ||||||||||||||||||
4631 | \li the hour without a leading zero (0 to 23, even with AM/PM display) | - | ||||||||||||||||||
4632 | \row \li HH | - | ||||||||||||||||||
4633 | \li the hour with a leading zero (00 to 23, even with AM/PM display) | - | ||||||||||||||||||
4634 | \row \li m \li the minute without a leading zero (0 to 59) | - | ||||||||||||||||||
4635 | \row \li mm \li the minute with a leading zero (00 to 59) | - | ||||||||||||||||||
4636 | \row \li s \li the second without a leading zero (0 to 59) | - | ||||||||||||||||||
4637 | \row \li ss \li the second with a leading zero (00 to 59) | - | ||||||||||||||||||
4638 | \row \li z \li the milliseconds without leading zeroes (0 to 999) | - | ||||||||||||||||||
4639 | \row \li zzz \li the milliseconds with leading zeroes (000 to 999) | - | ||||||||||||||||||
4640 | \row \li AP or A | - | ||||||||||||||||||
4641 | \li interpret as an AM/PM time. \e AP must be either "AM" or "PM". | - | ||||||||||||||||||
4642 | \row \li ap or a | - | ||||||||||||||||||
4643 | \li Interpret as an AM/PM time. \e ap must be either "am" or "pm". | - | ||||||||||||||||||
4644 | \endtable | - | ||||||||||||||||||
4645 | - | |||||||||||||||||||
4646 | All other input characters will be treated as text. Any sequence | - | ||||||||||||||||||
4647 | of characters that are enclosed in single quotes will also be | - | ||||||||||||||||||
4648 | treated as text and not be used as an expression. | - | ||||||||||||||||||
4649 | - | |||||||||||||||||||
4650 | \snippet code/src_corelib_tools_qdatetime.cpp 12 | - | ||||||||||||||||||
4651 | - | |||||||||||||||||||
4652 | If the format is not satisfied, an invalid QDateTime is returned. | - | ||||||||||||||||||
4653 | The expressions that don't have leading zeroes (d, M, h, m, s, z) will be | - | ||||||||||||||||||
4654 | greedy. This means that they will use two digits even if this will | - | ||||||||||||||||||
4655 | put them outside the range and/or leave too few digits for other | - | ||||||||||||||||||
4656 | sections. | - | ||||||||||||||||||
4657 | - | |||||||||||||||||||
4658 | \snippet code/src_corelib_tools_qdatetime.cpp 13 | - | ||||||||||||||||||
4659 | - | |||||||||||||||||||
4660 | This could have meant 1 January 00:30.00 but the M will grab | - | ||||||||||||||||||
4661 | two digits. | - | ||||||||||||||||||
4662 | - | |||||||||||||||||||
4663 | Incorrectly specified fields of the \a string will cause an invalid | - | ||||||||||||||||||
4664 | QDateTime to be returned. For example, consider the following code, | - | ||||||||||||||||||
4665 | where the two digit year 12 is read as 1912 (see the table below for all | - | ||||||||||||||||||
4666 | field defaults); the resulting datetime is invalid because 23 April 1912 | - | ||||||||||||||||||
4667 | was a Tuesday, not a Monday: | - | ||||||||||||||||||
4668 | - | |||||||||||||||||||
4669 | \snippet code/src_corelib_tools_qdatetime.cpp 20 | - | ||||||||||||||||||
4670 | - | |||||||||||||||||||
4671 | The correct code is: | - | ||||||||||||||||||
4672 | - | |||||||||||||||||||
4673 | \snippet code/src_corelib_tools_qdatetime.cpp 21 | - | ||||||||||||||||||
4674 | - | |||||||||||||||||||
4675 | For any field that is not represented in the format, the following | - | ||||||||||||||||||
4676 | defaults are used: | - | ||||||||||||||||||
4677 | - | |||||||||||||||||||
4678 | \table | - | ||||||||||||||||||
4679 | \header \li Field \li Default value | - | ||||||||||||||||||
4680 | \row \li Year \li 1900 | - | ||||||||||||||||||
4681 | \row \li Month \li 1 (January) | - | ||||||||||||||||||
4682 | \row \li Day \li 1 | - | ||||||||||||||||||
4683 | \row \li Hour \li 0 | - | ||||||||||||||||||
4684 | \row \li Minute \li 0 | - | ||||||||||||||||||
4685 | \row \li Second \li 0 | - | ||||||||||||||||||
4686 | \endtable | - | ||||||||||||||||||
4687 | - | |||||||||||||||||||
4688 | For example: | - | ||||||||||||||||||
4689 | - | |||||||||||||||||||
4690 | \snippet code/src_corelib_tools_qdatetime.cpp 14 | - | ||||||||||||||||||
4691 | - | |||||||||||||||||||
4692 | \sa toString(), QDate::fromString(), QTime::fromString(), | - | ||||||||||||||||||
4693 | QLocale::toDateTime() | - | ||||||||||||||||||
4694 | */ | - | ||||||||||||||||||
4695 | - | |||||||||||||||||||
4696 | QDateTime QDateTime::fromString(const QString &string, const QString &format) | - | ||||||||||||||||||
4697 | { | - | ||||||||||||||||||
4698 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
4699 | QTime time; | - | ||||||||||||||||||
4700 | QDate date; | - | ||||||||||||||||||
4701 | - | |||||||||||||||||||
4702 | QDateTimeParser dt(QVariant::DateTime, QDateTimeParser::FromString); | - | ||||||||||||||||||
4703 | if (dt.parseFormat(format) && dt.fromString(string, &date, &time))
| 0-49 | ||||||||||||||||||
4704 | return QDateTime(date, time); executed 41 times by 4 tests: return QDateTime(date, time); Executed by:
| 41 | ||||||||||||||||||
4705 | #else | - | ||||||||||||||||||
4706 | Q_UNUSED(string); | - | ||||||||||||||||||
4707 | Q_UNUSED(format); | - | ||||||||||||||||||
4708 | #endif | - | ||||||||||||||||||
4709 | return QDateTime(QDate(), QTime(-1, -1, -1));(); executed 8 times by 1 test: return QDateTime(); Executed by:
| 8 | ||||||||||||||||||
4710 | } | - | ||||||||||||||||||
4711 | - | |||||||||||||||||||
4712 | #endif // QT_NO_DATESTRING | - | ||||||||||||||||||
4713 | /*! | - | ||||||||||||||||||
4714 | \fn QDateTime QDateTime::toLocalTime() const | - | ||||||||||||||||||
4715 | - | |||||||||||||||||||
4716 | Returns a datetime containing the date and time information in | - | ||||||||||||||||||
4717 | this datetime, but specified using the Qt::LocalTime definition. | - | ||||||||||||||||||
4718 | - | |||||||||||||||||||
4719 | Example: | - | ||||||||||||||||||
4720 | - | |||||||||||||||||||
4721 | \snippet code/src_corelib_tools_qdatetime.cpp 17 | - | ||||||||||||||||||
4722 | - | |||||||||||||||||||
4723 | \sa toTimeSpec() | - | ||||||||||||||||||
4724 | */ | - | ||||||||||||||||||
4725 | - | |||||||||||||||||||
4726 | /*! | - | ||||||||||||||||||
4727 | \fn QDateTime QDateTime::toUTC() const | - | ||||||||||||||||||
4728 | - | |||||||||||||||||||
4729 | Returns a datetime containing the date and time information in | - | ||||||||||||||||||
4730 | this datetime, but specified using the Qt::UTC definition. | - | ||||||||||||||||||
4731 | - | |||||||||||||||||||
4732 | Example: | - | ||||||||||||||||||
4733 | - | |||||||||||||||||||
4734 | \snippet code/src_corelib_tools_qdatetime.cpp 18 | - | ||||||||||||||||||
4735 | - | |||||||||||||||||||
4736 | \sa toTimeSpec() | - | ||||||||||||||||||
4737 | */ | - | ||||||||||||||||||
4738 | - | |||||||||||||||||||
4739 | /***************************************************************************** | - | ||||||||||||||||||
4740 | Date/time stream functions | - | ||||||||||||||||||
4741 | *****************************************************************************/ | - | ||||||||||||||||||
4742 | - | |||||||||||||||||||
4743 | #ifndef QT_NO_DATASTREAM | - | ||||||||||||||||||
4744 | /*! | - | ||||||||||||||||||
4745 | \relates QDate | - | ||||||||||||||||||
4746 | - | |||||||||||||||||||
4747 | Writes the \a date to stream \a out. | - | ||||||||||||||||||
4748 | - | |||||||||||||||||||
4749 | \sa {Serializing Qt Data Types} | - | ||||||||||||||||||
4750 | */ | - | ||||||||||||||||||
4751 | - | |||||||||||||||||||
4752 | QDataStream &operator<<(QDataStream &out, const QDate &date) | - | ||||||||||||||||||
4753 | { | - | ||||||||||||||||||
4754 | if (out.version() < QDataStream::Qt_5_0) | - | ||||||||||||||||||
4755 | return out << quint32(date.jd); | - | ||||||||||||||||||
4756 | else | - | ||||||||||||||||||
4757 | return out << qint64(date.jd); | - | ||||||||||||||||||
4758 | } | - | ||||||||||||||||||
4759 | - | |||||||||||||||||||
4760 | /*! | - | ||||||||||||||||||
4761 | \relates QDate | - | ||||||||||||||||||
4762 | - | |||||||||||||||||||
4763 | Reads a date from stream \a in into the \a date. | - | ||||||||||||||||||
4764 | - | |||||||||||||||||||
4765 | \sa {Serializing Qt Data Types} | - | ||||||||||||||||||
4766 | */ | - | ||||||||||||||||||
4767 | - | |||||||||||||||||||
4768 | QDataStream &operator>>(QDataStream &in, QDate &date) | - | ||||||||||||||||||
4769 | { | - | ||||||||||||||||||
4770 | if (in.version() < QDataStream::Qt_5_0) { | - | ||||||||||||||||||
4771 | quint32 jd; | - | ||||||||||||||||||
4772 | in >> jd; | - | ||||||||||||||||||
4773 | // Older versions consider 0 an invalid jd. | - | ||||||||||||||||||
4774 | date.jd = (jd != 0 ? jd : QDate::nullJd()); | - | ||||||||||||||||||
4775 | } else { | - | ||||||||||||||||||
4776 | qint64 jd; | - | ||||||||||||||||||
4777 | in >> jd; | - | ||||||||||||||||||
4778 | date.jd = jd; | - | ||||||||||||||||||
4779 | } | - | ||||||||||||||||||
4780 | - | |||||||||||||||||||
4781 | return in; | - | ||||||||||||||||||
4782 | } | - | ||||||||||||||||||
4783 | - | |||||||||||||||||||
4784 | /*! | - | ||||||||||||||||||
4785 | \relates QTime | - | ||||||||||||||||||
4786 | - | |||||||||||||||||||
4787 | Writes \a time to stream \a out. | - | ||||||||||||||||||
4788 | - | |||||||||||||||||||
4789 | \sa {Serializing Qt Data Types} | - | ||||||||||||||||||
4790 | */ | - | ||||||||||||||||||
4791 | - | |||||||||||||||||||
4792 | QDataStream &operator<<(QDataStream &out, const QTime &time) | - | ||||||||||||||||||
4793 | { | - | ||||||||||||||||||
4794 | if (out.version() >= QDataStream::Qt_4_0) { | - | ||||||||||||||||||
4795 | return out << quint32(time.mds); | - | ||||||||||||||||||
4796 | } else { | - | ||||||||||||||||||
4797 | // Qt3 had no support for reading -1, QTime() was valid and serialized as 0 | - | ||||||||||||||||||
4798 | return out << quint32(time.isNull() ? 0 : time.mds); | - | ||||||||||||||||||
4799 | } | - | ||||||||||||||||||
4800 | } | - | ||||||||||||||||||
4801 | - | |||||||||||||||||||
4802 | /*! | - | ||||||||||||||||||
4803 | \relates QTime | - | ||||||||||||||||||
4804 | - | |||||||||||||||||||
4805 | Reads a time from stream \a in into the given \a time. | - | ||||||||||||||||||
4806 | - | |||||||||||||||||||
4807 | \sa {Serializing Qt Data Types} | - | ||||||||||||||||||
4808 | */ | - | ||||||||||||||||||
4809 | - | |||||||||||||||||||
4810 | QDataStream &operator>>(QDataStream &in, QTime &time) | - | ||||||||||||||||||
4811 | { | - | ||||||||||||||||||
4812 | quint32 ds; | - | ||||||||||||||||||
4813 | in >> ds; | - | ||||||||||||||||||
4814 | if (in.version() >= QDataStream::Qt_4_0) { | - | ||||||||||||||||||
4815 | time.mds = int(ds); | - | ||||||||||||||||||
4816 | } else { | - | ||||||||||||||||||
4817 | // Qt3 would write 0 for a null time | - | ||||||||||||||||||
4818 | time.mds = (ds == 0) ? QTime::NullTime : int(ds); | - | ||||||||||||||||||
4819 | } | - | ||||||||||||||||||
4820 | return in; | - | ||||||||||||||||||
4821 | } | - | ||||||||||||||||||
4822 | - | |||||||||||||||||||
4823 | /*! | - | ||||||||||||||||||
4824 | \relates QDateTime | - | ||||||||||||||||||
4825 | - | |||||||||||||||||||
4826 | Writes \a dateTime to the \a out stream. | - | ||||||||||||||||||
4827 | - | |||||||||||||||||||
4828 | \sa {Serializing Qt Data Types} | - | ||||||||||||||||||
4829 | */ | - | ||||||||||||||||||
4830 | QDataStream &operator<<(QDataStream &out, const QDateTime &dateTime) | - | ||||||||||||||||||
4831 | { | - | ||||||||||||||||||
4832 | QPair<QDate, QTime> dateAndTime; | - | ||||||||||||||||||
4833 | - | |||||||||||||||||||
4834 | if (out.version() >= QDataStream::Qt_5_2) { | - | ||||||||||||||||||
4835 | - | |||||||||||||||||||
4836 | // In 5.2 we switched to using Qt::TimeSpec and added offset support | - | ||||||||||||||||||
4837 | dateAndTime = dateTime.d->getDateTime(); | - | ||||||||||||||||||
4838 | out << dateAndTime << qint8(dateTime.timeSpec()); | - | ||||||||||||||||||
4839 | if (dateTime.timeSpec() == Qt::OffsetFromUTC) | - | ||||||||||||||||||
4840 | out << qint32(dateTime.offsetFromUtc()); | - | ||||||||||||||||||
4841 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
4842 | else if (dateTime.timeSpec() == Qt::TimeZone) | - | ||||||||||||||||||
4843 | out << dateTime.timeZone(); | - | ||||||||||||||||||
4844 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
4845 | - | |||||||||||||||||||
4846 | } else if (out.version() == QDataStream::Qt_5_0) { | - | ||||||||||||||||||
4847 | - | |||||||||||||||||||
4848 | // In Qt 5.0 we incorrectly serialised all datetimes as UTC. | - | ||||||||||||||||||
4849 | // This approach is wrong and should not be used again; it breaks | - | ||||||||||||||||||
4850 | // the guarantee that a deserialised local datetime is the same time | - | ||||||||||||||||||
4851 | // of day, regardless of which timezone it was serialised in. | - | ||||||||||||||||||
4852 | dateAndTime = (dateTime.isValid() ? dateTime.toUTC() : dateTime).d->getDateTime(); | - | ||||||||||||||||||
4853 | out << dateAndTime << qint8(dateTime.timeSpec()); | - | ||||||||||||||||||
4854 | - | |||||||||||||||||||
4855 | } else if (out.version() >= QDataStream::Qt_4_0) { | - | ||||||||||||||||||
4856 | - | |||||||||||||||||||
4857 | // From 4.0 to 5.1 (except 5.0) we used QDateTimePrivate::Spec | - | ||||||||||||||||||
4858 | dateAndTime = dateTime.d->getDateTime(); | - | ||||||||||||||||||
4859 | out << dateAndTime; | - | ||||||||||||||||||
4860 | switch (dateTime.timeSpec()) { | - | ||||||||||||||||||
4861 | case Qt::UTC: | - | ||||||||||||||||||
4862 | out << (qint8)QDateTimePrivate::UTC; | - | ||||||||||||||||||
4863 | break; | - | ||||||||||||||||||
4864 | case Qt::OffsetFromUTC: | - | ||||||||||||||||||
4865 | out << (qint8)QDateTimePrivate::OffsetFromUTC; | - | ||||||||||||||||||
4866 | break; | - | ||||||||||||||||||
4867 | case Qt::TimeZone: | - | ||||||||||||||||||
4868 | out << (qint8)QDateTimePrivate::TimeZone; | - | ||||||||||||||||||
4869 | break; | - | ||||||||||||||||||
4870 | case Qt::LocalTime: | - | ||||||||||||||||||
4871 | out << (qint8)QDateTimePrivate::LocalUnknown; | - | ||||||||||||||||||
4872 | break; | - | ||||||||||||||||||
4873 | } | - | ||||||||||||||||||
4874 | - | |||||||||||||||||||
4875 | } else { // version < QDataStream::Qt_4_0 | - | ||||||||||||||||||
4876 | - | |||||||||||||||||||
4877 | // Before 4.0 there was no TimeSpec, only Qt::LocalTime was supported | - | ||||||||||||||||||
4878 | dateAndTime = dateTime.d->getDateTime(); | - | ||||||||||||||||||
4879 | out << dateAndTime; | - | ||||||||||||||||||
4880 | - | |||||||||||||||||||
4881 | } | - | ||||||||||||||||||
4882 | - | |||||||||||||||||||
4883 | return out; | - | ||||||||||||||||||
4884 | } | - | ||||||||||||||||||
4885 | - | |||||||||||||||||||
4886 | /*! | - | ||||||||||||||||||
4887 | \relates QDateTime | - | ||||||||||||||||||
4888 | - | |||||||||||||||||||
4889 | Reads a datetime from the stream \a in into \a dateTime. | - | ||||||||||||||||||
4890 | - | |||||||||||||||||||
4891 | \sa {Serializing Qt Data Types} | - | ||||||||||||||||||
4892 | */ | - | ||||||||||||||||||
4893 | - | |||||||||||||||||||
4894 | QDataStream &operator>>(QDataStream &in, QDateTime &dateTime) | - | ||||||||||||||||||
4895 | { | - | ||||||||||||||||||
4896 | QDate dt; | - | ||||||||||||||||||
4897 | QTime tm; | - | ||||||||||||||||||
4898 | qint8 ts = 0; | - | ||||||||||||||||||
4899 | Qt::TimeSpec spec = Qt::LocalTime; | - | ||||||||||||||||||
4900 | qint32 offset = 0; | - | ||||||||||||||||||
4901 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
4902 | QTimeZone tz; | - | ||||||||||||||||||
4903 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
4904 | - | |||||||||||||||||||
4905 | if (in.version() >= QDataStream::Qt_5_2) { | - | ||||||||||||||||||
4906 | - | |||||||||||||||||||
4907 | // In 5.2 we switched to using Qt::TimeSpec and added offset support | - | ||||||||||||||||||
4908 | in >> dt >> tm >> ts; | - | ||||||||||||||||||
4909 | spec = static_cast<Qt::TimeSpec>(ts); | - | ||||||||||||||||||
4910 | if (spec == Qt::OffsetFromUTC) { | - | ||||||||||||||||||
4911 | in >> offset; | - | ||||||||||||||||||
4912 | dateTime = QDateTime(dt, tm, spec, offset); | - | ||||||||||||||||||
4913 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
4914 | } else if (spec == Qt::TimeZone) { | - | ||||||||||||||||||
4915 | in >> tz; | - | ||||||||||||||||||
4916 | dateTime = QDateTime(dt, tm, tz); | - | ||||||||||||||||||
4917 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
4918 | } else { | - | ||||||||||||||||||
4919 | dateTime = QDateTime(dt, tm, spec); | - | ||||||||||||||||||
4920 | } | - | ||||||||||||||||||
4921 | - | |||||||||||||||||||
4922 | } else if (in.version() == QDataStream::Qt_5_0) { | - | ||||||||||||||||||
4923 | - | |||||||||||||||||||
4924 | // In Qt 5.0 we incorrectly serialised all datetimes as UTC | - | ||||||||||||||||||
4925 | in >> dt >> tm >> ts; | - | ||||||||||||||||||
4926 | spec = static_cast<Qt::TimeSpec>(ts); | - | ||||||||||||||||||
4927 | dateTime = QDateTime(dt, tm, Qt::UTC); | - | ||||||||||||||||||
4928 | dateTime = dateTime.toTimeSpec(spec); | - | ||||||||||||||||||
4929 | - | |||||||||||||||||||
4930 | } else if (in.version() >= QDataStream::Qt_4_0) { | - | ||||||||||||||||||
4931 | - | |||||||||||||||||||
4932 | // From 4.0 to 5.1 (except 5.0) we used QDateTimePrivate::Spec | - | ||||||||||||||||||
4933 | in >> dt >> tm >> ts; | - | ||||||||||||||||||
4934 | switch ((QDateTimePrivate::Spec)ts) { | - | ||||||||||||||||||
4935 | case QDateTimePrivate::UTC: | - | ||||||||||||||||||
4936 | spec = Qt::UTC; | - | ||||||||||||||||||
4937 | break; | - | ||||||||||||||||||
4938 | case QDateTimePrivate::OffsetFromUTC: | - | ||||||||||||||||||
4939 | spec = Qt::OffsetFromUTC; | - | ||||||||||||||||||
4940 | break; | - | ||||||||||||||||||
4941 | case QDateTimePrivate::TimeZone: | - | ||||||||||||||||||
4942 | spec = Qt::TimeZone; | - | ||||||||||||||||||
4943 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
4944 | // FIXME: need to use a different constructor ! | - | ||||||||||||||||||
4945 | #endif | - | ||||||||||||||||||
4946 | break; | - | ||||||||||||||||||
4947 | case QDateTimePrivate::LocalUnknown: | - | ||||||||||||||||||
4948 | case QDateTimePrivate::LocalStandard: | - | ||||||||||||||||||
4949 | case QDateTimePrivate::LocalDST: | - | ||||||||||||||||||
4950 | spec = Qt::LocalTime; | - | ||||||||||||||||||
4951 | break; | - | ||||||||||||||||||
4952 | } | - | ||||||||||||||||||
4953 | dateTime = QDateTime(dt, tm, spec, offset); | - | ||||||||||||||||||
4954 | - | |||||||||||||||||||
4955 | } else { // version < QDataStream::Qt_4_0 | - | ||||||||||||||||||
4956 | - | |||||||||||||||||||
4957 | // Before 4.0 there was no TimeSpec, only Qt::LocalTime was supported | - | ||||||||||||||||||
4958 | in >> dt >> tm; | - | ||||||||||||||||||
4959 | dateTime = QDateTime(dt, tm, spec, offset); | - | ||||||||||||||||||
4960 | - | |||||||||||||||||||
4961 | } | - | ||||||||||||||||||
4962 | - | |||||||||||||||||||
4963 | return in; | - | ||||||||||||||||||
4964 | } | - | ||||||||||||||||||
4965 | #endif // QT_NO_DATASTREAM | - | ||||||||||||||||||
4966 | - | |||||||||||||||||||
4967 | /***************************************************************************** | - | ||||||||||||||||||
4968 | Date / Time Debug Streams | - | ||||||||||||||||||
4969 | *****************************************************************************/ | - | ||||||||||||||||||
4970 | - | |||||||||||||||||||
4971 | #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_NO_DATESTRING) | - | ||||||||||||||||||
4972 | QDebug operator<<(QDebug dbg, const QDate &date) | - | ||||||||||||||||||
4973 | { | - | ||||||||||||||||||
4974 | QDebugStateSaver saver(dbg); | - | ||||||||||||||||||
4975 | dbg.nospace() << "QDate(" << date.toString(Qt::ISODate) << ')'; | - | ||||||||||||||||||
4976 | return dbg; | - | ||||||||||||||||||
4977 | } | - | ||||||||||||||||||
4978 | - | |||||||||||||||||||
4979 | QDebug operator<<(QDebug dbg, const QTime &time) | - | ||||||||||||||||||
4980 | { | - | ||||||||||||||||||
4981 | QDebugStateSaver saver(dbg); | - | ||||||||||||||||||
4982 | dbg.nospace() << "QTime(" << time.toString(QStringLiteral("HH:mm:ss.zzz")) << ')'; | - | ||||||||||||||||||
4983 | return dbg; | - | ||||||||||||||||||
4984 | } | - | ||||||||||||||||||
4985 | - | |||||||||||||||||||
4986 | QDebug operator<<(QDebug dbg, const QDateTime &date) | - | ||||||||||||||||||
4987 | { | - | ||||||||||||||||||
4988 | QDebugStateSaver saver(dbg); | - | ||||||||||||||||||
4989 | const Qt::TimeSpec ts = date.timeSpec(); | - | ||||||||||||||||||
4990 | dbg.nospace() << "QDateTime("; | - | ||||||||||||||||||
4991 | dbg.noquote() << date.toString(QStringLiteral("yyyy-MM-dd HH:mm:ss.zzz t")) | - | ||||||||||||||||||
4992 | << ' ' << ts; | - | ||||||||||||||||||
4993 | switch (ts) { | - | ||||||||||||||||||
4994 | case Qt::UTC: | - | ||||||||||||||||||
4995 | break; | - | ||||||||||||||||||
4996 | case Qt::OffsetFromUTC: | - | ||||||||||||||||||
4997 | dbg << ' ' << date.offsetFromUtc() << 's'; | - | ||||||||||||||||||
4998 | break; | - | ||||||||||||||||||
4999 | case Qt::TimeZone: | - | ||||||||||||||||||
5000 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||||||||
5001 | dbg << ' ' << date.timeZone().id(); | - | ||||||||||||||||||
5002 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
5003 | break; | - | ||||||||||||||||||
5004 | case Qt::LocalTime: | - | ||||||||||||||||||
5005 | break; | - | ||||||||||||||||||
5006 | } | - | ||||||||||||||||||
5007 | return dbg << ')'; | - | ||||||||||||||||||
5008 | } | - | ||||||||||||||||||
5009 | #endif | - | ||||||||||||||||||
5010 | - | |||||||||||||||||||
5011 | /*! \fn uint qHash(const QDateTime &key, uint seed = 0) | - | ||||||||||||||||||
5012 | \relates QHash | - | ||||||||||||||||||
5013 | \since 5.0 | - | ||||||||||||||||||
5014 | - | |||||||||||||||||||
5015 | Returns the hash value for the \a key, using \a seed to seed the calculation. | - | ||||||||||||||||||
5016 | */ | - | ||||||||||||||||||
5017 | uint qHash(const QDateTime &key, uint seed) | - | ||||||||||||||||||
5018 | { | - | ||||||||||||||||||
5019 | // Use to toMSecsSinceEpoch instead of individual qHash functions for | - | ||||||||||||||||||
5020 | // QDate/QTime/spec/offset because QDateTime::operator== converts both arguments | - | ||||||||||||||||||
5021 | // to the same timezone. If we don't, qHash would return different hashes for | - | ||||||||||||||||||
5022 | // two QDateTimes that are equivalent once converted to the same timezone. | - | ||||||||||||||||||
5023 | return qHash(key.toMSecsSinceEpoch(), seed); | - | ||||||||||||||||||
5024 | } | - | ||||||||||||||||||
5025 | - | |||||||||||||||||||
5026 | /*! \fn uint qHash(const QDate &key, uint seed = 0) | - | ||||||||||||||||||
5027 | \relates QHash | - | ||||||||||||||||||
5028 | \since 5.0 | - | ||||||||||||||||||
5029 | - | |||||||||||||||||||
5030 | Returns the hash value for the \a key, using \a seed to seed the calculation. | - | ||||||||||||||||||
5031 | */ | - | ||||||||||||||||||
5032 | uint qHash(const QDate &key, uint seed) Q_DECL_NOTHROW | - | ||||||||||||||||||
5033 | { | - | ||||||||||||||||||
5034 | return qHash(key.toJulianDay(), seed); executed 6 times by 1 test: return qHash(key.toJulianDay(), seed); Executed by:
| 6 | ||||||||||||||||||
5035 | } | - | ||||||||||||||||||
5036 | - | |||||||||||||||||||
5037 | /*! \fn uint qHash(const QTime &key, uint seed = 0) | - | ||||||||||||||||||
5038 | \relates QHash | - | ||||||||||||||||||
5039 | \since 5.0 | - | ||||||||||||||||||
5040 | - | |||||||||||||||||||
5041 | Returns the hash value for the \a key, using \a seed to seed the calculation. | - | ||||||||||||||||||
5042 | */ | - | ||||||||||||||||||
5043 | uint qHash(const QTime &key, uint seed) Q_DECL_NOTHROW | - | ||||||||||||||||||
5044 | { | - | ||||||||||||||||||
5045 | return qHash(key.msecsSinceStartOfDay(), seed); executed 4 times by 1 test: return qHash(key.msecsSinceStartOfDay(), seed); Executed by:
| 4 | ||||||||||||||||||
5046 | } | - | ||||||||||||||||||
5047 | - | |||||||||||||||||||
5048 | QT_END_NAMESPACE | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |