tools/qdatetime.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/****************************************************************************-
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/****************************************************************************
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtCore module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
9** Commercial License Usage -
10** Licensees holding valid commercial Qt licenses may use this file in -
11** accordance with the commercial license agreement provided with the -
12** Software or, alternatively, in accordance with the terms contained in -
13** a written agreement between you and Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/contact-us. -
16** -
17** GNU Lesser General Public License Usage -
18** Alternatively, this file may be used under the terms of the GNU Lesser -
19** General Public License version 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#include "qplatformdefs.h" -
43#include "private/qdatetime_p.h" -
44 -
45#include "qdatastream.h" -
46#include "qset.h" -
47#include "qlocale.h" -
48#include "qdatetime.h" -
49#include "qregexp.h" -
50#include "qdebug.h" -
51#ifndef Q_OS_WIN -
52#include <locale.h> -
53#endif -
54 -
55#include <cmath> -
56#include <time.h> -
57#ifdef Q_OS_WIN -
58# include <qt_windows.h> -
59# ifdef Q_OS_WINCE -
60# include "qfunctions_wince.h" -
61# endif -
62#endif -
63 -
64//#define QDATETIMEPARSER_DEBUG -
65#if defined (QDATETIMEPARSER_DEBUG) && !defined(QT_NO_DEBUG_STREAM) -
66# define QDTPDEBUG qDebug() << QString("%1:%2").arg(__FILE__).arg(__LINE__) -
67# define QDTPDEBUGN qDebug -
68#else -
69# define QDTPDEBUG if (false) qDebug() -
70# define QDTPDEBUGN if (false) qDebug -
71#endif -
72 -
73#if defined(Q_OS_MAC) -
74#include <private/qcore_mac_p.h> -
75#endif -
76 -
77QT_BEGIN_NAMESPACE -
78 -
79enum { -
80 SECS_PER_DAY = 86400, -
81 MSECS_PER_DAY = 86400000, -
82 SECS_PER_HOUR = 3600, -
83 MSECS_PER_HOUR = 3600000, -
84 SECS_PER_MIN = 60, -
85 MSECS_PER_MIN = 60000, -
86 JULIAN_DAY_FOR_EPOCH = 2440588 // result of julianDayFromDate(1970, 1, 1) -
87}; -
88 -
89static inline QDate fixedDate(int y, int m, int d) -
90{ -
91 QDate result(y, m, 1); -
92 result.setDate(y, m, qMin(d, result.daysInMonth())); -
93 return result; -
94} -
95 -
96static inline qint64 floordiv(qint64 a, qint64 b) -
97{ -
98 return (a - (a < 0 ? b-1 : 0)) / b; -
99} -
100 -
101static inline qint64 floordiv(qint64 a, int b) -
102{ -
103 return (a - (a < 0 ? b-1 : 0)) / b; -
104} -
105 -
106static inline int floordiv(int a, int b) -
107{ -
108 return (a - (a < 0 ? b-1 : 0)) / b; -
109} -
110 -
111static inline qint64 julianDayFromDate(int year, int month, int day) -
112{ -
113 // Adjust for no year 0 -
114 if (year < 0) -
115 ++year; -
116 -
117/* -
118 * Math from The Calendar FAQ at http://www.tondering.dk/claus/cal/julperiod.php -
119 * This formula is correct for all julian days, when using mathematical integer -
120 * division (round to negative infinity), not c++11 integer division (round to zero) -
121 */ -
122 int a = floordiv(14 - month, 12); -
123 qint64 y = (qint64)year + 4800 - a; -
124 int m = month + 12 * a - 3; -
125 return day + floordiv(153 * m + 2, 5) + 365 * y + floordiv(y, 4) - floordiv(y, 100) + floordiv(y, 400) - 32045; -
126} -
127 -
128static void getDateFromJulianDay(qint64 julianDay, int *yearp, int *monthp, int *dayp) -
129{ -
130/* -
131 * Math from The Calendar FAQ at http://www.tondering.dk/claus/cal/julperiod.php -
132 * This formula is correct for all julian days, when using mathematical integer -
133 * division (round to negative infinity), not c++11 integer division (round to zero) -
134 */ -
135 qint64 a = julianDay + 32044; -
136 qint64 b = floordiv(4 * a + 3, 146097); -
137 int c = a - floordiv(146097 * b, 4); -
138 -
139 int d = floordiv(4 * c + 3, 1461); -
140 int e = c - floordiv(1461 * d, 4); -
141 int m = floordiv(5 * e + 2, 153); -
142 -
143 int day = e - floordiv(153 * m + 2, 5) + 1; -
144 int month = m + 3 - 12 * floordiv(m, 10); -
145 int year = 100 * b + d - 4800 + floordiv(m, 10); -
146 -
147 // Adjust for no year 0 -
148 if (year <= 0) -
149 --year ; -
150 -
151 if (yearp) -
152 *yearp = year; -
153 if (monthp) -
154 *monthp = month; -
155 if (dayp) -
156 *dayp = day; -
157} -
158 -
159 -
160static const char monthDays[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; -
161 -
162#ifndef QT_NO_TEXTDATE -
163static const char * const qt_shortMonthNames[] = { -
164 "Jan", "Feb", "Mar", "Apr", "May", "Jun", -
165 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; -
166#endif -
167#ifndef QT_NO_DATESTRING -
168static QString fmtDateTime(const QString& f, const QTime* dt = 0, const QDate* dd = 0); -
169#endif -
170 -
171/***************************************************************************** -
172 QDate member functions -
173 *****************************************************************************/ -
174 -
175/*! -
176 \since 4.5 -
177 -
178 \enum QDate::MonthNameType -
179 -
180 This enum describes the types of the string representation used -
181 for the month name. -
182 -
183 \value DateFormat This type of name can be used for date-to-string formatting. -
184 \value StandaloneFormat This type is used when you need to enumerate months or weekdays. -
185 Usually standalone names are represented in singular forms with -
186 capitalized first letter. -
187*/ -
188 -
189/*! -
190 \class QDate -
191 \inmodule QtCore -
192 \reentrant -
193 \brief The QDate class provides date functions. -
194 -
195 -
196 A QDate object contains a calendar date, i.e. year, month, and day -
197 numbers, in the Gregorian calendar. It can read the current date -
198 from the system clock. It provides functions for comparing dates, -
199 and for manipulating dates. For example, it is possible to add -
200 and subtract days, months, and years to dates. -
201 -
202 A QDate object is typically created by giving the year, -
203 month, and day numbers explicitly. Note that QDate interprets two -
204 digit years as is, i.e., years 0 - 99. A QDate can also be -
205 constructed with the static function currentDate(), which creates -
206 a QDate object containing the system clock's date. An explicit -
207 date can also be set using setDate(). The fromString() function -
208 returns a QDate given a string and a date format which is used to -
209 interpret the date within the string. -
210 -
211 The year(), month(), and day() functions provide access to the -
212 year, month, and day numbers. Also, dayOfWeek() and dayOfYear() -
213 functions are provided. The same information is provided in -
214 textual format by the toString(), shortDayName(), longDayName(), -
215 shortMonthName(), and longMonthName() functions. -
216 -
217 QDate provides a full set of operators to compare two QDate -
218 objects where smaller means earlier, and larger means later. -
219 -
220 You can increment (or decrement) a date by a given number of days -
221 using addDays(). Similarly you can use addMonths() and addYears(). -
222 The daysTo() function returns the number of days between two -
223 dates. -
224 -
225 The daysInMonth() and daysInYear() functions return how many days -
226 there are in this date's month and year, respectively. The -
227 isLeapYear() function indicates whether a date is in a leap year. -
228 -
229 \section1 -
230 -
231 \section2 No Year 0 -
232 -
233 There is no year 0. Dates in that year are considered invalid. The -
234 year -1 is the year "1 before Christ" or "1 before current era." -
235 The day before 1 January 1 CE is 31 December 1 BCE. -
236 -
237 \section2 Range of Valid Dates -
238 -
239 Dates are stored internally as a Julian Day number, an integer count of -
240 every day in a contiguous range, with 24 November 4714 BCE in the Gregorian -
241 calendar being Julian Day 0 (1 January 4713 BCE in the Julian calendar). -
242 As well as being an efficient and accurate way of storing an absolute date, -
243 it is suitable for converting a Date into other calendar systems such as -
244 Hebrew, Islamic or Chinese. The Julian Day number can be obtained using -
245 QDate::toJulianDay() and can be set using QDate::fromJulianDay(). -
246 -
247 The range of dates able to be stored by QDate as a Julian Day number is -
248 for technical reasons limited to between -784350574879 and 784354017364, -
249 which means from before 2 billion BCE to after 2 billion CE. -
250 -
251 \sa QTime, QDateTime, QDateEdit, QDateTimeEdit, QCalendarWidget -
252*/ -
253 -
254/*! -
255 \fn QDate::QDate() -
256 -
257 Constructs a null date. Null dates are invalid. -
258 -
259 \sa isNull(), isValid() -
260*/ -
261 -
262/*!-
Constructs a date with year \a y, month \a m and day \a d.
If the specified date is invalid, the date is not set and
isValid() returns false.
\warning Years 0 to 99 are interpreted as is, i.e., years
0-99.
\sa isValid()
*/*!
263 Constructs a date with year \a y, month \a m and day \a d. -
264 -
265 If the specified date is invalid, the date is not set and -
266 isValid() returns false. -
267 -
268 \warning Years 1 to 99 are interpreted as is. Year 0 is invalid. -
269 -
270 \sa isValid() -
271*/ -
272 -
273QDate::QDate(int y, int m, int d) -
274{ -
275 setDate(y, m, d); -
276} -
277 -
278 -
279/*! -
280 \fn bool QDate::isNull() const -
281 -
282 Returns true if the date is null; otherwise returns false. A null -
283 date is invalid. -
284 -
285 \note The behavior of this function is equivalent to isValid(). -
286 -
287 \sa isValid() -
288*/ -
289 -
290 -
291/*! -
292 \fn bool QDate::isValid() const -
293 -
294 Returns true if this date is valid; otherwise returns false. -
295 -
296 \sa isNull() -
297*/ -
298 -
299 -
300/*! -
301 Returns the year of this date. Negative numbers indicate years -
302 before 1 CE, such that year -44 is 44 BCE. -
303 -
304 Returns 0 if the date is invalid. -
305 -
306 \sa month(), day() -
307*/ -
308 -
309int QDate::year() const -
310{ -
311 if (isNull()) -
312 return 0; -
313 -
314 int y; -
315 getDateFromJulianDay(jd, &y, 0, 0); -
316 return y; -
317} -
318 -
319/*! -
320 Returns the number corresponding to the month of this date, using -
321 the following convention: -
322 -
323 \list -
324 \li 1 = "January" -
325 \li 2 = "February" -
326 \li 3 = "March" -
327 \li 4 = "April" -
328 \li 5 = "May" -
329 \li 6 = "June" -
330 \li 7 = "July" -
331 \li 8 = "August" -
332 \li 9 = "September" -
333 \li 10 = "October" -
334 \li 11 = "November" -
335 \li 12 = "December" -
336 \endlist -
337 -
338 Returns 0 if the date is invalid. -
339 -
340 \sa year(), day() -
341*/ -
342 -
343int QDate::month() const -
344{ -
345 if (isNull()) -
346 return 0; -
347 -
348 int m; -
349 getDateFromJulianDay(jd, 0, &m, 0); -
350 return m; -
351} -
352 -
353/*! -
354 Returns the day of the month (1 to 31) of this date. -
355 -
356 Returns 0 if the date is invalid. -
357 -
358 \sa year(), month(), dayOfWeek() -
359*/ -
360 -
361int QDate::day() const -
362{ -
363 if (isNull()) -
364 return 0; -
365 -
366 int d; -
367 getDateFromJulianDay(jd, 0, 0, &d); -
368 return d; -
369} -
370 -
371/*! -
372 Returns the weekday (1 = Monday to 7 = Sunday) for this date. -
373 -
374 Returns 0 if the date is invalid. -
375 -
376 \sa day(), dayOfYear(), Qt::DayOfWeek -
377*/ -
378 -
379int QDate::dayOfWeek() const -
380{ -
381 if (isNull()) -
382 return 0; -
383 -
384 if (jd >= 0) -
385 return (jd % 7) + 1; -
386 else -
387 return ((jd + 1) % 7) + 7; -
388} -
389 -
390/*! -
391 Returns the day of the year (1 to 365 or 366 on leap years) for -
392 this date. -
393 -
394 Returns 0 if the date is invalid. -
395 -
396 \sa day(), dayOfWeek() -
397*/ -
398 -
399int QDate::dayOfYear() const -
400{ -
401 if (isNull()) -
402 return 0; -
403 -
404 return jd - julianDayFromDate(year(), 1, 1) + 1; -
405} -
406 -
407/*! -
408 Returns the number of days in the month (28 to 31) for this date. -
409 -
410 Returns 0 if the date is invalid. -
411 -
412 \sa day(), daysInYear() -
413*/ -
414 -
415int QDate::daysInMonth() const -
416{ -
417 if (isNull()) -
418 return 0; -
419 -
420 int y, m; -
421 getDateFromJulianDay(jd, &y, &m, 0); -
422 if (m == 2 && isLeapYear(y)) -
423 return 29; -
424 else -
425 return monthDays[m]; -
426} -
427 -
428/*! -
429 Returns the number of days in the year (365 or 366) for this date. -
430 -
431 Returns 0 if the date is invalid. -
432 -
433 \sa day(), daysInMonth() -
434*/ -
435 -
436int QDate::daysInYear() const -
437{ -
438 if (isNull()) -
439 return 0; -
440 -
441 int y; -
442 getDateFromJulianDay(jd, &y, 0, 0); -
443 return isLeapYear(y) ? 366 : 365; -
444} -
445 -
446/*! -
447 Returns the week number (1 to 53), and stores the year in -
448 *\a{yearNumber} unless \a yearNumber is null (the default). -
449 -
450 Returns 0 if the date is invalid. -
451 -
452 In accordance with ISO 8601, weeks start on Monday and the first -
453 Thursday of a year is always in week 1 of that year. Most years -
454 have 52 weeks, but some have 53. -
455 -
456 *\a{yearNumber} is not always the same as year(). For example, 1 -
457 January 2000 has week number 52 in the year 1999, and 31 December -
458 2002 has week number 1 in the year 2003. -
459 -
460 \legalese -
461 Copyright (c) 1989 The Regents of the University of California. -
462 All rights reserved. -
463 -
464 Redistribution and use in source and binary forms are permitted -
465 provided that the above copyright notice and this paragraph are -
466 duplicated in all such forms and that any documentation, -
467 advertising materials, and other materials related to such -
468 distribution and use acknowledge that the software was developed -
469 by the University of California, Berkeley. The name of the -
470 University may not be used to endorse or promote products derived -
471 from this software without specific prior written permission. -
472 THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR -
473 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED -
474 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. -
475 -
476 \sa isValid() -
477*/ -
478 -
479int QDate::weekNumber(int *yearNumber) const -
480{ -
481 if (!isValid()) -
482 return 0; -
483 -
484 int year = QDate::year(); -
485 int yday = dayOfYear() - 1; -
486 int wday = dayOfWeek(); -
487 if (wday == 7) -
488 wday = 0; -
489 int w; -
490 -
491 for (;;) { -
492 int len; -
493 int bot; -
494 int top; -
495 -
496 len = isLeapYear(year) ? 366 : 365; -
497 /* -
498 ** What yday (-3 ... 3) does -
499 ** the ISO year begin on? -
500 */ -
501 bot = ((yday + 11 - wday) % 7) - 3; -
502 /* -
503 ** What yday does the NEXT -
504 ** ISO year begin on? -
505 */ -
506 top = bot - (len % 7); -
507 if (top < -3) -
508 top += 7; -
509 top += len; -
510 if (yday >= top) { -
511 ++year; -
512 w = 1; -
513 break; -
514 } -
515 if (yday >= bot) { -
516 w = 1 + ((yday - bot) / 7); -
517 break; -
518 } -
519 --year; -
520 yday += isLeapYear(year) ? 366 : 365; -
521 } -
522 if (yearNumber != 0) -
523 *yearNumber = year; -
524 return w; -
525} -
526 -
527#ifndef QT_NO_TEXTDATE -
528/*! -
529 \since 4.5 -
530 -
531 Returns the short name of the \a month for the representation specified -
532 by \a type. -
533 -
534 The months are enumerated using the following convention: -
535 -
536 \list -
537 \li 1 = "Jan" -
538 \li 2 = "Feb" -
539 \li 3 = "Mar" -
540 \li 4 = "Apr" -
541 \li 5 = "May" -
542 \li 6 = "Jun" -
543 \li 7 = "Jul" -
544 \li 8 = "Aug" -
545 \li 9 = "Sep" -
546 \li 10 = "Oct" -
547 \li 11 = "Nov" -
548 \li 12 = "Dec" -
549 \endlist -
550 -
551 The month names will be localized according to the system's locale -
552 settings. -
553 -
554 Returns an empty string if the date is invalid. -
555 -
556 \sa toString(), longMonthName(), shortDayName(), longDayName() -
557*/ -
558 -
559QString QDate::shortMonthName(int month, QDate::MonthNameType type) -
560{ -
561 if (month < 1 || month > 12) -
562 return QString(); -
563 -
564 switch (type) { -
565 case QDate::DateFormat: -
566 return QLocale::system().monthName(month, QLocale::ShortFormat); -
567 case QDate::StandaloneFormat: -
568 return QLocale::system().standaloneMonthName(month, QLocale::ShortFormat); -
569 default: -
570 break; -
571 } -
572 return QString(); -
573} -
574 -
575/*! -
576 \since 4.5 -
577 -
578 Returns the long name of the \a month for the representation specified -
579 by \a type. -
580 -
581 The months are enumerated using the following convention: -
582 -
583 \list -
584 \li 1 = "January" -
585 \li 2 = "February" -
586 \li 3 = "March" -
587 \li 4 = "April" -
588 \li 5 = "May" -
589 \li 6 = "June" -
590 \li 7 = "July" -
591 \li 8 = "August" -
592 \li 9 = "September" -
593 \li 10 = "October" -
594 \li 11 = "November" -
595 \li 12 = "December" -
596 \endlist -
597 -
598 The month names will be localized according to the system's locale -
599 settings. -
600 -
601 Returns an empty string if the date is invalid. -
602 -
603 \sa toString(), shortMonthName(), shortDayName(), longDayName() -
604*/ -
605 -
606QString QDate::longMonthName(int month, MonthNameType type) -
607{ -
608 if (month < 1 || month > 12) -
609 return QString(); -
610 -
611 switch (type) { -
612 case QDate::DateFormat: -
613 return QLocale::system().monthName(month, QLocale::LongFormat); -
614 case QDate::StandaloneFormat: -
615 return QLocale::system().standaloneMonthName(month, QLocale::LongFormat); -
616 default: -
617 break; -
618 } -
619 return QString(); -
620} -
621 -
622/*! -
623 \since 4.5 -
624 -
625 Returns the short name of the \a weekday for the representation specified -
626 by \a type. -
627 -
628 The days are enumerated using the following convention: -
629 -
630 \list -
631 \li 1 = "Mon" -
632 \li 2 = "Tue" -
633 \li 3 = "Wed" -
634 \li 4 = "Thu" -
635 \li 5 = "Fri" -
636 \li 6 = "Sat" -
637 \li 7 = "Sun" -
638 \endlist -
639 -
640 The day names will be localized according to the system's locale -
641 settings. -
642 -
643 Returns an empty string if the date is invalid. -
644 -
645 \sa toString(), shortMonthName(), longMonthName(), longDayName() -
646*/ -
647 -
648QString QDate::shortDayName(int weekday, MonthNameType type) -
649{ -
650 if (weekday < 1 || weekday > 7) -
651 return QString(); -
652 -
653 switch (type) { -
654 case QDate::DateFormat: -
655 return QLocale::system().dayName(weekday, QLocale::ShortFormat); -
656 case QDate::StandaloneFormat: -
657 return QLocale::system().standaloneDayName(weekday, QLocale::ShortFormat); -
658 default: -
659 break; -
660 } -
661 return QString(); -
662} -
663 -
664/*! -
665 \since 4.5 -
666 -
667 Returns the long name of the \a weekday for the representation specified -
668 by \a type. -
669 -
670 The days are enumerated using the following convention: -
671 -
672 \list -
673 \li 1 = "Monday" -
674 \li 2 = "Tuesday" -
675 \li 3 = "Wednesday" -
676 \li 4 = "Thursday" -
677 \li 5 = "Friday" -
678 \li 6 = "Saturday" -
679 \li 7 = "Sunday" -
680 \endlist -
681 -
682 The day names will be localized according to the system's locale -
683 settings. -
684 -
685 Returns an empty string if the date is invalid. -
686 -
687 \sa toString(), shortDayName(), shortMonthName(), longMonthName() -
688*/ -
689 -
690QString QDate::longDayName(int weekday, MonthNameType type) -
691{ -
692 if (weekday < 1 || weekday > 7) -
693 return QString(); -
694 -
695 switch (type) { -
696 case QDate::DateFormat: -
697 return QLocale::system().dayName(weekday, QLocale::LongFormat); -
698 case QDate::StandaloneFormat: -
699 return QLocale::system().standaloneDayName(weekday, QLocale::LongFormat); -
700 default: -
701 break; -
702 } -
703 return QLocale::system().dayName(weekday, QLocale::LongFormat); -
704} -
705#endif //QT_NO_TEXTDATE -
706 -
707#ifndef QT_NO_DATESTRING -
708 -
709/*! -
710 \fn QString QDate::toString(Qt::DateFormat format) const -
711 -
712 \overload -
713 -
714 Returns the date as a string. The \a format parameter determines -
715 the format of the string. -
716 -
717 If the \a format is Qt::TextDate, the string is formatted in -
718 the default way. QDate::shortDayName() and QDate::shortMonthName() -
719 are used to generate the string, so the day and month names will -
720 be localized names. An example of this formatting is -
721 "Sat May 20 1995". -
722 -
723 If the \a format is Qt::ISODate, the string format corresponds -
724 to the ISO 8601 extended specification for representations of -
725 dates and times, taking the form YYYY-MM-DD, where YYYY is the -
726 year, MM is the month of the year (between 01 and 12), and DD is -
727 the day of the month between 01 and 31. -
728 -
729 If the \a format is Qt::SystemLocaleShortDate or -
730 Qt::SystemLocaleLongDate, the string format depends on the locale -
731 settings of the system. Identical to calling -
732 QLocale::system().toString(date, QLocale::ShortFormat) or -
733 QLocale::system().toString(date, QLocale::LongFormat). -
734 -
735 If the \a format is Qt::DefaultLocaleShortDate or -
736 Qt::DefaultLocaleLongDate, the string format depends on the -
737 default application locale. This is the locale set with -
738 QLocale::setDefault(), or the system locale if no default locale -
739 has been set. Identical to calling QLocale().toString(date, -
740 QLocale::ShortFormat) or QLocale().toString(date, -
741 QLocale::LongFormat). -
742 -
743 If the date is invalid, an empty string will be returned. -
744 -
745 \warning The Qt::ISODate format is only valid for years in the -
746 range 0 to 9999. This restriction may apply to locale-aware -
747 formats as well, depending on the locale settings. -
748 -
749 \sa shortDayName(), shortMonthName() -
750*/ -
751QString QDate::toString(Qt::DateFormat f) const -
752{ -
753 if (!isValid()) -
754 return QString(); -
755 int y, m, d; -
756 getDateFromJulianDay(jd, &y, &m, &d); -
757 switch (f) { -
758 case Qt::SystemLocaleDate: -
759 case Qt::SystemLocaleShortDate: -
760 case Qt::SystemLocaleLongDate: -
761 return QLocale::system().toString(*this, f == Qt::SystemLocaleLongDate ? QLocale::LongFormat -
762 : QLocale::ShortFormat); -
763 case Qt::LocaleDate: -
764 case Qt::DefaultLocaleShortDate: -
765 case Qt::DefaultLocaleLongDate: -
766 return QLocale().toString(*this, f == Qt::DefaultLocaleLongDate ? QLocale::LongFormat -
767 : QLocale::ShortFormat); -
768 default: -
769#ifndef QT_NO_TEXTDATE -
770 case Qt::TextDate: -
771 { -
772 return QString::fromLatin1("%0 %1 %2 %3") -
773 .arg(shortDayName(dayOfWeek())) -
774 .arg(shortMonthName(m)) -
775 .arg(d) -
776 .arg(y); -
777 } -
778#endif -
779 case Qt::ISODate: -
780 { -
781 if (year() < 0 || year() > 9999) -
782 return QString(); -
783 QString year(QString::number(y).rightJustified(4, QLatin1Char('0'))); -
784 QString month(QString::number(m).rightJustified(2, QLatin1Char('0'))); -
785 QString day(QString::number(d).rightJustified(2, QLatin1Char('0'))); -
786 return year + QLatin1Char('-') + month + QLatin1Char('-') + day; -
787 } -
788 } -
789} -
790 -
791/*! -
792 Returns the date as a string. The \a format parameter determines -
793 the format of the result string. -
794 -
795 These expressions may be used: -
796 -
797 \table -
798 \header \li Expression \li Output -
799 \row \li d \li the day as number without a leading zero (1 to 31) -
800 \row \li dd \li the day as number with a leading zero (01 to 31) -
801 \row \li ddd -
802 \li the abbreviated localized day name (e.g. 'Mon' to 'Sun'). -
803 Uses QDate::shortDayName(). -
804 \row \li dddd -
805 \li the long localized day name (e.g. 'Monday' to 'Sunday'). -
806 Uses QDate::longDayName(). -
807 \row \li M \li the month as number without a leading zero (1 to 12) -
808 \row \li MM \li the month as number with a leading zero (01 to 12) -
809 \row \li MMM -
810 \li the abbreviated localized month name (e.g. 'Jan' to 'Dec'). -
811 Uses QDate::shortMonthName(). -
812 \row \li MMMM -
813 \li the long localized month name (e.g. 'January' to 'December'). -
814 Uses QDate::longMonthName(). -
815 \row \li yy \li the year as two digit number (00 to 99) -
816 \row \li yyyy \li the year as four digit number. If the year is negative, -
817 a minus sign is prepended in addition. -
818 \endtable -
819 -
820 All other input characters will be ignored. Any sequence of characters that -
821 are enclosed in single quotes will be treated as text and not be used as an -
822 expression. Two consecutive single quotes ("''") are replaced by a singlequote -
823 in the output. Formats without separators (e.g. "ddMM") are currently not supported. -
824 -
825 Example format strings (assuming that the QDate is the 20 July -
826 1969): -
827 -
828 \table -
829 \header \li Format \li Result -
830 \row \li dd.MM.yyyy \li 20.07.1969 -
831 \row \li ddd MMMM d yy \li Sun July 20 69 -
832 \row \li 'The day is' dddd \li The day is Sunday -
833 \endtable -
834 -
835 If the datetime is invalid, an empty string will be returned. -
836 -
837 \warning The Qt::ISODate format is only valid for years in the -
838 range 0 to 9999. This restriction may apply to locale-aware -
839 formats as well, depending on the locale settings. -
840 -
841 \sa QDateTime::toString(), QTime::toString() -
842 -
843*/ -
844QString QDate::toString(const QString& format) const -
845{ -
846 if (year() > 9999) -
847 return QString(); -
848 return fmtDateTime(format, 0, this); -
849} -
850#endif //QT_NO_DATESTRING -
851 -
852/*! -
853 \fn bool QDate::setYMD(int y, int m, int d) -
854 -
855 \deprecated in 5.0, use setDate() instead. -
856 -
857 Sets the date's year \a y, month \a m, and day \a d. -
858 -
859 If \a y is in the range 0 to 99, it is interpreted as 1900 to -
860 1999. -
861 Returns \c false if the date is invalid. -
862 -
863 Use setDate() instead. -
864*/ -
865 -
866/*! -
867 \since 4.2 -
868 -
869 Sets the date's \a year, \a month, and \a day. Returns true if -
870 the date is valid; otherwise returns false. -
871 -
872 If the specified date is invalid, the QDate object is set to be -
873 invalid. -
874 -
875 \sa isValid() -
876*/ -
877bool QDate::setDate(int year, int month, int day) -
878{ -
879 if (isValid(year, month, day)) -
880 jd = julianDayFromDate(year, month, day); -
881 else -
882 jd = nullJd(); -
883 -
884 return isValid(); -
885} -
886 -
887/*! -
888 \since 4.5 -
889 -
890 Extracts the date's year, month, and day, and assigns them to -
891 *\a year, *\a month, and *\a day. The pointers may be null. -
892 -
893 Returns 0 if the date is invalid. -
894 -
895 \sa year(), month(), day(), isValid() -
896*/ -
897void QDate::getDate(int *year, int *month, int *day) -
898{ -
899 if (isValid()) { -
900 getDateFromJulianDay(jd, year, month, day); -
901 } else { -
902 if (year) -
903 *year = 0; -
904 if (month) -
905 *month = 0; -
906 if (day) -
907 *day = 0; -
908 } -
909} -
910 -
911/*! -
912 Returns a QDate object containing a date \a ndays later than the -
913 date of this object (or earlier if \a ndays is negative). -
914 -
915 Returns a null date if the current date is invalid or the new date is -
916 out of range. -
917 -
918 \sa addMonths(), addYears(), daysTo() -
919*/ -
920 -
921QDate QDate::addDays(qint64 ndays) const -
922{ -
923 if (isNull()) -
924 return QDate(); -
925 -
926 // Due to limits on minJd() and maxJd() we know that any overflow -
927 // will be invalid and caught by fromJulianDay(). -
928 return fromJulianDay(jd + ndays); -
929} -
930 -
931/*! -
932 Returns a QDate object containing a date \a nmonths later than the -
933 date of this object (or earlier if \a nmonths is negative). -
934 -
935 \note If the ending day/month combination does not exist in the -
936 resulting month/year, this function will return a date that is the -
937 latest valid date. -
938 -
939 \sa addDays(), addYears() -
940*/ -
941 -
942QDate QDate::addMonths(int nmonths) const -
943{ -
944 if (!isValid()) -
945 return QDate(); -
946 if (!nmonths) -
947 return *this; -
948 -
949 int old_y, y, m, d; -
950 getDateFromJulianDay(jd, &y, &m, &d); -
951 old_y = y; -
952 -
953 bool increasing = nmonths > 0; -
954 -
955 while (nmonths != 0) { -
956 if (nmonths < 0 && nmonths + 12 <= 0) { -
957 y--; -
958 nmonths+=12; -
959 } else if (nmonths < 0) { -
960 m+= nmonths; -
961 nmonths = 0; -
962 if (m <= 0) { -
963 --y; -
964 m += 12; -
965 } -
966 } else if (nmonths - 12 >= 0) { -
967 y++; -
968 nmonths -= 12; -
969 } else if (m == 12) { -
970 y++; -
971 m = 0; -
972 } else { -
973 m += nmonths; -
974 nmonths = 0; -
975 if (m > 12) { -
976 ++y; -
977 m -= 12; -
978 } -
979 } -
980 } -
981 -
982 // was there a sign change? -
983 if ((old_y > 0 && y <= 0) || -
984 (old_y < 0 && y >= 0)) -
985 // yes, adjust the date by +1 or -1 years -
986 y += increasing ? +1 : -1; -
987 -
988 return fixedDate(y, m, d); -
989} -
990 -
991/*! -
992 Returns a QDate object containing a date \a nyears later than the -
993 date of this object (or earlier if \a nyears is negative). -
994 -
995 \note If the ending day/month combination does not exist in the -
996 resulting year (i.e., if the date was Feb 29 and the final year is -
997 not a leap year), this function will return a date that is the -
998 latest valid date (that is, Feb 28). -
999 -
1000 \sa addDays(), addMonths() -
1001*/ -
1002 -
1003QDate QDate::addYears(int nyears) const -
1004{ -
1005 if (!isValid()) -
1006 return QDate(); -
1007 -
1008 int y, m, d; -
1009 getDateFromJulianDay(jd, &y, &m, &d); -
1010 -
1011 int old_y = y; -
1012 y += nyears; -
1013 -
1014 // was there a sign change? -
1015 if ((old_y > 0 && y <= 0) || -
1016 (old_y < 0 && y >= 0)) -
1017 // yes, adjust the date by +1 or -1 years -
1018 y += nyears > 0 ? +1 : -1; -
1019 -
1020 return fixedDate(y, m, d); -
1021} -
1022 -
1023/*! -
1024 Returns the number of days from this date to \a d (which is -
1025 negative if \a d is earlier than this date). -
1026 -
1027 Returns 0 if either date is invalid. -
1028 -
1029 Example: -
1030 \snippet code/src_corelib_tools_qdatetime.cpp 0 -
1031 -
1032 \sa addDays() -
1033*/ -
1034 -
1035qint64 QDate::daysTo(const QDate &d) const -
1036{ -
1037 if (isNull() || d.isNull()) -
1038 return 0; -
1039 -
1040 // Due to limits on minJd() and maxJd() we know this will never overflow -
1041 return d.jd - jd; -
1042} -
1043 -
1044 -
1045/*! -
1046 \fn bool QDate::operator==(const QDate &d) const -
1047 -
1048 Returns true if this date is equal to \a d; otherwise returns -
1049 false. -
1050 -
1051*/ -
1052 -
1053/*! -
1054 \fn bool QDate::operator!=(const QDate &d) const -
1055 -
1056 Returns true if this date is different from \a d; otherwise -
1057 returns false. -
1058*/ -
1059 -
1060/*! -
1061 \fn bool QDate::operator<(const QDate &d) const -
1062 -
1063 Returns true if this date is earlier than \a d; otherwise returns -
1064 false. -
1065*/ -
1066 -
1067/*! -
1068 \fn bool QDate::operator<=(const QDate &d) const -
1069 -
1070 Returns true if this date is earlier than or equal to \a d; -
1071 otherwise returns false. -
1072*/ -
1073 -
1074/*! -
1075 \fn bool QDate::operator>(const QDate &d) const -
1076 -
1077 Returns true if this date is later than \a d; otherwise returns -
1078 false. -
1079*/ -
1080 -
1081/*! -
1082 \fn bool QDate::operator>=(const QDate &d) const -
1083 -
1084 Returns true if this date is later than or equal to \a d; -
1085 otherwise returns false. -
1086*/ -
1087 -
1088/*! -
1089 \fn QDate::currentDate() -
1090 Returns the current date, as reported by the system clock. -
1091 -
1092 \sa QTime::currentTime(), QDateTime::currentDateTime() -
1093*/ -
1094 -
1095#ifndef QT_NO_DATESTRING -
1096/*! -
1097 \fn QDate QDate::fromString(const QString &string, Qt::DateFormat format) -
1098 -
1099 Returns the QDate represented by the \a string, using the -
1100 \a format given, or an invalid date if the string cannot be -
1101 parsed. -
1102 -
1103 Note for Qt::TextDate: It is recommended that you use the -
1104 English short month names (e.g. "Jan"). Although localized month -
1105 names can also be used, they depend on the user's locale settings. -
1106*/ -
1107QDate QDate::fromString(const QString& s, Qt::DateFormat f) -
1108{ -
1109 if (s.isEmpty()) -
1110 return QDate(); -
1111 -
1112 switch (f) { -
1113 case Qt::ISODate: -
1114 { -
1115 int year(s.mid(0, 4).toInt()); -
1116 int month(s.mid(5, 2).toInt()); -
1117 int day(s.mid(8, 2).toInt()); -
1118 if (year && month && day) -
1119 return QDate(year, month, day); -
1120 } -
1121 break; -
1122 case Qt::SystemLocaleDate: -
1123 case Qt::SystemLocaleShortDate: -
1124 case Qt::SystemLocaleLongDate: -
1125 return fromString(s, QLocale::system().dateFormat(f == Qt::SystemLocaleLongDate ? QLocale::LongFormat -
1126 : QLocale::ShortFormat)); -
1127 case Qt::LocaleDate: -
1128 case Qt::DefaultLocaleShortDate: -
1129 case Qt::DefaultLocaleLongDate: -
1130 return fromString(s, QLocale().dateFormat(f == Qt::DefaultLocaleLongDate ? QLocale::LongFormat -
1131 : QLocale::ShortFormat)); -
1132 default: -
1133#ifndef QT_NO_TEXTDATE -
1134 case Qt::TextDate: { -
1135 QStringList parts = s.split(QLatin1Char(' '), QString::SkipEmptyParts); -
1136 -
1137 if (parts.count() != 4) { -
1138 return QDate(); -
1139 } -
1140 -
1141 QString monthName = parts.at(1); -
1142 int month = -1; -
1143 // Assume that English monthnames are the default -
1144 for (int i = 0; i < 12; ++i) { -
1145 if (monthName == QLatin1String(qt_shortMonthNames[i])) { -
1146 month = i + 1; -
1147 break; -
1148 } -
1149 } -
1150 // If English names can't be found, search the localized ones -
1151 if (month == -1) { -
1152 for (int i = 1; i <= 12; ++i) { -
1153 if (monthName == QDate::shortMonthName(i)) { -
1154 month = i; -
1155 break; -
1156 } -
1157 } -
1158 if (month == -1) { -
1159 // Month name matches neither English nor other localised name. -
1160 return QDate(); -
1161 } -
1162 } -
1163 -
1164 bool ok; -
1165 int day = parts.at(2).toInt(&ok); -
1166 if (!ok) { -
1167 return QDate(); -
1168 } -
1169 -
1170 int year = parts.at(3).toInt(&ok); -
1171 if (!ok) { -
1172 return QDate(); -
1173 } -
1174 -
1175 return QDate(year, month, day); -
1176 } -
1177#else -
1178 break; -
1179#endif -
1180 } -
1181 return QDate(); -
1182} -
1183 -
1184/*! -
1185 \fn QDate::fromString(const QString &string, const QString &format) -
1186 -
1187 Returns the QDate represented by the \a string, using the \a -
1188 format given, or an invalid date if the string cannot be parsed. -
1189 -
1190 These expressions may be used for the format: -
1191 -
1192 \table -
1193 \header \li Expression \li Output -
1194 \row \li d \li The day as a number without a leading zero (1 to 31) -
1195 \row \li dd \li The day as a number with a leading zero (01 to 31) -
1196 \row \li ddd -
1197 \li The abbreviated localized day name (e.g. 'Mon' to 'Sun'). -
1198 Uses QDate::shortDayName(). -
1199 \row \li dddd -
1200 \li The long localized day name (e.g. 'Monday' to 'Sunday'). -
1201 Uses QDate::longDayName(). -
1202 \row \li M \li The month as a number without a leading zero (1 to 12) -
1203 \row \li MM \li The month as a number with a leading zero (01 to 12) -
1204 \row \li MMM -
1205 \li The abbreviated localized month name (e.g. 'Jan' to 'Dec'). -
1206 Uses QDate::shortMonthName(). -
1207 \row \li MMMM -
1208 \li The long localized month name (e.g. 'January' to 'December'). -
1209 Uses QDate::longMonthName(). -
1210 \row \li yy \li The year as two digit number (00 to 99) -
1211 \row \li yyyy \li The year as four digit number. If the year is negative, -
1212 a minus sign is prepended in addition. -
1213 \endtable -
1214 -
1215 All other input characters will be treated as text. Any sequence -
1216 of characters that are enclosed in single quotes will also be -
1217 treated as text and will not be used as an expression. For example: -
1218 -
1219 \snippet code/src_corelib_tools_qdatetime.cpp 1 -
1220 -
1221 If the format is not satisfied, an invalid QDate is returned. The -
1222 expressions that don't expect leading zeroes (d, M) will be -
1223 greedy. This means that they will use two digits even if this -
1224 will put them outside the accepted range of values and leaves too -
1225 few digits for other sections. For example, the following format -
1226 string could have meant January 30 but the M will grab two -
1227 digits, resulting in an invalid date: -
1228 -
1229 \snippet code/src_corelib_tools_qdatetime.cpp 2 -
1230 -
1231 For any field that is not represented in the format the following -
1232 defaults are used: -
1233 -
1234 \table -
1235 \header \li Field \li Default value -
1236 \row \li Year \li 1900 -
1237 \row \li Month \li 1 -
1238 \row \li Day \li 1 -
1239 \endtable -
1240 -
1241 The following examples demonstrate the default values: -
1242 -
1243 \snippet code/src_corelib_tools_qdatetime.cpp 3 -
1244 -
1245 \sa QDateTime::fromString(), QTime::fromString(), QDate::toString(), -
1246 QDateTime::toString(), QTime::toString() -
1247*/ -
1248 -
1249QDate QDate::fromString(const QString &string, const QString &format) -
1250{ -
1251 QDate date; -
1252#ifndef QT_BOOTSTRAPPED -
1253 QDateTimeParser dt(QVariant::Date, QDateTimeParser::FromString); -
1254 if (dt.parseFormat(format)) -
1255 dt.fromString(string, &date, 0); -
1256#else -
1257 Q_UNUSED(string); -
1258 Q_UNUSED(format); -
1259#endif -
1260 return date; -
1261} -
1262#endif // QT_NO_DATESTRING -
1263 -
1264/*! -
1265 \overload -
1266 -
1267 Returns true if the specified date (\a year, \a month, and \a -
1268 day) is valid; otherwise returns false. -
1269 -
1270 Example: -
1271 \snippet code/src_corelib_tools_qdatetime.cpp 4 -
1272 -
1273 \sa isNull(), setDate() -
1274*/ -
1275 -
1276bool QDate::isValid(int year, int month, int day) -
1277{ -
1278 // there is no year 0 in the Gregorian calendar -
1279 if (year == 0) -
1280 return false; -
1281 -
1282 return (day > 0 && month > 0 && month <= 12) && -
1283 (day <= monthDays[month] || (day == 29 && month == 2 && isLeapYear(year))); -
1284} -
1285 -
1286/*! -
1287 \fn bool QDate::isLeapYear(int year) -
1288 -
1289 Returns true if the specified \a year is a leap year; otherwise -
1290 returns false. -
1291*/ -
1292 -
1293bool QDate::isLeapYear(int y) -
1294{ -
1295 // No year 0 in Gregorian calendar, so -1, -5, -9 etc are leap years -
1296 if ( y < 1) -
1297 ++y; -
1298 -
1299 return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0; -
1300} -
1301 -
1302/*! \fn static QDate QDate::fromJulianDay(qint64 jd) -
1303 -
1304 Converts the Julian day \a jd to a QDate. -
1305 -
1306 \sa toJulianDay() -
1307*/ -
1308 -
1309/*! \fn int QDate::toJulianDay() const -
1310 -
1311 Converts the date to a Julian day. -
1312 -
1313 \sa fromJulianDay() -
1314*/ -
1315 -
1316/***************************************************************************** -
1317 QTime member functions -
1318 *****************************************************************************/ -
1319 -
1320/*! -
1321 \class QTime -
1322 \inmodule QtCore -
1323 \reentrant -
1324 -
1325 \brief The QTime class provides clock time functions. -
1326 -
1327 -
1328 A QTime object contains a clock time, i.e. the number of hours, -
1329 minutes, seconds, and milliseconds since midnight. It can read the -
1330 current time from the system clock and measure a span of elapsed -
1331 time. It provides functions for comparing times and for -
1332 manipulating a time by adding a number of milliseconds. -
1333 -
1334 QTime uses the 24-hour clock format; it has no concept of AM/PM. -
1335 Unlike QDateTime, QTime knows nothing about time zones or -
1336 daylight savings time (DST). -
1337 -
1338 A QTime object is typically created either by giving the number -
1339 of hours, minutes, seconds, and milliseconds explicitly, or by -
1340 using the static function currentTime(), which creates a QTime -
1341 object that contains the system's local time. Note that the -
1342 accuracy depends on the accuracy of the underlying operating -
1343 system; not all systems provide 1-millisecond accuracy. -
1344 -
1345 The hour(), minute(), second(), and msec() functions provide -
1346 access to the number of hours, minutes, seconds, and milliseconds -
1347 of the time. The same information is provided in textual format by -
1348 the toString() function. -
1349 -
1350 QTime provides a full set of operators to compare two QTime -
1351 objects. QTime A is considered smaller than QTime B if A is -
1352 earlier than B. -
1353 -
1354 The addSecs() and addMSecs() functions provide the time a given -
1355 number of seconds or milliseconds later than a given time. -
1356 Correspondingly, the number of seconds or milliseconds -
1357 between two times can be found using secsTo() or msecsTo(). -
1358 -
1359 QTime can be used to measure a span of elapsed time using the -
1360 start(), restart(), and elapsed() functions. -
1361 -
1362 \sa QDate, QDateTime -
1363*/ -
1364 -
1365/*! -
1366 \fn QTime::QTime() -
1367 -
1368 Constructs a null time object. A null time can be a QTime(0, 0, 0, 0) -
1369 (i.e., midnight) object, except that isNull() returns true and isValid() -
1370 returns false. -
1371 -
1372 \sa isNull(), isValid() -
1373*/ -
1374 -
1375/*! -
1376 Constructs a time with hour \a h, minute \a m, seconds \a s and -
1377 milliseconds \a ms. -
1378 -
1379 \a h must be in the range 0 to 23, \a m and \a s must be in the -
1380 range 0 to 59, and \a ms must be in the range 0 to 999. -
1381 -
1382 \sa isValid() -
1383*/ -
1384 -
1385QTime::QTime(int h, int m, int s, int ms) -
1386{ -
1387 setHMS(h, m, s, ms); -
1388} -
1389 -
1390 -
1391/*! -
1392 \fn bool QTime::isNull() const -
1393 -
1394 Returns true if the time is null (i.e., the QTime object was -
1395 constructed using the default constructor); otherwise returns -
1396 false. A null time is also an invalid time. -
1397 -
1398 \sa isValid() -
1399*/ -
1400 -
1401/*! -
1402 Returns true if the time is valid; otherwise returns false. For example, -
1403 the time 23:30:55.746 is valid, but 24:12:30 is invalid. -
1404 -
1405 \sa isNull() -
1406*/ -
1407 -
1408bool QTime::isValid() const -
1409{ -
1410 return mds > NullTime && mds < MSECS_PER_DAY; -
1411} -
1412 -
1413 -
1414/*! -
1415 Returns the hour part (0 to 23) of the time. -
1416 -
1417 Returns -1 if the time is invalid. -
1418 -
1419 \sa minute(), second(), msec() -
1420*/ -
1421 -
1422int QTime::hour() const -
1423{ -
1424 if (!isValid()) -
1425 return -1; -
1426 -
1427 return ds() / MSECS_PER_HOUR; -
1428} -
1429 -
1430/*! -
1431 Returns the minute part (0 to 59) of the time. -
1432 -
1433 Returns -1 if the time is invalid. -
1434 -
1435 \sa hour(), second(), msec() -
1436*/ -
1437 -
1438int QTime::minute() const -
1439{ -
1440 if (!isValid()) -
1441 return -1; -
1442 -
1443 return (ds() % MSECS_PER_HOUR) / MSECS_PER_MIN; -
1444} -
1445 -
1446/*! -
1447 Returns the second part (0 to 59) of the time. -
1448 -
1449 Returns -1 if the time is invalid. -
1450 -
1451 \sa hour(), minute(), msec() -
1452*/ -
1453 -
1454int QTime::second() const -
1455{ -
1456 if (!isValid()) -
1457 return -1; -
1458 -
1459 return (ds() / 1000)%SECS_PER_MIN; -
1460} -
1461 -
1462/*! -
1463 Returns the millisecond part (0 to 999) of the time. -
1464 -
1465 Returns -1 if the time is invalid. -
1466 -
1467 \sa hour(), minute(), second() -
1468*/ -
1469 -
1470int QTime::msec() const -
1471{ -
1472 if (!isValid()) -
1473 return -1; -
1474 -
1475 return ds() % 1000; -
1476} -
1477 -
1478#ifndef QT_NO_DATESTRING -
1479/*!-
\overload
Returns the time as a string. Milliseconds are not included. The
\a format parameter determines the format of the string.
If \a format is Qt::TextDate, the string format is HH:MM:SS; e.g. 1
second before midnight would be "23:59:59".
If \a format is Qt::ISODate, the string format corresponds to the
ISO 8601 extended specification for representations of dates,
which is also HH:MM:SS. (However, contrary to ISO 8601, dates
before 15 October 1582 are handled as Julian dates, not Gregorian
dates. See \l{QDate G and J} {Use of Gregorian and Julian
Calendars}. This might change in a future version of Qt.)
If the \a format is Qt::SystemLocaleShortDate or
Qt::SystemLocaleLongDate, the string format depends on the locale
settings of the system. Identical to calling
QLocale::system().toString(time, QLocale::ShortFormat) or
QLocale::system().toString(time, QLocale::LongFormat).
If the \a format is Qt::DefaultLocaleShortDate or
Qt::DefaultLocaleLongDate, the string format depends on the
default application locale. This is the locale set with
QLocale::setDefault(), or the system locale if no default locale
has been set. Identical to calling QLocale().toString(time,
QLocale::ShortFormat) or QLocale().toString(time,
QLocale::LongFormat).
If the time is invalid, an empty string will be returned.
*/*!
1480 \overload -
1481 -
1482 Returns the time as a string. Milliseconds are not included. The -
1483 \a format parameter determines the format of the string. -
1484 -
1485 If \a format is Qt::TextDate, the string format is HH:MM:SS; e.g. 1 -
1486 second before midnight would be "23:59:59". -
1487 -
1488 If \a format is Qt::ISODate, the string format corresponds to the -
1489 ISO 8601 extended specification for representations of dates, -
1490 which is also HH:MM:SS. -
1491 -
1492 If the \a format is Qt::SystemLocaleShortDate or -
1493 Qt::SystemLocaleLongDate, the string format depends on the locale -
1494 settings of the system. Identical to calling -
1495 QLocale::system().toString(time, QLocale::ShortFormat) or -
1496 QLocale::system().toString(time, QLocale::LongFormat). -
1497 -
1498 If the \a format is Qt::DefaultLocaleShortDate or -
1499 Qt::DefaultLocaleLongDate, the string format depends on the -
1500 default application locale. This is the locale set with -
1501 QLocale::setDefault(), or the system locale if no default locale -
1502 has been set. Identical to calling QLocale().toString(time, -
1503 QLocale::ShortFormat) or QLocale().toString(time, -
1504 QLocale::LongFormat). -
1505 -
1506 If the time is invalid, an empty string will be returned. -
1507*/ -
1508 -
1509QString QTime::toString(Qt::DateFormat format) const -
1510{ -
1511 if (!isValid()) -
1512 return QString(); -
1513 -
1514 switch (format) { -
1515 case Qt::SystemLocaleDate: -
1516 case Qt::SystemLocaleShortDate: -
1517 case Qt::SystemLocaleLongDate: -
1518 return QLocale::system().toString(*this, format == Qt::SystemLocaleLongDate ? QLocale::LongFormat -
1519 : QLocale::ShortFormat); -
1520 case Qt::LocaleDate: -
1521 case Qt::DefaultLocaleShortDate: -
1522 case Qt::DefaultLocaleLongDate: -
1523 return QLocale().toString(*this, format == Qt::DefaultLocaleLongDate ? QLocale::LongFormat -
1524 : QLocale::ShortFormat); -
1525 -
1526 default: -
1527 case Qt::ISODate: -
1528 case Qt::TextDate: -
1529 return QString::fromLatin1("%1:%2:%3") -
1530 .arg(hour(), 2, 10, QLatin1Char('0')) -
1531 .arg(minute(), 2, 10, QLatin1Char('0')) -
1532 .arg(second(), 2, 10, QLatin1Char('0')); -
1533 } -
1534} -
1535 -
1536/*!-
Returns the time as a string. The \a format parameter determines
the format of the result string.
These expressions may be used:
\table
\header \li Expression \li Output
\row \li h
\li the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
\row \li hh
\li the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
\row \li H
\li the hour without a leading zero (0 to 23, even with AM/PM display)
\row \li HH
\li the hour with a leading zero (00 to 23, even with AM/PM display)
\row \li m \li the minute without a leading zero (0 to 59)
\row \li mm \li the minute with a leading zero (00 to 59)
\row \li s \li the second without a leading zero (0 to 59)
\row \li ss \li the second with a leading zero (00 to 59)
\row \li z \li the milliseconds without leading zeroes (0 to 999)
\row \li zzz \li the milliseconds with leading zeroes (000 to 999)
\row \li AP or A
\li use AM/PM display. \e AP will be replaced by either "AM" or "PM".
\row \li ap or a
\li use am/pm display. \e ap will be replaced by either "am" or "pm".
\row \li t \li the timezone (for example "CEST")
\endtable
All other input characters will be ignored. Any sequence of characters that
are enclosed in single quotes will be treated as text and not be used as an
expression. Two consecutive single quotes ("''") are replaced by a singlequote
in the output. Formats without separators (e.g. "HHmm") are currently not supported.
Example format strings (assuming that the QTime is 14:13:09.042)
\table
\header \li Format \li Result
\row \li hh:mm:ss.zzz \li 14:13:09.042
\row \li h:m:s ap \li 2:13:9 pm
\row \li H:m:s a \li 14:13:9 pm
\endtable
If the time is invalid, an empty string will be returned.
If \a format is empty, the default format "hh:mm:ss" is used.
\sa QDate::toString(), QDateTime::toString()
*/*!
1537 Returns the time as a string. The \a format parameter determines -
1538 the format of the result string. -
1539 -
1540 These expressions may be used: -
1541 -
1542 \table -
1543 \header \li Expression \li Output -
1544 \row \li h -
1545 \li the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) -
1546 \row \li hh -
1547 \li the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) -
1548 \row \li H -
1549 \li the hour without a leading zero (0 to 23, even with AM/PM display) -
1550 \row \li HH -
1551 \li the hour with a leading zero (00 to 23, even with AM/PM display) -
1552 \row \li m \li the minute without a leading zero (0 to 59) -
1553 \row \li mm \li the minute with a leading zero (00 to 59) -
1554 \row \li s \li the second without a leading zero (0 to 59) -
1555 \row \li ss \li the second with a leading zero (00 to 59) -
1556 \row \li z \li the milliseconds without leading zeroes (0 to 999) -
1557 \row \li zzz \li the milliseconds with leading zeroes (000 to 999) -
1558 \row \li AP or A -
1559 \li use AM/PM display. \e A/AP will be replaced by either "AM" or "PM". -
1560 \row \li ap or a -
1561 \li use am/pm display. \e a/ap will be replaced by either "am" or "pm". -
1562 \row \li t \li the timezone (for example "CEST") -
1563 \endtable -
1564 -
1565 All other input characters will be ignored. Any sequence of characters that -
1566 are enclosed in single quotes will be treated as text and not be used as an -
1567 expression. Two consecutive single quotes ("''") are replaced by a singlequote -
1568 in the output. Formats without separators (e.g. "HHmm") are currently not supported. -
1569 -
1570 Example format strings (assuming that the QTime is 14:13:09.042) -
1571 -
1572 \table -
1573 \header \li Format \li Result -
1574 \row \li hh:mm:ss.zzz \li 14:13:09.042 -
1575 \row \li h:m:s ap \li 2:13:9 pm -
1576 \row \li H:m:s a \li 14:13:9 pm -
1577 \endtable -
1578 -
1579 If the time is invalid, an empty string will be returned. -
1580 If \a format is empty, the default format "hh:mm:ss" is used. -
1581 -
1582 \sa QDate::toString(), QDateTime::toString() -
1583*/ -
1584QString QTime::toString(const QString& format) const -
1585{ -
1586 return fmtDateTime(format, this, 0); -
1587} -
1588#endif //QT_NO_DATESTRING -
1589/*! -
1590 Sets the time to hour \a h, minute \a m, seconds \a s and -
1591 milliseconds \a ms. -
1592 -
1593 \a h must be in the range 0 to 23, \a m and \a s must be in the -
1594 range 0 to 59, and \a ms must be in the range 0 to 999. -
1595 Returns true if the set time is valid; otherwise returns false. -
1596 -
1597 \sa isValid() -
1598*/ -
1599 -
1600bool QTime::setHMS(int h, int m, int s, int ms) -
1601{ -
1602#if defined(Q_OS_WINCE) -
1603 startTick = NullTime; -
1604#endif -
1605 if (!isValid(h,m,s,ms)) { -
1606 mds = NullTime; // make this invalid -
1607 return false; -
1608 } -
1609 mds = (h*SECS_PER_HOUR + m*SECS_PER_MIN + s)*1000 + ms; -
1610 return true; -
1611} -
1612 -
1613/*! -
1614 Returns a QTime object containing a time \a s seconds later -
1615 than the time of this object (or earlier if \a s is negative). -
1616 -
1617 Note that the time will wrap if it passes midnight. -
1618 -
1619 Returns a null time if this time is invalid. -
1620 -
1621 Example: -
1622 -
1623 \snippet code/src_corelib_tools_qdatetime.cpp 5 -
1624 -
1625 \sa addMSecs(), secsTo(), QDateTime::addSecs() -
1626*/ -
1627 -
1628QTime QTime::addSecs(int s) const -
1629{ -
1630 return addMSecs(s * 1000); -
1631} -
1632 -
1633/*! -
1634 Returns the number of seconds from this time to \a t. -
1635 If \a t is earlier than this time, the number of seconds returned -
1636 is negative. -
1637 -
1638 Because QTime measures time within a day and there are 86400 -
1639 seconds in a day, the result is always between -86400 and 86400. -
1640 -
1641 secsTo() does not take into account any milliseconds. -
1642 -
1643 Returns 0 if either time is invalid. -
1644 -
1645 \sa addSecs(), QDateTime::secsTo() -
1646*/ -
1647 -
1648int QTime::secsTo(const QTime &t) const -
1649{ -
1650 if (!isValid() || !t.isValid()) -
1651 return 0; -
1652 -
1653 // Truncate milliseconds as we do not want to consider them. -
1654 int ourSeconds = ds() / 1000; -
1655 int theirSeconds = t.ds() / 1000; -
1656 return theirSeconds - ourSeconds; -
1657} -
1658 -
1659/*! -
1660 Returns a QTime object containing a time \a ms milliseconds later -
1661 than the time of this object (or earlier if \a ms is negative). -
1662 -
1663 Note that the time will wrap if it passes midnight. See addSecs() -
1664 for an example. -
1665 -
1666 Returns a null time if this time is invalid. -
1667 -
1668 \sa addSecs(), msecsTo(), QDateTime::addMSecs() -
1669*/ -
1670 -
1671QTime QTime::addMSecs(int ms) const -
1672{ -
1673 QTime t; -
1674 if (isValid()) { -
1675 if (ms < 0) { -
1676 // % not well-defined for -ve, but / is. -
1677 int negdays = (MSECS_PER_DAY - ms) / MSECS_PER_DAY; -
1678 t.mds = (ds() + ms + negdays * MSECS_PER_DAY) % MSECS_PER_DAY; -
1679 } else { -
1680 t.mds = (ds() + ms) % MSECS_PER_DAY; -
1681 } -
1682 } -
1683#if defined(Q_OS_WINCE) -
1684 if (startTick > NullTime) -
1685 t.startTick = (startTick + ms) % MSECS_PER_DAY; -
1686#endif -
1687 return t; -
1688} -
1689 -
1690/*! -
1691 Returns the number of milliseconds from this time to \a t. -
1692 If \a t is earlier than this time, the number of milliseconds returned -
1693 is negative. -
1694 -
1695 Because QTime measures time within a day and there are 86400 -
1696 seconds in a day, the result is always between -86400000 and -
1697 86400000 ms. -
1698 -
1699 Returns 0 if either time is invalid. -
1700 -
1701 \sa secsTo(), addMSecs(), QDateTime::msecsTo() -
1702*/ -
1703 -
1704int QTime::msecsTo(const QTime &t) const -
1705{ -
1706 if (!isValid() || !t.isValid()) -
1707 return 0; -
1708#if defined(Q_OS_WINCE) -
1709 // GetLocalTime() for Windows CE has no milliseconds resolution -
1710 if (t.startTick > NullTime && startTick > NullTime) -
1711 return t.startTick - startTick; -
1712 else -
1713#endif -
1714 return t.ds() - ds(); -
1715} -
1716 -
1717 -
1718/*! -
1719 \fn bool QTime::operator==(const QTime &t) const -
1720 -
1721 Returns true if this time is equal to \a t; otherwise returns false. -
1722*/ -
1723 -
1724/*! -
1725 \fn bool QTime::operator!=(const QTime &t) const -
1726 -
1727 Returns true if this time is different from \a t; otherwise returns false. -
1728*/ -
1729 -
1730/*! -
1731 \fn bool QTime::operator<(const QTime &t) const -
1732 -
1733 Returns true if this time is earlier than \a t; otherwise returns false. -
1734*/ -
1735 -
1736/*! -
1737 \fn bool QTime::operator<=(const QTime &t) const -
1738 -
1739 Returns true if this time is earlier than or equal to \a t; -
1740 otherwise returns false. -
1741*/ -
1742 -
1743/*! -
1744 \fn bool QTime::operator>(const QTime &t) const -
1745 -
1746 Returns true if this time is later than \a t; otherwise returns false. -
1747*/ -
1748 -
1749/*! -
1750 \fn bool QTime::operator>=(const QTime &t) const -
1751 -
1752 Returns true if this time is later than or equal to \a t; -
1753 otherwise returns false. -
1754*/ -
1755 -
1756/*! -
1757 \fn QTime::currentTime() -
1758 -
1759 Returns the current time as reported by the system clock. -
1760 -
1761 Note that the accuracy depends on the accuracy of the underlying -
1762 operating system; not all systems provide 1-millisecond accuracy. -
1763*/ -
1764 -
1765#ifndef QT_NO_DATESTRING -
1766 -
1767// These anonymous functions tidy up QDateTime::fromString() -
1768// and avoid confusion of responsibility between it and QTime::fromString(). -
1769namespace { -
1770inline bool isMidnight(int hour, int minute, int second, int msec) -
1771{ -
1772 return hour == 24 && minute == 0 && second == 0 && msec == 0; -
1773} -
1774 -
1775QTime fromStringImpl(const QString &s, Qt::DateFormat f, bool &isMidnight24) -
1776{ -
1777 if (s.isEmpty()) { -
1778 // Return a null time. -
1779 return QTime(); -
1780 } -
1781 -
1782 switch (f) { -
1783 case Qt::SystemLocaleDate: -
1784 case Qt::SystemLocaleShortDate: -
1785 case Qt::SystemLocaleLongDate: -
1786 { -
1787 QLocale::FormatType formatType(Qt::SystemLocaleLongDate ? QLocale::LongFormat : QLocale::ShortFormat); -
1788 return QTime::fromString(s, QLocale::system().timeFormat(formatType)); -
1789 } -
1790 case Qt::LocaleDate: -
1791 case Qt::DefaultLocaleShortDate: -
1792 case Qt::DefaultLocaleLongDate: -
1793 { -
1794 QLocale::FormatType formatType(f == Qt::DefaultLocaleLongDate ? QLocale::LongFormat : QLocale::ShortFormat); -
1795 return QTime::fromString(s, QLocale().timeFormat(formatType)); -
1796 } -
1797 case Qt::TextDate: -
1798 case Qt::ISODate: -
1799 { -
1800 bool ok = true; -
1801 const int hour(s.mid(0, 2).toInt(&ok)); -
1802 if (!ok) -
1803 return QTime(); -
1804 const int minute(s.mid(3, 2).toInt(&ok)); -
1805 if (!ok) -
1806 return QTime(); -
1807 if (f == Qt::ISODate) { -
1808 if (s.size() == 5) { -
1809 // Do not need to specify seconds if using ISO format. -
1810 return QTime(hour, minute, 0, 0); -
1811 } else if ((s.size() > 6) && (s[5] == QLatin1Char(',') || s[5] == QLatin1Char('.'))) { -
1812 // Possibly specifying fraction of a minute. -
1813 -
1814 // We only want 5 digits worth of fraction of minute. This follows the existing -
1815 // behaviour that determines how milliseconds are read; 4 millisecond digits are -
1816 // read and then rounded to 3. If we read at most 5 digits for fraction of minute, -
1817 // the maximum amount of millisecond digits it will expand to once converted to -
1818 // seconds is 4. E.g. 12:34,99999 will expand to 12:34:59.9994. The milliseconds -
1819 // will then be rounded up AND clamped to 999. -
1820 const QString minuteFractionStr(QLatin1String("0.") + s.mid(6, 5)); -
1821 const float minuteFraction = minuteFractionStr.toFloat(&ok); -
1822 if (!ok) -
1823 return QTime(); -
1824 const float secondWithMs = minuteFraction * 60; -
1825 const float second = std::floor(secondWithMs); -
1826 const float millisecond = 1000 * (secondWithMs - second); -
1827 const int millisecondRounded = qMin(qRound(millisecond), 999); -
1828 -
1829 if (isMidnight(hour, minute, second, millisecondRounded)) { -
1830 isMidnight24 = true; -
1831 return QTime(0, 0, 0, 0); -
1832 } -
1833 -
1834 return QTime(hour, minute, second, millisecondRounded); -
1835 } -
1836 } -
1837 -
1838 const int second(s.mid(6, 2).toInt(&ok)); -
1839 if (!ok) -
1840 return QTime(); -
1841 const QString msec_s(QLatin1String("0.") + s.mid(9, 4)); -
1842 const double msec(msec_s.toDouble(&ok)); -
1843 if (!ok) -
1844 return QTime(hour, minute, second, 0); -
1845 -
1846 if (f == Qt::ISODate) { -
1847 if (isMidnight(hour, minute, second, msec)) { -
1848 isMidnight24 = true; -
1849 return QTime(0, 0, 0, 0); -
1850 } -
1851 } -
1852 return QTime(hour, minute, second, qMin(qRound(msec * 1000.0), 999)); -
1853 } -
1854 } -
1855 Q_UNREACHABLE(); -
1856 return QTime(); -
1857} -
1858} -
1859 -
1860 -
1861/*! -
1862 \fn QTime QTime::fromString(const QString &string, Qt::DateFormat format) -
1863 -
1864 Returns the time represented in the \a string as a QTime using the -
1865 \a format given, or an invalid time if this is not possible. -
1866 -
1867 Note that fromString() uses a "C" locale encoded string to convert -
1868 milliseconds to a float value. If the default locale is not "C", -
1869 this may result in two conversion attempts (if the conversion -
1870 fails for the default locale). This should be considered an -
1871 implementation detail. -
1872*/ -
1873QTime QTime::fromString(const QString& s, Qt::DateFormat f) -
1874{ -
1875 bool unused; -
1876 return fromStringImpl(s, f, unused); -
1877} -
1878 -
1879/*! -
1880 \fn QTime::fromString(const QString &string, const QString &format) -
1881 -
1882 Returns the QTime represented by the \a string, using the \a -
1883 format given, or an invalid time if the string cannot be parsed. -
1884 -
1885 These expressions may be used for the format: -
1886 -
1887 \table -
1888 \header \li Expression \li Output -
1889 \row \li h -
1890 \li the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) -
1891 \row \li hh -
1892 \li the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) -
1893 \row \li m \li the minute without a leading zero (0 to 59) -
1894 \row \li mm \li the minute with a leading zero (00 to 59) -
1895 \row \li s \li the second without a leading zero (0 to 59) -
1896 \row \li ss \li the second with a leading zero (00 to 59) -
1897 \row \li z \li the milliseconds without leading zeroes (0 to 999) -
1898 \row \li zzz \li the milliseconds with leading zeroes (000 to 999) -
1899 \row \li AP -
1900 \li interpret as an AM/PM time. \e AP must be either "AM" or "PM". -
1901 \row \li ap -
1902 \li Interpret as an AM/PM time. \e ap must be either "am" or "pm". -
1903 \endtable -
1904 -
1905 All other input characters will be treated as text. Any sequence -
1906 of characters that are enclosed in single quotes will also be -
1907 treated as text and not be used as an expression. -
1908 -
1909 \snippet code/src_corelib_tools_qdatetime.cpp 6 -
1910 -
1911 If the format is not satisfied, an invalid QTime is returned. -
1912 Expressions that do not expect leading zeroes to be given (h, m, s -
1913 and z) are greedy. This means that they will use two digits even if -
1914 this puts them outside the range of accepted values and leaves too -
1915 few digits for other sections. For example, the following string -
1916 could have meant 00:07:10, but the m will grab two digits, resulting -
1917 in an invalid time: -
1918 -
1919 \snippet code/src_corelib_tools_qdatetime.cpp 7 -
1920 -
1921 Any field that is not represented in the format will be set to zero. -
1922 For example: -
1923 -
1924 \snippet code/src_corelib_tools_qdatetime.cpp 8 -
1925 -
1926 \sa QDateTime::fromString(), QDate::fromString(), QDate::toString(), -
1927 QDateTime::toString(), QTime::toString() -
1928*/ -
1929 -
1930QTime QTime::fromString(const QString &string, const QString &format) -
1931{ -
1932 QTime time; -
1933#ifndef QT_BOOTSTRAPPED -
1934 QDateTimeParser dt(QVariant::Time, QDateTimeParser::FromString); -
1935 if (dt.parseFormat(format)) -
1936 dt.fromString(string, 0, &time); -
1937#else -
1938 Q_UNUSED(string); -
1939 Q_UNUSED(format); -
1940#endif -
1941 return time; -
1942} -
1943 -
1944#endif // QT_NO_DATESTRING -
1945 -
1946 -
1947/*! -
1948 \overload -
1949 -
1950 Returns true if the specified time is valid; otherwise returns -
1951 false. -
1952 -
1953 The time is valid if \a h is in the range 0 to 23, \a m and -
1954 \a s are in the range 0 to 59, and \a ms is in the range 0 to 999. -
1955 -
1956 Example: -
1957 -
1958 \snippet code/src_corelib_tools_qdatetime.cpp 9 -
1959*/ -
1960 -
1961bool QTime::isValid(int h, int m, int s, int ms) -
1962{ -
1963 return (uint)h < 24 && (uint)m < 60 && (uint)s < 60 && (uint)ms < 1000; -
1964} -
1965 -
1966 -
1967/*! -
1968 Sets this time to the current time. This is practical for timing: -
1969 -
1970 \snippet code/src_corelib_tools_qdatetime.cpp 10 -
1971 -
1972 \sa restart(), elapsed(), currentTime() -
1973*/ -
1974 -
1975void QTime::start() -
1976{ -
1977 *this = currentTime(); -
1978} -
1979 -
1980/*! -
1981 Sets this time to the current time and returns the number of -
1982 milliseconds that have elapsed since the last time start() or -
1983 restart() was called. -
1984 -
1985 This function is guaranteed to be atomic and is thus very handy -
1986 for repeated measurements. Call start() to start the first -
1987 measurement, and restart() for each later measurement. -
1988 -
1989 Note that the counter wraps to zero 24 hours after the last call -
1990 to start() or restart(). -
1991 -
1992 \warning If the system's clock setting has been changed since the -
1993 last time start() or restart() was called, the result is -
1994 undefined. This can happen when daylight savings time is turned on -
1995 or off. -
1996 -
1997 \sa start(), elapsed(), currentTime() -
1998*/ -
1999 -
2000int QTime::restart() -
2001{ -
2002 QTime t = currentTime(); -
2003 int n = msecsTo(t); -
2004 if (n < 0) // passed midnight -
2005 n += 86400*1000; -
2006 *this = t; -
2007 return n; -
2008} -
2009 -
2010/*! -
2011 Returns the number of milliseconds that have elapsed since the -
2012 last time start() or restart() was called. -
2013 -
2014 Note that the counter wraps to zero 24 hours after the last call -
2015 to start() or restart. -
2016 -
2017 Note that the accuracy depends on the accuracy of the underlying -
2018 operating system; not all systems provide 1-millisecond accuracy. -
2019 -
2020 \warning If the system's clock setting has been changed since the -
2021 last time start() or restart() was called, the result is -
2022 undefined. This can happen when daylight savings time is turned on -
2023 or off. -
2024 -
2025 \sa start(), restart() -
2026*/ -
2027 -
2028int QTime::elapsed() const -
2029{ -
2030 int n = msecsTo(currentTime()); -
2031 if (n < 0) // passed midnight -
2032 n += 86400 * 1000; -
2033 return n; -
2034} -
2035 -
2036 -
2037/***************************************************************************** -
2038 QDateTime member functions -
2039 *****************************************************************************/ -
2040 -
2041/*! -
2042 \class QDateTime -
2043 \inmodule QtCore -
2044 \ingroup shared -
2045 \reentrant -
2046 \brief The QDateTime class provides date and time functions. -
2047 -
2048 -
2049 A QDateTime object contains a calendar date and a clock time (a -
2050 "datetime"). It is a combination of the QDate and QTime classes. -
2051 It can read the current datetime from the system clock. It -
2052 provides functions for comparing datetimes and for manipulating a -
2053 datetime by adding a number of seconds, days, months, or years. -
2054 -
2055 A QDateTime object is typically created either by giving a date -
2056 and time explicitly in the constructor, or by using the static -
2057 function currentDateTime() that returns a QDateTime object set -
2058 to the system clock's time. The date and time can be changed with -
2059 setDate() and setTime(). A datetime can also be set using the -
2060 setTime_t() function that takes a POSIX-standard "number of -
2061 seconds since 00:00:00 on January 1, 1970" value. The fromString() -
2062 function returns a QDateTime, given a string and a date format -
2063 used to interpret the date within the string. -
2064 -
2065 The date() and time() functions provide access to the date and -
2066 time parts of the datetime. The same information is provided in -
2067 textual format by the toString() function. -
2068 -
2069 QDateTime provides a full set of operators to compare two -
2070 QDateTime objects, where smaller means earlier and larger means -
2071 later. -
2072 -
2073 You can increment (or decrement) a datetime by a given number of -
2074 milliseconds using addMSecs(), seconds using addSecs(), or days -
2075 using addDays(). Similarly, you can use addMonths() and addYears(). -
2076 The daysTo() function returns the number of days between two datetimes, -
2077 secsTo() returns the number of seconds between two datetimes, and -
2078 msecsTo() returns the number of milliseconds between two datetimes. -
2079 -
2080 QDateTime can store datetimes as \l{Qt::LocalTime}{local time} or -
2081 as \l{Qt::UTC}{UTC}. QDateTime::currentDateTime() returns a -
2082 QDateTime expressed as local time; use toUTC() to convert it to -
2083 UTC. You can also use timeSpec() to find out if a QDateTime -
2084 object stores a UTC time or a local time. Operations such as -
2085 addSecs() and secsTo() are aware of daylight saving time (DST). -
2086 -
2087 \note QDateTime does not account for leap seconds. -
2088 -
2089 \section1 -
2090 -
2091 \section2 No Year 0 -
2092 -
2093 There is no year 0. Dates in that year are considered invalid. The -
2094 year -1 is the year "1 before Christ" or "1 before current era." -
2095 The day before 1 January 1 CE is 31 December 1 BCE. -
2096 -
2097 \section2 Range of Valid Dates -
2098 -
2099 Dates are stored internally as a Julian Day number, an integer count of -
2100 every day in a contiguous range, with 24 November 4714 BCE in the Gregorian -
2101 calendar being Julian Day 0 (1 January 4713 BCE in the Julian calendar). -
2102 As well as being an efficient and accurate way of storing an absolute date, -
2103 it is suitable for converting a Date into other calendar systems such as -
2104 Hebrew, Islamic or Chinese. The Julian Day number can be obtained using -
2105 QDate::toJulianDay() and can be set using QDate::fromJulianDay(). -
2106 -
2107 The range of dates able to be stored by QDate as a Julian Day number is -
2108 for technical reasons limited to between -784350574879 and 784354017364, -
2109 which means from before 2 billion BCE to after 2 billion CE. -
2110 -
2111 \section2 -
2112 Use of System Timezone -
2113 -
2114 QDateTime uses the system's time zone information to determine the -
2115 offset of local time from UTC. If the system is not configured -
2116 correctly or not up-to-date, QDateTime will give wrong results as -
2117 well. -
2118 -
2119 \section2 Daylight Savings Time (DST) -
2120 -
2121 QDateTime takes into account the system's time zone information -
2122 when dealing with DST. On modern Unix systems, this means it -
2123 applies the correct historical DST data whenever possible. On -
2124 Windows and Windows CE, where the system doesn't support -
2125 historical DST data, historical accuracy is not maintained with -
2126 respect to DST. -
2127 -
2128 The range of valid dates taking DST into account is 1970-01-01 to -
2129 the present, and rules are in place for handling DST correctly -
2130 until 2037-12-31, but these could change. For dates falling -
2131 outside that range, QDateTime makes a \e{best guess} using the -
2132 rules for year 1970 or 2037, but we can't guarantee accuracy. This -
2133 means QDateTime doesn't take into account changes in a locale's -
2134 time zone before 1970, even if the system's time zone database -
2135 supports that information. -
2136 -
2137 \sa QDate, QTime, QDateTimeEdit -
2138*/ -
2139 -
2140/*! -
2141 Constructs a null datetime (i.e. null date and null time). A null -
2142 datetime is invalid, since the date is invalid. -
2143 -
2144 \sa isValid() -
2145*/ -
2146QDateTime::QDateTime() -
2147 : d(new QDateTimePrivate) -
2148{ -
2149} -
2150 -
2151 -
2152/*! -
2153 Constructs a datetime with the given \a date, a valid -
2154 time(00:00:00.000), and sets the timeSpec() to Qt::LocalTime. -
2155*/ -
2156 -
2157QDateTime::QDateTime(const QDate &date) -
2158 : d(new QDateTimePrivate) -
2159{ -
2160 d->date = date; -
2161 d->time = QTime(0, 0, 0); -
2162} -
2163 -
2164/*! -
2165 Constructs a datetime with the given \a date and \a time, using -
2166 the time specification defined by \a spec. -
2167 -
2168 If \a date is valid and \a time is not, the time will be set to midnight. -
2169*/ -
2170 -
2171QDateTime::QDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec) -
2172 : d(new QDateTimePrivate) -
2173{ -
2174 d->date = date; -
2175 d->time = date.isValid() && !time.isValid() ? QTime(0, 0, 0) : time; -
2176 d->spec = (spec == Qt::UTC) ? QDateTimePrivate::UTC : QDateTimePrivate::LocalUnknown; -
2177} -
2178 -
2179/*! -
2180 Constructs a copy of the \a other datetime. -
2181*/ -
2182 -
2183QDateTime::QDateTime(const QDateTime &other) -
2184 : d(other.d) -
2185{ -
2186} -
2187 -
2188/*! -
2189 Destroys the datetime. -
2190*/ -
2191QDateTime::~QDateTime() -
2192{ -
2193} -
2194 -
2195/*! -
2196 Makes a copy of the \a other datetime and returns a reference to the -
2197 copy. -
2198*/ -
2199 -
2200QDateTime &QDateTime::operator=(const QDateTime &other) -
2201{ -
2202 d = other.d; -
2203 return *this; -
2204} -
2205/*! -
2206 \fn void QDateTime::swap(QDateTime &other) -
2207 \since 5.0 -
2208 -
2209 Swaps this datetime with \a other. This operation is very fast -
2210 and never fails. -
2211*/ -
2212 -
2213/*! -
2214 Returns true if both the date and the time are null; otherwise -
2215 returns false. A null datetime is invalid. -
2216 -
2217 \sa QDate::isNull(), QTime::isNull(), isValid() -
2218*/ -
2219 -
2220bool QDateTime::isNull() const -
2221{ -
2222 return d->date.isNull() && d->time.isNull(); -
2223} -
2224 -
2225/*! -
2226 Returns true if both the date and the time are valid; otherwise -
2227 returns false. -
2228 -
2229 \sa QDate::isValid(), QTime::isValid() -
2230*/ -
2231 -
2232bool QDateTime::isValid() const -
2233{ -
2234 return d->date.isValid() && d->time.isValid(); -
2235} -
2236 -
2237/*! -
2238 Returns the date part of the datetime. -
2239 -
2240 \sa setDate(), time(), timeSpec() -
2241*/ -
2242 -
2243QDate QDateTime::date() const -
2244{ -
2245 return d->date; -
2246} -
2247 -
2248/*! -
2249 Returns the time part of the datetime. -
2250 -
2251 \sa setTime(), date(), timeSpec() -
2252*/ -
2253 -
2254QTime QDateTime::time() const -
2255{ -
2256 return d->time; -
2257} -
2258 -
2259/*! -
2260 Returns the time specification of the datetime. -
2261 -
2262 \sa setTimeSpec(), date(), time(), Qt::TimeSpec -
2263*/ -
2264 -
2265Qt::TimeSpec QDateTime::timeSpec() const -
2266{ -
2267 switch(d->spec) -
2268 { -
2269 case QDateTimePrivate::UTC: -
2270 return Qt::UTC; -
2271 case QDateTimePrivate::OffsetFromUTC: -
2272 return Qt::OffsetFromUTC; -
2273 default: -
2274 return Qt::LocalTime; -
2275 } -
2276} -
2277 -
2278/*! -
2279 Sets the date part of this datetime to \a date. -
2280 If no time is set, it is set to midnight. -
2281 -
2282 \sa date(), setTime(), setTimeSpec() -
2283*/ -
2284 -
2285void QDateTime::setDate(const QDate &date) -
2286{ -
2287 detach(); -
2288 d->date = date; -
2289 if (d->spec == QDateTimePrivate::LocalStandard -
2290 || d->spec == QDateTimePrivate::LocalDST) -
2291 d->spec = QDateTimePrivate::LocalUnknown; -
2292 if (date.isValid() && !d->time.isValid()) -
2293 d->time = QTime(0, 0, 0); -
2294} -
2295 -
2296/*! -
2297 Sets the time part of this datetime to \a time. -
2298 -
2299 \sa time(), setDate(), setTimeSpec() -
2300*/ -
2301 -
2302void QDateTime::setTime(const QTime &time) -
2303{ -
2304 detach(); -
2305 if (d->spec == QDateTimePrivate::LocalStandard -
2306 || d->spec == QDateTimePrivate::LocalDST) -
2307 d->spec = QDateTimePrivate::LocalUnknown; -
2308 d->time = time; -
2309} -
2310 -
2311/*! -
2312 Sets the time specification used in this datetime to \a spec. -
2313 The datetime will refer to a different point in time. -
2314 -
2315 Example: -
2316 \snippet code/src_corelib_tools_qdatetime.cpp 19 -
2317 -
2318 \sa timeSpec(), setDate(), setTime(), Qt::TimeSpec -
2319*/ -
2320 -
2321void QDateTime::setTimeSpec(Qt::TimeSpec spec) -
2322{ -
2323 detach(); -
2324 -
2325 switch(spec) -
2326 { -
2327 case Qt::UTC: -
2328 d->spec = QDateTimePrivate::UTC; -
2329 break; -
2330 case Qt::OffsetFromUTC: -
2331 d->spec = QDateTimePrivate::OffsetFromUTC; -
2332 break; -
2333 default: -
2334 d->spec = QDateTimePrivate::LocalUnknown; -
2335 break; -
2336 } -
2337} -
2338 -
2339qint64 toMSecsSinceEpoch_helper(qint64 jd, int msecs) -
2340{ -
2341 qint64 days = jd - JULIAN_DAY_FOR_EPOCH; -
2342 qint64 retval = (days * MSECS_PER_DAY) + msecs; -
2343 return retval; -
2344} -
2345 -
2346/*! -
2347 \since 4.7 -
2348 -
2349 Returns the datetime as the number of milliseconds that have passed -
2350 since 1970-01-01T00:00:00.000, Coordinated Universal Time (Qt::UTC). -
2351 -
2352 On systems that do not support time zones, this function will -
2353 behave as if local time were Qt::UTC. -
2354 -
2355 The behavior for this function is undefined if the datetime stored in -
2356 this object is not valid. However, for all valid dates, this function -
2357 returns a unique value. -
2358 -
2359 \sa toTime_t(), setMSecsSinceEpoch() -
2360*/ -
2361qint64 QDateTime::toMSecsSinceEpoch() const -
2362{ -
2363 QDate utcDate; -
2364 QTime utcTime; -
2365 d->getUTC(utcDate, utcTime); -
2366 -
2367 return toMSecsSinceEpoch_helper(utcDate.toJulianDay(), QTime(0, 0, 0).msecsTo(utcTime)); -
2368} -
2369 -
2370/*! -
2371 Returns the datetime as the number of seconds that have passed -
2372 since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC). -
2373 -
2374 On systems that do not support time zones, this function will -
2375 behave as if local time were Qt::UTC. -
2376 -
2377 \note This function returns a 32-bit unsigned integer, so it does not -
2378 support dates before 1970, but it does support dates after -
2379 2038-01-19T03:14:06, which may not be valid time_t values. Be careful -
2380 when passing those time_t values to system functions, which could -
2381 interpret them as negative dates. -
2382 -
2383 If the date is outside the range 1970-01-01T00:00:00 to -
2384 2106-02-07T06:28:14, this function returns -1 cast to an unsigned integer -
2385 (i.e., 0xFFFFFFFF). -
2386 -
2387 To get an extended range, use toMSecsSinceEpoch(). -
2388 -
2389 \sa toMSecsSinceEpoch(), setTime_t() -
2390*/ -
2391 -
2392uint QDateTime::toTime_t() const -
2393{ -
2394 qint64 retval = toMSecsSinceEpoch() / 1000; -
2395 if (quint64(retval) >= Q_UINT64_C(0xFFFFFFFF)) -
2396 return uint(-1); -
2397 return uint(retval); -
2398} -
2399 -
2400/*! -
2401 \since 4.7 -
2402 -
2403 Sets the date and time given the number of milliseconds \a msecs that have -
2404 passed since 1970-01-01T00:00:00.000, Coordinated Universal Time -
2405 (Qt::UTC). On systems that do not support time zones this function -
2406 will behave as if local time were Qt::UTC. -
2407 -
2408 Note that there are possible values for \a msecs that lie outside the -
2409 valid range of QDateTime, both negative and positive. The behavior of -
2410 this function is undefined for those values. -
2411 -
2412 \sa toMSecsSinceEpoch(), setTime_t() -
2413*/ -
2414void QDateTime::setMSecsSinceEpoch(qint64 msecs) -
2415{ -
2416 detach(); -
2417 -
2418 QDateTimePrivate::Spec oldSpec = d->spec; -
2419 -
2420 qint64 ddays = msecs / MSECS_PER_DAY; -
2421 msecs %= MSECS_PER_DAY; -
2422 if (msecs < 0) { -
2423 // negative -
2424 --ddays; -
2425 msecs += MSECS_PER_DAY; -
2426 } -
2427 -
2428 d->date = QDate(1970, 1, 1).addDays(ddays); -
2429 d->time = QTime(0, 0, 0).addMSecs(msecs); -
2430 d->spec = QDateTimePrivate::UTC; -
2431 -
2432 if (oldSpec != QDateTimePrivate::UTC) -
2433 d->spec = d->getLocal(d->date, d->time); -
2434} -
2435 -
2436/*! -
2437 \fn void QDateTime::setTime_t(uint seconds) -
2438 -
2439 Sets the date and time given the number of \a seconds that have -
2440 passed since 1970-01-01T00:00:00, Coordinated Universal Time -
2441 (Qt::UTC). On systems that do not support time zones this function -
2442 will behave as if local time were Qt::UTC. -
2443 -
2444 \sa toTime_t() -
2445*/ -
2446 -
2447void QDateTime::setTime_t(uint secsSince1Jan1970UTC) -
2448{ -
2449 detach(); -
2450 -
2451 QDateTimePrivate::Spec oldSpec = d->spec; -
2452 -
2453 d->date = QDate(1970, 1, 1).addDays(secsSince1Jan1970UTC / SECS_PER_DAY); -
2454 d->time = QTime(0, 0, 0).addSecs(secsSince1Jan1970UTC % SECS_PER_DAY); -
2455 d->spec = QDateTimePrivate::UTC; -
2456 -
2457 if (oldSpec != QDateTimePrivate::UTC) -
2458 d->spec = d->getLocal(d->date, d->time); -
2459} -
2460 -
2461#ifndef QT_NO_DATESTRING -
2462/*! -
2463 \fn QString QDateTime::toString(Qt::DateFormat format) const -
2464 -
2465 \overload -
2466 -
2467 Returns the datetime as a string in the \a format given. -
2468 -
2469 If the \a format is Qt::TextDate, the string is formatted in -
2470 the default way. QDate::shortDayName(), QDate::shortMonthName(), -
2471 and QTime::toString() are used to generate the string, so the -
2472 day and month names will be localized names. An example of this -
2473 formatting is "Wed May 20 03:40:13 1998". -
2474 -
2475 If the \a format is Qt::ISODate, the string format corresponds -
2476 to the ISO 8601 extended specification for representations of -
2477 dates and times, taking the form YYYY-MM-DDTHH:MM:SS[Z|[+|-]HH:MM], -
2478 depending on the timeSpec() of the QDateTime. If the timeSpec() -
2479 is Qt::UTC, Z will be appended to the string; if the timeSpec() is -
2480 Qt::OffsetFromUTC, the offset in hours and minutes from UTC will -
2481 be appended to the string. -
2482 -
2483 If the \a format is Qt::SystemLocaleShortDate or -
2484 Qt::SystemLocaleLongDate, the string format depends on the locale -
2485 settings of the system. Identical to calling -
2486 QLocale::system().toString(datetime, QLocale::ShortFormat) or -
2487 QLocale::system().toString(datetime, QLocale::LongFormat). -
2488 -
2489 If the \a format is Qt::DefaultLocaleShortDate or -
2490 Qt::DefaultLocaleLongDate, the string format depends on the -
2491 default application locale. This is the locale set with -
2492 QLocale::setDefault(), or the system locale if no default locale -
2493 has been set. Identical to calling QLocale().toString(datetime, -
2494 QLocale::ShortFormat) or QLocale().toString(datetime, -
2495 QLocale::LongFormat). -
2496 -
2497 If the datetime is invalid, an empty string will be returned. -
2498 -
2499 \warning The Qt::ISODate format is only valid for years in the -
2500 range 0 to 9999. This restriction may apply to locale-aware -
2501 formats as well, depending on the locale settings. -
2502 -
2503 \sa QDate::toString(), QTime::toString(), Qt::DateFormat -
2504*/ -
2505 -
2506QString QDateTime::toString(Qt::DateFormat f) const -
2507{ -
2508 QString buf; -
2509 if (!isValid()) -
2510 return buf; -
2511 -
2512 if (f == Qt::ISODate) { -
2513 buf = d->date.toString(Qt::ISODate); -
2514 if (buf.isEmpty()) -
2515 return QString(); // failed to convert -
2516 buf += QLatin1Char('T'); -
2517 buf += d->time.toString(Qt::ISODate); -
2518 switch (d->spec) { -
2519 case QDateTimePrivate::UTC: -
2520 buf += QLatin1Char('Z'); -
2521 break; -
2522 case QDateTimePrivate::OffsetFromUTC: { -
2523 int sign = d->utcOffset >= 0 ? 1: -1; -
2524 buf += QString::fromLatin1("%1%2:%3"). -
2525 arg(sign == 1 ? QLatin1Char('+') : QLatin1Char('-')). -
2526 arg(d->utcOffset * sign / SECS_PER_HOUR, 2, 10, QLatin1Char('0')). -
2527 arg((d->utcOffset / 60) % 60, 2, 10, QLatin1Char('0')); -
2528 break; -
2529 } -
2530 default: -
2531 break; -
2532 } -
2533 } -
2534#ifndef QT_NO_TEXTDATE -
2535 else if (f == Qt::TextDate) { -
2536#ifndef Q_OS_WIN -
2537 buf = d->date.shortDayName(d->date.dayOfWeek()); -
2538 buf += QLatin1Char(' '); -
2539 buf += d->date.shortMonthName(d->date.month()); -
2540 buf += QLatin1Char(' '); -
2541 buf += QString::number(d->date.day()); -
2542#else -
2543 wchar_t out[255]; -
2544 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ILDATE, out, 255); -
2545 QString winstr = QString::fromWCharArray(out); -
2546 switch (winstr.toInt()) { -
2547 case 1: -
2548 buf = d->date.shortDayName(d->date.dayOfWeek()); -
2549 buf += QLatin1Char(' '); -
2550 buf += QString::number(d->date.day()); -
2551 buf += QLatin1String(". "); -
2552 buf += d->date.shortMonthName(d->date.month()); -
2553 break; -
2554 default: -
2555 buf = d->date.shortDayName(d->date.dayOfWeek()); -
2556 buf += QLatin1Char(' '); -
2557 buf += d->date.shortMonthName(d->date.month()); -
2558 buf += QLatin1Char(' '); -
2559 buf += QString::number(d->date.day()); -
2560 } -
2561#endif -
2562 buf += QLatin1Char(' '); -
2563 buf += d->time.toString(); -
2564 buf += QLatin1Char(' '); -
2565 buf += QString::number(d->date.year()); -
2566 } -
2567#endif -
2568 else { -
2569 buf = d->date.toString(f); -
2570 if (buf.isEmpty()) -
2571 return QString(); // failed to convert -
2572 buf += QLatin1Char(' '); -
2573 buf += d->time.toString(f); -
2574 } -
2575 -
2576 return buf; -
2577} -
2578 -
2579/*! -
2580 Returns the datetime as a string. The \a format parameter -
2581 determines the format of the result string. -
2582 -
2583 These expressions may be used for the date: -
2584 -
2585 \table -
2586 \header \li Expression \li Output -
2587 \row \li d \li the day as number without a leading zero (1 to 31) -
2588 \row \li dd \li the day as number with a leading zero (01 to 31) -
2589 \row \li ddd -
2590 \li the abbreviated localized day name (e.g. 'Mon' to 'Sun'). -
2591 Uses QDate::shortDayName(). -
2592 \row \li dddd -
2593 \li the long localized day name (e.g. 'Monday' to 'Qt::Sunday'). -
2594 Uses QDate::longDayName(). -
2595 \row \li M \li the month as number without a leading zero (1-12) -
2596 \row \li MM \li the month as number with a leading zero (01-12) -
2597 \row \li MMM -
2598 \li the abbreviated localized month name (e.g. 'Jan' to 'Dec'). -
2599 Uses QDate::shortMonthName(). -
2600 \row \li MMMM -
2601 \li the long localized month name (e.g. 'January' to 'December'). -
2602 Uses QDate::longMonthName(). -
2603 \row \li yy \li the year as two digit number (00-99) -
2604 \row \li yyyy \li the year as four digit number -
2605 \endtable -
2606 -
2607 These expressions may be used for the time: -
2608 -
2609 \table -
2610 \header \li Expression \li Output -
2611 \row \li h -
2612 \li the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) -
2613 \row \li hh -
2614 \li the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) -
2615 \row \li m \li the minute without a leading zero (0 to 59) -
2616 \row \li mm \li the minute with a leading zero (00 to 59) -
2617 \row \li s \li the second without a leading zero (0 to 59) -
2618 \row \li ss \li the second with a leading zero (00 to 59) -
2619 \row \li z \li the milliseconds without leading zeroes (0 to 999) -
2620 \row \li zzz \li the milliseconds with leading zeroes (000 to 999) -
2621 \row \li AP -
2622 \li use AM/PM display. \e AP will be replaced by either "AM" or "PM". -
2623 \row \li ap -
2624 \li use am/pm display. \e ap will be replaced by either "am" or "pm". -
2625 \endtable -
2626 -
2627 All other input characters will be ignored. Any sequence of characters that -
2628 are enclosed in single quotes will be treated as text and not be used as an -
2629 expression. Two consecutive single quotes ("''") are replaced by a singlequote -
2630 in the output. Formats without separators (e.g. "HHmm") are currently not supported. -
2631 -
2632 Example format strings (assumed that the QDateTime is 21 May 2001 -
2633 14:13:09): -
2634 -
2635 \table -
2636 \header \li Format \li Result -
2637 \row \li dd.MM.yyyy \li 21.05.2001 -
2638 \row \li ddd MMMM d yy \li Tue May 21 01 -
2639 \row \li hh:mm:ss.zzz \li 14:13:09.042 -
2640 \row \li h:m:s ap \li 2:13:9 pm -
2641 \endtable -
2642 -
2643 If the datetime is invalid, an empty string will be returned. -
2644 -
2645 \sa QDate::toString(), QTime::toString() -
2646*/ -
2647QString QDateTime::toString(const QString& format) const -
2648{ -
2649 return fmtDateTime(format, &d->time, &d->date); -
2650} -
2651#endif //QT_NO_DATESTRING -
2652 -
2653/*! -
2654 Returns a QDateTime object containing a datetime \a ndays days -
2655 later than the datetime of this object (or earlier if \a ndays is -
2656 negative). -
2657 -
2658 \sa daysTo(), addMonths(), addYears(), addSecs() -
2659*/ -
2660 -
2661QDateTime QDateTime::addDays(qint64 ndays) const -
2662{ -
2663 return QDateTime(d->date.addDays(ndays), d->time, timeSpec()); -
2664} -
2665 -
2666/*! -
2667 Returns a QDateTime object containing a datetime \a nmonths months -
2668 later than the datetime of this object (or earlier if \a nmonths -
2669 is negative). -
2670 -
2671 \sa daysTo(), addDays(), addYears(), addSecs() -
2672*/ -
2673 -
2674QDateTime QDateTime::addMonths(int nmonths) const -
2675{ -
2676 return QDateTime(d->date.addMonths(nmonths), d->time, timeSpec()); -
2677} -
2678 -
2679/*! -
2680 Returns a QDateTime object containing a datetime \a nyears years -
2681 later than the datetime of this object (or earlier if \a nyears is -
2682 negative). -
2683 -
2684 \sa daysTo(), addDays(), addMonths(), addSecs() -
2685*/ -
2686 -
2687QDateTime QDateTime::addYears(int nyears) const -
2688{ -
2689 return QDateTime(d->date.addYears(nyears), d->time, timeSpec()); -
2690} -
2691 -
2692QDateTime QDateTimePrivate::addMSecs(const QDateTime &dt, qint64 msecs) -
2693{ -
2694 if (!dt.isValid()) -
2695 return QDateTime(); -
2696 -
2697 QDate utcDate; -
2698 QTime utcTime; -
2699 dt.d->getUTC(utcDate, utcTime); -
2700 -
2701 addMSecs(utcDate, utcTime, msecs); -
2702 -
2703 return QDateTime(utcDate, utcTime, Qt::UTC).toTimeSpec(dt.timeSpec()); -
2704} -
2705 -
2706/*! -
2707 Adds \a msecs to utcDate and \a utcTime as appropriate. It is assumed that -
2708 utcDate and utcTime are adjusted to UTC. -
2709 -
2710 \since 4.5 -
2711 \internal -
2712 */ -
2713void QDateTimePrivate::addMSecs(QDate &utcDate, QTime &utcTime, qint64 msecs) -
2714{ -
2715 qint64 dd = utcDate.toJulianDay(); -
2716 int tt = QTime(0, 0, 0).msecsTo(utcTime); -
2717 int sign = 1; -
2718 if (msecs < 0) { -
2719 msecs = -msecs; -
2720 sign = -1; -
2721 } -
2722 if (msecs >= int(MSECS_PER_DAY)) { -
2723 dd += sign * (msecs / MSECS_PER_DAY); -
2724 msecs %= MSECS_PER_DAY; -
2725 } -
2726 -
2727 tt += sign * msecs; -
2728 if (tt < 0) { -
2729 tt = MSECS_PER_DAY - tt - 1; -
2730 dd -= tt / MSECS_PER_DAY; -
2731 tt = tt % MSECS_PER_DAY; -
2732 tt = MSECS_PER_DAY - tt - 1; -
2733 } else if (tt >= int(MSECS_PER_DAY)) { -
2734 dd += tt / MSECS_PER_DAY; -
2735 tt = tt % MSECS_PER_DAY; -
2736 } -
2737 -
2738 utcDate = QDate::fromJulianDay(dd); -
2739 utcTime = QTime(0, 0, 0).addMSecs(tt); -
2740} -
2741 -
2742/*! -
2743 Returns a QDateTime object containing a datetime \a s seconds -
2744 later than the datetime of this object (or earlier if \a s is -
2745 negative). -
2746 -
2747 If this datetime is invalid, an invalid datetime will be returned. -
2748 -
2749 \sa addMSecs(), secsTo(), addDays(), addMonths(), addYears() -
2750*/ -
2751 -
2752QDateTime QDateTime::addSecs(qint64 s) const -
2753{ -
2754 return d->addMSecs(*this, s * 1000); -
2755} -
2756 -
2757/*! -
2758 Returns a QDateTime object containing a datetime \a msecs miliseconds -
2759 later than the datetime of this object (or earlier if \a msecs is -
2760 negative). -
2761 -
2762 If this datetime is invalid, an invalid datetime will be returned. -
2763 -
2764 \sa addSecs(), msecsTo(), addDays(), addMonths(), addYears() -
2765*/ -
2766QDateTime QDateTime::addMSecs(qint64 msecs) const -
2767{ -
2768 return d->addMSecs(*this, msecs); -
2769} -
2770 -
2771/*! -
2772 Returns the number of days from this datetime to the \a other -
2773 datetime. The number of days is counted as the number of times -
2774 midnight is reached between this datetime to the \a other -
2775 datetime. This means that a 10 minute difference from 23:55 to -
2776 0:05 the next day counts as one day. -
2777 -
2778 If the \a other datetime is earlier than this datetime, -
2779 the value returned is negative. -
2780 -
2781 Example: -
2782 \snippet code/src_corelib_tools_qdatetime.cpp 15 -
2783 -
2784 \sa addDays(), secsTo(), msecsTo() -
2785*/ -
2786 -
2787qint64 QDateTime::daysTo(const QDateTime &other) const -
2788{ -
2789 return d->date.daysTo(other.d->date); -
2790} -
2791 -
2792/*! -
2793 Returns the number of seconds from this datetime to the \a other -
2794 datetime. If the \a other datetime is earlier than this datetime, -
2795 the value returned is negative. -
2796 -
2797 Before performing the comparison, the two datetimes are converted -
2798 to Qt::UTC to ensure that the result is correct if one of the two -
2799 datetimes has daylight saving time (DST) and the other doesn't. -
2800 -
2801 Returns 0 if either datetime is invalid. -
2802 -
2803 Example: -
2804 \snippet code/src_corelib_tools_qdatetime.cpp 11 -
2805 -
2806 \sa addSecs(), daysTo(), QTime::secsTo() -
2807*/ -
2808 -
2809qint64 QDateTime::secsTo(const QDateTime &other) const -
2810{ -
2811 if (!isValid() || !other.isValid()) -
2812 return 0; -
2813 -
2814 QDate date1, date2; -
2815 QTime time1, time2; -
2816 -
2817 d->getUTC(date1, time1); -
2818 other.d->getUTC(date2, time2); -
2819 -
2820 return (date1.daysTo(date2) * SECS_PER_DAY) + time1.secsTo(time2); -
2821} -
2822 -
2823/*! -
2824 Returns the number of milliseconds from this datetime to the \a other -
2825 datetime. If the \a other datetime is earlier than this datetime, -
2826 the value returned is negative. -
2827 -
2828 Before performing the comparison, the two datetimes are converted -
2829 to Qt::UTC to ensure that the result is correct if one of the two -
2830 datetimes has daylight saving time (DST) and the other doesn't. -
2831 -
2832 Returns 0 if either datetime is invalid. -
2833 -
2834 \sa addMSecs(), daysTo(), QTime::msecsTo() -
2835*/ -
2836 -
2837qint64 QDateTime::msecsTo(const QDateTime &other) const -
2838{ -
2839 if (!isValid() || !other.isValid()) -
2840 return 0; -
2841 -
2842 QDate selfDate; -
2843 QDate otherDate; -
2844 QTime selfTime; -
2845 QTime otherTime; -
2846 -
2847 d->getUTC(selfDate, selfTime); -
2848 other.d->getUTC(otherDate, otherTime); -
2849 -
2850 return (static_cast<qint64>(selfDate.daysTo(otherDate)) * static_cast<qint64>(MSECS_PER_DAY)) -
2851 + static_cast<qint64>(selfTime.msecsTo(otherTime)); -
2852} -
2853 -
2854 -
2855/*! -
2856 \fn QDateTime QDateTime::toTimeSpec(Qt::TimeSpec specification) const -
2857 -
2858 Returns a copy of this datetime converted to the given time -
2859 \a specification. -
2860 -
2861 Example: -
2862 \snippet code/src_corelib_tools_qdatetime.cpp 16 -
2863 -
2864 \sa timeSpec(), toUTC(), toLocalTime() -
2865*/ -
2866 -
2867QDateTime QDateTime::toTimeSpec(Qt::TimeSpec spec) const -
2868{ -
2869 if ((d->spec == QDateTimePrivate::UTC) == (spec == Qt::UTC)) -
2870 return *this; -
2871 -
2872 QDateTime ret; -
2873 if (spec == Qt::UTC) { -
2874 d->getUTC(ret.d->date, ret.d->time); -
2875 ret.d->spec = QDateTimePrivate::UTC; -
2876 } else { -
2877 ret.d->spec = d->getLocal(ret.d->date, ret.d->time); -
2878 } -
2879 return ret; -
2880} -
2881 -
2882/*! -
2883 Returns true if this datetime is equal to the \a other datetime; -
2884 otherwise returns false. -
2885 -
2886 \sa operator!=() -
2887*/ -
2888 -
2889bool QDateTime::operator==(const QDateTime &other) const -
2890{ -
2891 if (d->spec == other.d->spec && d->utcOffset == other.d->utcOffset) -
2892 return d->time == other.d->time && d->date == other.d->date; -
2893 else { -
2894 QDate date1, date2; -
2895 QTime time1, time2; -
2896 -
2897 d->getUTC(date1, time1); -
2898 other.d->getUTC(date2, time2); -
2899 return time1 == time2 && date1 == date2; -
2900 } -
2901} -
2902 -
2903/*! -
2904 \fn bool QDateTime::operator!=(const QDateTime &other) const -
2905 -
2906 Returns true if this datetime is different from the \a other -
2907 datetime; otherwise returns false. -
2908 -
2909 Two datetimes are different if either the date, the time, or the -
2910 time zone components are different. -
2911 -
2912 \sa operator==() -
2913*/ -
2914 -
2915/*! -
2916 Returns true if this datetime is earlier than the \a other -
2917 datetime; otherwise returns false. -
2918*/ -
2919 -
2920bool QDateTime::operator<(const QDateTime &other) const -
2921{ -
2922 if (d->spec == other.d->spec && d->spec != QDateTimePrivate::OffsetFromUTC) { -
2923 if (d->date != other.d->date) -
2924 return d->date < other.d->date; -
2925 return d->time < other.d->time; -
2926 } else { -
2927 QDate date1, date2; -
2928 QTime time1, time2; -
2929 d->getUTC(date1, time1); -
2930 other.d->getUTC(date2, time2); -
2931 if (date1 != date2) -
2932 return date1 < date2; -
2933 return time1 < time2; -
2934 } -
2935} -
2936 -
2937/*! -
2938 \fn bool QDateTime::operator<=(const QDateTime &other) const -
2939 -
2940 Returns true if this datetime is earlier than or equal to the -
2941 \a other datetime; otherwise returns false. -
2942*/ -
2943 -
2944/*! -
2945 \fn bool QDateTime::operator>(const QDateTime &other) const -
2946 -
2947 Returns true if this datetime is later than the \a other datetime; -
2948 otherwise returns false. -
2949*/ -
2950 -
2951/*! -
2952 \fn bool QDateTime::operator>=(const QDateTime &other) const -
2953 -
2954 Returns true if this datetime is later than or equal to the -
2955 \a other datetime; otherwise returns false. -
2956*/ -
2957 -
2958/*! -
2959 \fn QDateTime QDateTime::currentDateTime() -
2960 Returns the current datetime, as reported by the system clock, in -
2961 the local time zone. -
2962 -
2963 \sa currentDateTimeUtc(), QDate::currentDate(), QTime::currentTime(), toTimeSpec() -
2964*/ -
2965 -
2966/*! -
2967 \fn QDateTime QDateTime::currentDateTimeUtc() -
2968 \since 4.7 -
2969 Returns the current datetime, as reported by the system clock, in -
2970 UTC. -
2971 -
2972 \sa currentDateTime(), QDate::currentDate(), QTime::currentTime(), toTimeSpec() -
2973*/ -
2974 -
2975/*! -
2976 \fn qint64 QDateTime::currentMSecsSinceEpoch() -
2977 \since 4.7 -
2978 -
2979 Returns the number of milliseconds since 1970-01-01T00:00:00 Universal -
2980 Coordinated Time. This number is like the POSIX time_t variable, but -
2981 expressed in milliseconds instead. -
2982 -
2983 \sa currentDateTime(), currentDateTimeUtc(), toTime_t(), toTimeSpec() -
2984*/ -
2985 -
2986static inline uint msecsFromDecomposed(int hour, int minute, int sec, int msec = 0) -
2987{ -
2988 return MSECS_PER_HOUR * hour + MSECS_PER_MIN * minute + 1000 * sec + msec; -
2989} -
2990 -
2991#if defined(Q_OS_WIN) -
2992QDate QDate::currentDate() -
2993{ -
2994 QDate d; -
2995 SYSTEMTIME st; -
2996 memset(&st, 0, sizeof(SYSTEMTIME)); -
2997 GetLocalTime(&st); -
2998 d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay); -
2999 return d; -
3000} -
3001 -
3002QTime QTime::currentTime() -
3003{ -
3004 QTime ct; -
3005 SYSTEMTIME st; -
3006 memset(&st, 0, sizeof(SYSTEMTIME)); -
3007 GetLocalTime(&st); -
3008 ct.setHMS(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); -
3009#if defined(Q_OS_WINCE) -
3010 ct.startTick = GetTickCount() % MSECS_PER_DAY; -
3011#endif -
3012 return ct; -
3013} -
3014 -
3015QDateTime QDateTime::currentDateTime() -
3016{ -
3017 QDate d; -
3018 QTime t; -
3019 SYSTEMTIME st; -
3020 memset(&st, 0, sizeof(SYSTEMTIME)); -
3021 GetLocalTime(&st); -
3022 d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay); -
3023 t.mds = msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); -
3024 return QDateTime(d, t); -
3025} -
3026 -
3027QDateTime QDateTime::currentDateTimeUtc() -
3028{ -
3029 QDate d; -
3030 QTime t; -
3031 SYSTEMTIME st; -
3032 memset(&st, 0, sizeof(SYSTEMTIME)); -
3033 GetSystemTime(&st); -
3034 d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay); -
3035 t.mds = msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); -
3036 return QDateTime(d, t, Qt::UTC); -
3037} -
3038 -
3039qint64 QDateTime::currentMSecsSinceEpoch() Q_DECL_NOTHROW -
3040{ -
3041 QDate d; -
3042 QTime t; -
3043 SYSTEMTIME st; -
3044 memset(&st, 0, sizeof(SYSTEMTIME)); -
3045 GetSystemTime(&st); -
3046 -
3047 return msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds) + -
3048 qint64(julianDayFromDate(st.wYear, st.wMonth, st.wDay) -
3049 - julianDayFromDate(1970, 1, 1)) * Q_INT64_C(86400000); -
3050} -
3051 -
3052#elif defined(Q_OS_UNIX) -
3053QDate QDate::currentDate() -
3054{ -
3055 QDate d; -
3056 // posix compliant system -
3057 time_t ltime; -
3058 time(&ltime); -
3059 struct tm *t = 0; -
3060 -
3061#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) -
3062 // use the reentrant version of localtime() where available -
3063 tzset(); -
3064 struct tm res; -
3065 t = localtime_r(&ltime, &res); -
3066#else -
3067 t = localtime(&ltime); -
3068#endif // !QT_NO_THREAD && _POSIX_THREAD_SAFE_FUNCTIONS -
3069 -
3070 d.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday); -
3071 return d; -
3072} -
3073 -
3074QTime QTime::currentTime() -
3075{ -
3076 QTime ct; -
3077 // posix compliant system -
3078 struct timeval tv; -
3079 gettimeofday(&tv, 0); -
3080 time_t ltime = tv.tv_sec; -
3081 struct tm *t = 0; -
3082 -
3083#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) -
3084 // use the reentrant version of localtime() where available -
3085 tzset(); -
3086 struct tm res; -
3087 t = localtime_r(&ltime, &res); -
3088#else -
3089 t = localtime(&ltime); -
3090#endif -
3091 Q_CHECK_PTR(t); -
3092 -
3093 ct.mds = msecsFromDecomposed(t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000); -
3094 return ct; -
3095} -
3096 -
3097QDateTime QDateTime::currentDateTime() -
3098{ -
3099 // posix compliant system -
3100 // we have milliseconds -
3101 struct timeval tv; -
3102 gettimeofday(&tv, 0); -
3103 time_t ltime = tv.tv_sec; -
3104 struct tm *t = 0; -
3105 -
3106#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) -
3107 // use the reentrant version of localtime() where available -
3108 tzset(); -
3109 struct tm res; -
3110 t = localtime_r(&ltime, &res); -
3111#else -
3112 t = localtime(&ltime); -
3113#endif -
3114 -
3115 QDateTime dt; -
3116 dt.d->time.mds = msecsFromDecomposed(t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000); -
3117 -
3118 dt.d->date.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday); -
3119 dt.d->spec = t->tm_isdst > 0 ? QDateTimePrivate::LocalDST : -
3120 t->tm_isdst == 0 ? QDateTimePrivate::LocalStandard : -
3121 QDateTimePrivate::LocalUnknown; -
3122 return dt; -
3123} -
3124 -
3125QDateTime QDateTime::currentDateTimeUtc() -
3126{ -
3127 // posix compliant system -
3128 // we have milliseconds -
3129 struct timeval tv; -
3130 gettimeofday(&tv, 0); -
3131 time_t ltime = tv.tv_sec; -
3132 struct tm *t = 0; -
3133 -
3134#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) -
3135 // use the reentrant version of localtime() where available -
3136 struct tm res; -
3137 t = gmtime_r(&ltime, &res); -
3138#else -
3139 t = gmtime(&ltime); -
3140#endif -
3141 -
3142 QDateTime dt; -
3143 dt.d->time.mds = msecsFromDecomposed(t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000); -
3144 -
3145 dt.d->date.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday); -
3146 dt.d->spec = QDateTimePrivate::UTC; -
3147 return dt; -
3148} -
3149 -
3150qint64 QDateTime::currentMSecsSinceEpoch() Q_DECL_NOTHROW -
3151{ -
3152 // posix compliant system -
3153 // we have milliseconds -
3154 struct timeval tv; -
3155 gettimeofday(&tv, 0); -
3156 return qint64(tv.tv_sec) * Q_INT64_C(1000) + tv.tv_usec / 1000; -
3157} -
3158 -
3159#else -
3160#error "What system is this?" -
3161#endif -
3162 -
3163/*! -
3164 \since 4.2 -
3165 -
3166 Returns a datetime whose date and time are the number of \a seconds -
3167 that have passed since 1970-01-01T00:00:00, Coordinated Universal -
3168 Time (Qt::UTC). On systems that do not support time zones, the time -
3169 will be set as if local time were Qt::UTC. -
3170 -
3171 \sa toTime_t(), setTime_t() -
3172*/ -
3173QDateTime QDateTime::fromTime_t(uint seconds) -
3174{ -
3175 QDateTime d; -
3176 d.setTime_t(seconds); -
3177 return d; -
3178} -
3179 -
3180/*! -
3181 \since 4.7 -
3182 -
3183 Returns a datetime whose date and time are the number of milliseconds, \a msecs, -
3184 that have passed since 1970-01-01T00:00:00.000, Coordinated Universal -
3185 Time (Qt::UTC). On systems that do not support time zones, the time -
3186 will be set as if local time were Qt::UTC. -
3187 -
3188 Note that there are possible values for \a msecs that lie outside the valid -
3189 range of QDateTime, both negative and positive. The behavior of this -
3190 function is undefined for those values. -
3191 -
3192 \sa toTime_t(), setTime_t() -
3193*/ -
3194QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs) -
3195{ -
3196 QDateTime d; -
3197 d.setMSecsSinceEpoch(msecs); -
3198 return d; -
3199} -
3200 -
3201/*! -
3202 \since 4.4 -
3203 \internal -
3204 -
3205 Sets the offset from UTC to \a seconds, and also sets timeSpec() to -
3206 Qt::OffsetFromUTC. -
3207 -
3208 The maximum and minimum offset is 14 positive or negative hours. If -
3209 \a seconds is larger or smaller than that, the result is undefined. -
3210 -
3211 0 as offset is identical to UTC. Therefore, if \a seconds is 0, the -
3212 timeSpec() will be set to Qt::UTC. Hence the UTC offset always -
3213 relates to UTC, and can never relate to local time. -
3214 -
3215 \sa isValid(), utcOffset() -
3216 */ -
3217void QDateTime::setUtcOffset(int seconds) -
3218{ -
3219 detach(); -
3220 -
3221 /* The motivation to also setting d->spec is to ensure that the QDateTime -
3222 * instance stays in well-defined states all the time; instead of that, -
3223 * we instruct the user to ensure it. */ -
3224 if(seconds == 0) -
3225 d->spec = QDateTimePrivate::UTC; -
3226 else -
3227 d->spec = QDateTimePrivate::OffsetFromUTC; -
3228 -
3229 /* Even if seconds is 0 we assign it to utcOffset. */ -
3230 d->utcOffset = seconds; -
3231} -
3232 -
3233/*! -
3234 \since 4.4 -
3235 \internal -
3236 -
3237 Returns the UTC offset in seconds. If the timeSpec() isn't -
3238 Qt::OffsetFromUTC, 0 is returned. However, since 0 is a valid UTC -
3239 offset, the return value of this function cannot be used to determine -
3240 whether a utcOffset() is used or is valid; in that case, timeSpec() must be -
3241 checked. -
3242 -
3243 Likewise, if this QDateTime() is invalid or if timeSpec() isn't -
3244 Qt::OffsetFromUTC, 0 is returned. -
3245 -
3246 The UTC offset only applies if the timeSpec() is Qt::OffsetFromUTC. -
3247 -
3248 \sa isValid(), setUtcOffset() -
3249 */ -
3250int QDateTime::utcOffset() const -
3251{ -
3252 if(isValid() && d->spec == QDateTimePrivate::OffsetFromUTC) -
3253 return d->utcOffset; -
3254 else -
3255 return 0; -
3256} -
3257 -
3258#ifndef QT_NO_DATESTRING -
3259 -
3260static int fromShortMonthName(const QString &monthName) -
3261{ -
3262 // Assume that English monthnames are the default -
3263 for (int i = 0; i < 12; ++i) { -
3264 if (monthName == QLatin1String(qt_shortMonthNames[i])) -
3265 return i + 1; -
3266 } -
3267 // If English names can't be found, search the localized ones -
3268 for (int i = 1; i <= 12; ++i) { -
3269 if (monthName == QDate::shortMonthName(i)) -
3270 return i; -
3271 } -
3272 return -1; -
3273} -
3274 -
3275/*! -
3276 \fn QDateTime QDateTime::fromString(const QString &string, Qt::DateFormat format) -
3277 -
3278 Returns the QDateTime represented by the \a string, using the -
3279 \a format given, or an invalid datetime if this is not possible. -
3280 -
3281 Note for Qt::TextDate: It is recommended that you use the -
3282 English short month names (e.g. "Jan"). Although localized month -
3283 names can also be used, they depend on the user's locale settings. -
3284*/ -
3285QDateTime QDateTime::fromString(const QString& s, Qt::DateFormat f) -
3286{ -
3287 if (s.isEmpty()) { -
3288 return QDateTime(); -
3289 } -
3290 -
3291 switch (f) { -
3292 case Qt::ISODate: { -
3293 QString tmp = s; -
3294 Qt::TimeSpec ts = Qt::LocalTime; -
3295 QDate date = QDate::fromString(tmp.left(10), Qt::ISODate); -
3296 if (tmp.size() == 10) -
3297 return QDateTime(date); -
3298 -
3299 tmp = tmp.mid(11); -
3300 -
3301 // Recognize UTC specifications -
3302 if (tmp.endsWith(QLatin1Char('Z'))) { -
3303 ts = Qt::UTC; -
3304 tmp.chop(1); -
3305 } -
3306 -
3307 // Recognize timezone specifications -
3308 QRegExp rx(QLatin1String("[+-]")); -
3309 if (tmp.contains(rx)) { -
3310 int idx = tmp.indexOf(rx); -
3311 QString tmp2 = tmp.mid(idx); -
3312 tmp = tmp.left(idx); -
3313 bool ok = true; -
3314 int ntzhour = 1; -
3315 int ntzminute = 3; -
3316 if ( tmp2.indexOf(QLatin1Char(':')) == 3 ) -
3317 ntzminute = 4; -
3318 const int tzhour(tmp2.mid(ntzhour, 2).toInt(&ok)); -
3319 const int tzminute(tmp2.mid(ntzminute, 2).toInt(&ok)); -
3320 QTime tzt(tzhour, tzminute); -
3321 int utcOffset = (tzt.hour() * 60 + tzt.minute()) * 60; -
3322 if ( utcOffset != 0 ) { -
3323 ts = Qt::OffsetFromUTC; -
3324 QDateTime dt(date, QTime::fromString(tmp, Qt::ISODate), ts); -
3325 dt.setUtcOffset( utcOffset * (tmp2.startsWith(QLatin1Char('-')) ? -1 : 1) ); -
3326 return dt; -
3327 } -
3328 } -
3329 -
3330 bool isMidnight24 = false; -
3331 // Might be end of day (24:00, including variants), which QTime considers invalid. -
3332 QTime time(fromStringImpl(tmp, Qt::ISODate, isMidnight24)); -
3333 if (isMidnight24) { -
3334 // ISO 8601 (section 4.2.3) says that 24:00 is equivalent to 00:00 the next day. -
3335 date = date.addDays(1); -
3336 } -
3337 -
3338 return QDateTime(date, time, ts); -
3339 } -
3340 case Qt::SystemLocaleDate: -
3341 case Qt::SystemLocaleShortDate: -
3342 case Qt::SystemLocaleLongDate: -
3343 return fromString(s, QLocale::system().dateTimeFormat(f == Qt::SystemLocaleLongDate ? QLocale::LongFormat -
3344 : QLocale::ShortFormat)); -
3345 case Qt::LocaleDate: -
3346 case Qt::DefaultLocaleShortDate: -
3347 case Qt::DefaultLocaleLongDate: -
3348 return fromString(s, QLocale().dateTimeFormat(f == Qt::DefaultLocaleLongDate ? QLocale::LongFormat -
3349 : QLocale::ShortFormat)); -
3350#if !defined(QT_NO_TEXTDATE) -
3351 case Qt::TextDate: { -
3352 QStringList parts = s.split(QLatin1Char(' '), QString::SkipEmptyParts); -
3353 -
3354 if ((parts.count() < 5) || (parts.count() > 6)) { -
3355 return QDateTime(); -
3356 } -
3357 -
3358 // Accept "Sun Dec 1 13:02:00 1974" and "Sun 1. Dec 13:02:00 1974" -
3359 int month = -1, day = -1; -
3360 bool ok; -
3361 -
3362 month = fromShortMonthName(parts.at(1)); -
3363 if (month != -1) { -
3364 day = parts.at(2).toInt(&ok); -
3365 if (!ok) -
3366 day = -1; -
3367 } -
3368 -
3369 if (month == -1 || day == -1) { -
3370 // first variant failed, lets try the other -
3371 month = fromShortMonthName(parts.at(2)); -
3372 if (month != -1) { -
3373 QString dayStr = parts.at(1); -
3374 if (dayStr.endsWith(QLatin1Char('.'))) { -
3375 dayStr.chop(1); -
3376 day = dayStr.toInt(&ok); -
3377 if (!ok) -
3378 day = -1; -
3379 } else { -
3380 day = -1; -
3381 } -
3382 } -
3383 } -
3384 -
3385 if (month == -1 || day == -1) { -
3386 // both variants failed, give up -
3387 return QDateTime(); -
3388 } -
3389 -
3390 int year; -
3391 QStringList timeParts = parts.at(3).split(QLatin1Char(':')); -
3392 if ((timeParts.count() == 3) || (timeParts.count() == 2)) { -
3393 // Year is after time, e.g. "Sun Dec 1 13:02:00 1974" -
3394 year = parts.at(4).toInt(&ok); -
3395 if (!ok) -
3396 return QDateTime(); -
3397 } else { // Year is before time, e.g. "Sun Dec 1 1974 13:02:00" -
3398 timeParts = parts.at(4).split(QLatin1Char(':')); -
3399 if ((timeParts.count() != 3) && (timeParts.count() != 2)) -
3400 return QDateTime(); -
3401 year = parts.at(3).toInt(&ok); -
3402 if (!ok) -
3403 return QDateTime(); -
3404 } -
3405 -
3406 int hour = timeParts.at(0).toInt(&ok); -
3407 if (!ok) { -
3408 return QDateTime(); -
3409 } -
3410 -
3411 int minute = timeParts.at(1).toInt(&ok); -
3412 if (!ok) { -
3413 return QDateTime(); -
3414 } -
3415 -
3416 int second = (timeParts.count() > 2) ? timeParts.at(2).toInt(&ok) : 0; -
3417 if (!ok) { -
3418 return QDateTime(); -
3419 } -
3420 -
3421 QDate date(year, month, day); -
3422 QTime time(hour, minute, second); -
3423 -
3424 if (parts.count() == 5) -
3425 return QDateTime(date, time, Qt::LocalTime); -
3426 -
3427 QString tz = parts.at(5); -
3428 if (!tz.startsWith(QLatin1String("GMT"), Qt::CaseInsensitive)) -
3429 return QDateTime(); -
3430 QDateTime dt(date, time, Qt::UTC); -
3431 if (tz.length() > 3) { -
3432 int tzoffset = 0; -
3433 QChar sign = tz.at(3); -
3434 if ((sign != QLatin1Char('+')) -
3435 && (sign != QLatin1Char('-'))) { -
3436 return QDateTime(); -
3437 } -
3438 int tzhour = tz.mid(4, 2).toInt(&ok); -
3439 if (!ok) -
3440 return QDateTime(); -
3441 int tzminute = tz.mid(6).toInt(&ok); -
3442 if (!ok) -
3443 return QDateTime(); -
3444 tzoffset = (tzhour*60 + tzminute) * 60; -
3445 if (sign == QLatin1Char('-')) -
3446 tzoffset = -tzoffset; -
3447 dt.setUtcOffset(tzoffset); -
3448 } -
3449 return dt.toLocalTime(); -
3450 } -
3451#endif //QT_NO_TEXTDATE -
3452 } -
3453 -
3454 return QDateTime(); -
3455} -
3456 -
3457/*! -
3458 \fn QDateTime::fromString(const QString &string, const QString &format) -
3459 -
3460 Returns the QDateTime represented by the \a string, using the \a -
3461 format given, or an invalid datetime if the string cannot be parsed. -
3462 -
3463 These expressions may be used for the date part of the format string: -
3464 -
3465 \table -
3466 \header \li Expression \li Output -
3467 \row \li d \li the day as number without a leading zero (1 to 31) -
3468 \row \li dd \li the day as number with a leading zero (01 to 31) -
3469 \row \li ddd -
3470 \li the abbreviated localized day name (e.g. 'Mon' to 'Sun'). -
3471 Uses QDate::shortDayName(). -
3472 \row \li dddd -
3473 \li the long localized day name (e.g. 'Monday' to 'Sunday'). -
3474 Uses QDate::longDayName(). -
3475 \row \li M \li the month as number without a leading zero (1-12) -
3476 \row \li MM \li the month as number with a leading zero (01-12) -
3477 \row \li MMM -
3478 \li the abbreviated localized month name (e.g. 'Jan' to 'Dec'). -
3479 Uses QDate::shortMonthName(). -
3480 \row \li MMMM -
3481 \li the long localized month name (e.g. 'January' to 'December'). -
3482 Uses QDate::longMonthName(). -
3483 \row \li yy \li the year as two digit number (00-99) -
3484 \row \li yyyy \li the year as four digit number -
3485 \endtable -
3486 -
3487 \note Unlike the other version of this function, day and month names must -
3488 be given in the user's local language. It is only possible to use the English -
3489 names if the user's language is English. -
3490 -
3491 These expressions may be used for the time part of the format string: -
3492 -
3493 \table -
3494 \header \li Expression \li Output -
3495 \row \li h -
3496 \li the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) -
3497 \row \li hh -
3498 \li the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) -
3499 \row \li H -
3500 \li the hour without a leading zero (0 to 23, even with AM/PM display) -
3501 \row \li HH -
3502 \li the hour with a leading zero (00 to 23, even with AM/PM display) -
3503 \row \li m \li the minute without a leading zero (0 to 59) -
3504 \row \li mm \li the minute with a leading zero (00 to 59) -
3505 \row \li s \li the second without a leading zero (0 to 59) -
3506 \row \li ss \li the second with a leading zero (00 to 59) -
3507 \row \li z \li the milliseconds without leading zeroes (0 to 999) -
3508 \row \li zzz \li the milliseconds with leading zeroes (000 to 999) -
3509 \row \li AP or A -
3510 \li interpret as an AM/PM time. \e AP must be either "AM" or "PM". -
3511 \row \li ap or a -
3512 \li Interpret as an AM/PM time. \e ap must be either "am" or "pm". -
3513 \endtable -
3514 -
3515 All other input characters will be treated as text. Any sequence -
3516 of characters that are enclosed in single quotes will also be -
3517 treated as text and not be used as an expression. -
3518 -
3519 \snippet code/src_corelib_tools_qdatetime.cpp 12 -
3520 -
3521 If the format is not satisfied, an invalid QDateTime is returned. -
3522 The expressions that don't have leading zeroes (d, M, h, m, s, z) will be -
3523 greedy. This means that they will use two digits even if this will -
3524 put them outside the range and/or leave too few digits for other -
3525 sections. -
3526 -
3527 \snippet code/src_corelib_tools_qdatetime.cpp 13 -
3528 -
3529 This could have meant 1 January 00:30.00 but the M will grab -
3530 two digits. -
3531 -
3532 Incorrectly specified fields of the \a string will cause an invalid -
3533 QDateTime to be returned. For example, consider the following code, -
3534 where the two digit year 12 is read as 1912 (see the table below for all -
3535 field defaults); the resulting datetime is invalid because 23 April 1912 -
3536 was a Tuesday, not a Monday: -
3537 -
3538 \snippet code/src_corelib_tools_qdatetime.cpp 20 -
3539 -
3540 The correct code is: -
3541 -
3542 \snippet code/src_corelib_tools_qdatetime.cpp 21 -
3543 -
3544 For any field that is not represented in the format, the following -
3545 defaults are used: -
3546 -
3547 \table -
3548 \header \li Field \li Default value -
3549 \row \li Year \li 1900 -
3550 \row \li Month \li 1 (January) -
3551 \row \li Day \li 1 -
3552 \row \li Hour \li 0 -
3553 \row \li Minute \li 0 -
3554 \row \li Second \li 0 -
3555 \endtable -
3556 -
3557 For example: -
3558 -
3559 \snippet code/src_corelib_tools_qdatetime.cpp 14 -
3560 -
3561 \sa QDate::fromString(), QTime::fromString(), QDate::toString(), -
3562 QDateTime::toString(), QTime::toString() -
3563*/ -
3564 -
3565QDateTime QDateTime::fromString(const QString &string, const QString &format) -
3566{ -
3567#ifndef QT_BOOTSTRAPPED -
3568 QTime time; -
3569 QDate date; -
3570 -
3571 QDateTimeParser dt(QVariant::DateTime, QDateTimeParser::FromString); -
3572 if (dt.parseFormat(format) && dt.fromString(string, &date, &time)) -
3573 return QDateTime(date, time); -
3574#else -
3575 Q_UNUSED(string); -
3576 Q_UNUSED(format); -
3577#endif -
3578 return QDateTime(QDate(), QTime(-1, -1, -1)); -
3579} -
3580 -
3581#endif // QT_NO_DATESTRING -
3582/*! -
3583 \fn QDateTime QDateTime::toLocalTime() const -
3584 -
3585 Returns a datetime containing the date and time information in -
3586 this datetime, but specified using the Qt::LocalTime definition. -
3587 -
3588 Example: -
3589 -
3590 \snippet code/src_corelib_tools_qdatetime.cpp 17 -
3591 -
3592 \sa toTimeSpec() -
3593*/ -
3594 -
3595/*! -
3596 \fn QDateTime QDateTime::toUTC() const -
3597 -
3598 Returns a datetime containing the date and time information in -
3599 this datetime, but specified using the Qt::UTC definition. -
3600 -
3601 Example: -
3602 -
3603 \snippet code/src_corelib_tools_qdatetime.cpp 18 -
3604 -
3605 \sa toTimeSpec() -
3606*/ -
3607 -
3608/*! -
3609 \internal -
3610 */ -
3611void QDateTime::detach() -
3612{ -
3613 d.detach(); -
3614} -
3615 -
3616/***************************************************************************** -
3617 Date/time stream functions -
3618 *****************************************************************************/ -
3619 -
3620#ifndef QT_NO_DATASTREAM -
3621/*! -
3622 \relates QDate -
3623 -
3624 Writes the \a date to stream \a out. -
3625 -
3626 \sa {Serializing Qt Data Types} -
3627*/ -
3628 -
3629QDataStream &operator<<(QDataStream &out, const QDate &date) -
3630{ -
3631 if (out.version() < QDataStream::Qt_5_0) -
3632 return out << quint32(date.jd); -
3633 else -
3634 return out << qint64(date.jd); -
3635} -
3636 -
3637/*! -
3638 \relates QDate -
3639 -
3640 Reads a date from stream \a in into the \a date. -
3641 -
3642 \sa {Serializing Qt Data Types} -
3643*/ -
3644 -
3645QDataStream &operator>>(QDataStream &in, QDate &date) -
3646{ -
3647 if (in.version() < QDataStream::Qt_5_0) { -
3648 quint32 jd; -
3649 in >> jd; -
3650 // Older versions consider 0 an invalid jd. -
3651 date.jd = (jd != 0 ? jd : QDate::nullJd()); -
3652 } else { -
3653 qint64 jd; -
3654 in >> jd; -
3655 date.jd = jd; -
3656 } -
3657 -
3658 return in; -
3659} -
3660 -
3661/*! -
3662 \relates QTime -
3663 -
3664 Writes \a time to stream \a out. -
3665 -
3666 \sa {Serializing Qt Data Types} -
3667*/ -
3668 -
3669QDataStream &operator<<(QDataStream &out, const QTime &time) -
3670{ -
3671 return out << quint32(time.mds); -
3672} -
3673 -
3674/*! -
3675 \relates QTime -
3676 -
3677 Reads a time from stream \a in into the given \a time. -
3678 -
3679 \sa {Serializing Qt Data Types} -
3680*/ -
3681 -
3682QDataStream &operator>>(QDataStream &in, QTime &time) -
3683{ -
3684 quint32 ds; -
3685 in >> ds; -
3686 time.mds = int(ds); -
3687 return in; -
3688} -
3689 -
3690/*! -
3691 \relates QDateTime -
3692 -
3693 Writes \a dateTime to the \a out stream. -
3694 -
3695 \sa {Serializing Qt Data Types} -
3696*/ -
3697QDataStream &operator<<(QDataStream &out, const QDateTime &dateTime) -
3698{ -
3699 if (out.version() >= 13) { -
3700 if (dateTime.isValid()) { -
3701 QDateTime asUTC = dateTime.toUTC(); -
3702 out << asUTC.d->date << asUTC.d->time; -
3703 } else { -
3704 out << dateTime.d->date << dateTime.d->time; -
3705 } -
3706 out << (qint8)dateTime.timeSpec(); -
3707 } else { -
3708 out << dateTime.d->date << dateTime.d->time; -
3709 if (out.version() >= 7) -
3710 out << (qint8)dateTime.d->spec; -
3711 } -
3712 return out; -
3713} -
3714 -
3715/*! -
3716 \relates QDateTime -
3717 -
3718 Reads a datetime from the stream \a in into \a dateTime. -
3719 -
3720 \sa {Serializing Qt Data Types} -
3721*/ -
3722 -
3723QDataStream &operator>>(QDataStream &in, QDateTime &dateTime) -
3724{ -
3725 dateTime.detach(); -
3726 -
3727 in >> dateTime.d->date >> dateTime.d->time; -
3728 -
3729 if (in.version() >= 13) { -
3730 qint8 ts = 0; -
3731 in >> ts; -
3732 if (dateTime.isValid()) { -
3733 // We always store the datetime as UTC in 13 onwards. -
3734 dateTime.d->spec = QDateTimePrivate::UTC; -
3735 dateTime = dateTime.toTimeSpec(static_cast<Qt::TimeSpec>(ts)); -
3736 } -
3737 } else { -
3738 qint8 ts = (qint8)QDateTimePrivate::LocalUnknown; -
3739 if (in.version() >= 7) -
3740 in >> ts; -
3741 dateTime.d->spec = (QDateTimePrivate::Spec)ts; -
3742 } -
3743 return in; -
3744} -
3745#endif // QT_NO_DATASTREAM -
3746 -
3747 -
3748 -
3749// checks if there is an unquoted 'AP' or 'ap' in the string -
3750static bool hasUnquotedAP(const QString &f) -
3751{ -
3752 const QLatin1Char quote('\'');
executed (the execution status of this line is deduced): const QLatin1Char quote('\'');
-
3753 bool inquote = false;
executed (the execution status of this line is deduced): bool inquote = false;
-
3754 const int max = f.size();
executed (the execution status of this line is deduced): const int max = f.size();
-
3755 for (int i=0; i<max; ++i) {
evaluated: i<max
TRUEFALSE
yes
Evaluation Count:322540
yes
Evaluation Count:27883
27883-322540
3756 if (f.at(i) == quote) {
evaluated: f.at(i) == quote
TRUEFALSE
yes
Evaluation Count:24
yes
Evaluation Count:322516
24-322516
3757 inquote = !inquote;
executed (the execution status of this line is deduced): inquote = !inquote;
-
3758 } else if (!inquote && f.at(i).toUpper() == QLatin1Char('A')8-322493
&& i + 1 < max && f.at(i + 1).toUpper() == QLatin1Char('P')) {
executed: }
Execution Count:24
evaluated: !inquote
TRUEFALSE
yes
Evaluation Count:322493
yes
Evaluation Count:23
evaluated: f.at(i).toUpper() == QLatin1Char('A')
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:322485
3759 return true;
executed: return true;
Execution Count:8
8
3760 } -
3761 } -
3762 return false;
executed: return false;
Execution Count:27883
27883
3763} -
3764 -
3765#ifndef QT_NO_DATESTRING -
3766/***************************************************************************** -
3767 Some static function used by QDate, QTime and QDateTime -
3768*****************************************************************************/ -
3769 -
3770// Replaces tokens by their value. See QDateTime::toString() for a list of valid tokens -
3771static QString getFmtString(const QString& f, const QTime* dt = 0, const QDate* dd = 0, bool am_pm = false) -
3772{ -
3773 if (f.isEmpty()) -
3774 return QString(); -
3775 -
3776 QString buf = f; -
3777 int removed = 0; -
3778 -
3779 if (dt) { -
3780 if (f.startsWith(QLatin1String("hh")) || f.startsWith(QLatin1String("HH"))) { -
3781 const bool hour12 = f.at(0) == QLatin1Char('h') && am_pm; -
3782 if (hour12 && dt->hour() > 12) -
3783 buf = QString::number(dt->hour() - 12).rightJustified(2, QLatin1Char('0'), true); -
3784 else if (hour12 && dt->hour() == 0) -
3785 buf = QLatin1String("12"); -
3786 else -
3787 buf = QString::number(dt->hour()).rightJustified(2, QLatin1Char('0'), true); -
3788 removed = 2; -
3789 } else if (f.at(0) == QLatin1Char('h') || f.at(0) == QLatin1Char('H')) { -
3790 const bool hour12 = f.at(0) == QLatin1Char('h') && am_pm; -
3791 if (hour12 && dt->hour() > 12) -
3792 buf = QString::number(dt->hour() - 12); -
3793 else if (hour12 && dt->hour() == 0) -
3794 buf = QLatin1String("12"); -
3795 else -
3796 buf = QString::number(dt->hour()); -
3797 removed = 1; -
3798 } else if (f.startsWith(QLatin1String("mm"))) { -
3799 buf = QString::number(dt->minute()).rightJustified(2, QLatin1Char('0'), true); -
3800 removed = 2; -
3801 } else if (f.at(0) == (QLatin1Char('m'))) { -
3802 buf = QString::number(dt->minute()); -
3803 removed = 1; -
3804 } else if (f.startsWith(QLatin1String("ss"))) { -
3805 buf = QString::number(dt->second()).rightJustified(2, QLatin1Char('0'), true); -
3806 removed = 2; -
3807 } else if (f.at(0) == QLatin1Char('s')) { -
3808 buf = QString::number(dt->second()); -
3809 } else if (f.startsWith(QLatin1String("zzz"))) { -
3810 buf = QString::number(dt->msec()).rightJustified(3, QLatin1Char('0'), true); -
3811 removed = 3; -
3812 } else if (f.at(0) == QLatin1Char('z')) { -
3813 buf = QString::number(dt->msec()); -
3814 removed = 1; -
3815 } else if (f.at(0).toUpper() == QLatin1Char('A')) { -
3816 const bool upper = f.at(0) == QLatin1Char('A'); -
3817 buf = dt->hour() < 12 ? QLatin1String("am") : QLatin1String("pm"); -
3818 if (upper) -
3819 buf = buf.toUpper(); -
3820 if (f.size() > 1 && f.at(1).toUpper() == QLatin1Char('P') && -
3821 f.at(0).isUpper() == f.at(1).isUpper()) { -
3822 removed = 2; -
3823 } else { -
3824 removed = 1; -
3825 } -
3826 } -
3827 } -
3828 -
3829 if (dd) { -
3830 if (f.startsWith(QLatin1String("dddd"))) { -
3831 buf = dd->longDayName(dd->dayOfWeek()); -
3832 removed = 4; -
3833 } else if (f.startsWith(QLatin1String("ddd"))) { -
3834 buf = dd->shortDayName(dd->dayOfWeek()); -
3835 removed = 3; -
3836 } else if (f.startsWith(QLatin1String("dd"))) { -
3837 buf = QString::number(dd->day()).rightJustified(2, QLatin1Char('0'), true); -
3838 removed = 2; -
3839 } else if (f.at(0) == QLatin1Char('d')) { -
3840 buf = QString::number(dd->day()); -
3841 removed = 1; -
3842 } else if (f.startsWith(QLatin1String("MMMM"))) { -
3843 buf = dd->longMonthName(dd->month()); -
3844 removed = 4; -
3845 } else if (f.startsWith(QLatin1String("MMM"))) { -
3846 buf = dd->shortMonthName(dd->month()); -
3847 removed = 3; -
3848 } else if (f.startsWith(QLatin1String("MM"))) { -
3849 buf = QString::number(dd->month()).rightJustified(2, QLatin1Char('0'), true); -
3850 removed = 2; -
3851 } else if (f.at(0) == QLatin1Char('M')) { -
3852 buf = QString::number(dd->month()); -
3853 removed = 1; -
3854 } else if (f.startsWith(QLatin1String("yyyy"))) { -
3855 const int year = dd->year(); -
3856 buf = QString::number(qAbs(year)).rightJustified(4, QLatin1Char('0')); -
3857 if(year > 0) -
3858 removed = 4; -
3859 else -
3860 { -
3861 buf.prepend(QLatin1Char('-')); -
3862 removed = 5; -
3863 } -
3864 -
3865 } else if (f.startsWith(QLatin1String("yy"))) { -
3866 buf = QString::number(dd->year()).right(2).rightJustified(2, QLatin1Char('0')); -
3867 removed = 2; -
3868 } -
3869 } -
3870 if (removed == 0 || removed >= f.size()) { -
3871 return buf; -
3872 } -
3873 -
3874 return buf + getFmtString(f.mid(removed), dt, dd, am_pm); -
3875} -
3876 -
3877// Parses the format string and uses getFmtString to get the values for the tokens. Ret -
3878static QString fmtDateTime(const QString& f, const QTime* dt, const QDate* dd) -
3879{ -
3880 QString buf; -
3881 -
3882 if (f.isEmpty()) -
3883 return buf; -
3884 if (dt && !dt->isValid()) -
3885 return buf; -
3886 if (dd && !dd->isValid()) -
3887 return buf; -
3888 -
3889 const bool ap = hasUnquotedAP(f); -
3890 -
3891 QString frm; -
3892 uint status = '0'; -
3893 -
3894 for (int i = 0, n = f.length(); i < n; ++i) { -
3895 const QChar c = f.at(i); -
3896 const uint cc = c.unicode(); -
3897 if (cc == '\'') { -
3898 if (status == cc) { -
3899 if (i > 0 && f.at(i - 1).unicode() == cc) -
3900 buf += c; -
3901 status = '0'; -
3902 } else { -
3903 if (!frm.isEmpty()) { -
3904 buf += getFmtString(frm, dt, dd, ap); -
3905 frm.clear(); -
3906 } -
3907 status = cc; -
3908 } -
3909 } else if (status == '\'') { -
3910 buf += c; -
3911 } else if (c == status) { -
3912 if (ap && (cc == 'P' || cc == 'p')) -
3913 status = '0'; -
3914 frm += c; -
3915 } else { -
3916 buf += getFmtString(frm, dt, dd, ap); -
3917 frm.clear(); -
3918 if (cc == 'h' || cc == 'm' || cc == 'H' || cc == 's' || cc == 'z') { -
3919 status = cc; -
3920 frm += c; -
3921 } else if (cc == 'd' || cc == 'M' || cc == 'y') { -
3922 status = cc; -
3923 frm += c; -
3924 } else if (ap && cc == 'A') { -
3925 status = 'P'; -
3926 frm += c; -
3927 } else if (ap && cc == 'a') { -
3928 status = 'p'; -
3929 frm += c; -
3930 } else { -
3931 buf += c; -
3932 status = '0'; -
3933 } -
3934 } -
3935 } -
3936 -
3937 buf += getFmtString(frm, dt, dd, ap); -
3938 -
3939 return buf; -
3940} -
3941#endif // QT_NO_DATESTRING -
3942 -
3943#ifdef Q_OS_WIN -
3944static const int LowerYear = 1980; -
3945#else -
3946static const int LowerYear = 1970; -
3947#endif -
3948static const int UpperYear = 2037; -
3949 -
3950static QDate adjustDate(QDate date) -
3951{ -
3952 QDate lowerLimit(LowerYear, 1, 2); -
3953 QDate upperLimit(UpperYear, 12, 30); -
3954 -
3955 if (date > lowerLimit && date < upperLimit) -
3956 return date; -
3957 -
3958 int month = date.month(); -
3959 int day = date.day(); -
3960 -
3961 // neither 1970 nor 2037 are leap years, so make sure date isn't Feb 29 -
3962 if (month == 2 && day == 29) -
3963 --day; -
3964 -
3965 if (date < lowerLimit) -
3966 date.setDate(LowerYear, month, day); -
3967 else -
3968 date.setDate(UpperYear, month, day); -
3969 -
3970 return date; -
3971} -
3972 -
3973static QDateTimePrivate::Spec utcToLocal(QDate &date, QTime &time) -
3974{ -
3975 QDate fakeDate = adjustDate(date); -
3976 -
3977 // won't overflow because of fakeDate -
3978 time_t secsSince1Jan1970UTC = toMSecsSinceEpoch_helper(fakeDate.toJulianDay(), QTime(0, 0, 0).msecsTo(time)) / 1000; -
3979 tm *brokenDown = 0; -
3980 -
3981#if defined(Q_OS_WINCE) -
3982 tm res; -
3983 FILETIME utcTime = time_tToFt(secsSince1Jan1970UTC); -
3984 FILETIME resultTime; -
3985 FileTimeToLocalFileTime(&utcTime , &resultTime); -
3986 SYSTEMTIME sysTime; -
3987 FileTimeToSystemTime(&resultTime , &sysTime); -
3988 -
3989 res.tm_sec = sysTime.wSecond; -
3990 res.tm_min = sysTime.wMinute; -
3991 res.tm_hour = sysTime.wHour; -
3992 res.tm_mday = sysTime.wDay; -
3993 res.tm_mon = sysTime.wMonth - 1; -
3994 res.tm_year = sysTime.wYear - 1900; -
3995 brokenDown = &res; -
3996#elif !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) -
3997 // use the reentrant version of localtime() where available -
3998 tzset(); -
3999 tm res; -
4000 brokenDown = localtime_r(&secsSince1Jan1970UTC, &res); -
4001#elif defined(_MSC_VER) && _MSC_VER >= 1400 -
4002 tm res; -
4003 if (!_localtime64_s(&res, &secsSince1Jan1970UTC)) -
4004 brokenDown = &res; -
4005#else -
4006 brokenDown = localtime(&secsSince1Jan1970UTC); -
4007#endif -
4008 if (!brokenDown) { -
4009 date = QDate(1970, 1, 1); -
4010 time = QTime(); -
4011 return QDateTimePrivate::LocalUnknown; -
4012 } else { -
4013 int deltaDays = fakeDate.daysTo(date); -
4014 date = QDate(brokenDown->tm_year + 1900, brokenDown->tm_mon + 1, brokenDown->tm_mday); -
4015 time = QTime(brokenDown->tm_hour, brokenDown->tm_min, brokenDown->tm_sec, time.msec()); -
4016 date = date.addDays(deltaDays); -
4017 if (brokenDown->tm_isdst > 0) -
4018 return QDateTimePrivate::LocalDST; -
4019 else if (brokenDown->tm_isdst < 0) -
4020 return QDateTimePrivate::LocalUnknown; -
4021 else -
4022 return QDateTimePrivate::LocalStandard; -
4023 } -
4024} -
4025 -
4026static void localToUtc(QDate &date, QTime &time, int isdst) -
4027{ -
4028 if (!date.isValid()) -
4029 return; -
4030 -
4031 QDate fakeDate = adjustDate(date); -
4032 -
4033 tm localTM; -
4034 localTM.tm_sec = time.second(); -
4035 localTM.tm_min = time.minute(); -
4036 localTM.tm_hour = time.hour(); -
4037 localTM.tm_mday = fakeDate.day(); -
4038 localTM.tm_mon = fakeDate.month() - 1; -
4039 localTM.tm_year = fakeDate.year() - 1900; -
4040 localTM.tm_isdst = (int)isdst; -
4041#if defined(Q_OS_WINCE) -
4042 time_t secsSince1Jan1970UTC = (toMSecsSinceEpoch_helper(fakeDate.toJulianDay(), QTime().msecsTo(time)) / 1000); -
4043#else -
4044#if defined(Q_OS_WIN) -
4045 _tzset(); -
4046#endif -
4047 time_t secsSince1Jan1970UTC = mktime(&localTM); -
4048#endif -
4049 tm *brokenDown = 0; -
4050#if defined(Q_OS_WINCE) -
4051 tm res; -
4052 FILETIME localTime = time_tToFt(secsSince1Jan1970UTC); -
4053 SYSTEMTIME sysTime; -
4054 FileTimeToSystemTime(&localTime, &sysTime); -
4055 FILETIME resultTime; -
4056 LocalFileTimeToFileTime(&localTime , &resultTime); -
4057 FileTimeToSystemTime(&resultTime , &sysTime); -
4058 res.tm_sec = sysTime.wSecond; -
4059 res.tm_min = sysTime.wMinute; -
4060 res.tm_hour = sysTime.wHour; -
4061 res.tm_mday = sysTime.wDay; -
4062 res.tm_mon = sysTime.wMonth - 1; -
4063 res.tm_year = sysTime.wYear - 1900; -
4064 res.tm_isdst = (int)isdst; -
4065 brokenDown = &res; -
4066#elif !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) -
4067 // use the reentrant version of gmtime() where available -
4068 tm res; -
4069 brokenDown = gmtime_r(&secsSince1Jan1970UTC, &res); -
4070#elif defined(_MSC_VER) && _MSC_VER >= 1400 -
4071 tm res; -
4072 if (!_gmtime64_s(&res, &secsSince1Jan1970UTC)) -
4073 brokenDown = &res; -
4074#else -
4075 brokenDown = gmtime(&secsSince1Jan1970UTC); -
4076#endif // !QT_NO_THREAD && _POSIX_THREAD_SAFE_FUNCTIONS -
4077 if (!brokenDown) { -
4078 date = QDate(1970, 1, 1); -
4079 time = QTime(); -
4080 } else { -
4081 int deltaDays = fakeDate.daysTo(date); -
4082 date = QDate(brokenDown->tm_year + 1900, brokenDown->tm_mon + 1, brokenDown->tm_mday); -
4083 time = QTime(brokenDown->tm_hour, brokenDown->tm_min, brokenDown->tm_sec, time.msec()); -
4084 date = date.addDays(deltaDays); -
4085 } -
4086} -
4087 -
4088QDateTimePrivate::Spec QDateTimePrivate::getLocal(QDate &outDate, QTime &outTime) const -
4089{ -
4090 outDate = date; -
4091 outTime = time; -
4092 if (spec == QDateTimePrivate::UTC) -
4093 return utcToLocal(outDate, outTime); -
4094 return spec; -
4095} -
4096 -
4097void QDateTimePrivate::getUTC(QDate &outDate, QTime &outTime) const -
4098{ -
4099 outDate = date; -
4100 outTime = time; -
4101 const bool isOffset = spec == QDateTimePrivate::OffsetFromUTC; -
4102 -
4103 if (spec != QDateTimePrivate::UTC && !isOffset) -
4104 localToUtc(outDate, outTime, (int)spec); -
4105 -
4106 if (isOffset) -
4107 addMSecs(outDate, outTime, -(qint64(utcOffset) * 1000)); -
4108} -
4109 -
4110#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_NO_DATESTRING) -
4111QDebug operator<<(QDebug dbg, const QDate &date) -
4112{ -
4113 dbg.nospace() << "QDate(" << date.toString() << ')'; -
4114 return dbg.space(); -
4115} -
4116 -
4117QDebug operator<<(QDebug dbg, const QTime &time) -
4118{ -
4119 dbg.nospace() << "QTime(" << time.toString() << ')'; -
4120 return dbg.space(); -
4121} -
4122 -
4123QDebug operator<<(QDebug dbg, const QDateTime &date) -
4124{ -
4125 dbg.nospace() << "QDateTime(" << date.toString() << ')'; -
4126 return dbg.space(); -
4127} -
4128#endif -
4129 -
4130/*! \fn uint qHash(const QDateTime &key, uint seed = 0) -
4131 \relates QHash -
4132 \since 5.0 -
4133 -
4134 Returns the hash value for the \a key, using \a seed to seed the calculation. -
4135*/ -
4136uint qHash(const QDateTime &key, uint seed) -
4137{ -
4138 // Use to toMSecsSinceEpoch instead of individual qHash functions for -
4139 // QDate/QTime/spec/offset because QDateTime::operator== converts both arguments -
4140 // to the same timezone. If we don't, qHash would return different hashes for -
4141 // two QDateTimes that are equivalent once converted to the same timezone. -
4142 return qHash(key.toMSecsSinceEpoch(), seed); -
4143} -
4144 -
4145/*! \fn uint qHash(const QDate &key, uint seed = 0) -
4146 \relates QHash -
4147 \since 5.0 -
4148 -
4149 Returns the hash value for the \a key, using \a seed to seed the calculation. -
4150*/ -
4151uint qHash(const QDate &key, uint seed) Q_DECL_NOTHROW -
4152{ -
4153 return qHash(key.toJulianDay(), seed); -
4154} -
4155 -
4156/*! \fn uint qHash(const QTime &key, uint seed = 0) -
4157 \relates QHash -
4158 \since 5.0 -
4159 -
4160 Returns the hash value for the \a key, using \a seed to seed the calculation. -
4161*/ -
4162uint qHash(const QTime &key, uint seed) Q_DECL_NOTHROW -
4163{ -
4164 return qHash(QTime(0, 0, 0, 0).msecsTo(key), seed); -
4165} -
4166 -
4167#ifndef QT_BOOTSTRAPPED -
4168 -
4169/*! -
4170 \internal -
4171 Gets the digit from a datetime. E.g. -
4172 -
4173 QDateTime var(QDate(2004, 02, 02)); -
4174 int digit = getDigit(var, Year); -
4175 // digit = 2004 -
4176*/ -
4177 -
4178int QDateTimeParser::getDigit(const QDateTime &t, int index) const -
4179{ -
4180 if (index < 0 || index >= sectionNodes.size()) { -
4181#ifndef QT_NO_DATESTRING -
4182 qWarning("QDateTimeParser::getDigit() Internal error (%s %d)", -
4183 qPrintable(t.toString()), index); -
4184#else -
4185 qWarning("QDateTimeParser::getDigit() Internal error (%d)", index); -
4186#endif -
4187 return -1; -
4188 } -
4189 const SectionNode &node = sectionNodes.at(index); -
4190 switch (node.type) { -
4191 case Hour24Section: case Hour12Section: return t.time().hour(); -
4192 case MinuteSection: return t.time().minute(); -
4193 case SecondSection: return t.time().second(); -
4194 case MSecSection: return t.time().msec(); -
4195 case YearSection2Digits: -
4196 case YearSection: return t.date().year(); -
4197 case MonthSection: return t.date().month(); -
4198 case DaySection: return t.date().day(); -
4199 case DayOfWeekSectionShort: -
4200 case DayOfWeekSectionLong: return t.date().day(); -
4201 case AmPmSection: return t.time().hour() > 11 ? 1 : 0; -
4202 -
4203 default: break; -
4204 } -
4205 -
4206#ifndef QT_NO_DATESTRING -
4207 qWarning("QDateTimeParser::getDigit() Internal error 2 (%s %d)", -
4208 qPrintable(t.toString()), index); -
4209#else -
4210 qWarning("QDateTimeParser::getDigit() Internal error 2 (%d)", index); -
4211#endif -
4212 return -1; -
4213} -
4214 -
4215/*! -
4216 \internal -
4217 Sets a digit in a datetime. E.g. -
4218 -
4219 QDateTime var(QDate(2004, 02, 02)); -
4220 int digit = getDigit(var, Year); -
4221 // digit = 2004 -
4222 setDigit(&var, Year, 2005); -
4223 digit = getDigit(var, Year); -
4224 // digit = 2005 -
4225*/ -
4226 -
4227bool QDateTimeParser::setDigit(QDateTime &v, int index, int newVal) const -
4228{ -
4229 if (index < 0 || index >= sectionNodes.size()) { -
4230#ifndef QT_NO_DATESTRING -
4231 qWarning("QDateTimeParser::setDigit() Internal error (%s %d %d)", -
4232 qPrintable(v.toString()), index, newVal); -
4233#else -
4234 qWarning("QDateTimeParser::setDigit() Internal error (%d %d)", index, newVal); -
4235#endif -
4236 return false; -
4237 } -
4238 const SectionNode &node = sectionNodes.at(index); -
4239 -
4240 int year, month, day, hour, minute, second, msec; -
4241 year = v.date().year(); -
4242 month = v.date().month(); -
4243 day = v.date().day(); -
4244 hour = v.time().hour(); -
4245 minute = v.time().minute(); -
4246 second = v.time().second(); -
4247 msec = v.time().msec(); -
4248 -
4249 switch (node.type) { -
4250 case Hour24Section: case Hour12Section: hour = newVal; break; -
4251 case MinuteSection: minute = newVal; break; -
4252 case SecondSection: second = newVal; break; -
4253 case MSecSection: msec = newVal; break; -
4254 case YearSection2Digits: -
4255 case YearSection: year = newVal; break; -
4256 case MonthSection: month = newVal; break; -
4257 case DaySection: -
4258 case DayOfWeekSectionShort: -
4259 case DayOfWeekSectionLong: -
4260 if (newVal > 31) { -
4261 // have to keep legacy behavior. setting the -
4262 // date to 32 should return false. Setting it -
4263 // to 31 for february should return true -
4264 return false; -
4265 } -
4266 day = newVal; -
4267 break; -
4268 case AmPmSection: hour = (newVal == 0 ? hour % 12 : (hour % 12) + 12); break; -
4269 default: -
4270 qWarning("QDateTimeParser::setDigit() Internal error (%s)", -
4271 qPrintable(sectionName(node.type))); -
4272 break; -
4273 } -
4274 -
4275 if (!(node.type & (DaySection|DayOfWeekSectionShort|DayOfWeekSectionLong))) { -
4276 if (day < cachedDay) -
4277 day = cachedDay; -
4278 const int max = QDate(year, month, 1).daysInMonth(); -
4279 if (day > max) { -
4280 day = max; -
4281 } -
4282 } -
4283 if (QDate::isValid(year, month, day) && QTime::isValid(hour, minute, second, msec)) { -
4284 v = QDateTime(QDate(year, month, day), QTime(hour, minute, second, msec), spec); -
4285 return true; -
4286 } -
4287 return false; -
4288} -
4289 -
4290 -
4291 -
4292/*! -
4293 \ -
4294 -
4295 Returns the absolute maximum for a section -
4296*/ -
4297 -
4298int QDateTimeParser::absoluteMax(int s, const QDateTime &cur) const -
4299{ -
4300 const SectionNode &sn = sectionNode(s); -
4301 switch (sn.type) { -
4302 case Hour24Section: -
4303 case Hour12Section: return 23; // this is special-cased in -
4304 // parseSection. We want it to be -
4305 // 23 for the stepBy case. -
4306 case MinuteSection: -
4307 case SecondSection: return 59; -
4308 case MSecSection: return 999; -
4309 case YearSection2Digits: -
4310 case YearSection: return 9999; // sectionMaxSize will prevent -
4311 // people from typing in a larger -
4312 // number in count == 2 sections. -
4313 // stepBy() will work on real years anyway -
4314 case MonthSection: return 12; -
4315 case DaySection: -
4316 case DayOfWeekSectionShort: -
4317 case DayOfWeekSectionLong: return cur.isValid() ? cur.date().daysInMonth() : 31; -
4318 case AmPmSection: return 1; -
4319 default: break; -
4320 } -
4321 qWarning("QDateTimeParser::absoluteMax() Internal error (%s)", -
4322 qPrintable(sectionName(sn.type))); -
4323 return -1; -
4324} -
4325 -
4326/*! -
4327 \internal -
4328 -
4329 Returns the absolute minimum for a section -
4330*/ -
4331 -
4332int QDateTimeParser::absoluteMin(int s) const -
4333{ -
4334 const SectionNode &sn = sectionNode(s); -
4335 switch (sn.type) { -
4336 case Hour24Section: -
4337 case Hour12Section: -
4338 case MinuteSection: -
4339 case SecondSection: -
4340 case MSecSection: -
4341 case YearSection2Digits: -
4342 case YearSection: return 0; -
4343 case MonthSection: -
4344 case DaySection: -
4345 case DayOfWeekSectionShort: -
4346 case DayOfWeekSectionLong: return 1; -
4347 case AmPmSection: return 0; -
4348 default: break; -
4349 } -
4350 qWarning("QDateTimeParser::absoluteMin() Internal error (%s, %0x)", -
4351 qPrintable(sectionName(sn.type)), sn.type); -
4352 return -1; -
4353} -
4354 -
4355/*! -
4356 \internal -
4357 -
4358 Returns the sectionNode for the Section \a s. -
4359*/ -
4360 -
4361const QDateTimeParser::SectionNode &QDateTimeParser::sectionNode(int sectionIndex) const -
4362{ -
4363 if (sectionIndex < 0) { -
4364 switch (sectionIndex) { -
4365 case FirstSectionIndex: -
4366 return first; -
4367 case LastSectionIndex: -
4368 return last; -
4369 case NoSectionIndex: -
4370 return none; -
4371 } -
4372 } else if (sectionIndex < sectionNodes.size()) { -
4373 return sectionNodes.at(sectionIndex); -
4374 } -
4375 -
4376 qWarning("QDateTimeParser::sectionNode() Internal error (%d)", -
4377 sectionIndex); -
4378 return none; -
4379} -
4380 -
4381QDateTimeParser::Section QDateTimeParser::sectionType(int sectionIndex) const -
4382{ -
4383 return sectionNode(sectionIndex).type; -
4384} -
4385 -
4386 -
4387/*! -
4388 \internal -
4389 -
4390 Returns the starting position for section \a s. -
4391*/ -
4392 -
4393int QDateTimeParser::sectionPos(int sectionIndex) const -
4394{ -
4395 return sectionPos(sectionNode(sectionIndex)); -
4396} -
4397 -
4398int QDateTimeParser::sectionPos(const SectionNode &sn) const -
4399{ -
4400 switch (sn.type) { -
4401 case FirstSection: return 0; -
4402 case LastSection: return displayText().size() - 1; -
4403 default: break; -
4404 } -
4405 if (sn.pos == -1) { -
4406 qWarning("QDateTimeParser::sectionPos Internal error (%s)", qPrintable(sectionName(sn.type))); -
4407 return -1; -
4408 } -
4409 return sn.pos; -
4410} -
4411 -
4412 -
4413/*! -
4414 \internal -
4415 -
4416 helper function for parseFormat. removes quotes that are -
4417 not escaped and removes the escaping on those that are escaped -
4418 -
4419*/ -
4420 -
4421static QString unquote(const QString &str) -
4422{ -
4423 const QChar quote(QLatin1Char('\'')); -
4424 const QChar slash(QLatin1Char('\\')); -
4425 const QChar zero(QLatin1Char('0')); -
4426 QString ret; -
4427 QChar status(zero); -
4428 const int max = str.size(); -
4429 for (int i=0; i<max; ++i) { -
4430 if (str.at(i) == quote) { -
4431 if (status != quote) { -
4432 status = quote; -
4433 } else if (!ret.isEmpty() && str.at(i - 1) == slash) { -
4434 ret[ret.size() - 1] = quote; -
4435 } else { -
4436 status = zero; -
4437 } -
4438 } else { -
4439 ret += str.at(i); -
4440 } -
4441 } -
4442 return ret; -
4443} -
4444/*! -
4445 \internal -
4446 -
4447 Parses the format \a newFormat. If successful, returns true and -
4448 sets up the format. Else keeps the old format and returns false. -
4449 -
4450*/ -
4451 -
4452static inline int countRepeat(const QString &str, int index, int maxCount) -
4453{ -
4454 int count = 1; -
4455 const QChar ch(str.at(index)); -
4456 const int max = qMin(index + maxCount, str.size()); -
4457 while (index + count < max && str.at(index + count) == ch) { -
4458 ++count; -
4459 } -
4460 return count; -
4461} -
4462 -
4463static inline void appendSeparator(QStringList *list, const QString &string, int from, int size, int lastQuote) -
4464{ -
4465 QString str(string.mid(from, size)); -
4466 if (lastQuote >= from) -
4467 str = unquote(str); -
4468 list->append(str); -
4469} -
4470 -
4471 -
4472bool QDateTimeParser::parseFormat(const QString &newFormat) -
4473{ -
4474 const QLatin1Char quote('\''); -
4475 const QLatin1Char slash('\\'); -
4476 const QLatin1Char zero('0'); -
4477 if (newFormat == displayFormat && !newFormat.isEmpty()) { -
4478 return true; -
4479 } -
4480 -
4481 QDTPDEBUGN("parseFormat: %s", newFormat.toLatin1().constData()); -
4482 -
4483 QVector<SectionNode> newSectionNodes; -
4484 Sections newDisplay = 0; -
4485 QStringList newSeparators; -
4486 int i, index = 0; -
4487 int add = 0; -
4488 QChar status(zero); -
4489 const int max = newFormat.size(); -
4490 int lastQuote = -1; -
4491 for (i = 0; i<max; ++i) { -
4492 if (newFormat.at(i) == quote) { -
4493 lastQuote = i; -
4494 ++add; -
4495 if (status != quote) { -
4496 status = quote; -
4497 } else if (newFormat.at(i - 1) != slash) { -
4498 status = zero; -
4499 } -
4500 } else if (status != quote) { -
4501 const char sect = newFormat.at(i).toLatin1(); -
4502 switch (sect) { -
4503 case 'H': -
4504 case 'h': -
4505 if (parserType != QVariant::Date) { -
4506 const Section hour = (sect == 'h') ? Hour12Section : Hour24Section; -
4507 const SectionNode sn = { hour, i - add, countRepeat(newFormat, i, 2), 0 }; -
4508 newSectionNodes.append(sn); -
4509 appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote); -
4510 i += sn.count - 1; -
4511 index = i + 1; -
4512 newDisplay |= hour; -
4513 } -
4514 break; -
4515 case 'm': -
4516 if (parserType != QVariant::Date) { -
4517 const SectionNode sn = { MinuteSection, i - add, countRepeat(newFormat, i, 2), 0 }; -
4518 newSectionNodes.append(sn); -
4519 appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote); -
4520 i += sn.count - 1; -
4521 index = i + 1; -
4522 newDisplay |= MinuteSection; -
4523 } -
4524 break; -
4525 case 's': -
4526 if (parserType != QVariant::Date) { -
4527 const SectionNode sn = { SecondSection, i - add, countRepeat(newFormat, i, 2), 0 }; -
4528 newSectionNodes.append(sn); -
4529 appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote); -
4530 i += sn.count - 1; -
4531 index = i + 1; -
4532 newDisplay |= SecondSection; -
4533 } -
4534 break; -
4535 -
4536 case 'z': -
4537 if (parserType != QVariant::Date) { -
4538 const SectionNode sn = { MSecSection, i - add, countRepeat(newFormat, i, 3) < 3 ? 1 : 3, 0 }; -
4539 newSectionNodes.append(sn); -
4540 appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote); -
4541 i += sn.count - 1; -
4542 index = i + 1; -
4543 newDisplay |= MSecSection; -
4544 } -
4545 break; -
4546 case 'A': -
4547 case 'a': -
4548 if (parserType != QVariant::Date) { -
4549 const bool cap = (sect == 'A'); -
4550 const SectionNode sn = { AmPmSection, i - add, (cap ? 1 : 0), 0 }; -
4551 newSectionNodes.append(sn); -
4552 appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote); -
4553 newDisplay |= AmPmSection; -
4554 if (i + 1 < newFormat.size() -
4555 && newFormat.at(i+1) == (cap ? QLatin1Char('P') : QLatin1Char('p'))) { -
4556 ++i; -
4557 } -
4558 index = i + 1; -
4559 } -
4560 break; -
4561 case 'y': -
4562 if (parserType != QVariant::Time) { -
4563 const int repeat = countRepeat(newFormat, i, 4); -
4564 if (repeat >= 2) { -
4565 const SectionNode sn = { repeat == 4 ? YearSection : YearSection2Digits, -
4566 i - add, repeat == 4 ? 4 : 2, 0 }; -
4567 newSectionNodes.append(sn); -
4568 appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote); -
4569 i += sn.count - 1; -
4570 index = i + 1; -
4571 newDisplay |= sn.type; -
4572 } -
4573 } -
4574 break; -
4575 case 'M': -
4576 if (parserType != QVariant::Time) { -
4577 const SectionNode sn = { MonthSection, i - add, countRepeat(newFormat, i, 4), 0 }; -
4578 newSectionNodes.append(sn); -
4579 newSeparators.append(unquote(newFormat.mid(index, i - index))); -
4580 i += sn.count - 1; -
4581 index = i + 1; -
4582 newDisplay |= MonthSection; -
4583 } -
4584 break; -
4585 case 'd': -
4586 if (parserType != QVariant::Time) { -
4587 const int repeat = countRepeat(newFormat, i, 4); -
4588 const Section sectionType = (repeat == 4 ? DayOfWeekSectionLong -
4589 : (repeat == 3 ? DayOfWeekSectionShort : DaySection)); -
4590 const SectionNode sn = { sectionType, i - add, repeat, 0 }; -
4591 newSectionNodes.append(sn); -
4592 appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote); -
4593 i += sn.count - 1; -
4594 index = i + 1; -
4595 newDisplay |= sn.type; -
4596 } -
4597 break; -
4598 -
4599 default: -
4600 break; -
4601 } -
4602 } -
4603 } -
4604 if (newSectionNodes.isEmpty() && context == DateTimeEdit) { -
4605 return false; -
4606 } -
4607 -
4608 if ((newDisplay & (AmPmSection|Hour12Section)) == Hour12Section) { -
4609 const int max = newSectionNodes.size(); -
4610 for (int i=0; i<max; ++i) { -
4611 SectionNode &node = newSectionNodes[i]; -
4612 if (node.type == Hour12Section) -
4613 node.type = Hour24Section; -
4614 } -
4615 } -
4616 -
4617 if (index < newFormat.size()) { -
4618 appendSeparator(&newSeparators, newFormat, index, index - max, lastQuote); -
4619 } else { -
4620 newSeparators.append(QString()); -
4621 } -
4622 -
4623 displayFormat = newFormat; -
4624 separators = newSeparators; -
4625 sectionNodes = newSectionNodes; -
4626 display = newDisplay; -
4627 last.pos = -1; -
4628 -
4629// for (int i=0; i<sectionNodes.size(); ++i) { -
4630// QDTPDEBUG << sectionName(sectionNodes.at(i).type) << sectionNodes.at(i).count; -
4631// } -
4632 -
4633 QDTPDEBUG << newFormat << displayFormat; -
4634 QDTPDEBUGN("separators:\n'%s'", separators.join(QLatin1String("\n")).toLatin1().constData()); -
4635 -
4636 return true; -
4637} -
4638 -
4639/*! -
4640 \internal -
4641 -
4642 Returns the size of section \a s. -
4643*/ -
4644 -
4645int QDateTimeParser::sectionSize(int sectionIndex) const -
4646{ -
4647 if (sectionIndex < 0) -
4648 return 0; -
4649 -
4650 if (sectionIndex >= sectionNodes.size()) { -
4651 qWarning("QDateTimeParser::sectionSize Internal error (%d)", sectionIndex); -
4652 return -1; -
4653 } -
4654 -
4655 if (sectionIndex == sectionNodes.size() - 1) { -
4656 // In some cases there is a difference between displayText() and text. -
4657 // e.g. when text is 2000/01/31 and displayText() is "2000/2/31" - text -
4658 // is the previous value and displayText() is the new value. -
4659 // The size difference is always due to leading zeroes. -
4660 int sizeAdjustment = 0; -
4661 if (displayText().size() != text.size()) { -
4662 // Any zeroes added before this section will affect our size. -
4663 int preceedingZeroesAdded = 0; -
4664 if (sectionNodes.size() > 1 && context == DateTimeEdit) { -
4665 for (QVector<SectionNode>::ConstIterator sectionIt = sectionNodes.constBegin(); -
4666 sectionIt != sectionNodes.constBegin() + sectionIndex; ++sectionIt) { -
4667 preceedingZeroesAdded += sectionIt->zeroesAdded; -
4668 } -
4669 } -
4670 sizeAdjustment = preceedingZeroesAdded; -
4671 } -
4672 -
4673 return displayText().size() + sizeAdjustment - sectionPos(sectionIndex) - separators.last().size(); -
4674 } else { -
4675 return sectionPos(sectionIndex + 1) - sectionPos(sectionIndex) -
4676 - separators.at(sectionIndex + 1).size(); -
4677 } -
4678} -
4679 -
4680 -
4681int QDateTimeParser::sectionMaxSize(Section s, int count) const -
4682{ -
4683#ifndef QT_NO_TEXTDATE -
4684 int mcount = 12; -
4685#endif -
4686 -
4687 switch (s) { -
4688 case FirstSection: -
4689 case NoSection: -
4690 case LastSection: return 0; -
4691 -
4692 case AmPmSection: { -
4693 const int lowerMax = qMin(getAmPmText(AmText, LowerCase).size(), -
4694 getAmPmText(PmText, LowerCase).size()); -
4695 const int upperMax = qMin(getAmPmText(AmText, UpperCase).size(), -
4696 getAmPmText(PmText, UpperCase).size()); -
4697 return qMin(4, qMin(lowerMax, upperMax)); -
4698 } -
4699 -
4700 case Hour24Section: -
4701 case Hour12Section: -
4702 case MinuteSection: -
4703 case SecondSection: -
4704 case DaySection: return 2; -
4705 case DayOfWeekSectionShort: -
4706 case DayOfWeekSectionLong: -
4707#ifdef QT_NO_TEXTDATE -
4708 return 2; -
4709#else -
4710 mcount = 7; -
4711 // fall through -
4712#endif -
4713 case MonthSection: -
4714 if (count <= 2) -
4715 return 2; -
4716 -
4717#ifdef QT_NO_TEXTDATE -
4718 return 2; -
4719#else -
4720 { -
4721 int ret = 0; -
4722 const QLocale l = locale(); -
4723 for (int i=1; i<=mcount; ++i) { -
4724 const QString str = (s == MonthSection -
4725 ? l.monthName(i, count == 4 ? QLocale::LongFormat : QLocale::ShortFormat) -
4726 : l.dayName(i, count == 4 ? QLocale::LongFormat : QLocale::ShortFormat)); -
4727 ret = qMax(str.size(), ret); -
4728 } -
4729 return ret; -
4730 } -
4731#endif -
4732 case MSecSection: return 3; -
4733 case YearSection: return 4; -
4734 case YearSection2Digits: return 2; -
4735 -
4736 case CalendarPopupSection: -
4737 case Internal: -
4738 case TimeSectionMask: -
4739 case DateSectionMask: -
4740 qWarning("QDateTimeParser::sectionMaxSize: Invalid section %s", -
4741 sectionName(s).toLatin1().constData()); -
4742 -
4743 case NoSectionIndex: -
4744 case FirstSectionIndex: -
4745 case LastSectionIndex: -
4746 case CalendarPopupIndex: -
4747 // these cases can't happen -
4748 break; -
4749 } -
4750 return -1; -
4751} -
4752 -
4753 -
4754int QDateTimeParser::sectionMaxSize(int index) const -
4755{ -
4756 const SectionNode &sn = sectionNode(index); -
4757 return sectionMaxSize(sn.type, sn.count); -
4758} -
4759 -
4760/*! -
4761 \internal -
4762 -
4763 Returns the text of section \a s. This function operates on the -
4764 arg text rather than edit->text(). -
4765*/ -
4766 -
4767 -
4768QString QDateTimeParser::sectionText(const QString &text, int sectionIndex, int index) const -
4769{ -
4770 const SectionNode &sn = sectionNode(sectionIndex); -
4771 switch (sn.type) { -
4772 case NoSectionIndex: -
4773 case FirstSectionIndex: -
4774 case LastSectionIndex: -
4775 return QString(); -
4776 default: break; -
4777 } -
4778 -
4779 return text.mid(index, sectionSize(sectionIndex)); -
4780} -
4781 -
4782QString QDateTimeParser::sectionText(int sectionIndex) const -
4783{ -
4784 const SectionNode &sn = sectionNode(sectionIndex); -
4785 switch (sn.type) { -
4786 case NoSectionIndex: -
4787 case FirstSectionIndex: -
4788 case LastSectionIndex: -
4789 return QString(); -
4790 default: break; -
4791 } -
4792 -
4793 return displayText().mid(sn.pos, sectionSize(sectionIndex)); -
4794} -
4795 -
4796 -
4797#ifndef QT_NO_TEXTDATE -
4798/*! -
4799 \internal:skipToNextSection -
4800 -
4801 Parses the part of \a text that corresponds to \a s and returns -
4802 the value of that field. Sets *stateptr to the right state if -
4803 stateptr != 0. -
4804*/ -
4805 -
4806int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionIndex, -
4807 QString &text, int &cursorPosition, int index, -
4808 State &state, int *usedptr) const -
4809{ -
4810 state = Invalid; -
4811 int num = 0; -
4812 const SectionNode &sn = sectionNode(sectionIndex); -
4813 if ((sn.type & Internal) == Internal) { -
4814 qWarning("QDateTimeParser::parseSection Internal error (%s %d)", -
4815 qPrintable(sectionName(sn.type)), sectionIndex); -
4816 return -1; -
4817 } -
4818 -
4819 const int sectionmaxsize = sectionMaxSize(sectionIndex); -
4820 QString sectiontext = text.mid(index, sectionmaxsize); -
4821 int sectiontextSize = sectiontext.size(); -
4822 -
4823 QDTPDEBUG << "sectionValue for" << sectionName(sn.type) -
4824 << "with text" << text << "and st" << sectiontext -
4825 << text.mid(index, sectionmaxsize) -
4826 << index; -
4827 -
4828 int used = 0; -
4829 switch (sn.type) { -
4830 case AmPmSection: { -
4831 const int ampm = findAmPm(sectiontext, sectionIndex, &used); -
4832 switch (ampm) { -
4833 case AM: // sectiontext == AM -
4834 case PM: // sectiontext == PM -
4835 num = ampm; -
4836 state = Acceptable; -
4837 break; -
4838 case PossibleAM: // sectiontext => AM -
4839 case PossiblePM: // sectiontext => PM -
4840 num = ampm - 2; -
4841 state = Intermediate; -
4842 break; -
4843 case PossibleBoth: // sectiontext => AM|PM -
4844 num = 0; -
4845 state = Intermediate; -
4846 break; -
4847 case Neither: -
4848 state = Invalid; -
4849 QDTPDEBUG << "invalid because findAmPm(" << sectiontext << ") returned -1"; -
4850 break; -
4851 default: -
4852 QDTPDEBUGN("This should never happen (findAmPm returned %d)", ampm); -
4853 break; -
4854 } -
4855 if (state != Invalid) { -
4856 QString str = text; -
4857 text.replace(index, used, sectiontext.left(used)); -
4858 } -
4859 break; } -
4860 case MonthSection: -
4861 case DayOfWeekSectionShort: -
4862 case DayOfWeekSectionLong: -
4863 if (sn.count >= 3) { -
4864 if (sn.type == MonthSection) { -
4865 int min = 1; -
4866 const QDate minDate = getMinimum().date(); -
4867 if (currentValue.date().year() == minDate.year()) { -
4868 min = minDate.month(); -
4869 } -
4870 num = findMonth(sectiontext.toLower(), min, sectionIndex, &sectiontext, &used); -
4871 } else { -
4872 num = findDay(sectiontext.toLower(), 1, sectionIndex, &sectiontext, &used); -
4873 } -
4874 -
4875 if (num != -1) { -
4876 state = (used == sectiontext.size() ? Acceptable : Intermediate); -
4877 QString str = text; -
4878 text.replace(index, used, sectiontext.left(used)); -
4879 } else { -
4880 state = Intermediate; -
4881 } -
4882 break; } -
4883 // fall through -
4884 case DaySection: -
4885 case YearSection: -
4886 case YearSection2Digits: -
4887 case Hour12Section: -
4888 case Hour24Section: -
4889 case MinuteSection: -
4890 case SecondSection: -
4891 case MSecSection: { -
4892 if (sectiontextSize == 0) { -
4893 num = 0; -
4894 used = 0; -
4895 state = Intermediate; -
4896 } else { -
4897 const int absMax = absoluteMax(sectionIndex); -
4898 QLocale loc; -
4899 bool ok = true; -
4900 int last = -1; -
4901 used = -1; -
4902 -
4903 QString digitsStr(sectiontext); -
4904 for (int i = 0; i < sectiontextSize; ++i) { -
4905 if (digitsStr.at(i).isSpace()) { -
4906 sectiontextSize = i; -
4907 break; -
4908 } -
4909 } -
4910 -
4911 const int max = qMin(sectionmaxsize, sectiontextSize); -
4912 for (int digits = max; digits >= 1; --digits) { -
4913 digitsStr.truncate(digits); -
4914 int tmp = (int)loc.toUInt(digitsStr, &ok); -
4915 if (ok && sn.type == Hour12Section) { -
4916 if (tmp > 12) { -
4917 tmp = -1; -
4918 ok = false; -
4919 } else if (tmp == 12) { -
4920 tmp = 0; -
4921 } -
4922 } -
4923 if (ok && tmp <= absMax) { -
4924 QDTPDEBUG << sectiontext.left(digits) << tmp << digits; -
4925 last = tmp; -
4926 used = digits; -
4927 break; -
4928 } -
4929 } -
4930 -
4931 if (last == -1) { -
4932 QChar first(sectiontext.at(0)); -
4933 if (separators.at(sectionIndex + 1).startsWith(first)) { -
4934 used = 0; -
4935 state = Intermediate; -
4936 } else { -
4937 state = Invalid; -
4938 QDTPDEBUG << "invalid because" << sectiontext << "can't become a uint" << last << ok; -
4939 } -
4940 } else { -
4941 num += last; -
4942 const FieldInfo fi = fieldInfo(sectionIndex); -
4943 const bool done = (used == sectionmaxsize); -
4944 if (!done && fi & Fraction) { // typing 2 in a zzz field should be .200, not .002 -
4945 for (int i=used; i<sectionmaxsize; ++i) { -
4946 num *= 10; -
4947 } -
4948 } -
4949 const int absMin = absoluteMin(sectionIndex); -
4950 if (num < absMin) { -
4951 state = done ? Invalid : Intermediate; -
4952 if (done) -
4953 QDTPDEBUG << "invalid because" << num << "is less than absoluteMin" << absMin; -
4954 } else if (num > absMax) { -
4955 state = Intermediate; -
4956 } else if (!done && (fi & (FixedWidth|Numeric)) == (FixedWidth|Numeric)) { -
4957 if (skipToNextSection(sectionIndex, currentValue, digitsStr)) { -
4958 state = Acceptable; -
4959 const int missingZeroes = sectionmaxsize - digitsStr.size(); -
4960 text.insert(index, QString().fill(QLatin1Char('0'), missingZeroes)); -
4961 used = sectionmaxsize; -
4962 cursorPosition += missingZeroes; -
4963 ++(const_cast<QDateTimeParser*>(this)->sectionNodes[sectionIndex].zeroesAdded); -
4964 } else { -
4965 state = Intermediate;; -
4966 } -
4967 } else { -
4968 state = Acceptable; -
4969 } -
4970 } -
4971 } -
4972 break; } -
4973 default: -
4974 qWarning("QDateTimeParser::parseSection Internal error (%s %d)", -
4975 qPrintable(sectionName(sn.type)), sectionIndex); -
4976 return -1; -
4977 } -
4978 -
4979 if (usedptr) -
4980 *usedptr = used; -
4981 -
4982 return (state != Invalid ? num : -1); -
4983} -
4984#endif // QT_NO_TEXTDATE -
4985 -
4986#ifndef QT_NO_DATESTRING -
4987/*! -
4988 \internal -
4989*/ -
4990 -
4991QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPosition, -
4992 const QDateTime &currentValue, bool fixup) const -
4993{ -
4994 const QDateTime minimum = getMinimum(); -
4995 const QDateTime maximum = getMaximum(); -
4996 -
4997 State state = Acceptable; -
4998 -
4999 QDateTime newCurrentValue; -
5000 int pos = 0; -
5001 bool conflicts = false; -
5002 const int sectionNodesCount = sectionNodes.size(); -
5003 -
5004 QDTPDEBUG << "parse" << input; -
5005 { -
5006 int year, month, day, hour12, hour, minute, second, msec, ampm, dayofweek, year2digits; -
5007 getDateFromJulianDay(currentValue.date().toJulianDay(), &year, &month, &day); -
5008 year2digits = year % 100; -
5009 hour = currentValue.time().hour(); -
5010 hour12 = -1; -
5011 minute = currentValue.time().minute(); -
5012 second = currentValue.time().second(); -
5013 msec = currentValue.time().msec(); -
5014 dayofweek = currentValue.date().dayOfWeek(); -
5015 -
5016 ampm = -1; -
5017 Sections isSet = NoSection; -
5018 int num; -
5019 State tmpstate; -
5020 -
5021 for (int index=0; state != Invalid && index<sectionNodesCount; ++index) { -
5022 if (QStringRef(&input, pos, separators.at(index).size()) != separators.at(index)) { -
5023 QDTPDEBUG << "invalid because" << input.mid(pos, separators.at(index).size()) -
5024 << "!=" << separators.at(index) -
5025 << index << pos << currentSectionIndex; -
5026 state = Invalid; -
5027 goto end; -
5028 } -
5029 pos += separators.at(index).size(); -
5030 sectionNodes[index].pos = pos; -
5031 int *current = 0; -
5032 const SectionNode sn = sectionNodes.at(index); -
5033 int used; -
5034 -
5035 num = parseSection(currentValue, index, input, cursorPosition, pos, tmpstate, &used); -
5036 QDTPDEBUG << "sectionValue" << sectionName(sectionType(index)) << input -
5037 << "pos" << pos << "used" << used << stateName(tmpstate); -
5038 if (fixup && tmpstate == Intermediate && used < sn.count) { -
5039 const FieldInfo fi = fieldInfo(index); -
5040 if ((fi & (Numeric|FixedWidth)) == (Numeric|FixedWidth)) { -
5041 const QString newText = QString::fromLatin1("%1").arg(num, sn.count, 10, QLatin1Char('0')); -
5042 input.replace(pos, used, newText); -
5043 used = sn.count; -
5044 } -
5045 } -
5046 pos += qMax(0, used); -
5047 -
5048 state = qMin<State>(state, tmpstate); -
5049 if (state == Intermediate && context == FromString) { -
5050 state = Invalid; -
5051 break; -
5052 } -
5053 -
5054 QDTPDEBUG << index << sectionName(sectionType(index)) << "is set to" -
5055 << pos << "state is" << stateName(state); -
5056 -
5057 -
5058 if (state != Invalid) { -
5059 switch (sn.type) { -
5060 case Hour24Section: current = &hour; break; -
5061 case Hour12Section: current = &hour12; break; -
5062 case MinuteSection: current = &minute; break; -
5063 case SecondSection: current = &second; break; -
5064 case MSecSection: current = &msec; break; -
5065 case YearSection: current = &year; break; -
5066 case YearSection2Digits: current = &year2digits; break; -
5067 case MonthSection: current = &month; break; -
5068 case DayOfWeekSectionShort: -
5069 case DayOfWeekSectionLong: current = &dayofweek; break; -
5070 case DaySection: current = &day; num = qMax<int>(1, num); break; -
5071 case AmPmSection: current = &ampm; break; -
5072 default: -
5073 qWarning("QDateTimeParser::parse Internal error (%s)", -
5074 qPrintable(sectionName(sn.type))); -
5075 break; -
5076 } -
5077 if (!current) { -
5078 qWarning("QDateTimeParser::parse Internal error 2"); -
5079 return StateNode(); -
5080 } -
5081 if (isSet & sn.type && *current != num) { -
5082 QDTPDEBUG << "CONFLICT " << sectionName(sn.type) << *current << num; -
5083 conflicts = true; -
5084 if (index != currentSectionIndex || num == -1) { -
5085 continue; -
5086 } -
5087 } -
5088 if (num != -1) -
5089 *current = num; -
5090 isSet |= sn.type; -
5091 } -
5092 } -
5093 -
5094 if (state != Invalid && QStringRef(&input, pos, input.size() - pos) != separators.last()) { -
5095 QDTPDEBUG << "invalid because" << input.mid(pos) -
5096 << "!=" << separators.last() << pos; -
5097 state = Invalid; -
5098 } -
5099 -
5100 if (state != Invalid) { -
5101 if (parserType != QVariant::Time) { -
5102 if (year % 100 != year2digits) { -
5103 switch (isSet & (YearSection2Digits|YearSection)) { -
5104 case YearSection2Digits: -
5105 year = (year / 100) * 100; -
5106 year += year2digits; -
5107 break; -
5108 case ((uint)YearSection2Digits|(uint)YearSection): { -
5109 conflicts = true; -
5110 const SectionNode &sn = sectionNode(currentSectionIndex); -
5111 if (sn.type == YearSection2Digits) { -
5112 year = (year / 100) * 100; -
5113 year += year2digits; -
5114 } -
5115 break; } -
5116 default: -
5117 break; -
5118 } -
5119 } -
5120 -
5121 const QDate date(year, month, day); -
5122 const int diff = dayofweek - date.dayOfWeek(); -
5123 if (diff != 0 && state == Acceptable && isSet & (DayOfWeekSectionShort|DayOfWeekSectionLong)) { -
5124 conflicts = isSet & DaySection; -
5125 const SectionNode &sn = sectionNode(currentSectionIndex); -
5126 if (sn.type & (DayOfWeekSectionShort|DayOfWeekSectionLong) || currentSectionIndex == -1) { -
5127 // dayofweek should be preferred -
5128 day += diff; -
5129 if (day <= 0) { -
5130 day += 7; -
5131 } else if (day > date.daysInMonth()) { -
5132 day -= 7; -
5133 } -
5134 QDTPDEBUG << year << month << day << dayofweek -
5135 << diff << QDate(year, month, day).dayOfWeek(); -
5136 } -
5137 } -
5138 bool needfixday = false; -
5139 if (sectionType(currentSectionIndex) & (DaySection|DayOfWeekSectionShort|DayOfWeekSectionLong)) { -
5140 cachedDay = day; -
5141 } else if (cachedDay > day) { -
5142 day = cachedDay; -
5143 needfixday = true; -
5144 } -
5145 -
5146 if (!QDate::isValid(year, month, day)) { -
5147 if (day < 32) { -
5148 cachedDay = day; -
5149 } -
5150 if (day > 28 && QDate::isValid(year, month, 1)) { -
5151 needfixday = true; -
5152 } -
5153 } -
5154 if (needfixday) { -
5155 if (context == FromString) { -
5156 state = Invalid; -
5157 goto end; -
5158 } -
5159 if (state == Acceptable && fixday) { -
5160 day = qMin<int>(day, QDate(year, month, 1).daysInMonth()); -
5161 -
5162 const QLocale loc = locale(); -
5163 for (int i=0; i<sectionNodesCount; ++i) { -
5164 const Section thisSectionType = sectionType(i); -
5165 if (thisSectionType & (DaySection)) { -
5166 input.replace(sectionPos(i), sectionSize(i), loc.toString(day)); -
5167 } else if (thisSectionType & (DayOfWeekSectionShort|DayOfWeekSectionLong)) { -
5168 const int dayOfWeek = QDate(year, month, day).dayOfWeek(); -
5169 const QLocale::FormatType dayFormat = (thisSectionType == DayOfWeekSectionShort -
5170 ? QLocale::ShortFormat : QLocale::LongFormat); -
5171 const QString dayName(loc.dayName(dayOfWeek, dayFormat)); -
5172 input.replace(sectionPos(i), sectionSize(i), dayName); -
5173 } -
5174 } -
5175 } else { -
5176 state = qMin(Intermediate, state); -
5177 } -
5178 } -
5179 } -
5180 -
5181 if (parserType != QVariant::Date) { -
5182 if (isSet & Hour12Section) { -
5183 const bool hasHour = isSet & Hour24Section; -
5184 if (ampm == -1) { -
5185 if (hasHour) { -
5186 ampm = (hour < 12 ? 0 : 1); -
5187 } else { -
5188 ampm = 0; // no way to tell if this is am or pm so I assume am -
5189 } -
5190 } -
5191 hour12 = (ampm == 0 ? hour12 % 12 : (hour12 % 12) + 12); -
5192 if (!hasHour) { -
5193 hour = hour12; -
5194 } else if (hour != hour12) { -
5195 conflicts = true; -
5196 } -
5197 } else if (ampm != -1) { -
5198 if (!(isSet & (Hour24Section))) { -
5199 hour = (12 * ampm); // special case. Only ap section -
5200 } else if ((ampm == 0) != (hour < 12)) { -
5201 conflicts = true; -
5202 } -
5203 } -
5204 -
5205 } -
5206 -
5207 newCurrentValue = QDateTime(QDate(year, month, day), QTime(hour, minute, second, msec), spec); -
5208 QDTPDEBUG << year << month << day << hour << minute << second << msec; -
5209 } -
5210 QDTPDEBUGN("'%s' => '%s'(%s)", input.toLatin1().constData(), -
5211 newCurrentValue.toString(QLatin1String("yyyy/MM/dd hh:mm:ss.zzz")).toLatin1().constData(), -
5212 stateName(state).toLatin1().constData()); -
5213 } -
5214end: -
5215 if (newCurrentValue.isValid()) { -
5216 if (context != FromString && state != Invalid && newCurrentValue < minimum) { -
5217 const QLatin1Char space(' '); -
5218 if (newCurrentValue >= minimum) -
5219 qWarning("QDateTimeParser::parse Internal error 3 (%s %s)", -
5220 qPrintable(newCurrentValue.toString()), qPrintable(minimum.toString())); -
5221 -
5222 bool done = false; -
5223 state = Invalid; -
5224 for (int i=0; i<sectionNodesCount && !done; ++i) { -
5225 const SectionNode &sn = sectionNodes.at(i); -
5226 QString t = sectionText(input, i, sn.pos).toLower(); -
5227 if ((t.size() < sectionMaxSize(i) && (((int)fieldInfo(i) & (FixedWidth|Numeric)) != Numeric)) -
5228 || t.contains(space)) { -
5229 switch (sn.type) { -
5230 case AmPmSection: -
5231 switch (findAmPm(t, i)) { -
5232 case AM: -
5233 case PM: -
5234 state = Acceptable; -
5235 done = true; -
5236 break; -
5237 case Neither: -
5238 state = Invalid; -
5239 done = true; -
5240 break; -
5241 case PossibleAM: -
5242 case PossiblePM: -
5243 case PossibleBoth: { -
5244 const QDateTime copy(newCurrentValue.addSecs(12 * 60 * 60)); -
5245 if (copy >= minimum && copy <= maximum) { -
5246 state = Intermediate; -
5247 done = true; -
5248 } -
5249 break; } -
5250 } -
5251 case MonthSection: -
5252 if (sn.count >= 3) { -
5253 int tmp = newCurrentValue.date().month(); -
5254 // I know the first possible month makes the date too early -
5255 while ((tmp = findMonth(t, tmp + 1, i)) != -1) { -
5256 const QDateTime copy(newCurrentValue.addMonths(tmp - newCurrentValue.date().month())); -
5257 if (copy >= minimum && copy <= maximum) -
5258 break; // break out of while -
5259 } -
5260 if (tmp == -1) { -
5261 break; -
5262 } -
5263 state = Intermediate; -
5264 done = true; -
5265 break; -
5266 } -
5267 // fallthrough -
5268 default: { -
5269 int toMin; -
5270 int toMax; -
5271 -
5272 if (sn.type & TimeSectionMask) { -
5273 if (newCurrentValue.daysTo(minimum) != 0) { -
5274 break; -
5275 } -
5276 toMin = newCurrentValue.time().msecsTo(minimum.time()); -
5277 if (newCurrentValue.daysTo(maximum) > 0) { -
5278 toMax = -1; // can't get to max -
5279 } else { -
5280 toMax = newCurrentValue.time().msecsTo(maximum.time()); -
5281 } -
5282 } else { -
5283 toMin = newCurrentValue.daysTo(minimum); -
5284 toMax = newCurrentValue.daysTo(maximum); -
5285 } -
5286 const int maxChange = QDateTimeParser::maxChange(i); -
5287 if (toMin > maxChange) { -
5288 QDTPDEBUG << "invalid because toMin > maxChange" << toMin -
5289 << maxChange << t << newCurrentValue << minimum; -
5290 state = Invalid; -
5291 done = true; -
5292 break; -
5293 } else if (toMax > maxChange) { -
5294 toMax = -1; // can't get to max -
5295 } -
5296 -
5297 const int min = getDigit(minimum, i); -
5298 if (min == -1) { -
5299 qWarning("QDateTimeParser::parse Internal error 4 (%s)", -
5300 qPrintable(sectionName(sn.type))); -
5301 state = Invalid; -
5302 done = true; -
5303 break; -
5304 } -
5305 -
5306 int max = toMax != -1 ? getDigit(maximum, i) : absoluteMax(i, newCurrentValue); -
5307 int pos = cursorPosition - sn.pos; -
5308 if (pos < 0 || pos >= t.size()) -
5309 pos = -1; -
5310 if (!potentialValue(t.simplified(), min, max, i, newCurrentValue, pos)) { -
5311 QDTPDEBUG << "invalid because potentialValue(" << t.simplified() << min << max -
5312 << sectionName(sn.type) << "returned" << toMax << toMin << pos; -
5313 state = Invalid; -
5314 done = true; -
5315 break; -
5316 } -
5317 state = Intermediate; -
5318 done = true; -
5319 break; } -
5320 } -
5321 } -
5322 } -
5323 } else { -
5324 if (context == FromString) { -
5325 // optimization -
5326 Q_ASSERT(getMaximum().date().toJulianDay() == 4642999); -
5327 if (newCurrentValue.date().toJulianDay() > 4642999) -
5328 state = Invalid; -
5329 } else { -
5330 if (newCurrentValue > getMaximum()) -
5331 state = Invalid; -
5332 } -
5333 -
5334 QDTPDEBUG << "not checking intermediate because newCurrentValue is" << newCurrentValue << getMinimum() << getMaximum(); -
5335 } -
5336 } -
5337 StateNode node; -
5338 node.input = input; -
5339 node.state = state; -
5340 node.conflicts = conflicts; -
5341 node.value = newCurrentValue.toTimeSpec(spec); -
5342 text = input; -
5343 return node; -
5344} -
5345#endif // QT_NO_DATESTRING -
5346 -
5347#ifndef QT_NO_TEXTDATE -
5348/*! -
5349 \internal -
5350 finds the first possible monthname that \a str1 can -
5351 match. Starting from \a index; str should already by lowered -
5352*/ -
5353 -
5354int QDateTimeParser::findMonth(const QString &str1, int startMonth, int sectionIndex, -
5355 QString *usedMonth, int *used) const -
5356{ -
5357 int bestMatch = -1; -
5358 int bestCount = 0; -
5359 if (!str1.isEmpty()) { -
5360 const SectionNode &sn = sectionNode(sectionIndex); -
5361 if (sn.type != MonthSection) { -
5362 qWarning("QDateTimeParser::findMonth Internal error"); -
5363 return -1; -
5364 } -
5365 -
5366 QLocale::FormatType type = sn.count == 3 ? QLocale::ShortFormat : QLocale::LongFormat; -
5367 QLocale l = locale(); -
5368 -
5369 for (int month=startMonth; month<=12; ++month) { -
5370 QString str2 = l.monthName(month, type).toLower(); -
5371 -
5372 if (str1.startsWith(str2)) { -
5373 if (used) { -
5374 QDTPDEBUG << "used is set to" << str2.size(); -
5375 *used = str2.size(); -
5376 } -
5377 if (usedMonth) -
5378 *usedMonth = l.monthName(month, type); -
5379 -
5380 return month; -
5381 } -
5382 if (context == FromString) -
5383 continue; -
5384 -
5385 const int limit = qMin(str1.size(), str2.size()); -
5386 -
5387 QDTPDEBUG << "limit is" << limit << str1 << str2; -
5388 bool equal = true; -
5389 for (int i=0; i<limit; ++i) { -
5390 if (str1.at(i) != str2.at(i)) { -
5391 equal = false; -
5392 if (i > bestCount) { -
5393 bestCount = i; -
5394 bestMatch = month; -
5395 } -
5396 break; -
5397 } -
5398 } -
5399 if (equal) { -
5400 if (used) -
5401 *used = limit; -
5402 if (usedMonth) -
5403 *usedMonth = l.monthName(month, type); -
5404 return month; -
5405 } -
5406 } -
5407 if (usedMonth && bestMatch != -1) -
5408 *usedMonth = l.monthName(bestMatch, type); -
5409 } -
5410 if (used) { -
5411 QDTPDEBUG << "used is set to" << bestCount; -
5412 *used = bestCount; -
5413 } -
5414 return bestMatch; -
5415} -
5416 -
5417int QDateTimeParser::findDay(const QString &str1, int startDay, int sectionIndex, QString *usedDay, int *used) const -
5418{ -
5419 int bestMatch = -1; -
5420 int bestCount = 0; -
5421 if (!str1.isEmpty()) { -
5422 const SectionNode &sn = sectionNode(sectionIndex); -
5423 if (!(sn.type & (DaySection|DayOfWeekSectionShort|DayOfWeekSectionLong))) { -
5424 qWarning("QDateTimeParser::findDay Internal error"); -
5425 return -1; -
5426 } -
5427 const QLocale l = locale(); -
5428 for (int day=startDay; day<=7; ++day) { -
5429 const QString str2 = l.dayName(day, sn.count == 4 ? QLocale::LongFormat : QLocale::ShortFormat); -
5430 -
5431 if (str1.startsWith(str2.toLower())) { -
5432 if (used) -
5433 *used = str2.size(); -
5434 if (usedDay) { -
5435 *usedDay = str2; -
5436 } -
5437 return day; -
5438 } -
5439 if (context == FromString) -
5440 continue; -
5441 -
5442 const int limit = qMin(str1.size(), str2.size()); -
5443 bool found = true; -
5444 for (int i=0; i<limit; ++i) { -
5445 if (str1.at(i) != str2.at(i) && !str1.at(i).isSpace()) { -
5446 if (i > bestCount) { -
5447 bestCount = i; -
5448 bestMatch = day; -
5449 } -
5450 found = false; -
5451 break; -
5452 } -
5453 -
5454 } -
5455 if (found) { -
5456 if (used) -
5457 *used = limit; -
5458 if (usedDay) -
5459 *usedDay = str2; -
5460 -
5461 return day; -
5462 } -
5463 } -
5464 if (usedDay && bestMatch != -1) { -
5465 *usedDay = l.dayName(bestMatch, sn.count == 4 ? QLocale::LongFormat : QLocale::ShortFormat); -
5466 } -
5467 } -
5468 if (used) -
5469 *used = bestCount; -
5470 -
5471 return bestMatch; -
5472} -
5473#endif // QT_NO_TEXTDATE -
5474 -
5475/*! -
5476 \internal -
5477 -
5478 returns -
5479 0 if str == QDateTimeEdit::tr("AM") -
5480 1 if str == QDateTimeEdit::tr("PM") -
5481 2 if str can become QDateTimeEdit::tr("AM") -
5482 3 if str can become QDateTimeEdit::tr("PM") -
5483 4 if str can become QDateTimeEdit::tr("PM") and can become QDateTimeEdit::tr("AM") -
5484 -1 can't become anything sensible -
5485 -
5486*/ -
5487 -
5488int QDateTimeParser::findAmPm(QString &str, int index, int *used) const -
5489{ -
5490 const SectionNode &s = sectionNode(index); -
5491 if (s.type != AmPmSection) { -
5492 qWarning("QDateTimeParser::findAmPm Internal error"); -
5493 return -1; -
5494 } -
5495 if (used) -
5496 *used = str.size(); -
5497 if (str.trimmed().isEmpty()) { -
5498 return PossibleBoth; -
5499 } -
5500 const QLatin1Char space(' '); -
5501 int size = sectionMaxSize(index); -
5502 -
5503 enum { -
5504 amindex = 0, -
5505 pmindex = 1 -
5506 }; -
5507 QString ampm[2]; -
5508 ampm[amindex] = getAmPmText(AmText, s.count == 1 ? UpperCase : LowerCase); -
5509 ampm[pmindex] = getAmPmText(PmText, s.count == 1 ? UpperCase : LowerCase); -
5510 for (int i=0; i<2; ++i) -
5511 ampm[i].truncate(size); -
5512 -
5513 QDTPDEBUG << "findAmPm" << str << ampm[0] << ampm[1]; -
5514 -
5515 if (str.indexOf(ampm[amindex], 0, Qt::CaseInsensitive) == 0) { -
5516 str = ampm[amindex]; -
5517 return AM; -
5518 } else if (str.indexOf(ampm[pmindex], 0, Qt::CaseInsensitive) == 0) { -
5519 str = ampm[pmindex]; -
5520 return PM; -
5521 } else if (context == FromString || (str.count(space) == 0 && str.size() >= size)) { -
5522 return Neither; -
5523 } -
5524 size = qMin(size, str.size()); -
5525 -
5526 bool broken[2] = {false, false}; -
5527 for (int i=0; i<size; ++i) { -
5528 if (str.at(i) != space) { -
5529 for (int j=0; j<2; ++j) { -
5530 if (!broken[j]) { -
5531 int index = ampm[j].indexOf(str.at(i)); -
5532 QDTPDEBUG << "looking for" << str.at(i) -
5533 << "in" << ampm[j] << "and got" << index; -
5534 if (index == -1) { -
5535 if (str.at(i).category() == QChar::Letter_Uppercase) { -
5536 index = ampm[j].indexOf(str.at(i).toLower()); -
5537 QDTPDEBUG << "trying with" << str.at(i).toLower() -
5538 << "in" << ampm[j] << "and got" << index; -
5539 } else if (str.at(i).category() == QChar::Letter_Lowercase) { -
5540 index = ampm[j].indexOf(str.at(i).toUpper()); -
5541 QDTPDEBUG << "trying with" << str.at(i).toUpper() -
5542 << "in" << ampm[j] << "and got" << index; -
5543 } -
5544 if (index == -1) { -
5545 broken[j] = true; -
5546 if (broken[amindex] && broken[pmindex]) { -
5547 QDTPDEBUG << str << "didn't make it"; -
5548 return Neither; -
5549 } -
5550 continue; -
5551 } else { -
5552 str[i] = ampm[j].at(index); // fix case -
5553 } -
5554 } -
5555 ampm[j].remove(index, 1); -
5556 } -
5557 } -
5558 } -
5559 } -
5560 if (!broken[pmindex] && !broken[amindex]) -
5561 return PossibleBoth; -
5562 return (!broken[amindex] ? PossibleAM : PossiblePM); -
5563} -
5564 -
5565/*! -
5566 \internal -
5567 Max number of units that can be changed by this section. -
5568*/ -
5569 -
5570int QDateTimeParser::maxChange(int index) const -
5571{ -
5572 const SectionNode &sn = sectionNode(index); -
5573 switch (sn.type) { -
5574 // Time. unit is msec -
5575 case MSecSection: return 999; -
5576 case SecondSection: return 59 * 1000; -
5577 case MinuteSection: return 59 * 60 * 1000; -
5578 case Hour24Section: case Hour12Section: return 59 * 60 * 60 * 1000; -
5579 -
5580 // Date. unit is day -
5581 case DayOfWeekSectionShort: -
5582 case DayOfWeekSectionLong: return 7; -
5583 case DaySection: return 30; -
5584 case MonthSection: return 365 - 31; -
5585 case YearSection: return 9999 * 365; -
5586 case YearSection2Digits: return 100 * 365; -
5587 default: -
5588 qWarning("QDateTimeParser::maxChange() Internal error (%s)", -
5589 qPrintable(sectionName(sectionType(index)))); -
5590 } -
5591 -
5592 return -1; -
5593} -
5594 -
5595QDateTimeParser::FieldInfo QDateTimeParser::fieldInfo(int index) const -
5596{ -
5597 FieldInfo ret = 0; -
5598 const SectionNode &sn = sectionNode(index); -
5599 const Section s = sn.type; -
5600 switch (s) { -
5601 case MSecSection: -
5602 ret |= Fraction; -
5603 // fallthrough -
5604 case SecondSection: -
5605 case MinuteSection: -
5606 case Hour24Section: -
5607 case Hour12Section: -
5608 case YearSection: -
5609 case YearSection2Digits: -
5610 ret |= Numeric; -
5611 if (s != YearSection) { -
5612 ret |= AllowPartial; -
5613 } -
5614 if (sn.count != 1) { -
5615 ret |= FixedWidth; -
5616 } -
5617 break; -
5618 case MonthSection: -
5619 case DaySection: -
5620 switch (sn.count) { -
5621 case 2: -
5622 ret |= FixedWidth; -
5623 // fallthrough -
5624 case 1: -
5625 ret |= (Numeric|AllowPartial); -
5626 break; -
5627 } -
5628 break; -
5629 case DayOfWeekSectionShort: -
5630 case DayOfWeekSectionLong: -
5631 if (sn.count == 3) -
5632 ret |= FixedWidth; -
5633 break; -
5634 case AmPmSection: -
5635 ret |= FixedWidth; -
5636 break; -
5637 default: -
5638 qWarning("QDateTimeParser::fieldInfo Internal error 2 (%d %s %d)", -
5639 index, qPrintable(sectionName(sn.type)), sn.count); -
5640 break; -
5641 } -
5642 return ret; -
5643} -
5644 -
5645/*! -
5646 \internal -
5647 -
5648 Get a number that str can become which is between min -
5649 and max or -1 if this is not possible. -
5650*/ -
5651 -
5652 -
5653QString QDateTimeParser::sectionFormat(int index) const -
5654{ -
5655 const SectionNode &sn = sectionNode(index); -
5656 return sectionFormat(sn.type, sn.count); -
5657} -
5658 -
5659QString QDateTimeParser::sectionFormat(Section s, int count) const -
5660{ -
5661 QChar fillChar; -
5662 switch (s) { -
5663 case AmPmSection: return count == 1 ? QLatin1String("AP") : QLatin1String("ap"); -
5664 case MSecSection: fillChar = QLatin1Char('z'); break; -
5665 case SecondSection: fillChar = QLatin1Char('s'); break; -
5666 case MinuteSection: fillChar = QLatin1Char('m'); break; -
5667 case Hour24Section: fillChar = QLatin1Char('H'); break; -
5668 case Hour12Section: fillChar = QLatin1Char('h'); break; -
5669 case DayOfWeekSectionShort: -
5670 case DayOfWeekSectionLong: -
5671 case DaySection: fillChar = QLatin1Char('d'); break; -
5672 case MonthSection: fillChar = QLatin1Char('M'); break; -
5673 case YearSection2Digits: -
5674 case YearSection: fillChar = QLatin1Char('y'); break; -
5675 default: -
5676 qWarning("QDateTimeParser::sectionFormat Internal error (%s)", -
5677 qPrintable(sectionName(s))); -
5678 return QString(); -
5679 } -
5680 if (fillChar.isNull()) { -
5681 qWarning("QDateTimeParser::sectionFormat Internal error 2"); -
5682 return QString(); -
5683 } -
5684 -
5685 QString str; -
5686 str.fill(fillChar, count); -
5687 return str; -
5688} -
5689 -
5690 -
5691/*! -
5692 \internal -
5693 -
5694 Returns true if str can be modified to represent a -
5695 number that is within min and max. -
5696*/ -
5697 -
5698bool QDateTimeParser::potentialValue(const QString &str, int min, int max, int index, -
5699 const QDateTime &currentValue, int insert) const -
5700{ -
5701 if (str.isEmpty()) { -
5702 return true; -
5703 } -
5704 const int size = sectionMaxSize(index); -
5705 int val = (int)locale().toUInt(str); -
5706 const SectionNode &sn = sectionNode(index); -
5707 if (sn.type == YearSection2Digits) { -
5708 val += currentValue.date().year() - (currentValue.date().year() % 100); -
5709 } -
5710 if (val >= min && val <= max && str.size() == size) { -
5711 return true; -
5712 } else if (val > max) { -
5713 return false; -
5714 } else if (str.size() == size && val < min) { -
5715 return false; -
5716 } -
5717 -
5718 const int len = size - str.size(); -
5719 for (int i=0; i<len; ++i) { -
5720 for (int j=0; j<10; ++j) { -
5721 if (potentialValue(str + QLatin1Char('0' + j), min, max, index, currentValue, insert)) { -
5722 return true; -
5723 } else if (insert >= 0) { -
5724 QString tmp = str; -
5725 tmp.insert(insert, QLatin1Char('0' + j)); -
5726 if (potentialValue(tmp, min, max, index, currentValue, insert)) -
5727 return true; -
5728 } -
5729 } -
5730 } -
5731 -
5732 return false; -
5733} -
5734 -
5735bool QDateTimeParser::skipToNextSection(int index, const QDateTime &current, const QString &text) const -
5736{ -
5737 Q_ASSERT(current >= getMinimum() && current <= getMaximum()); -
5738 -
5739 const SectionNode &node = sectionNode(index); -
5740 Q_ASSERT(text.size() < sectionMaxSize(index)); -
5741 -
5742 const QDateTime maximum = getMaximum(); -
5743 const QDateTime minimum = getMinimum(); -
5744 QDateTime tmp = current; -
5745 int min = absoluteMin(index); -
5746 setDigit(tmp, index, min); -
5747 if (tmp < minimum) { -
5748 min = getDigit(minimum, index); -
5749 } -
5750 -
5751 int max = absoluteMax(index, current); -
5752 setDigit(tmp, index, max); -
5753 if (tmp > maximum) { -
5754 max = getDigit(maximum, index); -
5755 } -
5756 int pos = cursorPosition() - node.pos; -
5757 if (pos < 0 || pos >= text.size()) -
5758 pos = -1; -
5759 -
5760 const bool potential = potentialValue(text, min, max, index, current, pos); -
5761 return !potential; -
5762 -
5763 /* If the value potentially can become another valid entry we -
5764 * don't want to skip to the next. E.g. In a M field (month -
5765 * without leading 0 if you type 1 we don't want to autoskip but -
5766 * if you type 3 we do -
5767 */ -
5768} -
5769 -
5770/*! -
5771 \internal -
5772 For debugging. Returns the name of the section \a s. -
5773*/ -
5774 -
5775QString QDateTimeParser::sectionName(int s) const -
5776{ -
5777 switch (s) { -
5778 case QDateTimeParser::AmPmSection: return QLatin1String("AmPmSection"); -
5779 case QDateTimeParser::DaySection: return QLatin1String("DaySection"); -
5780 case QDateTimeParser::DayOfWeekSectionShort: return QLatin1String("DayOfWeekSectionShort"); -
5781 case QDateTimeParser::DayOfWeekSectionLong: return QLatin1String("DayOfWeekSectionLong"); -
5782 case QDateTimeParser::Hour24Section: return QLatin1String("Hour24Section"); -
5783 case QDateTimeParser::Hour12Section: return QLatin1String("Hour12Section"); -
5784 case QDateTimeParser::MSecSection: return QLatin1String("MSecSection"); -
5785 case QDateTimeParser::MinuteSection: return QLatin1String("MinuteSection"); -
5786 case QDateTimeParser::MonthSection: return QLatin1String("MonthSection"); -
5787 case QDateTimeParser::SecondSection: return QLatin1String("SecondSection"); -
5788 case QDateTimeParser::YearSection: return QLatin1String("YearSection"); -
5789 case QDateTimeParser::YearSection2Digits: return QLatin1String("YearSection2Digits"); -
5790 case QDateTimeParser::NoSection: return QLatin1String("NoSection"); -
5791 case QDateTimeParser::FirstSection: return QLatin1String("FirstSection"); -
5792 case QDateTimeParser::LastSection: return QLatin1String("LastSection"); -
5793 default: return QLatin1String("Unknown section ") + QString::number(s); -
5794 } -
5795} -
5796 -
5797/*! -
5798 \internal -
5799 For debugging. Returns the name of the state \a s. -
5800*/ -
5801 -
5802QString QDateTimeParser::stateName(int s) const -
5803{ -
5804 switch (s) { -
5805 case Invalid: return QLatin1String("Invalid"); -
5806 case Intermediate: return QLatin1String("Intermediate"); -
5807 case Acceptable: return QLatin1String("Acceptable"); -
5808 default: return QLatin1String("Unknown state ") + QString::number(s); -
5809 } -
5810} -
5811 -
5812#ifndef QT_NO_DATESTRING -
5813bool QDateTimeParser::fromString(const QString &t, QDate *date, QTime *time) const -
5814{ -
5815 QDateTime val(QDate(1900, 1, 1), QDATETIMEEDIT_TIME_MIN); -
5816 QString text = t; -
5817 int copy = -1; -
5818 const StateNode tmp = parse(text, copy, val, false); -
5819 if (tmp.state != Acceptable || tmp.conflicts) { -
5820 return false; -
5821 } -
5822 if (time) { -
5823 const QTime t = tmp.value.time(); -
5824 if (!t.isValid()) { -
5825 return false; -
5826 } -
5827 *time = t; -
5828 } -
5829 -
5830 if (date) { -
5831 const QDate d = tmp.value.date(); -
5832 if (!d.isValid()) { -
5833 return false; -
5834 } -
5835 *date = d; -
5836 } -
5837 return true; -
5838} -
5839#endif // QT_NO_DATESTRING -
5840 -
5841QDateTime QDateTimeParser::getMinimum() const -
5842{ -
5843 return QDateTime(QDATETIMEEDIT_DATE_MIN, QDATETIMEEDIT_TIME_MIN, spec); -
5844} -
5845 -
5846QDateTime QDateTimeParser::getMaximum() const -
5847{ -
5848 return QDateTime(QDATETIMEEDIT_DATE_MAX, QDATETIMEEDIT_TIME_MAX, spec); -
5849} -
5850 -
5851QString QDateTimeParser::getAmPmText(AmPm ap, Case cs) const -
5852{ -
5853 if (ap == AmText) { -
5854 return (cs == UpperCase ? QLatin1String("AM") : QLatin1String("am")); -
5855 } else { -
5856 return (cs == UpperCase ? QLatin1String("PM") : QLatin1String("pm")); -
5857 } -
5858} -
5859 -
5860/* -
5861 \internal -
5862 -
5863 I give arg2 preference because arg1 is always a QDateTime. -
5864*/ -
5865 -
5866bool operator==(const QDateTimeParser::SectionNode &s1, const QDateTimeParser::SectionNode &s2) -
5867{ -
5868 return (s1.type == s2.type) && (s1.pos == s2.pos) && (s1.count == s2.count); -
5869} -
5870 -
5871#endif // QT_BOOTSTRAPPED -
5872 -
5873QT_END_NAMESPACE -
5874 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial