summaryrefslogtreecommitdiff
path: root/qmake/tools/qdatetime.cpp
Unidiff
Diffstat (limited to 'qmake/tools/qdatetime.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--qmake/tools/qdatetime.cpp70
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
@@ -1,62 +1,61 @@
1/**************************************************************************** 1/****************************************************************************
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.
15** 15**
16** This file may be distributed and/or modified under the terms of the 16** This file may be distributed and/or modified under the terms of the
17** GNU General Public License version 2 as published by the Free Software 17** GNU General Public License version 2 as published by the Free Software
18** Foundation and appearing in the file LICENSE.GPL included in the 18** Foundation and appearing in the file LICENSE.GPL included in the
19** packaging of this file. 19** packaging of this file.
20** 20**
21** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition 21** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22** licenses may use this file in accordance with the Qt Commercial License 22** licenses may use this file in accordance with the Qt Commercial License
23** Agreement provided with the Software. 23** Agreement provided with the Software.
24** 24**
25** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 25** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 26** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27** 27**
28** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for 28** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29** information about Qt Commercial License Agreements. 29** information about Qt Commercial License Agreements.
30** See http://www.trolltech.com/qpl/ for QPL licensing information. 30** See http://www.trolltech.com/qpl/ for QPL licensing information.
31** See http://www.trolltech.com/gpl/ for GPL licensing information. 31** See http://www.trolltech.com/gpl/ for GPL licensing information.
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
45#include <stdio.h> 44#include <stdio.h>
46#ifndef Q_OS_TEMP 45#ifndef Q_OS_TEMP
47#include <time.h> 46#include <time.h>
48#endif 47#endif
49 48
50#if defined(Q_OS_WIN32) 49#if defined(Q_OS_WIN32)
51#include <windows.h> 50#include <windows.h>
52#endif 51#endif
53 52
54 static const uint FIRST_DAY = 2361222;// Julian day for 1752-09-14 53 static const uint FIRST_DAY = 2361222;// Julian day for 1752-09-14
55 static const int FIRST_YEAR = 1752; // ### wrong for many countries 54 static const int FIRST_YEAR = 1752; // ### wrong for many countries
56 static const uint SECS_PER_DAY= 86400; 55 static const uint SECS_PER_DAY= 86400;
57static const uint MSECS_PER_DAY = 86400000; 56static const uint MSECS_PER_DAY = 86400000;
58static const uint SECS_PER_HOUR = 3600; 57static const uint SECS_PER_HOUR = 3600;
59static const uint MSECS_PER_HOUR= 3600000; 58static const uint MSECS_PER_HOUR= 3600000;
60 static const uint SECS_PER_MIN= 60; 59 static const uint SECS_PER_MIN= 60;
61static const uint MSECS_PER_MIN = 60000; 60static const uint MSECS_PER_MIN = 60000;
62 61
@@ -874,48 +873,54 @@ QDate QDate::addMonths( int nmonths ) const
874 } 873 }
875 874
876 QDate tmp(y,m,1); 875 QDate tmp(y,m,1);
877 876
878 if( d > tmp.daysInMonth() ) 877 if( d > tmp.daysInMonth() )
879 d = tmp.daysInMonth(); 878 d = tmp.daysInMonth();
880 879
881 QDate date(y, m, d); 880 QDate date(y, m, d);
882 return date; 881 return date;
883 882
884} 883}
885 884
886/*! 885/*!
887 Returns a QDate object containing a date \a nyears later than the 886 Returns a QDate object containing a date \a nyears later than the
888 date of this object (or earlier if \a nyears is negative). 887 date of this object (or earlier if \a nyears is negative).
889 888
890 \sa addDays(), addMonths() 889 \sa addDays(), addMonths()
891*/ 890*/
892 891
893QDate QDate::addYears( int nyears ) const 892QDate 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
904/*! 909/*!
905 Returns the number of days from this date to \a d (which is 910 Returns the number of days from this date to \a d (which is
906 negative if \a d is earlier than this date). 911 negative if \a d is earlier than this date).
907 912
908 Example: 913 Example:
909 \code 914 \code
910 QDate d1( 1995, 5, 17 ); // May 17th 1995 915 QDate d1( 1995, 5, 17 ); // May 17th 1995
911 QDate d2( 1995, 5, 20 ); // May 20th 1995 916 QDate d2( 1995, 5, 20 ); // May 20th 1995
912 d1.daysTo( d2 ); // returns 3 917 d1.daysTo( d2 ); // returns 3
913 d2.daysTo( d1 ); // returns -3 918 d2.daysTo( d1 ); // returns -3
914 \endcode 919 \endcode
915 920
916 \sa addDays() 921 \sa addDays()
917*/ 922*/
918 923
919int QDate::daysTo( const QDate &d ) const 924int QDate::daysTo( const QDate &d ) const
920{ 925{
921 return d.jd - jd; 926 return d.jd - jd;
@@ -969,55 +974,67 @@ int QDate::daysTo( const QDate &d ) const
969 974
970QDate QDate::currentDate() 975QDate QDate::currentDate()
971{ 976{
972 return currentDate( Qt::LocalTime ); 977 return currentDate( Qt::LocalTime );
973} 978}
974 979
975/*! 980/*!
976 Returns the current date, as reported by the system clock, for the 981 Returns the current date, as reported by the system clock, for the
977 TimeSpec \a ts. The default TimeSpec is LocalTime. 982 TimeSpec \a ts. The default TimeSpec is LocalTime.
978 983
979 \sa QTime::currentTime(), QDateTime::currentDateTime(), Qt::TimeSpec 984 \sa QTime::currentTime(), QDateTime::currentDateTime(), Qt::TimeSpec
980*/ 985*/
981QDate QDate::currentDate( Qt::TimeSpec ts ) 986QDate QDate::currentDate( Qt::TimeSpec ts )
982{ 987{
983 QDate d; 988 QDate d;
984#if defined(Q_OS_WIN32) 989#if defined(Q_OS_WIN32)
985 SYSTEMTIME t; 990 SYSTEMTIME t;
986 memset( &t, 0, sizeof(SYSTEMTIME) ); 991 memset( &t, 0, sizeof(SYSTEMTIME) );
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( &ltime ); 1000 time( &ltime );
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( &ltime, &res );
1008 else
1009 t = gmtime_r( &ltime, &res );
1010# else
996 if ( ts == Qt::LocalTime ) 1011 if ( ts == Qt::LocalTime )
997 t = localtime( &ltime ); 1012 t = localtime( &ltime );
998 else 1013 else
999 t = gmtime( &ltime ); 1014 t = gmtime( &ltime );
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
1006/*! 1023/*!
1007 Returns the QDate represented by the string \a s, using the format 1024 Returns the QDate represented by the string \a s, using the format
1008 \a f, or an invalid date if the string cannot be parsed. 1025 \a f, or an invalid date if the string cannot be parsed.
1009 1026
1010 Note for \c Qt::TextDate: It is recommended that you use the 1027 Note for \c Qt::TextDate: It is recommended that you use the
1011 English short month names (e.g. "Jan"). Although localized month 1028 English short month names (e.g. "Jan"). Although localized month
1012 names can also be used, they depend on the user's locale settings. 1029 names can also be used, they depend on the user's locale settings.
1013 1030
1014 \warning \c Qt::LocalDate cannot be used here. 1031 \warning \c Qt::LocalDate cannot be used here.
1015*/ 1032*/
1016QDate QDate::fromString( const QString& s, Qt::DateFormat f ) 1033QDate QDate::fromString( const QString& s, Qt::DateFormat f )
1017{ 1034{
1018 if ( ( s.isEmpty() ) || ( f == Qt::LocalDate ) ) { 1035 if ( ( s.isEmpty() ) || ( f == Qt::LocalDate ) ) {
1019#if defined(QT_CHECK_RANGE) 1036#if defined(QT_CHECK_RANGE)
1020 qWarning( "QDate::fromString: Parameter out of range." ); 1037 qWarning( "QDate::fromString: Parameter out of range." );
1021#endif 1038#endif
1022 return QDate(); 1039 return QDate();
1023 } 1040 }
@@ -1534,49 +1551,49 @@ int QTime::msecsTo( const QTime &t ) const
1534*/ 1551*/
1535 1552
1536/*! 1553/*!
1537 \fn bool QTime::operator<=( const QTime &t ) const 1554 \fn bool QTime::operator<=( const QTime &t ) const
1538 1555
1539 Returns TRUE if this time is earlier than or equal to \a t; 1556 Returns TRUE if this time is earlier than or equal to \a t;
1540 otherwise returns FALSE. 1557 otherwise returns FALSE.
1541*/ 1558*/
1542 1559
1543/*! 1560/*!
1544 \fn bool QTime::operator>( const QTime &t ) const 1561 \fn bool QTime::operator>( const QTime &t ) const
1545 1562
1546 Returns TRUE if this time is later than \a t; otherwise returns FALSE. 1563 Returns TRUE if this time is later than \a t; otherwise returns FALSE.
1547*/ 1564*/
1548 1565
1549/*! 1566/*!
1550 \fn bool QTime::operator>=( const QTime &t ) const 1567 \fn bool QTime::operator>=( const QTime &t ) const
1551 1568
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.
1565*/ 1582*/
1566 1583
1567QTime QTime::currentTime() 1584QTime QTime::currentTime()
1568{ 1585{
1569 return currentTime( Qt::LocalTime ); 1586 return currentTime( Qt::LocalTime );
1570} 1587}
1571 1588
1572/*! 1589/*!
1573 Returns the current time as reported by the system clock, for the 1590 Returns the current time as reported by the system clock, for the
1574 TimeSpec \a ts. The default TimeSpec is LocalTime. 1591 TimeSpec \a ts. The default TimeSpec is LocalTime.
1575 1592
1576 Note that the accuracy depends on the accuracy of the underlying 1593 Note that the accuracy depends on the accuracy of the underlying
1577 operating system; not all systems provide 1-millisecond accuracy. 1594 operating system; not all systems provide 1-millisecond accuracy.
1578 1595
1579 \sa Qt::TimeSpec 1596 \sa Qt::TimeSpec
1580*/ 1597*/
1581QTime QTime::currentTime( Qt::TimeSpec ts ) 1598QTime QTime::currentTime( Qt::TimeSpec ts )
1582{ 1599{
@@ -1632,104 +1649,115 @@ bool QTime::currentTime( QTime *ct )
1632 return value is used by QDateTime::currentDateTime() to ensure that 1649 return value is used by QDateTime::currentDateTime() to ensure that
1633 the date there is correct. The default TimeSpec is LocalTime. 1650 the date there is correct. The default TimeSpec is LocalTime.
1634 1651
1635 \sa Qt::TimeSpec 1652 \sa Qt::TimeSpec
1636*/ 1653*/
1637bool QTime::currentTime( QTime *ct, Qt::TimeSpec ts ) 1654bool QTime::currentTime( QTime *ct, Qt::TimeSpec ts )
1638{ 1655{
1639 if ( !ct ) { 1656 if ( !ct ) {
1640#if defined(QT_CHECK_NULL) 1657#if defined(QT_CHECK_NULL)
1641 qWarning( "QTime::currentTime(QTime *): Null pointer not allowed" ); 1658 qWarning( "QTime::currentTime(QTime *): Null pointer not allowed" );
1642#endif 1659#endif
1643 return FALSE; 1660 return FALSE;
1644 } 1661 }
1645 1662
1646#if defined(Q_OS_WIN32) 1663#if defined(Q_OS_WIN32)
1647 SYSTEMTIME t; 1664 SYSTEMTIME t;
1648 if ( ts == Qt::LocalTime ) { 1665 if ( ts == Qt::LocalTime ) {
1649 GetLocalTime( &t ); 1666 GetLocalTime( &t );
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( &ltime, &res );
1684 else
1685 t = gmtime_r( &ltime, &res );
1686# else
1687 if ( ts == Qt::LocalTime )
1661 t = localtime( &ltime ); 1688 t = localtime( &ltime );
1662 } else { 1689 else
1663 t = gmtime( &ltime ); 1690 t = gmtime( &ltime );
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( &ltime ); 1697 ::time( &ltime );
1670 tm *t; 1698 tm *t;
1671 if ( ts == Qt::LocalTime ) 1699 if ( ts == Qt::LocalTime )
1672 localtime( &ltime ); 1700 localtime( &ltime );
1673 else 1701 else
1674 gmtime( &ltime ); 1702 gmtime( &ltime );
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
1678 // 00:00.00 to 00:00.59.999 is considered as "midnight or right after" 1706 // 00:00.00 to 00:00.59.999 is considered as "midnight or right after"
1679 return ct->ds < (uint) MSECS_PER_MIN; 1707 return ct->ds < (uint) MSECS_PER_MIN;
1680} 1708}
1681 1709
1682/*! 1710/*!
1683 \overload 1711 \overload
1684 1712
1685 Returns TRUE if the specified time is valid; otherwise returns 1713 Returns TRUE if the specified time is valid; otherwise returns
1686 FALSE. 1714 FALSE.
1687 1715
1688 The time is valid if \a h is in the range 0..23, \a m and \a s are 1716 The time is valid if \a h is in the range 0..23, \a m and \a s are
1689 in the range 0..59, and \a ms is in the range 0..999. 1717 in the range 0..59, and \a ms is in the range 0..999.
1690 1718
1691 Example: 1719 Example:
1692 \code 1720 \code
1693 QTime::isValid(21, 10, 30); // returns TRUE 1721 QTime::isValid(21, 10, 30); // returns TRUE
1694 QTime::isValid(22, 5, 62); // returns FALSE 1722 QTime::isValid(22, 5, 62); // returns FALSE
1695 \endcode 1723 \endcode
1696*/ 1724*/
1697 1725
1698bool QTime::isValid( int h, int m, int s, int ms ) 1726bool QTime::isValid( int h, int m, int s, int ms )
1699{ 1727{
1700 return (uint)h < 24 && (uint)m < 60 && (uint)s < 60 && (uint)ms < 1000; 1728 return (uint)h < 24 && (uint)m < 60 && (uint)s < 60 && (uint)ms < 1000;
1701} 1729}
1702 1730
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
1717void QTime::start() 1745void QTime::start()
1718{ 1746{
1719 *this = currentTime(); 1747 *this = currentTime();
1720} 1748}
1721 1749
1722/*! 1750/*!
1723 Sets this time to the current time and returns the number of 1751 Sets this time to the current time and returns the number of
1724 milliseconds that have elapsed since the last time start() or 1752 milliseconds that have elapsed since the last time start() or
1725 restart() was called. 1753 restart() was called.
1726 1754
1727 This function is guaranteed to be atomic and is thus very handy 1755 This function is guaranteed to be atomic and is thus very handy
1728 for repeated measurements. Call start() to start the first 1756 for repeated measurements. Call start() to start the first
1729 measurement and then restart() for each later measurement. 1757 measurement and then restart() for each later measurement.
1730 1758
1731 Note that the counter wraps to zero 24 hours after the last call 1759 Note that the counter wraps to zero 24 hours after the last call
1732 to start() or restart(). 1760 to start() or restart().
1733 1761
1734 \warning If the system's clock setting has been changed since the 1762 \warning If the system's clock setting has been changed since the
1735 last time start() or restart() was called, the result is 1763 last time start() or restart() was called, the result is
@@ -1939,58 +1967,76 @@ uint QDateTime::toTime_t() const
1939 based on the given UTC time. 1967 based on the given UTC time.
1940*/ 1968*/
1941 1969
1942void QDateTime::setTime_t( uint secsSince1Jan1970UTC ) 1970void QDateTime::setTime_t( uint secsSince1Jan1970UTC )
1943{ 1971{
1944 setTime_t( secsSince1Jan1970UTC, Qt::LocalTime ); 1972 setTime_t( secsSince1Jan1970UTC, Qt::LocalTime );
1945} 1973}
1946 1974
1947/*! 1975/*!
1948 Sets the date and time to \a ts time (\c Qt::LocalTime or \c 1976 Sets the date and time to \a ts time (\c Qt::LocalTime or \c
1949 Qt::UTC) given the number of seconds that have passed since 1977 Qt::UTC) given the number of seconds that have passed since
1950 1970-01-01T00:00:00, Coordinated Universal Time (UTC). On systems 1978 1970-01-01T00:00:00, Coordinated Universal Time (UTC). On systems
1951 that do not support timezones this function will behave as if 1979 that do not support timezones this function will behave as if
1952 local time were UTC. 1980 local time were UTC.
1953 1981
1954 On Windows, only a subset of \a secsSince1Jan1970UTC values are 1982 On Windows, only a subset of \a secsSince1Jan1970UTC values are
1955 supported, as Windows starts counting from 1980. 1983 supported, as Windows starts counting from 1980.
1956 1984
1957 \sa toTime_t() 1985 \sa toTime_t()
1958*/ 1986*/
1959void QDateTime::setTime_t( uint secsSince1Jan1970UTC, Qt::TimeSpec ts ) 1987void 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;
1979} 2025}
1980#ifndef QT_NO_DATESTRING 2026#ifndef QT_NO_DATESTRING
1981#ifndef QT_NO_SPRINTF 2027#ifndef QT_NO_SPRINTF
1982/*! 2028/*!
1983 \overload 2029 \overload
1984 2030
1985 Returns the datetime as a string. The \a f parameter determines 2031 Returns the datetime as a string. The \a f parameter determines
1986 the format of the string. 2032 the format of the string.
1987 2033
1988 If \a f is \c Qt::TextDate, the string format is "Wed May 20 2034 If \a f is \c Qt::TextDate, the string format is "Wed May 20
1989 03:40:13 1998" (using QDate::shortDayName(), QDate::shortMonthName(), 2035 03:40:13 1998" (using QDate::shortDayName(), QDate::shortMonthName(),
1990 and QTime::toString() to generate the string, so the day and month 2036 and QTime::toString() to generate the string, so the day and month
1991 names will have localized names). 2037 names will have localized names).
1992 2038
1993 If \a f is \c Qt::ISODate, the string format corresponds to the 2039 If \a f is \c Qt::ISODate, the string format corresponds to the
1994 ISO 8601 extended specification for representations of dates and 2040 ISO 8601 extended specification for representations of dates and
1995 times, which is YYYY-MM-DDTHH:MM:SS. 2041 times, which is YYYY-MM-DDTHH:MM:SS.
1996 2042
@@ -2279,49 +2325,49 @@ bool QDateTime::operator<=( const QDateTime &dt ) const
2279 returns FALSE. 2325 returns FALSE.
2280*/ 2326*/
2281 2327
2282bool QDateTime::operator>( const QDateTime &dt ) const 2328bool QDateTime::operator>( const QDateTime &dt ) const
2283{ 2329{
2284 if ( d > dt.d ) 2330 if ( d > dt.d )
2285 return TRUE; 2331 return TRUE;
2286 return d == dt.d ? t > dt.t : FALSE; 2332 return d == dt.d ? t > dt.t : FALSE;
2287} 2333}
2288 2334
2289/*! 2335/*!
2290 Returns TRUE if this datetime is later than or equal to \a dt; 2336 Returns TRUE if this datetime is later than or equal to \a dt;
2291 otherwise returns FALSE. 2337 otherwise returns FALSE.
2292*/ 2338*/
2293 2339
2294bool QDateTime::operator>=( const QDateTime &dt ) const 2340bool QDateTime::operator>=( const QDateTime &dt ) const
2295{ 2341{
2296 if ( d > dt.d ) 2342 if ( d > dt.d )
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
2309QDateTime QDateTime::currentDateTime() 2355QDateTime QDateTime::currentDateTime()
2310{ 2356{
2311 return currentDateTime( Qt::LocalTime ); 2357 return currentDateTime( Qt::LocalTime );
2312} 2358}
2313 2359
2314/*! 2360/*!
2315 Returns the current datetime, as reported by the system clock, for the 2361 Returns the current datetime, as reported by the system clock, for the
2316 TimeSpec \a ts. The default TimeSpec is LocalTime. 2362 TimeSpec \a ts. The default TimeSpec is LocalTime.
2317 2363
2318 \sa QDate::currentDate(), QTime::currentTime(), Qt::TimeSpec 2364 \sa QDate::currentDate(), QTime::currentTime(), Qt::TimeSpec
2319*/ 2365*/
2320 2366
2321QDateTime QDateTime::currentDateTime( Qt::TimeSpec ts ) 2367QDateTime QDateTime::currentDateTime( Qt::TimeSpec ts )
2322{ 2368{
2323 QDateTime dt; 2369 QDateTime dt;
2324 QTime t; 2370 QTime t;
2325 dt.setDate( QDate::currentDate(ts) ); 2371 dt.setDate( QDate::currentDate(ts) );
2326 if ( QTime::currentTime(&t, ts) ) // midnight or right after? 2372 if ( QTime::currentTime(&t, ts) ) // midnight or right after?
2327 dt.setDate( QDate::currentDate(ts) ); // fetch date again 2373 dt.setDate( QDate::currentDate(ts) ); // fetch date again