-rw-r--r-- | qmake/tools/qdatetime.cpp | 70 |
1 files changed, 58 insertions, 12 deletions
diff --git a/qmake/tools/qdatetime.cpp b/qmake/tools/qdatetime.cpp index 93e40a8..3137877 100644 --- a/qmake/tools/qdatetime.cpp +++ b/qmake/tools/qdatetime.cpp | |||
@@ -2,13 +2,13 @@ | |||
2 | ** $Id$ | 2 | ** $Id$ |
3 | ** | 3 | ** |
4 | ** Implementation of date and time classes | 4 | ** Implementation of date and time classes |
5 | ** | 5 | ** |
6 | ** Created : 940124 | 6 | ** Created : 940124 |
7 | ** | 7 | ** |
8 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved. | 8 | ** Copyright (C) 1992-2002 Trolltech AS. All rights reserved. |
9 | ** | 9 | ** |
10 | ** This file is part of the tools module of the Qt GUI Toolkit. | 10 | ** This file is part of the tools module of the Qt GUI Toolkit. |
11 | ** | 11 | ** |
12 | ** This file may be distributed under the terms of the Q Public License | 12 | ** This file may be distributed under the terms of the Q Public License |
13 | ** as defined by Trolltech AS of Norway and appearing in the file | 13 | ** as defined by Trolltech AS of Norway and appearing in the file |
14 | ** LICENSE.QPL included in the packaging of this file. | 14 | ** LICENSE.QPL included in the packaging of this file. |
@@ -32,13 +32,12 @@ | |||
32 | ** | 32 | ** |
33 | ** Contact info@trolltech.com if any conditions of this licensing are | 33 | ** Contact info@trolltech.com if any conditions of this licensing are |
34 | ** not clear to you. | 34 | ** not clear to you. |
35 | ** | 35 | ** |
36 | **********************************************************************/ | 36 | **********************************************************************/ |
37 | 37 | ||
38 | // Get the system specific includes and defines | ||
39 | #include "qplatformdefs.h" | 38 | #include "qplatformdefs.h" |
40 | 39 | ||
41 | #include "qdatetime.h" | 40 | #include "qdatetime.h" |
42 | #include "qdatastream.h" | 41 | #include "qdatastream.h" |
43 | #include "qregexp.h" | 42 | #include "qregexp.h" |
44 | 43 | ||
@@ -892,12 +891,18 @@ QDate QDate::addMonths( int nmonths ) const | |||
892 | 891 | ||
893 | QDate QDate::addYears( int nyears ) const | 892 | QDate QDate::addYears( int nyears ) const |
894 | { | 893 | { |
895 | int y, m, d; | 894 | int y, m, d; |
896 | julianToGregorian( jd, y, m, d ); | 895 | julianToGregorian( jd, y, m, d ); |
897 | y += nyears; | 896 | y += nyears; |
897 | |||
898 | QDate tmp(y,m,1); | ||
899 | |||
900 | if( d > tmp.daysInMonth() ) | ||
901 | d = tmp.daysInMonth(); | ||
902 | |||
898 | QDate date(y, m, d); | 903 | QDate date(y, m, d); |
899 | return date; | 904 | return date; |
900 | } | 905 | } |
901 | 906 | ||
902 | 907 | ||
903 | 908 | ||
@@ -987,19 +992,31 @@ QDate QDate::currentDate( Qt::TimeSpec ts ) | |||
987 | if ( ts == Qt::LocalTime ) | 992 | if ( ts == Qt::LocalTime ) |
988 | GetLocalTime( &t ); | 993 | GetLocalTime( &t ); |
989 | else | 994 | else |
990 | GetSystemTime( &t ); | 995 | GetSystemTime( &t ); |
991 | d.jd = gregorianToJulian( t.wYear, t.wMonth, t.wDay ); | 996 | d.jd = gregorianToJulian( t.wYear, t.wMonth, t.wDay ); |
992 | #else | 997 | #else |
998 | // posix compliant system | ||
993 | time_t ltime; | 999 | time_t ltime; |
994 | time( <ime ); | 1000 | time( <ime ); |
995 | tm *t; | 1001 | tm *t; |
1002 | |||
1003 | # if defined(QT_THREAD_SUPPORT) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) | ||
1004 | // use the reentrant versions of localtime() and gmtime() where available | ||
1005 | tm res; | ||
1006 | if ( ts == Qt::LocalTime ) | ||
1007 | t = localtime_r( <ime, &res ); | ||
1008 | else | ||
1009 | t = gmtime_r( <ime, &res ); | ||
1010 | # else | ||
996 | if ( ts == Qt::LocalTime ) | 1011 | if ( ts == Qt::LocalTime ) |
997 | t = localtime( <ime ); | 1012 | t = localtime( <ime ); |
998 | else | 1013 | else |
999 | t = gmtime( <ime ); | 1014 | t = gmtime( <ime ); |
1015 | # endif // QT_THREAD_SUPPORT && _POSIX_THREAD_SAFE_FUNCTIONS | ||
1016 | |||
1000 | d.jd = gregorianToJulian( t->tm_year + 1900, t->tm_mon + 1, t->tm_mday ); | 1017 | d.jd = gregorianToJulian( t->tm_year + 1900, t->tm_mon + 1, t->tm_mday ); |
1001 | #endif | 1018 | #endif |
1002 | return d; | 1019 | return d; |
1003 | } | 1020 | } |
1004 | 1021 | ||
1005 | #ifndef QT_NO_DATESTRING | 1022 | #ifndef QT_NO_DATESTRING |
@@ -1552,13 +1569,13 @@ int QTime::msecsTo( const QTime &t ) const | |||
1552 | Returns TRUE if this time is later than or equal to \a t; | 1569 | Returns TRUE if this time is later than or equal to \a t; |
1553 | otherwise returns FALSE. | 1570 | otherwise returns FALSE. |
1554 | */ | 1571 | */ |
1555 | 1572 | ||
1556 | 1573 | ||
1557 | 1574 | ||
1558 | /*! | 1575 | /*! |
1559 | \overload | 1576 | \overload |
1560 | 1577 | ||
1561 | Returns the current time as reported by the system clock. | 1578 | Returns the current time as reported by the system clock. |
1562 | 1579 | ||
1563 | Note that the accuracy depends on the accuracy of the underlying | 1580 | Note that the accuracy depends on the accuracy of the underlying |
1564 | operating system; not all systems provide 1-millisecond accuracy. | 1581 | operating system; not all systems provide 1-millisecond accuracy. |
@@ -1650,28 +1667,39 @@ bool QTime::currentTime( QTime *ct, Qt::TimeSpec ts ) | |||
1650 | } else { | 1667 | } else { |
1651 | GetSystemTime( &t ); | 1668 | GetSystemTime( &t ); |
1652 | } | 1669 | } |
1653 | ct->ds = (uint)( MSECS_PER_HOUR*t.wHour + MSECS_PER_MIN*t.wMinute + | 1670 | ct->ds = (uint)( MSECS_PER_HOUR*t.wHour + MSECS_PER_MIN*t.wMinute + |
1654 | 1000*t.wSecond + t.wMilliseconds ); | 1671 | 1000*t.wSecond + t.wMilliseconds ); |
1655 | #elif defined(Q_OS_UNIX) | 1672 | #elif defined(Q_OS_UNIX) |
1673 | // posix compliant system | ||
1656 | struct timeval tv; | 1674 | struct timeval tv; |
1657 | gettimeofday( &tv, 0 ); | 1675 | gettimeofday( &tv, 0 ); |
1658 | time_t ltime = tv.tv_sec; | 1676 | time_t ltime = tv.tv_sec; |
1659 | tm *t; | 1677 | tm *t; |
1660 | if ( ts == Qt::LocalTime ) { | 1678 | |
1679 | # if defined(QT_THREAD_SUPPORT) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) | ||
1680 | // use the reentrant versions of localtime() and gmtime() where available | ||
1681 | tm res; | ||
1682 | if ( ts == Qt::LocalTime ) | ||
1683 | t = localtime_r( <ime, &res ); | ||
1684 | else | ||
1685 | t = gmtime_r( <ime, &res ); | ||
1686 | # else | ||
1687 | if ( ts == Qt::LocalTime ) | ||
1661 | t = localtime( <ime ); | 1688 | t = localtime( <ime ); |
1662 | } else { | 1689 | else |
1663 | t = gmtime( <ime ); | 1690 | t = gmtime( <ime ); |
1664 | } | 1691 | # endif // QT_THREAD_SUPPORT && _POSIX_THREAD_SAFE_FUNCTIONS |
1692 | |||
1665 | ct->ds = (uint)( MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + | 1693 | ct->ds = (uint)( MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + |
1666 | 1000 * t->tm_sec + tv.tv_usec / 1000 ); | 1694 | 1000 * t->tm_sec + tv.tv_usec / 1000 ); |
1667 | #else | 1695 | #else |
1668 | time_t ltime; // no millisecond resolution | 1696 | time_t ltime; // no millisecond resolution |
1669 | ::time( <ime ); | 1697 | ::time( <ime ); |
1670 | tm *t; | 1698 | tm *t; |
1671 | if ( ts == Qt::LocalTime ) | 1699 | if ( ts == Qt::LocalTime ) |
1672 | localtime( <ime ); | 1700 | localtime( <ime ); |
1673 | else | 1701 | else |
1674 | gmtime( <ime ); | 1702 | gmtime( <ime ); |
1675 | ct->ds = (uint) ( MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + | 1703 | ct->ds = (uint) ( MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + |
1676 | 1000 * t->tm_sec ); | 1704 | 1000 * t->tm_sec ); |
1677 | #endif | 1705 | #endif |
@@ -1703,15 +1731,15 @@ bool QTime::isValid( int h, int m, int s, int ms ) | |||
1703 | 1731 | ||
1704 | /*! | 1732 | /*! |
1705 | Sets this time to the current time. This is practical for timing: | 1733 | Sets this time to the current time. This is practical for timing: |
1706 | 1734 | ||
1707 | \code | 1735 | \code |
1708 | QTime t; | 1736 | QTime t; |
1709 | t.start(); // start clock | 1737 | t.start(); |
1710 | ... // some lengthy task | 1738 | some_lengthy_task(); |
1711 | qDebug( "%d\n", t.elapsed() ); // prints the number of msecs elapsed | 1739 | qDebug( "Time elapsed: %d ms", t.elapsed() ); |
1712 | \endcode | 1740 | \endcode |
1713 | 1741 | ||
1714 | \sa restart(), elapsed(), currentTime() | 1742 | \sa restart(), elapsed(), currentTime() |
1715 | */ | 1743 | */ |
1716 | 1744 | ||
1717 | void QTime::start() | 1745 | void QTime::start() |
@@ -1957,22 +1985,40 @@ void QDateTime::setTime_t( uint secsSince1Jan1970UTC ) | |||
1957 | \sa toTime_t() | 1985 | \sa toTime_t() |
1958 | */ | 1986 | */ |
1959 | void QDateTime::setTime_t( uint secsSince1Jan1970UTC, Qt::TimeSpec ts ) | 1987 | void QDateTime::setTime_t( uint secsSince1Jan1970UTC, Qt::TimeSpec ts ) |
1960 | { | 1988 | { |
1961 | time_t tmp = (time_t) secsSince1Jan1970UTC; | 1989 | time_t tmp = (time_t) secsSince1Jan1970UTC; |
1962 | tm *brokenDown = 0; | 1990 | tm *brokenDown = 0; |
1991 | |||
1992 | #if defined(Q_OS_UNIX) && defined(QT_THREAD_SUPPORT) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) | ||
1993 | // posix compliant system | ||
1994 | // use the reentrant versions of localtime() and gmtime() where available | ||
1995 | tm res; | ||
1996 | if ( ts == Qt::LocalTime ) | ||
1997 | brokenDown = localtime_r( &tmp, &res ); | ||
1998 | if ( !brokenDown ) { | ||
1999 | brokenDown = gmtime_r( &tmp, &res ); | ||
2000 | if ( !brokenDown ) { | ||
2001 | d.jd = QDate::gregorianToJulian( 1970, 1, 1 ); | ||
2002 | t.ds = 0; | ||
2003 | return; | ||
2004 | } | ||
2005 | } | ||
2006 | #else | ||
1963 | if ( ts == Qt::LocalTime ) | 2007 | if ( ts == Qt::LocalTime ) |
1964 | brokenDown = localtime( &tmp ); | 2008 | brokenDown = localtime( &tmp ); |
1965 | if ( !brokenDown ) { | 2009 | if ( !brokenDown ) { |
1966 | brokenDown = gmtime( &tmp ); | 2010 | brokenDown = gmtime( &tmp ); |
1967 | if ( !brokenDown ) { | 2011 | if ( !brokenDown ) { |
1968 | d.jd = QDate::gregorianToJulian( 1970, 1, 1 ); | 2012 | d.jd = QDate::gregorianToJulian( 1970, 1, 1 ); |
1969 | t.ds = 0; | 2013 | t.ds = 0; |
1970 | return; | 2014 | return; |
1971 | } | 2015 | } |
1972 | } | 2016 | } |
2017 | #endif | ||
2018 | |||
1973 | d.jd = QDate::gregorianToJulian( brokenDown->tm_year + 1900, | 2019 | d.jd = QDate::gregorianToJulian( brokenDown->tm_year + 1900, |
1974 | brokenDown->tm_mon + 1, | 2020 | brokenDown->tm_mon + 1, |
1975 | brokenDown->tm_mday ); | 2021 | brokenDown->tm_mday ); |
1976 | t.ds = MSECS_PER_HOUR * brokenDown->tm_hour + | 2022 | t.ds = MSECS_PER_HOUR * brokenDown->tm_hour + |
1977 | MSECS_PER_MIN * brokenDown->tm_min + | 2023 | MSECS_PER_MIN * brokenDown->tm_min + |
1978 | 1000 * brokenDown->tm_sec; | 2024 | 1000 * brokenDown->tm_sec; |
@@ -2297,13 +2343,13 @@ bool QDateTime::operator>=( const QDateTime &dt ) const | |||
2297 | return TRUE; | 2343 | return TRUE; |
2298 | return d == dt.d ? t >= dt.t : FALSE; | 2344 | return d == dt.d ? t >= dt.t : FALSE; |
2299 | } | 2345 | } |
2300 | 2346 | ||
2301 | /*! | 2347 | /*! |
2302 | \overload | 2348 | \overload |
2303 | 2349 | ||
2304 | Returns the current datetime, as reported by the system clock. | 2350 | Returns the current datetime, as reported by the system clock. |
2305 | 2351 | ||
2306 | \sa QDate::currentDate(), QTime::currentTime() | 2352 | \sa QDate::currentDate(), QTime::currentTime() |
2307 | */ | 2353 | */ |
2308 | 2354 | ||
2309 | QDateTime QDateTime::currentDateTime() | 2355 | QDateTime QDateTime::currentDateTime() |