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