summaryrefslogtreecommitdiff
path: root/qmake/tools/qdatetime.cpp
Unidiff
Diffstat (limited to 'qmake/tools/qdatetime.cpp') (more/less context) (show whitespace changes)
-rw-r--r--qmake/tools/qdatetime.cpp62
1 files changed, 54 insertions, 8 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,20 +1,20 @@
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**
@@ -26,25 +26,24 @@
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)
@@ -886,24 +885,30 @@ QDate QDate::addMonths( int nmonths ) const
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
@@ -981,31 +986,43 @@ QDate QDate::currentDate()
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
@@ -1644,33 +1661,44 @@ bool QTime::currentTime( QTime *ct, Qt::TimeSpec ts )
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 );
@@ -1697,27 +1725,27 @@ bool QTime::currentTime( QTime *ct, Qt::TimeSpec ts )
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
@@ -1951,34 +1979,52 @@ void QDateTime::setTime_t( uint secsSince1Jan1970UTC )
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