src/corelib/tools/qdatetime.cpp
changeset 30 5dc02b23752f
parent 18 2f34d5167611
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
    86     SECS_PER_DAY = 86400,
    86     SECS_PER_DAY = 86400,
    87     MSECS_PER_DAY = 86400000,
    87     MSECS_PER_DAY = 86400000,
    88     SECS_PER_HOUR = 3600,
    88     SECS_PER_HOUR = 3600,
    89     MSECS_PER_HOUR = 3600000,
    89     MSECS_PER_HOUR = 3600000,
    90     SECS_PER_MIN = 60,
    90     SECS_PER_MIN = 60,
    91     MSECS_PER_MIN = 60000
    91     MSECS_PER_MIN = 60000,
       
    92     JULIAN_DAY_FOR_EPOCH = 2440588 // result of julianDayFromGregorianDate(1970, 1, 1)
    92 };
    93 };
    93 
    94 
    94 static inline QDate fixedDate(int y, int m, int d)
    95 static inline QDate fixedDate(int y, int m, int d)
    95 {
    96 {
    96     QDate result(y, m, 1);
    97     QDate result(y, m, 1);
    97     result.setDate(y, m, qMin(d, result.daysInMonth()));
    98     result.setDate(y, m, qMin(d, result.daysInMonth()));
    98     return result;
    99     return result;
    99 }
   100 }
   100 
   101 
       
   102 static inline uint julianDayFromGregorianDate(int year, int month, int day)
       
   103 {
       
   104     // Gregorian calendar starting from October 15, 1582
       
   105     // Algorithm from Henry F. Fliegel and Thomas C. Van Flandern
       
   106     return (1461 * (year + 4800 + (month - 14) / 12)) / 4
       
   107            + (367 * (month - 2 - 12 * ((month - 14) / 12))) / 12
       
   108            - (3 * ((year + 4900 + (month - 14) / 12) / 100)) / 4
       
   109            + day - 32075;
       
   110 }
       
   111 
   101 static uint julianDayFromDate(int year, int month, int day)
   112 static uint julianDayFromDate(int year, int month, int day)
   102 {
   113 {
   103     if (year < 0)
   114     if (year < 0)
   104         ++year;
   115         ++year;
   105 
   116 
   106     if (year > 1582 || (year == 1582 && (month > 10 || (month == 10 && day >= 15)))) {
   117     if (year > 1582 || (year == 1582 && (month > 10 || (month == 10 && day >= 15)))) {
   107         // Gregorian calendar starting from October 15, 1582
   118         return julianDayFromGregorianDate(year, month, day);
   108         // Algorithm from Henry F. Fliegel and Thomas C. Van Flandern
       
   109         return (1461 * (year + 4800 + (month - 14) / 12)) / 4
       
   110                + (367 * (month - 2 - 12 * ((month - 14) / 12))) / 12
       
   111                - (3 * ((year + 4900 + (month - 14) / 12) / 100)) / 4
       
   112                + day - 32075;
       
   113     } else if (year < 1582 || (year == 1582 && (month < 10 || (month == 10 && day <= 4)))) {
   119     } else if (year < 1582 || (year == 1582 && (month < 10 || (month == 10 && day <= 4)))) {
   114         // Julian calendar until October 4, 1582
   120         // Julian calendar until October 4, 1582
   115         // Algorithm from Frequently Asked Questions about Calendars by Claus Toendering
   121         // Algorithm from Frequently Asked Questions about Calendars by Claus Toendering
   116         int a = (14 - month) / 12;
   122         int a = (14 - month) / 12;
   117         return (153 * (month + (12 * a) - 3) + 2) / 5
   123         return (153 * (month + (12 * a) - 3) + 2) / 5
  1116     Returns true if this date is later than or equal to \a d;
  1122     Returns true if this date is later than or equal to \a d;
  1117     otherwise returns false.
  1123     otherwise returns false.
  1118 */
  1124 */
  1119 
  1125 
  1120 /*!
  1126 /*!
  1121     \overload
  1127     \fn QDate::currentDate()
  1122     Returns the current date, as reported by the system clock.
  1128     Returns the current date, as reported by the system clock.
  1123 
  1129 
  1124     \sa QTime::currentTime(), QDateTime::currentDateTime()
  1130     \sa QTime::currentTime(), QDateTime::currentDateTime()
  1125 */
  1131 */
  1126 
       
  1127 QDate QDate::currentDate()
       
  1128 {
       
  1129     QDate d;
       
  1130 #if defined(Q_OS_WIN)
       
  1131     SYSTEMTIME st;
       
  1132     memset(&st, 0, sizeof(SYSTEMTIME));
       
  1133     GetLocalTime(&st);
       
  1134     d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay);
       
  1135 #elif defined(Q_OS_SYMBIAN)
       
  1136     TTime localTime;
       
  1137     localTime.HomeTime();
       
  1138     TDateTime localDateTime = localTime.DateTime();
       
  1139     // months and days are zero indexed
       
  1140     d.jd = julianDayFromDate(localDateTime.Year(), localDateTime.Month() + 1, localDateTime.Day() + 1 );
       
  1141 #else
       
  1142     // posix compliant system
       
  1143     time_t ltime;
       
  1144     time(&ltime);
       
  1145     tm *t = 0;
       
  1146 
       
  1147 #if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
       
  1148     // use the reentrant version of localtime() where available
       
  1149     tzset();
       
  1150     tm res;
       
  1151     t = localtime_r(&ltime, &res);
       
  1152 #else
       
  1153     t = localtime(&ltime);
       
  1154 #endif // !QT_NO_THREAD && _POSIX_THREAD_SAFE_FUNCTIONS
       
  1155 
       
  1156     d.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
       
  1157 #endif
       
  1158     return d;
       
  1159 }
       
  1160 
  1132 
  1161 #ifndef QT_NO_DATESTRING
  1133 #ifndef QT_NO_DATESTRING
  1162 /*!
  1134 /*!
  1163     \fn QDate QDate::fromString(const QString &string, Qt::DateFormat format)
  1135     \fn QDate QDate::fromString(const QString &string, Qt::DateFormat format)
  1164 
  1136 
  1363 */
  1335 */
  1364 
  1336 
  1365 bool QDate::isLeapYear(int y)
  1337 bool QDate::isLeapYear(int y)
  1366 {
  1338 {
  1367     if (y < 1582) {
  1339     if (y < 1582) {
  1368         return qAbs(y) % 4 == 0;
  1340         if ( y < 1) {  // No year 0 in Julian calendar, so -1, -5, -9 etc are leap years
       
  1341             ++y;
       
  1342         }
       
  1343         return y % 4 == 0;
  1369     } else {
  1344     } else {
  1370         return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
  1345         return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
  1371     }
  1346     }
  1372 }
  1347 }
  1373 
  1348 
  1635     \row \i zzz \i the milliseconds with leading zeroes (000 to 999)
  1610     \row \i zzz \i the milliseconds with leading zeroes (000 to 999)
  1636     \row \i AP or A
  1611     \row \i AP or A
  1637          \i use AM/PM display. \e AP will be replaced by either "AM" or "PM".
  1612          \i use AM/PM display. \e AP will be replaced by either "AM" or "PM".
  1638     \row \i ap or a
  1613     \row \i ap or a
  1639          \i use am/pm display. \e ap will be replaced by either "am" or "pm".
  1614          \i use am/pm display. \e ap will be replaced by either "am" or "pm".
       
  1615     \row \i t \i the timezone (for example "CEST")
  1640     \endtable
  1616     \endtable
  1641 
  1617 
  1642     All other input characters will be ignored. Any sequence of characters that
  1618     All other input characters will be ignored. Any sequence of characters that
  1643     are enclosed in singlequotes will be treated as text and not be used as an
  1619     are enclosed in singlequotes will be treated as text and not be used as an
  1644     expression. Two consecutive singlequotes ("''") are replaced by a singlequote
  1620     expression. Two consecutive singlequotes ("''") are replaced by a singlequote
  1727     than the time of this object (or earlier if \a ms is negative).
  1703     than the time of this object (or earlier if \a ms is negative).
  1728 
  1704 
  1729     Note that the time will wrap if it passes midnight. See addSecs()
  1705     Note that the time will wrap if it passes midnight. See addSecs()
  1730     for an example.
  1706     for an example.
  1731 
  1707 
  1732     \sa addSecs(), msecsTo()
  1708     \sa addSecs(), msecsTo(), QDateTime::addMSecs()
  1733 */
  1709 */
  1734 
  1710 
  1735 QTime QTime::addMSecs(int ms) const
  1711 QTime QTime::addMSecs(int ms) const
  1736 {
  1712 {
  1737     QTime t;
  1713     QTime t;
  1756 
  1732 
  1757     Because QTime measures time within a day and there are 86400
  1733     Because QTime measures time within a day and there are 86400
  1758     seconds in a day, the result is always between -86400000 and
  1734     seconds in a day, the result is always between -86400000 and
  1759     86400000 ms.
  1735     86400000 ms.
  1760 
  1736 
  1761     \sa secsTo(), addMSecs()
  1737     \sa secsTo(), addMSecs(), QDateTime::msecsTo()
  1762 */
  1738 */
  1763 
  1739 
  1764 int QTime::msecsTo(const QTime &t) const
  1740 int QTime::msecsTo(const QTime &t) const
  1765 {
  1741 {
  1766 #if defined(Q_OS_WINCE)
  1742 #if defined(Q_OS_WINCE)
  1810     Returns true if this time is later than or equal to \a t;
  1786     Returns true if this time is later than or equal to \a t;
  1811     otherwise returns false.
  1787     otherwise returns false.
  1812 */
  1788 */
  1813 
  1789 
  1814 /*!
  1790 /*!
  1815     \overload
  1791     \fn QTime::currentTime()
  1816 
  1792 
  1817     Returns the current time as reported by the system clock.
  1793     Returns the current time as reported by the system clock.
  1818 
  1794 
  1819     Note that the accuracy depends on the accuracy of the underlying
  1795     Note that the accuracy depends on the accuracy of the underlying
  1820     operating system; not all systems provide 1-millisecond accuracy.
  1796     operating system; not all systems provide 1-millisecond accuracy.
  1821 */
  1797 */
  1822 
       
  1823 QTime QTime::currentTime()
       
  1824 {
       
  1825     QTime ct;
       
  1826 
       
  1827 #if defined(Q_OS_WIN)
       
  1828     SYSTEMTIME st;
       
  1829     memset(&st, 0, sizeof(SYSTEMTIME));
       
  1830     GetLocalTime(&st);
       
  1831     ct.mds = MSECS_PER_HOUR * st.wHour + MSECS_PER_MIN * st.wMinute + 1000 * st.wSecond
       
  1832              + st.wMilliseconds;
       
  1833 #if defined(Q_OS_WINCE)
       
  1834     ct.startTick = GetTickCount() % MSECS_PER_DAY;
       
  1835 #endif
       
  1836 #elif defined(Q_OS_SYMBIAN)
       
  1837     TTime localTime;
       
  1838     localTime.HomeTime();
       
  1839     TDateTime localDateTime = localTime.DateTime();
       
  1840     ct.mds = MSECS_PER_HOUR * localDateTime.Hour() + MSECS_PER_MIN * localDateTime.Minute()
       
  1841                  + 1000 * localDateTime.Second() + (localDateTime.MicroSecond() / 1000);
       
  1842 #elif defined(Q_OS_UNIX)
       
  1843     // posix compliant system
       
  1844     struct timeval tv;
       
  1845     gettimeofday(&tv, 0);
       
  1846     time_t ltime = tv.tv_sec;
       
  1847     tm *t = 0;
       
  1848 
       
  1849 #if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
       
  1850     // use the reentrant version of localtime() where available
       
  1851     tzset();
       
  1852     tm res;
       
  1853     t = localtime_r(&ltime, &res);
       
  1854 #else
       
  1855     t = localtime(&ltime);
       
  1856 #endif
       
  1857     Q_CHECK_PTR(t);
       
  1858 
       
  1859     ct.mds = MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + 1000 * t->tm_sec
       
  1860              + tv.tv_usec / 1000;
       
  1861 #else
       
  1862     time_t ltime; // no millisecond resolution
       
  1863     ::time(&ltime);
       
  1864     const tm *const t = localtime(&ltime);
       
  1865     ct.mds = MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + 1000 * t->tm_sec;
       
  1866 #endif
       
  1867     return ct;
       
  1868 }
       
  1869 
  1798 
  1870 #ifndef QT_NO_DATESTRING
  1799 #ifndef QT_NO_DATESTRING
  1871 /*!
  1800 /*!
  1872     \fn QTime QTime::fromString(const QString &string, Qt::DateFormat format)
  1801     \fn QTime QTime::fromString(const QString &string, Qt::DateFormat format)
  1873 
  1802 
  2111     QDateTime provides a full set of operators to compare two
  2040     QDateTime provides a full set of operators to compare two
  2112     QDateTime objects where smaller means earlier and larger means
  2041     QDateTime objects where smaller means earlier and larger means
  2113     later.
  2042     later.
  2114 
  2043 
  2115     You can increment (or decrement) a datetime by a given number of
  2044     You can increment (or decrement) a datetime by a given number of
  2116     seconds using addSecs(), or days using addDays(). Similarly you can
  2045     milliseconds using addMSecs(), seconds using addSecs(), or days
  2117     use addMonths() and addYears(). The daysTo() function returns the
  2046     using addDays(). Similarly you can use addMonths() and addYears().
  2118     number of days between two datetimes, and secsTo() returns the
  2047     The daysTo() function returns the number of days between two datetimes,
  2119     number of seconds between two datetimes.
  2048     secsTo() returns the number of seconds between two datetimes, and
       
  2049     msecsTo() returns the number of milliseconds between two datetimes.
  2120 
  2050 
  2121     QDateTime can store datetimes as \l{Qt::LocalTime}{local time} or
  2051     QDateTime can store datetimes as \l{Qt::LocalTime}{local time} or
  2122     as \l{Qt::UTC}{UTC}. QDateTime::currentDateTime() returns a
  2052     as \l{Qt::UTC}{UTC}. QDateTime::currentDateTime() returns a
  2123     QDateTime expressed as local time; use toUTC() to convert it to
  2053     QDateTime expressed as local time; use toUTC() to convert it to
  2124     UTC. You can also use timeSpec() to find out if a QDateTime
  2054     UTC. You can also use timeSpec() to find out if a QDateTime
  2389             d->spec = QDateTimePrivate::LocalUnknown;
  2319             d->spec = QDateTimePrivate::LocalUnknown;
  2390             break;
  2320             break;
  2391     }
  2321     }
  2392 }
  2322 }
  2393 
  2323 
  2394 static uint toTime_tHelper(const QDate &utcDate, const QTime &utcTime)
  2324 qint64 toMSecsSinceEpoch_helper(qint64 jd, int msecs)
  2395 {
  2325 {
  2396     int days = QDate(1970, 1, 1).daysTo(utcDate);
  2326     qint64 days = jd - JULIAN_DAY_FOR_EPOCH;
  2397     int secs = QTime().secsTo(utcTime);
  2327     qint64 retval = (days * MSECS_PER_DAY) + msecs;
  2398     if (days < 0 || (days == 0 && secs < 0))
  2328     return retval;
  2399         return uint(-1);
  2329 }
  2400 
  2330 
  2401     qlonglong retval = (qlonglong(days) * SECS_PER_DAY) + secs;
  2331 /*!
  2402     if (retval >= Q_INT64_C(0xFFFFFFFF))
  2332     \since 4.7
  2403         return uint(-1);
  2333 
  2404     return uint(retval);
  2334     Returns the datetime as the number of milliseconds that have passed
  2405 }
  2335     since 1970-01-01T00:00:00.000, Coordinated Universal Time (Qt::UTC).
  2406 
       
  2407 /*!
       
  2408     Returns the datetime as the number of seconds that have passed
       
  2409     since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC).
       
  2410 
  2336 
  2411     On systems that do not support time zones, this function will
  2337     On systems that do not support time zones, this function will
  2412     behave as if local time were Qt::UTC.
  2338     behave as if local time were Qt::UTC.
  2413 
  2339 
  2414     \sa setTime_t()
  2340     The behavior for this function is undefined if the datetime stored in
  2415 */
  2341     this object is not valid. However, for all valid dates, this function
  2416 
  2342     returns a unique value.
  2417 uint QDateTime::toTime_t() const
  2343 
       
  2344     \sa toTime_t(), setMSecsSinceEpoch()
       
  2345 */
       
  2346 qint64 QDateTime::toMSecsSinceEpoch() const
  2418 {
  2347 {
  2419     QDate utcDate;
  2348     QDate utcDate;
  2420     QTime utcTime;
  2349     QTime utcTime;
  2421     d->getUTC(utcDate, utcTime);
  2350     d->getUTC(utcDate, utcTime);
  2422 
  2351 
  2423     return toTime_tHelper(utcDate, utcTime);
  2352     return toMSecsSinceEpoch_helper(utcDate.jd, utcTime.ds());
       
  2353 }
       
  2354 
       
  2355 /*!
       
  2356     Returns the datetime as the number of seconds that have passed
       
  2357     since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC).
       
  2358 
       
  2359     On systems that do not support time zones, this function will
       
  2360     behave as if local time were Qt::UTC.
       
  2361 
       
  2362     \note This function returns a 32-bit unsigned integer, so it does not
       
  2363     support dates before 1970, but it does support dates after
       
  2364     2038-01-19T03:14:06, which may not be valid time_t values. Be careful
       
  2365     when passing those time_t values to system functions, which could
       
  2366     interpret them as negative dates.
       
  2367 
       
  2368     If the date is outside the range 1970-01-01T00:00:00 to
       
  2369     2106-02-07T06:28:14, this function returns -1 cast to an unsigned integer
       
  2370     (i.e., 0xFFFFFFFF).
       
  2371 
       
  2372     To get an extended range, use toMSecsSinceEpoch().
       
  2373 
       
  2374     \sa toMSecsSinceEpoch(), setTime_t()
       
  2375 */
       
  2376 
       
  2377 uint QDateTime::toTime_t() const
       
  2378 {
       
  2379     qint64 retval = toMSecsSinceEpoch() / 1000;
       
  2380     if (quint64(retval) >= Q_UINT64_C(0xFFFFFFFF))
       
  2381         return uint(-1);
       
  2382     return uint(retval);
       
  2383 }
       
  2384 
       
  2385 /*!
       
  2386     \since 4.7
       
  2387 
       
  2388     Sets the date and time given the number of \a mulliseconds that have
       
  2389     passed since 1970-01-01T00:00:00.000, Coordinated Universal Time
       
  2390     (Qt::UTC). On systems that do not support time zones this function
       
  2391     will behave as if local time were Qt::UTC.
       
  2392 
       
  2393     Note that there are possible values for \a msecs that lie outside the
       
  2394     valid range of QDateTime, both negative and positive. The behavior of
       
  2395     this function is undefined for those values.
       
  2396 
       
  2397     \sa toMSecsSinceEpoch(), setTime_t()
       
  2398 */
       
  2399 void QDateTime::setMSecsSinceEpoch(qint64 msecs)
       
  2400 {
       
  2401     detach();
       
  2402 
       
  2403     QDateTimePrivate::Spec oldSpec = d->spec;
       
  2404 
       
  2405     int ddays = msecs / MSECS_PER_DAY;
       
  2406     msecs %= MSECS_PER_DAY;
       
  2407     if (msecs < 0) {
       
  2408         // negative
       
  2409         --ddays;
       
  2410         msecs += MSECS_PER_DAY;
       
  2411     }
       
  2412 
       
  2413     d->date = QDate(1970, 1, 1).addDays(ddays);
       
  2414     d->time = QTime().addMSecs(msecs);
       
  2415     d->spec = QDateTimePrivate::UTC;
       
  2416 
       
  2417     if (oldSpec != QDateTimePrivate::UTC)
       
  2418         d->spec = d->getLocal(d->date, d->time);
  2424 }
  2419 }
  2425 
  2420 
  2426 /*!
  2421 /*!
  2427     \fn void QDateTime::setTime_t(uint seconds)
  2422     \fn void QDateTime::setTime_t(uint seconds)
  2428 
  2423 
  2723 /*!
  2718 /*!
  2724     Returns a QDateTime object containing a datetime \a msecs miliseconds
  2719     Returns a QDateTime object containing a datetime \a msecs miliseconds
  2725     later than the datetime of this object (or earlier if \a msecs is
  2720     later than the datetime of this object (or earlier if \a msecs is
  2726     negative).
  2721     negative).
  2727 
  2722 
  2728     \sa addSecs(), secsTo(), addDays(), addMonths(), addYears()
  2723     \sa addSecs(), msecsTo(), addDays(), addMonths(), addYears()
  2729 */
  2724 */
  2730 QDateTime QDateTime::addMSecs(qint64 msecs) const
  2725 QDateTime QDateTime::addMSecs(qint64 msecs) const
  2731 {
  2726 {
  2732     return d->addMSecs(*this, msecs);
  2727     return d->addMSecs(*this, msecs);
  2733 }
  2728 }
  2735 /*!
  2730 /*!
  2736     Returns the number of days from this datetime to the \a other
  2731     Returns the number of days from this datetime to the \a other
  2737     datetime. If the \a other datetime is earlier than this datetime,
  2732     datetime. If the \a other datetime is earlier than this datetime,
  2738     the value returned is negative.
  2733     the value returned is negative.
  2739 
  2734 
  2740     \sa addDays(), secsTo()
  2735     \sa addDays(), secsTo(), msecsTo()
  2741 */
  2736 */
  2742 
  2737 
  2743 int QDateTime::daysTo(const QDateTime &other) const
  2738 int QDateTime::daysTo(const QDateTime &other) const
  2744 {
  2739 {
  2745     return d->date.daysTo(other.d->date);
  2740     return d->date.daysTo(other.d->date);
  2768     d->getUTC(date1, time1);
  2763     d->getUTC(date1, time1);
  2769     other.d->getUTC(date2, time2);
  2764     other.d->getUTC(date2, time2);
  2770 
  2765 
  2771     return (date1.daysTo(date2) * SECS_PER_DAY) + time1.secsTo(time2);
  2766     return (date1.daysTo(date2) * SECS_PER_DAY) + time1.secsTo(time2);
  2772 }
  2767 }
       
  2768 
       
  2769 /*!
       
  2770     Returns the number of milliseconds from this datetime to the \a other
       
  2771     datetime. If the \a other datetime is earlier than this datetime,
       
  2772     the value returned is negative.
       
  2773 
       
  2774     Before performing the comparison, the two datetimes are converted
       
  2775     to Qt::UTC to ensure that the result is correct if one of the two
       
  2776     datetimes has daylight saving time (DST) and the other doesn't.
       
  2777 
       
  2778     \sa addMSecs(), daysTo(), QTime::msecsTo()
       
  2779 */
       
  2780 
       
  2781 qint64 QDateTime::msecsTo(const QDateTime &other) const
       
  2782 {
       
  2783     QDate selfDate;
       
  2784     QDate otherDate;
       
  2785     QTime selfTime;
       
  2786     QTime otherTime;
       
  2787 
       
  2788     d->getUTC(selfDate, selfTime);
       
  2789     other.d->getUTC(otherDate, otherTime);
       
  2790 
       
  2791     return (static_cast<qint64>(selfDate.daysTo(otherDate)) * static_cast<qint64>(MSECS_PER_DAY))
       
  2792            + static_cast<qint64>(selfTime.msecsTo(otherTime));
       
  2793 }
       
  2794 
  2773 
  2795 
  2774 /*!
  2796 /*!
  2775     \fn QDateTime QDateTime::toTimeSpec(Qt::TimeSpec specification) const
  2797     \fn QDateTime QDateTime::toTimeSpec(Qt::TimeSpec specification) const
  2776 
  2798 
  2777     Returns a copy of this datetime configured to use the given time
  2799     Returns a copy of this datetime configured to use the given time
  2870     Returns true if this datetime is later than or equal to the
  2892     Returns true if this datetime is later than or equal to the
  2871     \a other datetime; otherwise returns false.
  2893     \a other datetime; otherwise returns false.
  2872 */
  2894 */
  2873 
  2895 
  2874 /*!
  2896 /*!
       
  2897     \fn QDateTime QDateTime::currentDateTime()
  2875     Returns the current datetime, as reported by the system clock, in
  2898     Returns the current datetime, as reported by the system clock, in
  2876     the local time zone.
  2899     the local time zone.
  2877 
  2900 
  2878     \sa QDate::currentDate(), QTime::currentTime(), toTimeSpec()
  2901     \sa currentDateTimeUtc(), QDate::currentDate(), QTime::currentTime(), toTimeSpec()
  2879 */
  2902 */
       
  2903 
       
  2904 /*!
       
  2905     \fn QDateTime QDateTime::currentDateTimeUtc()
       
  2906     \since 4.7
       
  2907     Returns the current datetime, as reported by the system clock, in
       
  2908     UTC.
       
  2909 
       
  2910     \sa currentDateTime(), QDate::currentDate(), QTime::currentTime(), toTimeSpec()
       
  2911 */
       
  2912 
       
  2913 /*!
       
  2914     \fn qint64 QDateTime::currentMSecsSinceEpoch()
       
  2915     \since 4.7
       
  2916 
       
  2917     Returns the number of milliseconds since 1970-01-01T00:00:00 Universal
       
  2918     Coordinated Time. This number is like the POSIX time_t variable, but
       
  2919     expressed in milliseconds instead.
       
  2920 
       
  2921     \sa currentDateTime(), currentDateTimeUtc(), toTime_t(), toTimeSpec()
       
  2922 */
       
  2923 
       
  2924 static inline uint msecsFromDecomposed(int hour, int minute, int sec, int msec = 0)
       
  2925 {
       
  2926     return MSECS_PER_HOUR * hour + MSECS_PER_MIN * minute + 1000 * sec + msec;
       
  2927 }
       
  2928 
       
  2929 #if defined(Q_OS_WIN)
       
  2930 QDate QDate::currentDate()
       
  2931 {
       
  2932     QDate d;
       
  2933     SYSTEMTIME st;
       
  2934     memset(&st, 0, sizeof(SYSTEMTIME));
       
  2935     GetLocalTime(&st);
       
  2936     d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay);
       
  2937     return d;
       
  2938 }
       
  2939 
       
  2940 QTime QTime::currentTime()
       
  2941 {
       
  2942     QTime ct;
       
  2943     SYSTEMTIME st;
       
  2944     memset(&st, 0, sizeof(SYSTEMTIME));
       
  2945     GetLocalTime(&st);
       
  2946     ct.mds = msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
       
  2947 #if defined(Q_OS_WINCE)
       
  2948     ct.startTick = GetTickCount() % MSECS_PER_DAY;
       
  2949 #endif
       
  2950     return ct;
       
  2951 }
  2880 
  2952 
  2881 QDateTime QDateTime::currentDateTime()
  2953 QDateTime QDateTime::currentDateTime()
  2882 {
  2954 {
  2883 #if defined(Q_OS_WIN)
       
  2884     QDate d;
  2955     QDate d;
  2885     QTime t;
  2956     QTime t;
  2886     SYSTEMTIME st;
  2957     SYSTEMTIME st;
  2887     memset(&st, 0, sizeof(SYSTEMTIME));
  2958     memset(&st, 0, sizeof(SYSTEMTIME));
  2888     GetLocalTime(&st);
  2959     GetLocalTime(&st);
  2889     d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay);
  2960     d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay);
  2890     t.mds = MSECS_PER_HOUR * st.wHour + MSECS_PER_MIN * st.wMinute + 1000 * st.wSecond
  2961     t.mds = msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
  2891             + st.wMilliseconds;
       
  2892     return QDateTime(d, t);
  2962     return QDateTime(d, t);
       
  2963 }
       
  2964 
       
  2965 QDateTime QDateTime::currentDateTimeUtc()
       
  2966 {
       
  2967     QDate d;
       
  2968     QTime t;
       
  2969     SYSTEMTIME st;
       
  2970     memset(&st, 0, sizeof(SYSTEMTIME));
       
  2971     GetSystemTime(&st);
       
  2972     d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay);
       
  2973     t.mds = msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
       
  2974     return QDateTime(d, t, Qt::UTC);
       
  2975 }
       
  2976 
       
  2977 qint64 QDateTime::currentMSecsSinceEpoch()
       
  2978 {
       
  2979     QDate d;
       
  2980     QTime t;
       
  2981     SYSTEMTIME st;
       
  2982     memset(&st, 0, sizeof(SYSTEMTIME));
       
  2983     GetSystemTime(&st);
       
  2984 
       
  2985     return msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds) +
       
  2986             qint64(julianDayFromGregorianDate(st.wYear, st.wMonth, st.wDay)
       
  2987                    - julianDayFromGregorianDate(1970, 1, 1)) * Q_INT64_C(86400000);
       
  2988 }
       
  2989 
  2893 #elif defined(Q_OS_SYMBIAN)
  2990 #elif defined(Q_OS_SYMBIAN)
  2894     return QDateTime(QDate::currentDate(), QTime::currentTime());
  2991 QDate QDate::currentDate()
       
  2992 {
       
  2993     QDate d;
       
  2994     TTime localTime;
       
  2995     localTime.HomeTime();
       
  2996     TDateTime localDateTime = localTime.DateTime();
       
  2997     // months and days are zero indexed
       
  2998     d.jd = julianDayFromDate(localDateTime.Year(), localDateTime.Month() + 1, localDateTime.Day() + 1 );
       
  2999     return d;
       
  3000 }
       
  3001 
       
  3002 QTime QTime::currentTime()
       
  3003 {
       
  3004     QTime ct;
       
  3005     TTime localTime;
       
  3006     localTime.HomeTime();
       
  3007     TDateTime localDateTime = localTime.DateTime();
       
  3008     ct.mds = msecsFromDecomposed(localDateTime.Hour(), localDateTime.Minute(),
       
  3009                                  localDateTime.Second(), localDateTime.MicroSecond() / 1000);
       
  3010     return ct;
       
  3011 }
       
  3012 
       
  3013 QDateTime QDateTime::currentDateTime()
       
  3014 {
       
  3015     QDate d;
       
  3016     QTime ct;
       
  3017     TTime localTime;
       
  3018     localTime.HomeTime();
       
  3019     TDateTime localDateTime = localTime.DateTime();
       
  3020     // months and days are zero indexed
       
  3021     d.jd = julianDayFromDate(localDateTime.Year(), localDateTime.Month() + 1, localDateTime.Day() + 1);
       
  3022     ct.mds = msecsFromDecomposed(localDateTime.Hour(), localDateTime.Minute(),
       
  3023                                  localDateTime.Second(), localDateTime.MicroSecond() / 1000);
       
  3024     return QDateTime(d, ct);
       
  3025 }
       
  3026 
       
  3027 QDateTime QDateTime::currentDateTimeUtc()
       
  3028 {
       
  3029     QDate d;
       
  3030     QTime ct;
       
  3031     TTime gmTime;
       
  3032     gmTime.UniversalTime();
       
  3033     TDateTime gmtDateTime = gmTime.DateTime();
       
  3034     // months and days are zero indexed
       
  3035     d.jd = julianDayFromDate(gmtDateTime.Year(), gmtDateTime.Month() + 1, gmtDateTime.Day() + 1);
       
  3036     ct.mds = msecsFromDecomposed(gmtDateTime.Hour(), gmtDateTime.Minute(),
       
  3037                                  gmtDateTime.Second(), gmtDateTime.MicroSecond() / 1000);
       
  3038     return QDateTime(d, ct, Qt::UTC);
       
  3039 }
       
  3040 
       
  3041 qint64 QDateTime::currentMSecsSinceEpoch()
       
  3042 {
       
  3043     QDate d;
       
  3044     QTime ct;
       
  3045     TTime gmTime;
       
  3046     gmTime.UniversalTime();
       
  3047     TDateTime gmtDateTime = gmTime.DateTime();
       
  3048 
       
  3049     // according to the documentation, the value is:
       
  3050     // "a date and time as a number of microseconds since midnight, January 1st, 0 AD nominal Gregorian"
       
  3051     qint64 value = gmTime.Int64();
       
  3052 
       
  3053     // whereas 1970-01-01T00:00:00 is (in the same representation):
       
  3054     //   ((1970 * 365) + (1970 / 4) - (1970 / 100) + (1970 / 400) - 13) * 86400 * 1000000
       
  3055     static const qint64 unixEpoch = Q_INT64_C(0xdcddb30f2f8000);
       
  3056 
       
  3057     return (value - unixEpoch) / 1000;
       
  3058 }
       
  3059 
       
  3060 #elif defined(Q_OS_UNIX)
       
  3061 QDate QDate::currentDate()
       
  3062 {
       
  3063     QDate d;
       
  3064     // posix compliant system
       
  3065     time_t ltime;
       
  3066     time(&ltime);
       
  3067     struct tm *t = 0;
       
  3068 
       
  3069 #if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
       
  3070     // use the reentrant version of localtime() where available
       
  3071     tzset();
       
  3072     struct tm res;
       
  3073     t = localtime_r(&ltime, &res);
  2895 #else
  3074 #else
  2896 #if defined(Q_OS_UNIX)
  3075     t = localtime(&ltime);
       
  3076 #endif // !QT_NO_THREAD && _POSIX_THREAD_SAFE_FUNCTIONS
       
  3077 
       
  3078     d.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
       
  3079     return d;
       
  3080 }
       
  3081 
       
  3082 QTime QTime::currentTime()
       
  3083 {
       
  3084     QTime ct;
       
  3085     // posix compliant system
       
  3086     struct timeval tv;
       
  3087     gettimeofday(&tv, 0);
       
  3088     time_t ltime = tv.tv_sec;
       
  3089     struct tm *t = 0;
       
  3090 
       
  3091 #if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
       
  3092     // use the reentrant version of localtime() where available
       
  3093     tzset();
       
  3094     struct tm res;
       
  3095     t = localtime_r(&ltime, &res);
       
  3096 #else
       
  3097     t = localtime(&ltime);
       
  3098 #endif
       
  3099     Q_CHECK_PTR(t);
       
  3100 
       
  3101     ct.mds = msecsFromDecomposed(t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000);
       
  3102     return ct;
       
  3103 }
       
  3104 
       
  3105 QDateTime QDateTime::currentDateTime()
       
  3106 {
  2897     // posix compliant system
  3107     // posix compliant system
  2898     // we have milliseconds
  3108     // we have milliseconds
  2899     struct timeval tv;
  3109     struct timeval tv;
  2900     gettimeofday(&tv, 0);
  3110     gettimeofday(&tv, 0);
  2901     time_t ltime = tv.tv_sec;
  3111     time_t ltime = tv.tv_sec;
  2902     tm *t = 0;
  3112     struct tm *t = 0;
  2903 
  3113 
  2904 #if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
  3114 #if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
  2905     // use the reentrant version of localtime() where available
  3115     // use the reentrant version of localtime() where available
  2906     tzset();
  3116     tzset();
  2907     tm res;
  3117     struct tm res;
  2908     t = localtime_r(&ltime, &res);
  3118     t = localtime_r(&ltime, &res);
  2909 #else
  3119 #else
  2910     t = localtime(&ltime);
  3120     t = localtime(&ltime);
  2911 #endif
  3121 #endif
  2912 
  3122 
  2913     QDateTime dt;
  3123     QDateTime dt;
  2914     dt.d->time.mds = MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + 1000 * t->tm_sec
  3124     dt.d->time.mds = msecsFromDecomposed(t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000);
  2915                      + tv.tv_usec / 1000;
       
  2916 #else
       
  2917     time_t ltime; // no millisecond resolution
       
  2918     ::time(&ltime);
       
  2919     tm *t = 0;
       
  2920     localtime(&ltime);
       
  2921     dt.d->time.mds = MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + 1000 * t->tm_sec;
       
  2922 #endif // Q_OS_UNIX
       
  2923 
  3125 
  2924     dt.d->date.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
  3126     dt.d->date.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
  2925     dt.d->spec = t->tm_isdst > 0  ? QDateTimePrivate::LocalDST :
  3127     dt.d->spec = t->tm_isdst > 0  ? QDateTimePrivate::LocalDST :
  2926                  t->tm_isdst == 0 ? QDateTimePrivate::LocalStandard :
  3128                  t->tm_isdst == 0 ? QDateTimePrivate::LocalStandard :
  2927                  QDateTimePrivate::LocalUnknown;
  3129                  QDateTimePrivate::LocalUnknown;
  2928     return dt;
  3130     return dt;
       
  3131 }
       
  3132 
       
  3133 QDateTime QDateTime::currentDateTimeUtc()
       
  3134 {
       
  3135     // posix compliant system
       
  3136     // we have milliseconds
       
  3137     struct timeval tv;
       
  3138     gettimeofday(&tv, 0);
       
  3139     time_t ltime = tv.tv_sec;
       
  3140     struct tm *t = 0;
       
  3141 
       
  3142 #if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
       
  3143     // use the reentrant version of localtime() where available
       
  3144     struct tm res;
       
  3145     t = gmtime_r(&ltime, &res);
       
  3146 #else
       
  3147     t = gmtime(&ltime);
  2929 #endif
  3148 #endif
  2930 }
  3149 
       
  3150     QDateTime dt;
       
  3151     dt.d->time.mds = msecsFromDecomposed(t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000);
       
  3152 
       
  3153     dt.d->date.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
       
  3154     dt.d->spec = QDateTimePrivate::UTC;
       
  3155     return dt;
       
  3156 }
       
  3157 
       
  3158 qint64 QDateTime::currentMSecsSinceEpoch()
       
  3159 {
       
  3160     // posix compliant system
       
  3161     // we have milliseconds
       
  3162     struct timeval tv;
       
  3163     gettimeofday(&tv, 0);
       
  3164     return qint64(tv.tv_sec) * Q_INT64_C(1000) + tv.tv_usec / 1000;
       
  3165 }
       
  3166 
       
  3167 #else
       
  3168 #error "What system is this?"
       
  3169 #endif
  2931 
  3170 
  2932 /*!
  3171 /*!
  2933   \since 4.2
  3172   \since 4.2
  2934 
  3173 
  2935   Returns a datetime whose date and time are the number of \a seconds
  3174   Returns a datetime whose date and time are the number of \a seconds
  2941 */
  3180 */
  2942 QDateTime QDateTime::fromTime_t(uint seconds)
  3181 QDateTime QDateTime::fromTime_t(uint seconds)
  2943 {
  3182 {
  2944     QDateTime d;
  3183     QDateTime d;
  2945     d.setTime_t(seconds);
  3184     d.setTime_t(seconds);
       
  3185     return d;
       
  3186 }
       
  3187 
       
  3188 /*!
       
  3189   \since 4.7
       
  3190 
       
  3191   Returns a datetime whose date and time are the number of milliseconds \a msec
       
  3192   that have passed since 1970-01-01T00:00:00.000, Coordinated Universal
       
  3193   Time (Qt::UTC). On systems that do not support time zones, the time
       
  3194   will be set as if local time were Qt::UTC.
       
  3195 
       
  3196   Note that there are possible values for \a msecs that lie outside the valid
       
  3197   range of QDateTime, both negative and positive. The behavior of this
       
  3198   function is undefined for those values.
       
  3199 
       
  3200   \sa toTime_t(), setTime_t()
       
  3201 */
       
  3202 QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs)
       
  3203 {
       
  3204     QDateTime d;
       
  3205     d.setMSecsSinceEpoch(msecs);
  2946     return d;
  3206     return d;
  2947 }
  3207 }
  2948 
  3208 
  2949 /*!
  3209 /*!
  2950  \since 4.4
  3210  \since 4.4
  3311 /*!
  3571 /*!
  3312     \relates QDate
  3572     \relates QDate
  3313 
  3573 
  3314     Writes the \a date to stream \a out.
  3574     Writes the \a date to stream \a out.
  3315 
  3575 
  3316     \sa {Format of the QDataStream operators}
  3576     \sa {Serializing Qt Data Types}
  3317 */
  3577 */
  3318 
  3578 
  3319 QDataStream &operator<<(QDataStream &out, const QDate &date)
  3579 QDataStream &operator<<(QDataStream &out, const QDate &date)
  3320 {
  3580 {
  3321     return out << (quint32)(date.jd);
  3581     return out << (quint32)(date.jd);
  3324 /*!
  3584 /*!
  3325     \relates QDate
  3585     \relates QDate
  3326 
  3586 
  3327     Reads a date from stream \a in into the \a date.
  3587     Reads a date from stream \a in into the \a date.
  3328 
  3588 
  3329     \sa {Format of the QDataStream operators}
  3589     \sa {Serializing Qt Data Types}
  3330 */
  3590 */
  3331 
  3591 
  3332 QDataStream &operator>>(QDataStream &in, QDate &date)
  3592 QDataStream &operator>>(QDataStream &in, QDate &date)
  3333 {
  3593 {
  3334     quint32 jd;
  3594     quint32 jd;
  3340 /*!
  3600 /*!
  3341     \relates QTime
  3601     \relates QTime
  3342 
  3602 
  3343     Writes \a time to stream \a out.
  3603     Writes \a time to stream \a out.
  3344 
  3604 
  3345     \sa {Format of the QDataStream operators}
  3605     \sa {Serializing Qt Data Types}
  3346 */
  3606 */
  3347 
  3607 
  3348 QDataStream &operator<<(QDataStream &out, const QTime &time)
  3608 QDataStream &operator<<(QDataStream &out, const QTime &time)
  3349 {
  3609 {
  3350     return out << quint32(time.mds);
  3610     return out << quint32(time.mds);
  3353 /*!
  3613 /*!
  3354     \relates QTime
  3614     \relates QTime
  3355 
  3615 
  3356     Reads a time from stream \a in into the given \a time.
  3616     Reads a time from stream \a in into the given \a time.
  3357 
  3617 
  3358     \sa {Format of the QDataStream operators}
  3618     \sa {Serializing Qt Data Types}
  3359 */
  3619 */
  3360 
  3620 
  3361 QDataStream &operator>>(QDataStream &in, QTime &time)
  3621 QDataStream &operator>>(QDataStream &in, QTime &time)
  3362 {
  3622 {
  3363     quint32 ds;
  3623     quint32 ds;
  3369 /*!
  3629 /*!
  3370     \relates QDateTime
  3630     \relates QDateTime
  3371 
  3631 
  3372     Writes \a dateTime to the \a out stream.
  3632     Writes \a dateTime to the \a out stream.
  3373 
  3633 
  3374     \sa {Format of the QDataStream operators}
  3634     \sa {Serializing Qt Data Types}
  3375 */
  3635 */
  3376 QDataStream &operator<<(QDataStream &out, const QDateTime &dateTime)
  3636 QDataStream &operator<<(QDataStream &out, const QDateTime &dateTime)
  3377 {
  3637 {
  3378     out << dateTime.d->date << dateTime.d->time;
  3638     out << dateTime.d->date << dateTime.d->time;
  3379     if (out.version() >= 7)
  3639     if (out.version() >= 7)
  3384 /*!
  3644 /*!
  3385     \relates QDateTime
  3645     \relates QDateTime
  3386 
  3646 
  3387     Reads a datetime from the stream \a in into \a dateTime.
  3647     Reads a datetime from the stream \a in into \a dateTime.
  3388 
  3648 
  3389     \sa {Format of the QDataStream operators}
  3649     \sa {Serializing Qt Data Types}
  3390 */
  3650 */
  3391 
  3651 
  3392 QDataStream &operator>>(QDataStream &in, QDateTime &dateTime)
  3652 QDataStream &operator>>(QDataStream &in, QDateTime &dateTime)
  3393 {
  3653 {
  3394     dateTime.detach();
  3654     dateTime.detach();
  3698 
  3958 
  3699 static QDateTimePrivate::Spec utcToLocal(QDate &date, QTime &time)
  3959 static QDateTimePrivate::Spec utcToLocal(QDate &date, QTime &time)
  3700 {
  3960 {
  3701     QDate fakeDate = adjustDate(date);
  3961     QDate fakeDate = adjustDate(date);
  3702 
  3962 
  3703     time_t secsSince1Jan1970UTC = toTime_tHelper(fakeDate, time);
  3963     // won't overflow because of fakeDate
       
  3964     time_t secsSince1Jan1970UTC = toMSecsSinceEpoch_helper(fakeDate.toJulianDay(), QTime().msecsTo(time)) / 1000;
  3704     tm *brokenDown = 0;
  3965     tm *brokenDown = 0;
  3705 
  3966 
  3706 #if defined(Q_OS_WINCE)
  3967 #if defined(Q_OS_WINCE)
  3707     tm res;
  3968     tm res;
  3708     FILETIME utcTime = time_tToFt(secsSince1Jan1970UTC);
  3969     FILETIME utcTime = time_tToFt(secsSince1Jan1970UTC);
  3783     localTM.tm_mday = fakeDate.day();
  4044     localTM.tm_mday = fakeDate.day();
  3784     localTM.tm_mon = fakeDate.month() - 1;
  4045     localTM.tm_mon = fakeDate.month() - 1;
  3785     localTM.tm_year = fakeDate.year() - 1900;
  4046     localTM.tm_year = fakeDate.year() - 1900;
  3786     localTM.tm_isdst = (int)isdst;
  4047     localTM.tm_isdst = (int)isdst;
  3787 #if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
  4048 #if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
  3788     time_t secsSince1Jan1970UTC = toTime_tHelper(fakeDate, time);
  4049     time_t secsSince1Jan1970UTC = (toMSecsSinceEpoch_helper(fakeDate.toJulianDay(), QTime().msecsTo(time)) / 1000);
  3789 #else
  4050 #else
  3790 #if defined(Q_OS_WIN)
  4051 #if defined(Q_OS_WIN)
  3791     _tzset();
  4052     _tzset();
  3792 #endif
  4053 #endif
  3793     time_t secsSince1Jan1970UTC = mktime(&localTM);
  4054     time_t secsSince1Jan1970UTC = mktime(&localTM);