1 /****************************************************************************
4 ** Implementation of date and time classes
8 ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
10 ** This file is part of the tools module of the Qt GUI Toolkit.
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
14 ** LICENSE.QPL included in the packaging of this file.
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
18 ** Foundation and appearing in the file LICENSE.GPL included in the
19 ** packaging of this file.
21 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22 ** licenses may use this file in accordance with the Qt Commercial License
23 ** Agreement provided with the Software.
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.
28 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29 ** information about Qt Commercial License Agreements.
30 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
31 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
33 ** Contact info@trolltech.com if any conditions of this licensing are
36 **********************************************************************/
38 #define gettimeofday __hide_gettimeofday
39 #include "qdatetime.h"
40 #include "qdatastream.h"
43 #if defined(_OS_WIN32_)
44 #if defined(_CC_BOOL_DEF_)
51 #elif defined(_OS_MSDOS_)
53 #elif defined(_OS_OS2_)
55 #elif defined(_OS_UNIX_)
59 extern "C" int gettimeofday( struct timeval *, struct timezone * );
62 static const uint FIRST_DAY = 2361222; // Julian day for 1752/09/14
63 static const int FIRST_YEAR = 1752; // ### wrong for many countries
64 static const uint SECS_PER_DAY = 86400;
65 static const uint MSECS_PER_DAY = 86400000;
66 static const uint SECS_PER_HOUR = 3600;
67 static const uint MSECS_PER_HOUR= 3600000;
68 static const uint SECS_PER_MIN = 60;
69 static const uint MSECS_PER_MIN = 60000;
71 static const short monthDays[] ={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
75 const char * const QDate::monthNames[] = {
76 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
77 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
79 const char * const QDate::weekdayNames[] ={
80 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
83 /*****************************************************************************
84 QDate member functions
85 *****************************************************************************/
90 \class QDate qdatetime.h
91 \brief The QDate class provides date functions.
95 A QDate object contains a calendar date, i.e. year, month, and day
96 numbers in the modern western (Gregorian) calendar. It can read the
97 current date from the system clock. It provides functions for
98 comparing dates and for manipulating a date by adding a number of
101 A QDate object is typically created either by giving the year, month
102 and day numbers explicitly, or by using the static function
103 currentDate(), which makes a QDate object which contains the
104 system's clock date. An explicit date can also be set using
107 The year(), month(), and day() functions provide access to the year,
108 month, and day numbers. Also, dayOfWeek() and dayOfYear() functions
109 are provided. The same information is provided in textual format by
110 the toString(), dayName(), and monthName() functions.
112 QDate provides a full set of operators to compare two QDate
113 objects. A date is considered smaller than another if it is earlier
116 The date a given number of days later than a given date can be found
117 using the addDays() function. Correspondingly, the number of days
118 between two dates can be found using the daysTo() function.
120 The daysInMonth() and daysInYear() functions tell how many days
121 there are in this date's month and year, respectively. The
122 isLeapYear() function tells whether this date is in a leap year.
124 Note that QDate may not be used for date calculations for dates in
125 the remote past, i.e. prior to the introduction of the Gregorian
126 calendar. This calendar was adopted by England Sep. 14. 1752 (hence
127 this is the earliest valid QDate), and subsequently by most other
128 western countries, until 1923.
130 The end of time is reached around 8000AD, by which time we expect Qt
139 Constructs a null date. Null dates are invalid.
141 \sa isNull(), isValid()
146 Constructs a date with the year \a y, month \a m and day \a d.
148 \a y must be in the range 1752-ca. 8000, \a m must be in the range
149 1-12, and \a d must be in the range 1-31. Exception: if \a y is in
150 the range 0-99, it is interpreted as 1900-1999.
155 QDate::QDate( int y, int m, int d )
163 \fn bool QDate::isNull() const
165 Returns TRUE if the date is null. A null date is invalid.
172 Returns TRUE if this date is valid.
177 bool QDate::isValid() const
179 return jd >= FIRST_DAY;
184 Returns the year (>= 1752) of this date.
189 int QDate::year() const
192 jul2greg( jd, y, m, d );
197 Returns the month (January=1 .. December=12) of this date.
202 int QDate::month() const
205 jul2greg( jd, y, m, d );
210 Returns the day of the month (1..31) of this date.
212 \sa year(), month(), dayOfWeek()
215 int QDate::day() const
218 jul2greg( jd, y, m, d );
223 Returns the weekday (Monday=1 .. Sunday=7) for this date.
225 \sa day(), dayOfYear()
228 int QDate::dayOfWeek() const
230 return (((jd+1) % 7) + 6)%7 + 1;
234 Returns the day of the year (1..365) for this date.
236 \sa day(), dayOfWeek()
239 int QDate::dayOfYear() const
241 return jd - greg2jul(year(), 1, 1) + 1;
245 Returns the number of days in the month (28..31) for this date.
247 \sa day(), daysInYear()
250 int QDate::daysInMonth() const
253 jul2greg( jd, y, m, d );
254 if ( m == 2 && leapYear(y) )
261 Returns the number of days in the year (365 or 366) for this date.
263 \sa day(), daysInMonth()
266 int QDate::daysInYear() const
269 jul2greg( jd, y, m, d );
270 return leapYear(y) ? 366 : 365;
275 Returns the name of the \a month.
277 Month 1 == "Jan", month 2 == "Feb" etc.
279 \sa toString(), dayName()
282 QString QDate::monthName( int month ) const
284 #if defined(CHECK_RANGE)
285 if ( month < 1 || month > 12 ) {
286 qWarning( "QDate::monthName: Parameter out ouf range." );
290 // ### Remove the fromLatin1 during localization
291 return QString::fromLatin1(monthNames[month-1]);
295 Returns the name of the \a weekday.
297 Weekday 1 == "Mon", day 2 == "Tue" etc.
299 \sa toString(), monthName()
302 QString QDate::dayName( int weekday ) const
304 #if defined(CHECK_RANGE)
305 if ( weekday < 1 || weekday > 7 ) {
306 qWarning( "QDate::dayName: Parameter out of range." );
310 // ### Remove the fromLatin1 during localization
311 return QString::fromLatin1(weekdayNames[weekday-1]);
316 Returns the date as a string.
318 The string format is "Sat May 20 1995". This function uses the
319 dayName() and monthName() functions to generate the string.
321 \sa dayName(), monthName()
324 QString QDate::toString() const
327 jul2greg( jd, y, m, d );
328 QString buf = dayName(dayOfWeek());
332 t.sprintf( " %d %d", d, y);
339 Sets the year \a y, month \a m and day \a d.
341 \a y must be in the range 1752-ca. 8000, \a m must be in the range
342 1-12, and \a d must be in the range 1-31. Exception: if \a y is in
343 the range 0-99, it is interpreted as 1900-1999.
345 Returns TRUE if the date is valid, otherwise FALSE.
348 bool QDate::setYMD( int y, int m, int d )
350 if ( !isValid(y,m,d) ) {
351 #if defined(CHECK_RANGE)
352 qWarning( "QDate::setYMD: Invalid date %04d/%02d/%02d", y, m, d );
356 jd = greg2jul( y, m, d );
358 ASSERT( year() == (y > 99 ? y : 1900+y) && month() == m && day() == d );
364 Returns a QDate object containing a date \a ndays later than the
365 date of this object (or earlier if \a ndays is negative).
370 QDate QDate::addDays( int ndays ) const
378 Returns the number of days from this date to \a d (which is negative
379 if \a d is earlier than this date).
383 QDate d1( 1995, 5, 17 ); // May 17th 1995
384 QDate d2( 1995, 5, 20 ); // May 20th 1995
385 d1.daysTo( d2 ); // returns 3
386 d2.daysTo( d1 ); // returns -3
392 int QDate::daysTo( const QDate &d ) const
399 \fn bool QDate::operator==( const QDate &d ) const
400 Returns TRUE if this date is equal to \a d, or FALSE if
405 \fn bool QDate::operator!=( const QDate &d ) const
406 Returns TRUE if this date is different from \a d, or FALSE if
411 \fn bool QDate::operator<( const QDate &d ) const
412 Returns TRUE if this date is earlier than \a d, otherwise FALSE.
416 \fn bool QDate::operator<=( const QDate &d ) const
417 Returns TRUE if this date is earlier than or equal to \a d, otherwise FALSE.
421 \fn bool QDate::operator>( const QDate &d ) const
422 Returns TRUE if this date is later than \a d, otherwise FALSE.
426 \fn bool QDate::operator>=( const QDate &d ) const
427 Returns TRUE if this date is later than or equal to \a d, otherwise FALSE.
432 Returns the current date, as reported by the system clock.
434 \sa QTime::currentTime(), QDateTime::currentDateTime()
437 QDate QDate::currentDate()
439 #if defined(_OS_WIN32_)
444 d.jd = greg2jul( t.wYear, t.wMonth, t.wDay );
451 tm *t = localtime( <ime );
453 d.jd = greg2jul( t->tm_year + 1900, t->tm_mon + 1, t->tm_mday );
460 Returns TRUE if the specified date (year \a y, month \a m and day \a
465 QDate::isValid( 2002, 5, 17 ); // TRUE; May 17th 2002 is OK.
466 QDate::isValid( 2002, 2, 30 ); // FALSE; Feb 30th does not exist
467 QDate::isValid( 2004, 2, 29 ); // TRUE; 2004 is a leap year
468 QDate::isValid( 1202, 6, 6 ); // FALSE; 1202 is pre-Gregorian
471 Note that a \a y value in the range 00-99 is interpreted as
474 \sa isNull(), setYMD()
477 bool QDate::isValid( int y, int m, int d )
479 if ( y >= 0 && y <= 99 )
481 else if ( y < FIRST_YEAR || (y == FIRST_YEAR && (m < 9 ||
482 (m == 9 && d < 14))) )
484 return (d > 0 && m > 0 && m <= 12) &&
485 (d <= monthDays[m] || (d == 29 && m == 2 && leapYear(y)));
489 Returns TRUE if the specified year \a y is a leap year.
492 bool QDate::leapYear( int y )
494 return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
499 Converts a Gregorian date to a Julian day.
500 This algorithm is taken from Communications of the ACM, Vol 6, No 8.
504 uint QDate::greg2jul( int y, int m, int d )
515 c = y; // NOTE: Sym C++ 6.0 bug
518 return 1721119 + d + (146097*c)/4 + (1461*ya)/4 + (153*m+2)/5;
523 Converts a Julian day to a Gregorian date.
524 This algorithm is taken from Communications of the ACM, Vol 6, No 8.
528 void QDate::jul2greg( uint jd, int &y, int &m, int &d )
531 uint j = jd - 1721119;
532 y = (j*4 - 1)/146097;
533 j = j*4 - 146097*y - 1;
535 j = (x*4 + 3) / 1461;
537 x = (x*4) + 3 - 1461*j;
551 /*****************************************************************************
552 QTime member functions
553 *****************************************************************************/
556 \class QTime qdatetime.h
558 \brief The QTime class provides clock time functions.
562 A QTime object contains a clock time, i.e. a number of hours,
563 minutes, seconds and milliseconds since midnight. It can read the
564 current time from the system clock, and measure a span of elapsed
565 time. It provides functions for comparing times and for manipulating
566 a time by adding a number of (milli)seconds.
568 QTime operates with 24-hour clock format; it has no concept of
569 AM/PM. It operates with local time; it does not know anything about
570 time zones or daylight savings time.
572 A QTime object is typically created either by giving the number of
573 hours, minutes, seconds, and milliseconds explicitly, or by using
574 the static function currentTime(), which makes a QTime object which
575 contains the system's clock time. Note that the accuracy depends on
576 the accuracy of the underlying operating system; not all systems
577 provide 1-millisecond accuracy.
579 The hour(), minute(), second(), and msec() functions provide access
580 to the number of hours, minutes, seconds, and milliseconds of the
581 time. The same information is provided in textual format by the
584 QTime provides a full set of operators to compare two QTime
585 objects. A time is considered smaller than another if it is earlier
588 The time a given number of seconds or milliseconds later than a
589 given time can be found using the addSecs() or addMSecs()
590 functions. Correspondingly, the number of (milli)seconds between two
591 times can be found using the secsTo() or msecsTo() functions.
593 QTime can be used to measure a span of elapsed time using the
594 start(), restart(), and elapsed() functions.
602 Constructs the time 0 hours, minutes, seconds and milliseconds,
603 i.e. 00:00:00.000 (midnight). This is a valid time.
609 Constructs a time with hour \a h, minute \a m, seconds \a s and
612 \a h must be in the range 0-23, \a m and \a s must be in the range
613 0-59, and \a ms must be in the range 0-999.
618 QTime::QTime( int h, int m, int s, int ms )
620 setHMS( h, m, s, ms );
625 \fn bool QTime::isNull() const
626 Returns TRUE if the time is equal to 00:00:00.000. A null time is valid.
632 Returns TRUE if the time is valid, or FALSE if the time is invalid.
633 The time 23:30:55.746 is valid, while 24:12:30 is invalid.
638 bool QTime::isValid() const
640 return ds < MSECS_PER_DAY;
645 Returns the hour part (0..23) of the time.
648 int QTime::hour() const
650 return ds / MSECS_PER_HOUR;
654 Returns the minute part (0..59) of the time.
657 int QTime::minute() const
659 return (ds % MSECS_PER_HOUR)/MSECS_PER_MIN;
663 Returns the second part (0..59) of the time.
666 int QTime::second() const
668 return (ds / 1000)%SECS_PER_MIN;
672 Returns the millisecond part (0..999) of the time.
675 int QTime::msec() const
682 Returns the time of this object in a textual format. Milliseconds
683 are not included. The string format is HH:MM:SS, e.g. 1 second
684 before midnight would be "23:59:59".
687 QString QTime::toString() const
690 buf.sprintf( "%.2d:%.2d:%.2d", hour(), minute(), second() );
696 Sets the time to hour \a h, minute \a m, seconds \a s and
699 \a h must be in the range 0-23, \a m and \a s must be in the range
700 0-59, and \a ms must be in the range 0-999. Returns TRUE if the set
701 time is valid, otherwise FALSE.
706 bool QTime::setHMS( int h, int m, int s, int ms )
708 if ( !isValid(h,m,s,ms) ) {
709 #if defined(CHECK_RANGE)
710 qWarning( "QTime::setHMS Invalid time %02d:%02d:%02d.%03d", h, m, s,
713 ds = MSECS_PER_DAY; // make this invalid
716 ds = (h*SECS_PER_HOUR + m*SECS_PER_MIN + s)*1000 + ms;
721 Returns a QTime object containing a time \a nsecs seconds later than
722 the time of this object (or earlier if \a ms is negative).
724 Note that the time will wrap if it passes midnight.
728 QTime n( 14, 0, 0 ); // n == 14:00:00
730 t = n.addSecs( 70 ); // t == 14:01:10
731 t = n.addSecs( -70 ); // t == 13:58:50
732 t = n.addSecs( 10*60*60 + 5 ); // t == 00:00:05
733 t = n.addSecs( -15*60*60 ); // t == 23:00:00
736 \sa addMSecs(), secsTo(), QDateTime::addSecs()
739 QTime QTime::addSecs( int nsecs ) const
741 return addMSecs(nsecs*1000);
745 Returns the number of seconds from this time to \a t (which is
746 negative if \a t is earlier than this time).
748 Since QTime measures time within a day and there are 86400 seconds
749 in a day, the result is between -86400 and 86400.
751 \sa addSecs() QDateTime::secsTo()
754 int QTime::secsTo( const QTime &t ) const
756 return ((int)t.ds - (int)ds)/1000;
760 Returns a QTime object containing a time \a ms milliseconds later than
761 the time of this object (or earlier if \a ms is negative).
763 Note that the time will wrap if it passes midnight. See addSecs()
766 \sa addSecs(), msecsTo()
769 QTime QTime::addMSecs( int ms ) const
773 // % not well-defined for -ve, but / is.
774 int negdays = (MSECS_PER_DAY-ms) / MSECS_PER_DAY;
775 t.ds = ((int)ds + ms + negdays*MSECS_PER_DAY)
778 t.ds = ((int)ds + ms) % MSECS_PER_DAY;
784 Returns the number of milliseconds from this time to \a t (which is
785 negative if \a t is earlier than this time).
787 Since QTime measures time within a day and there are 86400000
788 milliseconds in a day, the result is between -86400000 and 86400000.
793 int QTime::msecsTo( const QTime &t ) const
795 return (int)t.ds - (int)ds;
800 \fn bool QTime::operator==( const QTime &t ) const
802 Returns TRUE if this time is equal to \a t, or FALSE if they are
807 \fn bool QTime::operator!=( const QTime &t ) const
809 Returns TRUE if this time is different from \a t, or FALSE if they
814 \fn bool QTime::operator<( const QTime &t ) const
816 Returns TRUE if this time is earlier than \a t, otherwise FALSE.
820 \fn bool QTime::operator<=( const QTime &t ) const
822 Returns TRUE if this time is earlier than or equal to \a t,
827 \fn bool QTime::operator>( const QTime &t ) const
829 Returns TRUE if this time is later than \a t, otherwise FALSE.
833 \fn bool QTime::operator>=( const QTime &t ) const
835 Returns TRUE if this time is later than or equal to \a t, otherwise
842 Returns the current time, as reported by the system clock.
844 Note that the accuracy depends on the accuracy of the underlying
845 operating system; not all systems provide 1-millisecond accuracy.
848 QTime QTime::currentTime()
858 Fetches the current time and returns TRUE if the time is within one
859 minute after midnight, otherwise FALSE. The return value is used by
860 QDateTime::currentDateTime() to ensure that the date there is correct.
863 bool QTime::currentTime( QTime *ct )
866 #if defined(CHECK_NULL)
867 qWarning( "QTime::currentTime(QTime *): Null pointer not allowed" );
872 #if defined(_OS_WIN32_)
876 ct->ds = MSECS_PER_HOUR*t.wHour + MSECS_PER_MIN*t.wMinute +
877 1000*t.wSecond + t.wMilliseconds;
878 return (t.wHour == 0 && t.wMinute == 0);
880 #elif defined(_OS_OS2_)
883 DosGetDateTime( &t );
884 ct->ds = MSECS_PER_HOUR*t.hours + MSECS_PER_MIN*t.minutes +
885 1000*t.seconds + 10*t.hundredths;
886 return (t.hours == 0 && t.minutes == 0);
888 #elif defined(_OS_MSDOS_)
892 ct->ds = MSECS_PER_HOUR*t.hour + MSECS_PER_MIN*t.minute +
893 t.second*1000 + t.hsecond*10;
894 return (t.hour== 0 && t.minute == 0);
896 #elif defined(_OS_UNIX_)
899 gettimeofday( &tv, 0 );
900 time_t ltime = tv.tv_sec;
901 tm *t = localtime( <ime );
902 ct->ds = (uint)( MSECS_PER_HOUR*t->tm_hour + MSECS_PER_MIN*t->tm_min +
903 1000*t->tm_sec + tv.tv_usec/1000 );
904 return (t->tm_hour== 0 && t->tm_min == 0);
908 time_t ltime; // no millisecond resolution!!
910 tm *t = localtime( <ime );
911 ct->ds = MSECS_PER_HOUR*t->tm_hour + MSECS_PER_MIN*t->tm_min +
913 return (t->tm_hour== 0 && t->tm_min == 0);
918 Returns TRUE if the specified time is valid, otherwise FALSE.
920 The time is valid if \a h is in the range 0-23, \a m and \a s are in
921 the range 0-59, and \a ms is in the range 0-999.
925 QTime::isValid(21, 10, 30); // returns TRUE
926 QTime::isValid(22, 5, 62); // returns FALSE
930 bool QTime::isValid( int h, int m, int s, int ms )
932 return (uint)h < 24 && (uint)m < 60 && (uint)s < 60 && (uint)ms < 1000;
937 Sets this time to the current time. This is practical for timing:
941 t.start(); // start clock
942 ... // some lengthy task
943 qDebug( "%d\n", t.elapsed() ); // prints # msecs elapsed
946 \sa restart(), elapsed(), currentTime()
951 *this = currentTime();
955 Sets this time to the current time, and returns the number of
956 milliseconds that have elapsed since the last time start() or
957 restart() was called.
959 This function is guaranteed to be atomic, and is thus very handy for
960 repeated measurements: call start() to start the first measurement,
961 then restart() for each later measurement.
963 Note that the counter wraps to zero 24 hours after the last call to
964 start() or restart().
966 \warning If the system's clock setting has been changed since the
967 last time start() or restart() was called, the result is undefined.
968 This can happen e.g. when daylight saving is turned on or off.
970 \sa start(), elapsed(), currentTime()
975 QTime t = currentTime();
976 int n = msecsTo( t );
977 if ( n < 0 ) // passed midnight
984 Returns the number of milliseconds that have elapsed since the last
985 time start() or restart() was called.
987 Note that the counter wraps to zero 24 hours after the last call to
990 Note that the accuracy depends on the accuracy of the underlying
991 operating system; not all systems provide 1-millisecond accuracy.
993 \warning If the system's clock setting has been changed since the
994 last time start() or restart() was called, the result is undefined.
995 This can happen e.g. when daylight saving is turned on or off.
997 \sa start(), restart()
1000 int QTime::elapsed()
1002 int n = msecsTo( currentTime() );
1003 if ( n < 0 ) // passed midnight
1009 /*****************************************************************************
1010 QDateTime member functions
1011 *****************************************************************************/
1014 \class QDateTime qdatetime.h
1015 \brief The QDateTime class provides date and time functions.
1019 A QDateTime object contains a calendar date and a clock time (a
1020 "datetime"). It is a combination of the QDate and QTime classes. It
1021 can read the current datetime from the system clock. It provides
1022 functions for comparing datetimes and for manipulating a datetime by
1023 adding a number of seconds or days.
1025 A QDateTime object is typically created either by giving a date and
1026 time explicitly, or by using the static function currentTime(),
1027 which makes a QDateTime object which contains the system's clock
1030 The date() and time() functions provide access to the date and time
1031 parts of the datetime. The same information is provided in textual
1032 format by the toString() function.
1034 QDateTime provides a full set of operators to compare two QDateTime
1035 objects. A datetime is considered smaller than another if it is
1036 earlier than the other.
1038 The datetime a given number of days or seconds later than a given
1039 datetime can be found using the addDays() and addSecs()
1040 functions. Correspondingly, the number of days or seconds between
1041 two times can be found using the daysTo() or secsTo() functions.
1043 A datetime can also be set using the setTime_t() function, which
1044 takes a POSIX-standard "number of seconds since 00:00:00 on January
1047 The limitations regarding range and resolution mentioned in the
1048 QDate and QTime documentation apply for QDateTime also.
1055 \fn QDateTime::QDateTime()
1057 Constructs a null datetime (i.e. null date and null time). A null
1058 datetime is invalid, since the date is invalid.
1065 Constructs a datetime with date \a date and null time (00:00:00.000).
1068 QDateTime::QDateTime( const QDate &date )
1074 Constructs a datetime with date \a date and time \a time.
1077 QDateTime::QDateTime( const QDate &date, const QTime &time )
1084 \fn bool QDateTime::isNull() const
1086 Returns TRUE if both the date and the time are null. A null date is invalid.
1088 \sa QDate::isNull(), QTime::isNull()
1092 \fn bool QDateTime::isValid() const
1094 Returns TRUE if both the date and the time are valid.
1096 \sa QDate::isValid(), QTime::isValid()
1100 \fn QDate QDateTime::date() const
1102 Returns the date part of this datetime.
1104 \sa setDate(), time()
1108 \fn QTime QDateTime::time() const
1110 Returns the time part of this datetime.
1112 \sa setTime(), date()
1116 \fn void QDateTime::setDate( const QDate &date )
1118 Sets the date part of this datetime.
1120 \sa date(), setTime()
1124 \fn void QDateTime::setTime( const QTime &time )
1126 Sets the time part of this datetime.
1128 \sa time(), setDate()
1133 Sets the local date and time given the number of seconds that have passed
1134 since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC).
1135 On systems that do not support timezones this function will behave as if
1136 local time were UTC.
1138 Note that Microsoft Windows supports only a limited range of values for
1139 \a secsSince1Jan1970UTC.
1142 void QDateTime::setTime_t( uint secsSince1Jan1970UTC )
1144 time_t tmp = (time_t) secsSince1Jan1970UTC;
1145 tm *tM = localtime( &tmp );
1147 tM = gmtime( &tmp );
1149 d.jd = QDate::greg2jul( 1970, 1, 1 );
1154 d.jd = QDate::greg2jul( tM->tm_year + 1900, tM->tm_mon + 1, tM->tm_mday );
1155 t.ds = MSECS_PER_HOUR*tM->tm_hour + MSECS_PER_MIN*tM->tm_min +
1161 Returns the datetime as a string.
1163 The string format is "Sat May 20 03:40:13 1998".
1165 This function uses QDate::dayName(), QDate::monthName(), and
1166 QTime::toString() to generate the string.
1170 QString QDateTime::toString() const
1172 QString buf = d.dayName(d.dayOfWeek());
1174 buf += d.monthName(d.month());
1176 buf += QString().setNum(d.day());
1178 buf += t.toString();
1180 buf += QString().setNum(d.year());
1185 Returns a QDateTime object containing a datetime \a ndays days later
1186 than the datetime of this object (or earlier if \a ndays is
1189 \sa daysTo(), addSecs()
1192 QDateTime QDateTime::addDays( int ndays ) const
1194 return QDateTime( d.addDays(ndays), t );
1198 Returns a QDateTime object containing a datetime \a nsecs seconds
1199 later than the datetime of this object (or earlier if \a nsecs is
1202 \sa secsTo(), addDays()
1205 QDateTime QDateTime::addSecs( int nsecs ) const
1214 if ( nsecs >= (int)SECS_PER_DAY ) {
1215 dd += sign*(nsecs/SECS_PER_DAY);
1216 nsecs %= SECS_PER_DAY;
1218 tt += sign*nsecs*1000;
1220 tt = MSECS_PER_DAY - tt - 1;
1221 dd -= tt / MSECS_PER_DAY;
1222 tt = tt % MSECS_PER_DAY;
1223 tt = MSECS_PER_DAY - tt - 1;
1224 } else if ( tt >= (int)MSECS_PER_DAY ) {
1225 dd += ( tt / MSECS_PER_DAY );
1226 tt = tt % MSECS_PER_DAY;
1235 Returns the number of days from this datetime to \a dt (which is
1236 negative if \a dt is earlier than this datetime).
1238 \sa addDays(), secsTo()
1241 int QDateTime::daysTo( const QDateTime &dt ) const
1243 return d.daysTo( dt.d );
1247 Returns the number of seconds from this datetime to \a dt (which is
1248 negative if \a dt is earlier than this datetime).
1252 QDateTime dt = QDateTime::currentDateTime();
1253 QDateTime x( QDate(dt.year(),12,24), QTime(17,00) );
1254 qDebug( "There are %d seconds to Christmas", dt.secsTo(x) );
1257 \sa addSecs(), daysTo(), QTime::secsTo()
1260 int QDateTime::secsTo( const QDateTime &dt ) const
1262 return t.secsTo(dt.t) + d.daysTo(dt.d)*SECS_PER_DAY;
1267 Returns TRUE if this datetime is equal to \a dt, or FALSE if
1272 bool QDateTime::operator==( const QDateTime &dt ) const
1274 return t == dt.t && d == dt.d;
1278 Returns TRUE if this datetime is different from \a dt, or FALSE if
1283 bool QDateTime::operator!=( const QDateTime &dt ) const
1285 return t != dt.t || d != dt.d;
1289 Returns TRUE if this datetime is earlier than \a dt, otherwise FALSE.
1292 bool QDateTime::operator<( const QDateTime &dt ) const
1296 return d == dt.d ? t < dt.t : FALSE;
1300 Returns TRUE if this datetime is earlier than or equal to \a dt,
1304 bool QDateTime::operator<=( const QDateTime &dt ) const
1308 return d == dt.d ? t <= dt.t : FALSE;
1312 Returns TRUE if this datetime is later than \a dt, otherwise FALSE.
1315 bool QDateTime::operator>( const QDateTime &dt ) const
1319 return d == dt.d ? t > dt.t : FALSE;
1323 Returns TRUE if this datetime is later than or equal to \a dt,
1327 bool QDateTime::operator>=( const QDateTime &dt ) const
1331 return d == dt.d ? t >= dt.t : FALSE;
1335 Returns the current datetime, as reported by the system clock.
1337 \sa QDate::currentDate(), QTime::currentTime()
1340 QDateTime QDateTime::currentDateTime()
1342 QDate cd = QDate::currentDate();
1344 if ( QTime::currentTime(&ct) ) // too close to midnight?
1345 cd = QDate::currentDate(); // YES! time for some midnight
1346 // voodoo, fetch date again
1347 return QDateTime( cd, ct );
1351 /*****************************************************************************
1352 Date/time stream functions
1353 *****************************************************************************/
1355 #ifndef QT_NO_DATASTREAM
1358 Writes the date to the stream.
1360 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
1363 QDataStream &operator<<( QDataStream &s, const QDate &d )
1365 return s << (Q_UINT32)(d.jd);
1370 Reads a date from the stream.
1372 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
1375 QDataStream &operator>>( QDataStream &s, QDate &d )
1385 Writes a time to the stream.
1387 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
1390 QDataStream &operator<<( QDataStream &s, const QTime &t )
1392 return s << (Q_UINT32)(t.ds);
1397 Reads a time from the stream.
1399 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
1402 QDataStream &operator>>( QDataStream &s, QTime &t )
1412 Writes a datetime to the stream.
1414 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
1417 QDataStream &operator<<( QDataStream &s, const QDateTime &dt )
1419 return s << dt.d << dt.t;
1424 Reads a datetime from the stream.
1426 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
1429 QDataStream &operator>>( QDataStream &s, QDateTime &dt )
1434 #endif //QT_NO_DATASTREAM