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.cpp2490
1 files changed, 2490 insertions, 0 deletions
diff --git a/qmake/tools/qdatetime.cpp b/qmake/tools/qdatetime.cpp
new file mode 100644
index 0000000..93e40a8
--- a/dev/null
+++ b/qmake/tools/qdatetime.cpp
@@ -0,0 +1,2490 @@
1/****************************************************************************
2** $Id$
3**
4** Implementation of date and time classes
5**
6** Created : 940124
7**
8** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
9**
10** This file is part of the tools module of the Qt GUI Toolkit.
11**
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.
15**
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.
20**
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.
24**
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.
27**
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.
32**
33** Contact info@trolltech.com if any conditions of this licensing are
34** not clear to you.
35**
36**********************************************************************/
37
38// Get the system specific includes and defines
39#include "qplatformdefs.h"
40
41#include "qdatetime.h"
42#include "qdatastream.h"
43#include "qregexp.h"
44
45#include <stdio.h>
46#ifndef Q_OS_TEMP
47#include <time.h>
48#endif
49
50#if defined(Q_OS_WIN32)
51#include <windows.h>
52#endif
53
54 static const uint FIRST_DAY = 2361222;// Julian day for 1752-09-14
55 static const int FIRST_YEAR = 1752; // ### wrong for many countries
56 static const uint SECS_PER_DAY= 86400;
57static const uint MSECS_PER_DAY = 86400000;
58static const uint SECS_PER_HOUR = 3600;
59static const uint MSECS_PER_HOUR= 3600000;
60 static const uint SECS_PER_MIN= 60;
61static const uint MSECS_PER_MIN = 60000;
62
63static const short monthDays[] = {
64 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
65
66static const char * const qt_shortMonthNames[] = {
67 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
68 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
69
70#ifndef QT_NO_DATESTRING
71/*****************************************************************************
72 Some static function used by QDate, QTime and QDateTime
73 *****************************************************************************/
74
75// Replaces tokens by their value. See QDateTime::toString() for a list of valid tokens
76static QString getFmtString( const QString& f, const QTime* dt = 0, const QDate* dd = 0, bool am_pm = FALSE )
77{
78 if ( f.isEmpty() )
79 return QString::null;
80
81 QString buf = f;
82
83 if ( dt ) {
84 if ( f == "h" ) {
85 if ( ( am_pm ) && ( dt->hour() > 12 ) )
86 buf = QString::number( dt->hour() - 12 );
87 else if ( ( am_pm ) && ( dt->hour() == 0 ) )
88 buf = "12";
89 else
90 buf = QString::number( dt->hour() );
91 } else if ( f == "hh" ) {
92 if ( ( am_pm ) && ( dt->hour() > 12 ) )
93 buf = QString::number( dt->hour() - 12 ).rightJustify( 2, '0', TRUE );
94 else if ( ( am_pm ) && ( dt->hour() == 0 ) )
95 buf = "12";
96 else
97 buf = QString::number( dt->hour() ).rightJustify( 2, '0', TRUE );
98 } else if ( f == "m" ) {
99 buf = QString::number( dt->minute() );
100 } else if ( f == "mm" ) {
101 buf = QString::number( dt->minute() ).rightJustify( 2, '0', TRUE );
102 } else if ( f == "s" ) {
103 buf = QString::number( dt->second() );
104 } else if ( f == "ss" ) {
105 buf = QString::number( dt->second() ).rightJustify( 2, '0', TRUE );
106 } else if ( f == "z" ) {
107 buf = QString::number( dt->msec() );
108 } else if ( f == "zzz" ) {
109 buf = QString::number( dt->msec() ).rightJustify( 3, '0', TRUE );
110 } else if ( f == "ap" ) {
111 buf = dt->hour() < 12 ? "am" : "pm";
112 } else if ( f == "AP" ) {
113 buf = dt->hour() < 12 ? "AM" : "PM";
114 }
115 }
116
117 if ( dd ) {
118 if ( f == "d" ) {
119 buf = QString::number( dd->day() );
120 } else if ( f == "dd" ) {
121 buf = QString::number( dd->day() ).rightJustify( 2, '0', TRUE );
122 } else if ( f == "M" ) {
123 buf = QString::number( dd->month() );
124 } else if ( f == "MM" ) {
125 buf = QString::number( dd->month() ).rightJustify( 2, '0', TRUE );
126#ifndef QT_NO_TEXTDATE
127 } else if ( f == "ddd" ) {
128 buf = dd->shortDayName( dd->dayOfWeek() );
129 } else if ( f == "dddd" ) {
130 buf = dd->longDayName( dd->dayOfWeek() );
131 } else if ( f == "MMM" ) {
132 buf = dd->shortMonthName( dd->month() );
133 } else if ( f == "MMMM" ) {
134 buf = dd->longMonthName( dd->month() );
135#endif
136 } else if ( f == "yy" ) {
137 buf = QString::number( dd->year() ).right( 2 );
138 } else if ( f == "yyyy" ) {
139 buf = QString::number( dd->year() );
140 }
141 }
142
143 return buf;
144}
145
146// Parses the format string and uses getFmtString to get the values for the tokens. Ret
147static QString fmtDateTime( const QString& f, const QTime* dt = 0, const QDate* dd = 0 )
148{
149 if ( f.isEmpty() ) {
150 return QString::null;
151 }
152
153 bool ap = ( f.contains( "AP" ) || f.contains( "ap" ) );
154
155 QString buf;
156 QString frm;
157 QChar status = '0';
158
159 for ( int i = 0; i < (int)f.length(); ++i ) {
160
161 if ( f[ i ] == status ) {
162 if ( ( ap ) && ( ( f[ i ] == 'P' ) || ( f[ i ] == 'p' ) ) )
163 status = '0';
164 frm += f[ i ];
165 } else {
166 buf += getFmtString( frm, dt, dd, ap );
167 frm = QString::null;
168 if ( ( f[ i ] == 'h' ) || ( f[ i ] == 'm' ) || ( f[ i ] == 's' ) || ( f[ i ] == 'z' ) ) {
169 status = f[ i ];
170 frm += f[ i ];
171 } else if ( ( f[ i ] == 'd' ) || ( f[ i ] == 'M' ) || ( f[ i ] == 'y' ) ) {
172 status = f[ i ];
173 frm += f[ i ];
174 } else if ( ( ap ) && ( f[ i ] == 'A' ) ) {
175 status = 'P';
176 frm += f[ i ];
177 } else if( ( ap ) && ( f[ i ] == 'a' ) ) {
178 status = 'p';
179 frm += f[ i ];
180 } else {
181 buf += f[ i ];
182 status = '0';
183 }
184 }
185 }
186
187 buf += getFmtString( frm, dt, dd, ap );
188
189 return buf;
190}
191#endif // QT_NO_DATESTRING
192
193/*****************************************************************************
194 QDate member functions
195 *****************************************************************************/
196
197/*!
198 \class QDate qdatetime.h
199 \reentrant
200 \brief The QDate class provides date functions.
201
202 \ingroup time
203 \mainclass
204
205 A QDate object contains a calendar date, i.e. year, month, and day
206 numbers, in the modern Western (Gregorian) calendar. It can read
207 the current date from the system clock. It provides functions for
208 comparing dates and for manipulating dates, e.g. by adding a
209 number of days or months or years.
210
211 A QDate object is typically created either by giving the year,
212 month and day numbers explicitly, or by using the static function
213 currentDate(), which creates a QDate object containing the system
214 clock's date. An explicit date can also be set using setYMD(). The
215 fromString() function returns a QDate given a string and a date
216 format which is used to interpret the date within the string.
217
218 The year(), month(), and day() functions provide access to the
219 year, month, and day numbers. Also, dayOfWeek() and dayOfYear()
220 functions are provided. The same information is provided in
221 textual format by the toString(), shortDayName(), longDayName(),
222 shortMonthName() and longMonthName() functions.
223
224 QDate provides a full set of operators to compare two QDate
225 objects where smaller means earlier and larger means later.
226
227 You can increment (or decrement) a date by a given number of days
228 using addDays(). Similarly you can use addMonths() and addYears().
229 The daysTo() function returns the number of days between two
230 dates.
231
232 The daysInMonth() and daysInYear() functions return how many days
233 there are in this date's month and year, respectively. The
234 leapYear() function indicates whether this date is in a leap year.
235
236 Note that QDate should not be used for date calculations for dates
237 prior to the introduction of the Gregorian calendar. This calendar
238 was adopted by England from the 14<sup><small>th</small></sup>
239 September 1752 (hence this is the earliest valid QDate), and
240 subsequently by most other Western countries, until 1923.
241
242 The end of time is reached around the year 8000, by which time we
243 expect Qt to be obsolete.
244
245 \sa QTime QDateTime QDateEdit QDateTimeEdit
246*/
247
248/*!
249 \enum Qt::DateFormat
250
251 \value TextDate (default) Qt format
252 \value ISODate ISO 8601 extended format (YYYY-MM-DD, or with time,
253 YYYY-MM-DDTHH:MM:SS)
254 \value LocalDate locale dependent format
255*/
256
257
258/*!
259 \enum Qt::TimeSpec
260
261 \value LocalTime Locale dependent time (Timezones and Daylight Savings Time)
262 \value UTC Coordinated Universal Time, replaces Greenwich Time
263*/
264
265/*!
266 \fn QDate::QDate()
267
268 Constructs a null date. Null dates are invalid.
269
270 \sa isNull(), isValid()
271*/
272
273
274/*!
275 Constructs a date with year \a y, month \a m and day \a d.
276
277 \a y must be in the range 1752..8000, \a m must be in the range
278 1..12, and \a d must be in the range 1..31.
279
280 \warning If \a y is in the range 0..99, it is interpreted as
281 1900..1999.
282
283 \sa isValid()
284*/
285
286QDate::QDate( int y, int m, int d )
287{
288 jd = 0;
289 setYMD( y, m, d );
290}
291
292
293/*!
294 \fn bool QDate::isNull() const
295
296 Returns TRUE if the date is null; otherwise returns FALSE. A null
297 date is invalid.
298
299 \sa isValid()
300*/
301
302
303/*!
304 Returns TRUE if this date is valid; otherwise returns FALSE.
305
306 \sa isNull()
307*/
308
309bool QDate::isValid() const
310{
311 return jd >= FIRST_DAY;
312}
313
314
315/*!
316 Returns the year (1752..8000) of this date.
317
318 \sa month(), day()
319*/
320
321int QDate::year() const
322{
323 int y, m, d;
324 julianToGregorian( jd, y, m, d );
325 return y;
326}
327
328/*!
329 Returns the month (January=1..December=12) of this date.
330
331 \sa year(), day()
332*/
333
334int QDate::month() const
335{
336 int y, m, d;
337 julianToGregorian( jd, y, m, d );
338 return m;
339}
340
341/*!
342 Returns the day of the month (1..31) of this date.
343
344 \sa year(), month(), dayOfWeek()
345*/
346
347int QDate::day() const
348{
349 int y, m, d;
350 julianToGregorian( jd, y, m, d );
351 return d;
352}
353
354/*!
355 Returns the weekday (Monday=1..Sunday=7) for this date.
356
357 \sa day(), dayOfYear()
358*/
359
360int QDate::dayOfWeek() const
361{
362 return ( jd % 7 ) + 1;
363}
364
365/*!
366 Returns the day of the year (1..365) for this date.
367
368 \sa day(), dayOfWeek()
369*/
370
371int QDate::dayOfYear() const
372{
373 return jd - gregorianToJulian(year(), 1, 1) + 1;
374}
375
376/*!
377 Returns the number of days in the month (28..31) for this date.
378
379 \sa day(), daysInYear()
380*/
381
382int QDate::daysInMonth() const
383{
384 int y, m, d;
385 julianToGregorian( jd, y, m, d );
386 if ( m == 2 && leapYear(y) )
387 return 29;
388 else
389 return monthDays[m];
390}
391
392/*!
393 Returns the number of days in the year (365 or 366) for this date.
394
395 \sa day(), daysInMonth()
396*/
397
398int QDate::daysInYear() const
399{
400 int y, m, d;
401 julianToGregorian( jd, y, m, d );
402 return leapYear( y ) ? 366 : 365;
403}
404
405/*!
406 Returns the week number (1 to 53), and stores the year in \a
407 *yearNumber unless \a yearNumber is null (the default).
408
409 Returns 0 if the date is invalid.
410
411 In accordance with ISO 8601, weeks start on Monday and the first
412 Thursday of a year is always in week 1 of that year. Most years
413 have 52 weeks, but some have 53.
414
415 \a *yearNumber is not always the same as year(). For example, 1
416 January 2000 has week number 52 in the year 1999, and 31 December
417 2002 has week number 1 in the year 2003.
418
419 \sa isValid()
420*/
421
422int QDate::weekNumber( int *yearNumber ) const
423{
424 if ( !isValid() )
425 return 0;
426
427 int dow = dayOfWeek();
428 int doy = dayOfYear();
429 int currYear = year();
430 int jan1WeekDay = QDate( currYear, 1, 1 ).dayOfWeek();
431 int yearNum;
432 int weekNum;
433
434 if ( doy <= (8 - jan1WeekDay) && jan1WeekDay > 4 ) {
435 yearNum = currYear - 1;
436 weekNum = 52;
437 if ( jan1WeekDay == 5 ||
438 (jan1WeekDay == 6 && QDate::leapYear(yearNum)) )
439 weekNum++;
440 } else {
441 int totalDays = 365;
442 if ( QDate::leapYear(currYear) )
443 totalDays++;
444
445 if ( (totalDays - doy < 4 - dow)
446 || (jan1WeekDay == 7 && totalDays - doy < 3) ) {
447 yearNum = currYear + 1;
448 weekNum = 1;
449 } else {
450 int j = doy + ( 7 - dow ) + ( jan1WeekDay - 1 );
451 yearNum = currYear;
452 weekNum = j / 7;
453 if ( jan1WeekDay > 4 )
454 weekNum--;
455 }
456 }
457 if ( yearNumber )
458 *yearNumber = yearNum;
459 return weekNum;
460}
461
462/*!
463 \fn QString QDate::monthName( int month )
464 \obsolete
465
466 Use shortMonthName() instead.
467*/
468#ifndef QT_NO_TEXTDATE
469/*!
470 Returns the name of the \a month.
471
472 1 = "Jan", 2 = "Feb", ... 12 = "Dec"
473
474 The month names will be localized according to the system's locale
475 settings.
476
477 \sa toString(), longMonthName(), shortDayName(), longDayName()
478*/
479
480QString QDate::shortMonthName( int month )
481{
482#if defined(QT_CHECK_RANGE)
483 if ( month < 1 || month > 12 ) {
484 qWarning( "QDate::shortMonthName: Parameter out ouf range." );
485 month = 1;
486 }
487#endif
488#ifndef Q_WS_WIN
489 char buffer[255];
490 tm tt;
491 memset( &tt, 0, sizeof( tm ) );
492 tt.tm_mon = month - 1;
493 if ( strftime( buffer, sizeof( buffer ), "%b", &tt ) )
494 return QString::fromLocal8Bit( buffer );
495#else
496 SYSTEMTIME st;
497 memset( &st, 0, sizeof(SYSTEMTIME) );
498 st.wYear = 2000;
499 st.wMonth = month;
500 st.wDay = 1;
501 const wchar_t mmm_t[] = L"MMM"; // workaround for Borland
502 QT_WA( {
503 TCHAR buf[255];
504 if ( GetDateFormat( LOCALE_USER_DEFAULT, 0, &st, mmm_t, buf, 255 ) )
505 return QString::fromUcs2( (ushort*)buf );
506 } , {
507 char buf[255];
508 if ( GetDateFormatA( LOCALE_USER_DEFAULT, 0, &st, "MMM", (char*)&buf, 255 ) )
509 return QString::fromLocal8Bit( buf );
510 } );
511#endif
512 return QString::null;
513}
514
515/*!
516 Returns the long name of the \a month.
517
518 1 = "January", 2 = "February", ... 12 = "December"
519
520 The month names will be localized according to the system's locale
521 settings.
522
523 \sa toString(), shortMonthName(), shortDayName(), longDayName()
524*/
525
526QString QDate::longMonthName( int month )
527{
528#if defined(QT_CHECK_RANGE)
529 if ( month < 1 || month > 12 ) {
530 qWarning( "QDate::longMonthName: Parameter out ouf range." );
531 month = 1;
532 }
533#endif
534#ifndef Q_WS_WIN
535 char buffer[255];
536 tm tt;
537 memset( &tt, 0, sizeof( tm ) );
538 tt.tm_mon = month - 1;
539 if ( strftime( buffer, sizeof( buffer ), "%B", &tt ) )
540 return QString::fromLocal8Bit( buffer );
541#else
542 SYSTEMTIME st;
543 memset( &st, 0, sizeof(SYSTEMTIME) );
544 st.wYear = 2000;
545 st.wMonth = month;
546 st.wDay = 1 ;
547 const wchar_t mmmm_t[] = L"MMMM"; // workaround for Borland
548 QT_WA( {
549 TCHAR buf[255];
550 if ( GetDateFormat( LOCALE_USER_DEFAULT, 0, &st, mmmm_t, buf, 255 ) )
551 return QString::fromUcs2( (ushort*)buf );
552 } , {
553 char buf[255];
554 if ( GetDateFormatA( LOCALE_USER_DEFAULT, 0, &st, "MMMM", (char*)&buf, 255 ) )
555 return QString::fromLocal8Bit( buf );
556 } )
557#endif
558
559 return QString::null;
560}
561
562/*!
563 \fn QString QDate::dayName( int weekday )
564 \obsolete
565
566 Use shortDayName() instead.
567*/
568
569/*!
570 Returns the name of the \a weekday.
571
572 1 = "Mon", 2 = "Tue", ... 7 = "Sun"
573
574 The day names will be localized according to the system's locale
575 settings.
576
577 \sa toString(), shortMonthName(), longMonthName(), longDayName()
578*/
579
580QString QDate::shortDayName( int weekday )
581{
582#if defined(QT_CHECK_RANGE)
583 if ( weekday < 1 || weekday > 7 ) {
584 qWarning( "QDate::shortDayName: Parameter out of range." );
585 weekday = 1;
586 }
587#endif
588#ifndef Q_WS_WIN
589 char buffer[255];
590 tm tt;
591 memset( &tt, 0, sizeof( tm ) );
592 tt.tm_wday = ( weekday == 7 ) ? 0 : weekday;
593 if ( strftime( buffer, sizeof( buffer ), "%a", &tt ) )
594 return QString::fromLocal8Bit( buffer );
595#else
596 SYSTEMTIME st;
597 memset( &st, 0, sizeof(SYSTEMTIME) );
598 st.wYear = 2001;
599 st.wMonth = 10;
600 st.wDayOfWeek = ( weekday == 7 ) ? 0 : weekday;
601 st.wDay = 21 + st.wDayOfWeek;
602 const wchar_t ddd_t[] = L"ddd"; // workaround for Borland
603 QT_WA( {
604 TCHAR buf[255];
605 if ( GetDateFormat( LOCALE_USER_DEFAULT, 0, &st, ddd_t, buf, 255 ) )
606 return QString::fromUcs2( (ushort*)buf );
607 } , {
608 char buf[255];
609 if ( GetDateFormatA( LOCALE_USER_DEFAULT, 0, &st, "ddd", (char*)&buf, 255 ) )
610 return QString::fromLocal8Bit( buf );
611 } );
612#endif
613
614 return QString::null;
615}
616
617/*!
618 Returns the long name of the \a weekday.
619
620 1 = "Monday", 2 = "Tuesday", ... 7 = "Sunday"
621
622 The day names will be localized according to the system's locale
623 settings.
624
625 \sa toString(), shortDayName(), shortMonthName(), longMonthName()
626*/
627
628QString QDate::longDayName( int weekday )
629{
630#if defined(QT_CHECK_RANGE)
631 if ( weekday < 1 || weekday > 7 ) {
632 qWarning( "QDate::longDayName: Parameter out of range." );
633 weekday = 1;
634 }
635#endif
636#ifndef Q_WS_WIN
637 char buffer[255];
638 tm tt;
639 memset( &tt, 0, sizeof( tm ) );
640 tt.tm_wday = ( weekday == 7 ) ? 0 : weekday;
641 if ( strftime( buffer, sizeof( buffer ), "%A", &tt ) )
642 return QString::fromLocal8Bit( buffer );
643#else
644 SYSTEMTIME st;
645 memset( &st, 0, sizeof(SYSTEMTIME) );
646 st.wYear = 2001;
647 st.wMonth = 10;
648 st.wDayOfWeek = ( weekday == 7 ) ? 0 : weekday;
649 st.wDay = 21 + st.wDayOfWeek;
650 const wchar_t dddd_t[] = L"dddd"; // workaround for Borland
651 QT_WA( {
652 TCHAR buf[255];
653 if ( GetDateFormat( LOCALE_USER_DEFAULT, 0, &st, dddd_t, buf, 255 ) )
654 return QString::fromUcs2( (ushort*)buf );
655 } , {
656 char buf[255];
657 if ( GetDateFormatA( LOCALE_USER_DEFAULT, 0, &st, "dddd", (char*)&buf, 255 ) )
658 return QString::fromLocal8Bit( buf );
659 } );
660#endif
661
662 return QString::null;
663}
664#endif //QT_NO_TEXTDATE
665
666#ifndef QT_NO_DATESTRING
667
668#if !defined(QT_NO_SPRINTF)
669/*!
670 \overload
671
672 Returns the date as a string. The \a f parameter determines the
673 format of the string.
674
675 If \a f is \c Qt::TextDate, the string format is "Sat May 20 1995"
676 (using the shortDayName() and shortMonthName() functions to
677 generate the string, so the day and month names are locale
678 specific).
679
680 If \a f is \c Qt::ISODate, the string format corresponds to the
681 ISO 8601 specification for representations of dates, which is
682 YYYY-MM-DD where YYYY is the year, MM is the month of the year
683 (between 01 and 12), and DD is the day of the month between 01 and
684 31.
685
686 If \a f is \c Qt::LocalDate, the string format depends on the
687 locale settings of the system.
688
689 \sa shortDayName(), shortMonthName()
690*/
691QString QDate::toString( Qt::DateFormat f ) const
692{
693 int y, m, d;
694 julianToGregorian( jd, y, m, d );
695 switch ( f ) {
696 case Qt::LocalDate:
697 {
698#ifndef Q_WS_WIN
699 tm tt;
700 memset( &tt, 0, sizeof( tm ) );
701 char buf[255];
702 tt.tm_mday = day();
703 tt.tm_mon = month() - 1;
704 tt.tm_year = year() - 1900;
705
706 static const char * avoidEgcsWarning = "%x";
707 if ( strftime( buf, sizeof(buf), avoidEgcsWarning, &tt ) )
708 return QString::fromLocal8Bit( buf );
709#else
710 SYSTEMTIME st;
711 memset( &st, 0, sizeof(SYSTEMTIME) );
712 st.wYear = year();
713 st.wMonth = month();
714 st.wDay = day();
715 QT_WA( {
716 TCHAR buf[255];
717 if ( GetDateFormat( LOCALE_USER_DEFAULT, 0, &st, 0, buf, 255 ) )
718 return QString::fromUcs2( (ushort*)buf );
719 } , {
720 char buf[255];
721 if ( GetDateFormatA( LOCALE_USER_DEFAULT, 0, &st, 0, (char*)&buf, 255 ) )
722 return QString::fromLocal8Bit( buf );
723 } );
724#endif
725 return QString::null;
726 }
727 default:
728#ifndef QT_NO_TEXTDATE
729 case Qt::TextDate:
730 {
731 QString buf = shortDayName( dayOfWeek() );
732 buf += ' ';
733 buf += shortMonthName( m );
734 QString t;
735 t.sprintf( " %d %d", d, y );
736 buf += t;
737 return buf;
738 }
739#endif
740 case Qt::ISODate:
741 {
742 QString month( QString::number( m ).rightJustify( 2, '0' ) );
743 QString day( QString::number( d ).rightJustify( 2, '0' ) );
744 return QString::number( y ) + "-" + month + "-" + day;
745 }
746 }
747}
748#endif //QT_NO_SPRINTF
749
750/*!
751 Returns the date as a string. The \a format parameter determines
752 the format of the result string.
753
754 These expressions may be used:
755
756 \table
757 \header \i Expression \i Output
758 \row \i d \i the day as number without a leading zero (1-31)
759 \row \i dd \i the day as number with a leading zero (01-31)
760 \row \i ddd
761 \i the abbreviated localized day name (e.g. 'Mon'..'Sun').
762 Uses QDate::shortDayName().
763 \row \i dddd
764 \i the long localized day name (e.g. 'Monday'..'Sunday').
765 Uses QDate::longDayName().
766 \row \i M \i the month as number without a leading zero (1-12)
767 \row \i MM \i the month as number with a leading zero (01-12)
768 \row \i MMM
769 \i the abbreviated localized month name (e.g. 'Jan'..'Dec').
770 Uses QDate::shortMonthName().
771 \row \i MMMM
772 \i the long localized month name (e.g. 'January'..'December').
773 Uses QDate::longMonthName().
774 \row \i yy \i the year as two digit number (00-99)
775 \row \i yyyy \i the year as four digit number (1752-8000)
776 \endtable
777
778 All other input characters will be ignored.
779
780 Example format strings (assuming that the QDate is the
781 20<sup><small>th</small></sup> July 1969):
782 \table
783 \header \i Format \i Result
784 \row \i dd.MM.yyyy \i11 20.07.1969
785 \row \i ddd MMMM d yy \i11 Sun July 20 69
786 \endtable
787
788 \sa QDate::toString() QTime::toString()
789
790*/
791QString QDate::toString( const QString& format ) const
792{
793 return fmtDateTime( format, 0, this );
794}
795#endif //QT_NO_DATESTRING
796
797/*!
798 Sets the date's year \a y, month \a m and day \a d.
799
800 \a y must be in the range 1752..8000, \a m must be in the range
801 1..12, and \a d must be in the range 1..31.
802
803 \warning If \a y is in the range 0..99, it is interpreted as
804 1900..1999.
805
806 Returns TRUE if the date is valid; otherwise returns FALSE.
807*/
808
809bool QDate::setYMD( int y, int m, int d )
810{
811 if ( year() == y && month() == m && day() == d )
812 return isValid();
813 if ( !isValid(y,m,d) ) {
814#if defined(QT_CHECK_RANGE)
815 qWarning( "QDate::setYMD: Invalid date %04d-%02d-%02d", y, m, d );
816#endif
817 return FALSE;
818 }
819 jd = gregorianToJulian( y, m, d );
820 return TRUE;
821}
822
823/*!
824 Returns a QDate object containing a date \a ndays later than the
825 date of this object (or earlier if \a ndays is negative).
826
827 \sa addMonths() addYears() daysTo()
828*/
829
830QDate QDate::addDays( int ndays ) const
831{
832 QDate d;
833 d.jd = jd + ndays;
834 return d;
835}
836
837/*!
838 Returns a QDate object containing a date \a nmonths later than the
839 date of this object (or earlier if \a nmonths is negative).
840
841 \sa addDays() addYears()
842*/
843
844QDate QDate::addMonths( int nmonths ) const
845{
846 int y, m, d;
847 julianToGregorian( jd, y, m, d );
848
849 while ( nmonths != 0 ) {
850 if ( nmonths < 0 && nmonths + 12 <= 0 ) {
851 y--;
852 nmonths+=12;
853 } else if ( nmonths < 0 ) {
854 m+= nmonths;
855 nmonths = 0;
856 if ( m <= 0 ) {
857 --y;
858 m+=12;
859 }
860 } else if ( nmonths - 12 >= 0 ) {
861 y++;
862 nmonths-=12;
863 } else if ( m == 12 ) {
864 y++;
865 m = 0;
866 } else {
867 m+= nmonths;
868 nmonths = 0;
869 if ( m > 12 ) {
870 ++y;
871 m -= 12;
872 }
873 }
874 }
875
876 QDate tmp(y,m,1);
877
878 if( d > tmp.daysInMonth() )
879 d = tmp.daysInMonth();
880
881 QDate date(y, m, d);
882 return date;
883
884}
885
886/*!
887 Returns a QDate object containing a date \a nyears later than the
888 date of this object (or earlier if \a nyears is negative).
889
890 \sa addDays(), addMonths()
891*/
892
893QDate QDate::addYears( int nyears ) const
894{
895 int y, m, d;
896 julianToGregorian( jd, y, m, d );
897 y += nyears;
898 QDate date(y, m, d);
899 return date;
900}
901
902
903
904/*!
905 Returns the number of days from this date to \a d (which is
906 negative if \a d is earlier than this date).
907
908 Example:
909 \code
910 QDate d1( 1995, 5, 17 ); // May 17th 1995
911 QDate d2( 1995, 5, 20 ); // May 20th 1995
912 d1.daysTo( d2 ); // returns 3
913 d2.daysTo( d1 ); // returns -3
914 \endcode
915
916 \sa addDays()
917*/
918
919int QDate::daysTo( const QDate &d ) const
920{
921 return d.jd - jd;
922}
923
924
925/*!
926 \fn bool QDate::operator==( const QDate &d ) const
927
928 Returns TRUE if this date is equal to \a d; otherwise returns FALSE.
929*/
930
931/*!
932 \fn bool QDate::operator!=( const QDate &d ) const
933
934 Returns TRUE if this date is different from \a d; otherwise returns FALSE.
935*/
936
937/*!
938 \fn bool QDate::operator<( const QDate &d ) const
939
940 Returns TRUE if this date is earlier than \a d, otherwise returns FALSE.
941*/
942
943/*!
944 \fn bool QDate::operator<=( const QDate &d ) const
945
946 Returns TRUE if this date is earlier than or equal to \a d,
947 otherwise returns FALSE.
948*/
949
950/*!
951 \fn bool QDate::operator>( const QDate &d ) const
952
953 Returns TRUE if this date is later than \a d, otherwise returns FALSE.
954*/
955
956/*!
957 \fn bool QDate::operator>=( const QDate &d ) const
958
959 Returns TRUE if this date is later than or equal to \a d,
960 otherwise returns FALSE.
961*/
962
963/*!
964 \overload
965 Returns the current date, as reported by the system clock.
966
967 \sa QTime::currentTime(), QDateTime::currentDateTime()
968*/
969
970QDate QDate::currentDate()
971{
972 return currentDate( Qt::LocalTime );
973}
974
975/*!
976 Returns the current date, as reported by the system clock, for the
977 TimeSpec \a ts. The default TimeSpec is LocalTime.
978
979 \sa QTime::currentTime(), QDateTime::currentDateTime(), Qt::TimeSpec
980*/
981QDate QDate::currentDate( Qt::TimeSpec ts )
982{
983 QDate d;
984#if defined(Q_OS_WIN32)
985 SYSTEMTIME t;
986 memset( &t, 0, sizeof(SYSTEMTIME) );
987 if ( ts == Qt::LocalTime )
988 GetLocalTime( &t );
989 else
990 GetSystemTime( &t );
991 d.jd = gregorianToJulian( t.wYear, t.wMonth, t.wDay );
992#else
993 time_t ltime;
994 time( &ltime );
995 tm *t;
996 if ( ts == Qt::LocalTime )
997 t = localtime( &ltime );
998 else
999 t = gmtime( &ltime );
1000 d.jd = gregorianToJulian( t->tm_year + 1900, t->tm_mon + 1, t->tm_mday );
1001#endif
1002 return d;
1003}
1004
1005#ifndef QT_NO_DATESTRING
1006/*!
1007 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.
1009
1010 Note for \c Qt::TextDate: It is recommended that you use the
1011 English short month names (e.g. "Jan"). Although localized month
1012 names can also be used, they depend on the user's locale settings.
1013
1014 \warning \c Qt::LocalDate cannot be used here.
1015*/
1016QDate QDate::fromString( const QString& s, Qt::DateFormat f )
1017{
1018 if ( ( s.isEmpty() ) || ( f == Qt::LocalDate ) ) {
1019#if defined(QT_CHECK_RANGE)
1020 qWarning( "QDate::fromString: Parameter out of range." );
1021#endif
1022 return QDate();
1023 }
1024 switch ( f ) {
1025 case Qt::ISODate:
1026 {
1027 int year( s.mid( 0, 4 ).toInt() );
1028 int month( s.mid( 5, 2 ).toInt() );
1029 int day( s.mid( 8, 2 ).toInt() );
1030 if ( year && month && day )
1031 return QDate( year, month, day );
1032 }
1033 break;
1034 default:
1035#ifndef QT_NO_TEXTDATE
1036 case Qt::TextDate:
1037 {
1038 /*
1039 This will fail gracefully if the input string doesn't
1040 contain any space.
1041 */
1042 int monthPos = s.find( ' ' ) + 1;
1043 int dayPos = s.find( ' ', monthPos ) + 1;
1044
1045 QString monthName( s.mid(monthPos, dayPos - monthPos - 1) );
1046 int month = -1;
1047
1048 // try English names first
1049 for ( int i = 0; i < 12; i++ ) {
1050 if ( monthName == qt_shortMonthNames[i] ) {
1051 month = i + 1;
1052 break;
1053 }
1054 }
1055
1056 // try the localized names
1057 if ( month == -1 ) {
1058 for ( int i = 0; i < 12; i++ ) {
1059 if ( monthName == shortMonthName( i + 1 ) ) {
1060 month = i + 1;
1061 break;
1062 }
1063 }
1064 }
1065#if defined(QT_CHECK_RANGE)
1066 if ( month < 1 || month > 12 ) {
1067 qWarning( "QDate::fromString: Parameter out of range." );
1068 month = 1;
1069 }
1070#endif
1071 int day = s.mid( dayPos, 2 ).stripWhiteSpace().toInt();
1072 int year = s.right( 4 ).toInt();
1073 return QDate( year, month, day );
1074 }
1075#else
1076 break;
1077#endif
1078 }
1079 return QDate();
1080}
1081#endif //QT_NO_DATESTRING
1082
1083/*!
1084 \overload
1085
1086 Returns TRUE if the specified date (year \a y, month \a m and day
1087 \a d) is valid; otherwise returns FALSE.
1088
1089 Example:
1090 \code
1091 QDate::isValid( 2002, 5, 17 ); // TRUE May 17th 2002 is valid
1092 QDate::isValid( 2002, 2, 30 ); // FALSE Feb 30th does not exist
1093 QDate::isValid( 2004, 2, 29 ); // TRUE 2004 is a leap year
1094 QDate::isValid( 1202, 6, 6 ); // FALSE 1202 is pre-Gregorian
1095 \endcode
1096
1097 \warning A \a y value in the range 00..99 is interpreted as
1098 1900..1999.
1099
1100 \sa isNull(), setYMD()
1101*/
1102
1103bool QDate::isValid( int y, int m, int d )
1104{
1105 if ( y >= 0 && y <= 99 )
1106 y += 1900;
1107 else if ( y < FIRST_YEAR || (y == FIRST_YEAR && (m < 9 ||
1108 (m == 9 && d < 14))) )
1109 return FALSE;
1110 return (d > 0 && m > 0 && m <= 12) &&
1111 (d <= monthDays[m] || (d == 29 && m == 2 && leapYear(y)));
1112}
1113
1114/*!
1115 Returns TRUE if the specified year \a y is a leap year; otherwise
1116 returns FALSE.
1117*/
1118
1119bool QDate::leapYear( int y )
1120{
1121 return y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
1122}
1123
1124/*!
1125 \internal
1126 Converts a Gregorian date to a Julian day.
1127 This algorithm is taken from Communications of the ACM, Vol 6, No 8.
1128 \sa julianToGregorian()
1129*/
1130
1131uint QDate::gregorianToJulian( int y, int m, int d )
1132{
1133 uint c, ya;
1134 if ( y <= 99 )
1135 y += 1900;
1136 if ( m > 2 ) {
1137 m -= 3;
1138 } else {
1139 m += 9;
1140 y--;
1141 }
1142 c = y; // NOTE: Sym C++ 6.0 bug
1143 c /= 100;
1144 ya = y - 100*c;
1145 return 1721119 + d + (146097*c)/4 + (1461*ya)/4 + (153*m+2)/5;
1146}
1147
1148/*!
1149 \internal
1150 Converts a Julian day to a Gregorian date.
1151 This algorithm is taken from Communications of the ACM, Vol 6, No 8.
1152 \sa gregorianToJulian()
1153*/
1154
1155void QDate::julianToGregorian( uint jd, int &y, int &m, int &d )
1156{
1157 uint x;
1158 uint j = jd - 1721119;
1159 y = (j*4 - 1)/146097;
1160 j = j*4 - 146097*y - 1;
1161 x = j/4;
1162 j = (x*4 + 3) / 1461;
1163 y = 100*y + j;
1164 x = (x*4) + 3 - 1461*j;
1165 x = (x + 4)/4;
1166 m = (5*x - 3)/153;
1167 x = 5*x - 3 - 153*m;
1168 d = (x + 5)/5;
1169 if ( m < 10 ) {
1170 m += 3;
1171 } else {
1172 m -= 9;
1173 y++;
1174 }
1175}
1176
1177
1178/*****************************************************************************
1179 QTime member functions
1180 *****************************************************************************/
1181
1182/*!
1183 \class QTime qdatetime.h
1184 \reentrant
1185
1186 \brief The QTime class provides clock time functions.
1187
1188 \ingroup time
1189 \mainclass
1190
1191 A QTime object contains a clock time, i.e. the number of hours,
1192 minutes, seconds, and milliseconds since midnight. It can read the
1193 current time from the system clock and measure a span of elapsed
1194 time. It provides functions for comparing times and for
1195 manipulating a time by adding a number of (milli)seconds.
1196
1197 QTime uses the 24-hour clock format; it has no concept of AM/PM.
1198 It operates in local time; it knows nothing about time zones or
1199 daylight savings time.
1200
1201 A QTime object is typically created either by giving the number of
1202 hours, minutes, seconds, and milliseconds explicitly, or by using
1203 the static function currentTime(), which creates a QTime object
1204 that contains the system's clock time. Note that the accuracy
1205 depends on the accuracy of the underlying operating system; not
1206 all systems provide 1-millisecond accuracy.
1207
1208 The hour(), minute(), second(), and msec() functions provide
1209 access to the number of hours, minutes, seconds, and milliseconds
1210 of the time. The same information is provided in textual format by
1211 the toString() function.
1212
1213 QTime provides a full set of operators to compare two QTime
1214 objects. One time is considered smaller than another if it is
1215 earlier than the other.
1216
1217 The time a given number of seconds or milliseconds later than a
1218 given time can be found using the addSecs() or addMSecs()
1219 functions. Correspondingly, the number of (milli)seconds between
1220 two times can be found using the secsTo() or msecsTo() functions.
1221
1222 QTime can be used to measure a span of elapsed time using the
1223 start(), restart(), and elapsed() functions.
1224
1225 \sa QDate, QDateTime
1226*/
1227
1228/*!
1229 \fn QTime::QTime()
1230
1231 Constructs the time 0 hours, minutes, seconds and milliseconds,
1232 i.e. 00:00:00.000 (midnight). This is a valid time.
1233
1234 \sa isValid()
1235*/
1236
1237/*!
1238 Constructs a time with hour \a h, minute \a m, seconds \a s and
1239 milliseconds \a ms.
1240
1241 \a h must be in the range 0..23, \a m and \a s must be in the
1242 range 0..59, and \a ms must be in the range 0..999.
1243
1244 \sa isValid()
1245*/
1246
1247QTime::QTime( int h, int m, int s, int ms )
1248{
1249 setHMS( h, m, s, ms );
1250}
1251
1252
1253/*!
1254 \fn bool QTime::isNull() const
1255
1256 Returns TRUE if the time is equal to 00:00:00.000; otherwise
1257 returns FALSE. A null time is valid.
1258
1259 \sa isValid()
1260*/
1261
1262/*!
1263 Returns TRUE if the time is valid; otherwise returns FALSE. The
1264 time 23:30:55.746 is valid, whereas 24:12:30 is invalid.
1265
1266 \sa isNull()
1267*/
1268
1269bool QTime::isValid() const
1270{
1271 return ds < MSECS_PER_DAY;
1272}
1273
1274
1275/*!
1276 Returns the hour part (0..23) of the time.
1277*/
1278
1279int QTime::hour() const
1280{
1281 return ds / MSECS_PER_HOUR;
1282}
1283
1284/*!
1285 Returns the minute part (0..59) of the time.
1286*/
1287
1288int QTime::minute() const
1289{
1290 return (ds % MSECS_PER_HOUR)/MSECS_PER_MIN;
1291}
1292
1293/*!
1294 Returns the second part (0..59) of the time.
1295*/
1296
1297int QTime::second() const
1298{
1299 return (ds / 1000)%SECS_PER_MIN;
1300}
1301
1302/*!
1303 Returns the millisecond part (0..999) of the time.
1304*/
1305
1306int QTime::msec() const
1307{
1308 return ds % 1000;
1309}
1310
1311#ifndef QT_NO_DATESTRING
1312#ifndef QT_NO_SPRINTF
1313/*!
1314 \overload
1315
1316 Returns the time as a string. Milliseconds are not included. The
1317 \a f parameter determines the format of the string.
1318
1319 If \a f is \c Qt::TextDate, the string format is HH:MM:SS; e.g. 1
1320 second before midnight would be "23:59:59".
1321
1322 If \a f is \c Qt::ISODate, the string format corresponds to the
1323 ISO 8601 extended specification for representations of dates,
1324 which is also HH:MM:SS.
1325
1326 If \a f is Qt::LocalDate, the string format depends on the locale
1327 settings of the system.
1328*/
1329
1330QString QTime::toString( Qt::DateFormat f ) const
1331{
1332 switch ( f ) {
1333 case Qt::LocalDate:
1334 {
1335#ifndef Q_WS_WIN
1336 tm tt;
1337 memset( &tt, 0, sizeof( tm ) );
1338 char buf[255];
1339 tt.tm_sec = second();
1340 tt.tm_min = minute();
1341 tt.tm_hour = hour();
1342 if ( strftime( buf, sizeof(buf), "%X", &tt ) )
1343 return QString::fromLocal8Bit( buf );
1344#else
1345 SYSTEMTIME st;
1346 memset( &st, 0, sizeof(SYSTEMTIME) );
1347 st.wHour = hour();
1348 st.wMinute = minute();
1349 st.wSecond = second();
1350 st.wMilliseconds = 0;
1351 QT_WA( {
1352 TCHAR buf[255];
1353 if ( GetTimeFormat( LOCALE_USER_DEFAULT, 0, &st, 0, buf, 255 ) )
1354 return QString::fromUcs2( (ushort*)buf );
1355 } , {
1356 char buf[255];
1357 if ( GetTimeFormatA( LOCALE_USER_DEFAULT, 0, &st, 0, (char*)&buf, 255 ) )
1358 return QString::fromLocal8Bit( buf );
1359 } );
1360#endif
1361 return QString::null;
1362 }
1363 default:
1364 case Qt::ISODate:
1365 case Qt::TextDate:
1366 QString buf;
1367 buf.sprintf( "%.2d:%.2d:%.2d", hour(), minute(), second() );
1368 return buf;
1369 }
1370}
1371#endif
1372
1373/*!
1374 Returns the time as a string. The \a format parameter determines
1375 the format of the result string.
1376
1377 These expressions may be used:
1378
1379 \table
1380 \header \i Expression \i Output
1381 \row \i h
1382 \i the hour without a leading zero (0..23 or 1..12 if AM/PM display)
1383 \row \i hh
1384 \i the hour with a leading zero (00..23 or 01..12 if AM/PM display)
1385 \row \i m \i the minute without a leading zero (0..59)
1386 \row \i mm \i the minute with a leading zero (00..59)
1387 \row \i s \i the second whithout a leading zero (0..59)
1388 \row \i ss \i the second whith a leading zero (00..59)
1389 \row \i z \i the milliseconds without leading zeroes (0..999)
1390 \row \i zzz \i the milliseconds with leading zeroes (000..999)
1391 \row \i AP
1392 \i use AM/PM display. \e AP will be replaced by either "AM" or "PM".
1393 \row \i ap
1394 \i use am/pm display. \e ap will be replaced by either "am" or "pm".
1395 \endtable
1396
1397 All other input characters will be ignored.
1398
1399 Example format strings (assuming that the QTime is 14:13:09.042)
1400
1401 \table
1402 \header \i Format \i Result
1403 \row \i hh:mm:ss.zzz \i11 14:13:09.042
1404 \row \i h:m:s ap \i11 2:13:9 pm
1405 \endtable
1406
1407 \sa QDate::toString() QTime::toString()
1408*/
1409QString QTime::toString( const QString& format ) const
1410{
1411 return fmtDateTime( format, this, 0 );
1412}
1413#endif //QT_NO_DATESTRING
1414/*!
1415 Sets the time to hour \a h, minute \a m, seconds \a s and
1416 milliseconds \a ms.
1417
1418 \a h must be in the range 0..23, \a m and \a s must be in the
1419 range 0..59, and \a ms must be in the range 0..999. Returns TRUE
1420 if the set time is valid; otherwise returns FALSE.
1421
1422 \sa isValid()
1423*/
1424
1425bool QTime::setHMS( int h, int m, int s, int ms )
1426{
1427 if ( !isValid(h,m,s,ms) ) {
1428#if defined(QT_CHECK_RANGE)
1429 qWarning( "QTime::setHMS Invalid time %02d:%02d:%02d.%03d", h, m, s,
1430 ms );
1431#endif
1432 ds = MSECS_PER_DAY; // make this invalid
1433 return FALSE;
1434 }
1435 ds = (h*SECS_PER_HOUR + m*SECS_PER_MIN + s)*1000 + ms;
1436 return TRUE;
1437}
1438
1439/*!
1440 Returns a QTime object containing a time \a nsecs seconds later
1441 than the time of this object (or earlier if \a nsecs is negative).
1442
1443 Note that the time will wrap if it passes midnight.
1444
1445 Example:
1446 \code
1447 QTime n( 14, 0, 0 ); // n == 14:00:00
1448 QTime t;
1449 t = n.addSecs( 70 ); // t == 14:01:10
1450 t = n.addSecs( -70 ); // t == 13:58:50
1451 t = n.addSecs( 10*60*60 + 5 ); // t == 00:00:05
1452 t = n.addSecs( -15*60*60 ); // t == 23:00:00
1453 \endcode
1454
1455 \sa addMSecs(), secsTo(), QDateTime::addSecs()
1456*/
1457
1458QTime QTime::addSecs( int nsecs ) const
1459{
1460 return addMSecs( nsecs * 1000 );
1461}
1462
1463/*!
1464 Returns the number of seconds from this time to \a t (which is
1465 negative if \a t is earlier than this time).
1466
1467 Because QTime measures time within a day and there are 86400
1468 seconds in a day, the result is always between -86400 and 86400.
1469
1470 \sa addSecs() QDateTime::secsTo()
1471*/
1472
1473int QTime::secsTo( const QTime &t ) const
1474{
1475 return ((int)t.ds - (int)ds)/1000;
1476}
1477
1478/*!
1479 Returns a QTime object containing a time \a ms milliseconds later
1480 than the time of this object (or earlier if \a ms is negative).
1481
1482 Note that the time will wrap if it passes midnight. See addSecs()
1483 for an example.
1484
1485 \sa addSecs(), msecsTo()
1486*/
1487
1488QTime QTime::addMSecs( int ms ) const
1489{
1490 QTime t;
1491 if ( ms < 0 ) {
1492 // % not well-defined for -ve, but / is.
1493 int negdays = (MSECS_PER_DAY-ms) / MSECS_PER_DAY;
1494 t.ds = ((int)ds + ms + negdays*MSECS_PER_DAY)
1495 % MSECS_PER_DAY;
1496 } else {
1497 t.ds = ((int)ds + ms) % MSECS_PER_DAY;
1498 }
1499 return t;
1500}
1501
1502/*!
1503 Returns the number of milliseconds from this time to \a t (which
1504 is negative if \a t is earlier than this time).
1505
1506 Because QTime measures time within a day and there are 86400
1507 seconds in a day, the result is always between -86400 and 86400s.
1508
1509 \sa secsTo()
1510*/
1511
1512int QTime::msecsTo( const QTime &t ) const
1513{
1514 return (int)t.ds - (int)ds;
1515}
1516
1517
1518/*!
1519 \fn bool QTime::operator==( const QTime &t ) const
1520
1521 Returns TRUE if this time is equal to \a t; otherwise returns FALSE.
1522*/
1523
1524/*!
1525 \fn bool QTime::operator!=( const QTime &t ) const
1526
1527 Returns TRUE if this time is different from \a t; otherwise returns FALSE.
1528*/
1529
1530/*!
1531 \fn bool QTime::operator<( const QTime &t ) const
1532
1533 Returns TRUE if this time is earlier than \a t; otherwise returns FALSE.
1534*/
1535
1536/*!
1537 \fn bool QTime::operator<=( const QTime &t ) const
1538
1539 Returns TRUE if this time is earlier than or equal to \a t;
1540 otherwise returns FALSE.
1541*/
1542
1543/*!
1544 \fn bool QTime::operator>( const QTime &t ) const
1545
1546 Returns TRUE if this time is later than \a t; otherwise returns FALSE.
1547*/
1548
1549/*!
1550 \fn bool QTime::operator>=( const QTime &t ) const
1551
1552 Returns TRUE if this time is later than or equal to \a t;
1553 otherwise returns FALSE.
1554*/
1555
1556
1557
1558/*!
1559 \overload
1560
1561 Returns the current time as reported by the system clock.
1562
1563 Note that the accuracy depends on the accuracy of the underlying
1564 operating system; not all systems provide 1-millisecond accuracy.
1565*/
1566
1567QTime QTime::currentTime()
1568{
1569 return currentTime( Qt::LocalTime );
1570}
1571
1572/*!
1573 Returns the current time as reported by the system clock, for the
1574 TimeSpec \a ts. The default TimeSpec is LocalTime.
1575
1576 Note that the accuracy depends on the accuracy of the underlying
1577 operating system; not all systems provide 1-millisecond accuracy.
1578
1579 \sa Qt::TimeSpec
1580*/
1581QTime QTime::currentTime( Qt::TimeSpec ts )
1582{
1583 QTime t;
1584 currentTime( &t, ts );
1585 return t;
1586}
1587
1588#ifndef QT_NO_DATESTRING
1589/*!
1590 Returns the representation \a s as a QTime using the format \a f,
1591 or an invalid time if this is not possible.
1592
1593 \warning Note that \c Qt::LocalDate cannot be used here.
1594*/
1595QTime QTime::fromString( const QString& s, Qt::DateFormat f )
1596{
1597 if ( ( s.isEmpty() ) || ( f == Qt::LocalDate ) ) {
1598#if defined(QT_CHECK_RANGE)
1599 qWarning( "QTime::fromString: Parameter out of range." );
1600#endif
1601 return QTime();
1602 }
1603
1604 int hour( s.mid( 0, 2 ).toInt() );
1605 int minute( s.mid( 3, 2 ).toInt() );
1606 int second( s.mid( 6, 2 ).toInt() );
1607 int msec( s.mid( 9, 3 ).toInt() );
1608 return QTime( hour, minute, second, msec );
1609}
1610#endif
1611
1612/*!
1613 \internal
1614 \obsolete
1615
1616 Fetches the current time and returns TRUE if the time is within one
1617 minute after midnight, otherwise FALSE. The return value is used by
1618 QDateTime::currentDateTime() to ensure that the date there is correct.
1619*/
1620
1621bool QTime::currentTime( QTime *ct )
1622{
1623 return currentTime( ct, Qt::LocalTime );
1624}
1625
1626
1627/*!
1628 \internal
1629
1630 Fetches the current time, for the TimeSpec \a ts, and returns TRUE
1631 if the time is within one minute after midnight, otherwise FALSE. The
1632 return value is used by QDateTime::currentDateTime() to ensure that
1633 the date there is correct. The default TimeSpec is LocalTime.
1634
1635 \sa Qt::TimeSpec
1636*/
1637bool QTime::currentTime( QTime *ct, Qt::TimeSpec ts )
1638{
1639 if ( !ct ) {
1640#if defined(QT_CHECK_NULL)
1641 qWarning( "QTime::currentTime(QTime *): Null pointer not allowed" );
1642#endif
1643 return FALSE;
1644 }
1645
1646#if defined(Q_OS_WIN32)
1647 SYSTEMTIME t;
1648 if ( ts == Qt::LocalTime ) {
1649 GetLocalTime( &t );
1650 } else {
1651 GetSystemTime( &t );
1652 }
1653 ct->ds = (uint)( MSECS_PER_HOUR*t.wHour + MSECS_PER_MIN*t.wMinute +
1654 1000*t.wSecond + t.wMilliseconds );
1655#elif defined(Q_OS_UNIX)
1656 struct timeval tv;
1657 gettimeofday( &tv, 0 );
1658 time_t ltime = tv.tv_sec;
1659 tm *t;
1660 if ( ts == Qt::LocalTime ) {
1661 t = localtime( &ltime );
1662 } else {
1663 t = gmtime( &ltime );
1664 }
1665 ct->ds = (uint)( MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min +
1666 1000 * t->tm_sec + tv.tv_usec / 1000 );
1667#else
1668 time_t ltime; // no millisecond resolution
1669 ::time( &ltime );
1670 tm *t;
1671 if ( ts == Qt::LocalTime )
1672 localtime( &ltime );
1673 else
1674 gmtime( &ltime );
1675 ct->ds = (uint) ( MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min +
1676 1000 * t->tm_sec );
1677#endif
1678 // 00:00.00 to 00:00.59.999 is considered as "midnight or right after"
1679 return ct->ds < (uint) MSECS_PER_MIN;
1680}
1681
1682/*!
1683 \overload
1684
1685 Returns TRUE if the specified time is valid; otherwise returns
1686 FALSE.
1687
1688 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.
1690
1691 Example:
1692 \code
1693 QTime::isValid(21, 10, 30); // returns TRUE
1694 QTime::isValid(22, 5, 62); // returns FALSE
1695 \endcode
1696*/
1697
1698bool QTime::isValid( int h, int m, int s, int ms )
1699{
1700 return (uint)h < 24 && (uint)m < 60 && (uint)s < 60 && (uint)ms < 1000;
1701}
1702
1703
1704/*!
1705 Sets this time to the current time. This is practical for timing:
1706
1707 \code
1708 QTime t;
1709 t.start(); // start clock
1710 ... // some lengthy task
1711 qDebug( "%d\n", t.elapsed() ); // prints the number of msecs elapsed
1712 \endcode
1713
1714 \sa restart(), elapsed(), currentTime()
1715*/
1716
1717void QTime::start()
1718{
1719 *this = currentTime();
1720}
1721
1722/*!
1723 Sets this time to the current time and returns the number of
1724 milliseconds that have elapsed since the last time start() or
1725 restart() was called.
1726
1727 This function is guaranteed to be atomic and is thus very handy
1728 for repeated measurements. Call start() to start the first
1729 measurement and then restart() for each later measurement.
1730
1731 Note that the counter wraps to zero 24 hours after the last call
1732 to start() or restart().
1733
1734 \warning If the system's clock setting has been changed since the
1735 last time start() or restart() was called, the result is
1736 undefined. This can happen when daylight savings time is turned on
1737 or off.
1738
1739 \sa start(), elapsed(), currentTime()
1740*/
1741
1742int QTime::restart()
1743{
1744 QTime t = currentTime();
1745 int n = msecsTo( t );
1746 if ( n < 0 ) // passed midnight
1747 n += 86400*1000;
1748 *this = t;
1749 return n;
1750}
1751
1752/*!
1753 Returns the number of milliseconds that have elapsed since the
1754 last time start() or restart() was called.
1755
1756 Note that the counter wraps to zero 24 hours after the last call
1757 to start() or restart.
1758
1759 Note that the accuracy depends on the accuracy of the underlying
1760 operating system; not all systems provide 1-millisecond accuracy.
1761
1762 \warning If the system's clock setting has been changed since the
1763 last time start() or restart() was called, the result is
1764 undefined. This can happen when daylight savings time is turned on
1765 or off.
1766
1767 \sa start(), restart()
1768*/
1769
1770int QTime::elapsed() const
1771{
1772 int n = msecsTo( currentTime() );
1773 if ( n < 0 ) // passed midnight
1774 n += 86400*1000;
1775 return n;
1776}
1777
1778
1779/*****************************************************************************
1780 QDateTime member functions
1781 *****************************************************************************/
1782
1783/*!
1784 \class QDateTime qdatetime.h
1785 \reentrant
1786 \brief The QDateTime class provides date and time functions.
1787
1788 \ingroup time
1789 \mainclass
1790
1791 A QDateTime object contains a calendar date and a clock time (a
1792 "datetime"). It is a combination of the QDate and QTime classes.
1793 It can read the current datetime from the system clock. It
1794 provides functions for comparing datetimes and for manipulating a
1795 datetime by adding a number of seconds, days, months or years.
1796
1797 A QDateTime object is typically created either by giving a date
1798 and time explicitly in the constructor, or by using the static
1799 function currentDateTime(), which returns a QDateTime object set
1800 to the system clock's time. The date and time can be changed with
1801 setDate() and setTime(). A datetime can also be set using the
1802 setTime_t() function, which takes a POSIX-standard "number of
1803 seconds since 00:00:00 on January 1, 1970" value. The fromString()
1804 function returns a QDateTime given a string and a date format
1805 which is used to interpret the date within the string.
1806
1807 The date() and time() functions provide access to the date and
1808 time parts of the datetime. The same information is provided in
1809 textual format by the toString() function.
1810
1811 QDateTime provides a full set of operators to compare two
1812 QDateTime objects where smaller means earlier and larger means
1813 later.
1814
1815 You can increment (or decrement) a datetime by a given number of
1816 seconds using addSecs() or days using addDays(). Similarly you can
1817 use addMonths() and addYears(). The daysTo() function returns the
1818 number of days between two datetimes, and secsTo() returns the
1819 number of seconds between two datetimes.
1820
1821 The range of a datetime object is constrained to the ranges of the
1822 QDate and QTime objects which it embodies.
1823
1824 \sa QDate QTime QDateTimeEdit
1825*/
1826
1827
1828/*!
1829 \fn QDateTime::QDateTime()
1830
1831 Constructs a null datetime (i.e. null date and null time). A null
1832 datetime is invalid, since the date is invalid.
1833
1834 \sa isValid()
1835*/
1836
1837
1838/*!
1839 Constructs a datetime with date \a date and null (but valid) time
1840 (00:00:00.000).
1841*/
1842
1843QDateTime::QDateTime( const QDate &date )
1844 : d(date)
1845{
1846}
1847
1848/*!
1849 Constructs a datetime with date \a date and time \a time.
1850*/
1851
1852QDateTime::QDateTime( const QDate &date, const QTime &time )
1853 : d(date), t(time)
1854{
1855}
1856
1857
1858/*!
1859 \fn bool QDateTime::isNull() const
1860
1861 Returns TRUE if both the date and the time are null; otherwise
1862 returns FALSE. A null datetime is invalid.
1863
1864 \sa QDate::isNull(), QTime::isNull()
1865*/
1866
1867/*!
1868 \fn bool QDateTime::isValid() const
1869
1870 Returns TRUE if both the date and the time are valid; otherwise
1871 returns FALSE.
1872
1873 \sa QDate::isValid(), QTime::isValid()
1874*/
1875
1876/*!
1877 \fn QDate QDateTime::date() const
1878
1879 Returns the date part of the datetime.
1880
1881 \sa setDate(), time()
1882*/
1883
1884/*!
1885 \fn QTime QDateTime::time() const
1886
1887 Returns the time part of the datetime.
1888
1889 \sa setTime(), date()
1890*/
1891
1892/*!
1893 \fn void QDateTime::setDate( const QDate &date )
1894
1895 Sets the date part of this datetime to \a date.
1896
1897 \sa date(), setTime()
1898*/
1899
1900/*!
1901 \fn void QDateTime::setTime( const QTime &time )
1902
1903 Sets the time part of this datetime to \a time.
1904
1905 \sa time(), setDate()
1906*/
1907
1908
1909/*!
1910 Returns the datetime as the number of seconds that have passed
1911 since 1970-01-01T00:00:00, Coordinated Universal Time (UTC).
1912
1913 On systems that do not support timezones, this function will
1914 behave as if local time were UTC.
1915
1916 \sa setTime_t()
1917*/
1918
1919uint QDateTime::toTime_t() const
1920{
1921 tm brokenDown;
1922 brokenDown.tm_sec = t.second();
1923 brokenDown.tm_min = t.minute();
1924 brokenDown.tm_hour = t.hour();
1925 brokenDown.tm_mday = d.day();
1926 brokenDown.tm_mon = d.month() - 1;
1927 brokenDown.tm_year = d.year() - 1900;
1928 brokenDown.tm_isdst = -1;
1929 int secsSince1Jan1970UTC = (int) mktime( &brokenDown );
1930 if ( secsSince1Jan1970UTC < -1 )
1931 secsSince1Jan1970UTC = -1;
1932 return (uint) secsSince1Jan1970UTC;
1933}
1934
1935/*!
1936 \overload
1937
1938 Convenience function that sets the date and time to local time
1939 based on the given UTC time.
1940*/
1941
1942void QDateTime::setTime_t( uint secsSince1Jan1970UTC )
1943{
1944 setTime_t( secsSince1Jan1970UTC, Qt::LocalTime );
1945}
1946
1947/*!
1948 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
1950 1970-01-01T00:00:00, Coordinated Universal Time (UTC). On systems
1951 that do not support timezones this function will behave as if
1952 local time were UTC.
1953
1954 On Windows, only a subset of \a secsSince1Jan1970UTC values are
1955 supported, as Windows starts counting from 1980.
1956
1957 \sa toTime_t()
1958*/
1959void QDateTime::setTime_t( uint secsSince1Jan1970UTC, Qt::TimeSpec ts )
1960{
1961 time_t tmp = (time_t) secsSince1Jan1970UTC;
1962 tm *brokenDown = 0;
1963 if ( ts == Qt::LocalTime )
1964 brokenDown = localtime( &tmp );
1965 if ( !brokenDown ) {
1966 brokenDown = gmtime( &tmp );
1967 if ( !brokenDown ) {
1968 d.jd = QDate::gregorianToJulian( 1970, 1, 1 );
1969 t.ds = 0;
1970 return;
1971 }
1972 }
1973 d.jd = QDate::gregorianToJulian( brokenDown->tm_year + 1900,
1974 brokenDown->tm_mon + 1,
1975 brokenDown->tm_mday );
1976 t.ds = MSECS_PER_HOUR * brokenDown->tm_hour +
1977 MSECS_PER_MIN * brokenDown->tm_min +
1978 1000 * brokenDown->tm_sec;
1979}
1980#ifndef QT_NO_DATESTRING
1981#ifndef QT_NO_SPRINTF
1982/*!
1983 \overload
1984
1985 Returns the datetime as a string. The \a f parameter determines
1986 the format of the string.
1987
1988 If \a f is \c Qt::TextDate, the string format is "Wed May 20
1989 03:40:13 1998" (using QDate::shortDayName(), QDate::shortMonthName(),
1990 and QTime::toString() to generate the string, so the day and month
1991 names will have localized names).
1992
1993 If \a f is \c Qt::ISODate, the string format corresponds to the
1994 ISO 8601 extended specification for representations of dates and
1995 times, which is YYYY-MM-DDTHH:MM:SS.
1996
1997 If \a f is \c Qt::LocalDate, the string format depends on the
1998 locale settings of the system.
1999
2000 If the format \a f is invalid, toString() returns a null string.
2001
2002 \sa QDate::toString() QTime::toString()
2003*/
2004
2005QString QDateTime::toString( Qt::DateFormat f ) const
2006{
2007 if ( f == Qt::ISODate ) {
2008 return d.toString( Qt::ISODate ) + "T" + t.toString( Qt::ISODate );
2009 }
2010#ifndef QT_NO_TEXTDATE
2011 else if ( f == Qt::TextDate ) {
2012#ifndef Q_WS_WIN
2013 QString buf = d.shortDayName( d.dayOfWeek() );
2014 buf += ' ';
2015 buf += d.shortMonthName( d.month() );
2016 buf += ' ';
2017 buf += QString().setNum( d.day() );
2018 buf += ' ';
2019#else
2020 QString buf;
2021 QString winstr;
2022 QT_WA( {
2023 TCHAR out[255];
2024 GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_ILDATE, out, 255 );
2025 winstr = QString::fromUcs2( (ushort*)out );
2026 } , {
2027 char out[255];
2028 GetLocaleInfoA( LOCALE_USER_DEFAULT, LOCALE_ILDATE, (char*)&out, 255 );
2029 winstr = QString::fromLocal8Bit( out );
2030 } );
2031 switch ( winstr.toInt() ) {
2032 case 1:
2033 buf = d.shortDayName( d.dayOfWeek() ) + " " + QString().setNum( d.day() ) + ". " + d.shortMonthName( d.month() ) + " ";
2034 break;
2035 default:
2036 buf = d.shortDayName( d.dayOfWeek() ) + " " + d.shortMonthName( d.month() ) + " " + QString().setNum( d.day() ) + " ";
2037 break;
2038 }
2039#endif
2040 buf += t.toString();
2041 buf += ' ';
2042 buf += QString().setNum( d.year() );
2043 return buf;
2044 }
2045#endif
2046 else if ( f == Qt::LocalDate ) {
2047 return d.toString( Qt::LocalDate ) + " " + t.toString( Qt::LocalDate );
2048 }
2049 return QString::null;
2050}
2051#endif
2052
2053/*!
2054 Returns the datetime as a string. The \a format parameter
2055 determines the format of the result string.
2056
2057 These expressions may be used for the date:
2058
2059 \table
2060 \header \i Expression \i Output
2061 \row \i d \i the day as number without a leading zero (1-31)
2062 \row \i dd \i the day as number with a leading zero (01-31)
2063 \row \i ddd
2064 \i the abbreviated localized day name (e.g. 'Mon'..'Sun').
2065 Uses QDate::shortDayName().
2066 \row \i dddd
2067 \i the long localized day name (e.g. 'Monday'..'Sunday').
2068 Uses QDate::longDayName().
2069 \row \i M \i the month as number without a leading zero (1-12)
2070 \row \i MM \i the month as number with a leading zero (01-12)
2071 \row \i MMM
2072 \i the abbreviated localized month name (e.g. 'Jan'..'Dec').
2073 Uses QDate::shortMonthName().
2074 \row \i MMMM
2075 \i the long localized month name (e.g. 'January'..'December').
2076 Uses QDate::longMonthName().
2077 \row \i yy \i the year as two digit number (00-99)
2078 \row \i yyyy \i the year as four digit number (1752-8000)
2079 \endtable
2080
2081 These expressions may be used for the time:
2082
2083 \table
2084 \header \i Expression \i Output
2085 \row \i h
2086 \i the hour without a leading zero (0..23 or 1..12 if AM/PM display)
2087 \row \i hh
2088 \i the hour with a leading zero (00..23 or 01..12 if AM/PM display)
2089 \row \i m \i the minute without a leading zero (0..59)
2090 \row \i mm \i the minute with a leading zero (00..59)
2091 \row \i s \i the second whithout a leading zero (0..59)
2092 \row \i ss \i the second whith a leading zero (00..59)
2093 \row \i z \i the milliseconds without leading zeroes (0..999)
2094 \row \i zzz \i the milliseconds with leading zeroes (000..999)
2095 \row \i AP
2096 \i use AM/PM display. \e AP will be replaced by either "AM" or "PM".
2097 \row \i ap
2098 \i use am/pm display. \e ap will be replaced by either "am" or "pm".
2099 \endtable
2100
2101 All other input characters will be ignored.
2102
2103 Example format strings (assumed that the QDateTime is
2104 21<small><sup>st</sup></small> May 2001 14:13:09)
2105
2106 \table
2107 \header \i Format \i Result
2108 \row \i dd.MM.yyyy \i11 21.05.2001
2109 \row \i ddd MMMM d yy \i11 Tue May 21 01
2110 \row \i hh:mm:ss.zzz \i11 14:13:09.042
2111 \row \i h:m:s ap \i11 2:13:9 pm
2112 \endtable
2113
2114 \sa QDate::toString() QTime::toString()
2115*/
2116QString QDateTime::toString( const QString& format ) const
2117{
2118 return fmtDateTime( format, &t, &d );
2119}
2120#endif //QT_NO_DATESTRING
2121
2122/*!
2123 Returns a QDateTime object containing a datetime \a ndays days
2124 later than the datetime of this object (or earlier if \a ndays is
2125 negative).
2126
2127 \sa daysTo(), addMonths(), addYears(), addSecs()
2128*/
2129
2130QDateTime QDateTime::addDays( int ndays ) const
2131{
2132 return QDateTime( d.addDays(ndays), t );
2133}
2134
2135/*!
2136 Returns a QDateTime object containing a datetime \a nmonths months
2137 later than the datetime of this object (or earlier if \a nmonths
2138 is negative).
2139
2140 \sa daysTo(), addDays(), addYears(), addSecs()
2141*/
2142
2143QDateTime QDateTime::addMonths( int nmonths ) const
2144{
2145 return QDateTime( d.addMonths(nmonths), t );
2146}
2147
2148/*!
2149 Returns a QDateTime object containing a datetime \a nyears years
2150 later than the datetime of this object (or earlier if \a nyears is
2151 negative).
2152
2153 \sa daysTo(), addDays(), addMonths(), addSecs()
2154*/
2155
2156QDateTime QDateTime::addYears( int nyears ) const
2157{
2158 return QDateTime( d.addYears(nyears), t );
2159}
2160
2161/*!
2162 Returns a QDateTime object containing a datetime \a nsecs seconds
2163 later than the datetime of this object (or earlier if \a nsecs is
2164 negative).
2165
2166 \sa secsTo(), addDays(), addMonths(), addYears()
2167*/
2168
2169QDateTime QDateTime::addSecs( int nsecs ) const
2170{
2171 uint dd = d.jd;
2172 int tt = t.ds;
2173 int sign = 1;
2174 if ( nsecs < 0 ) {
2175 nsecs = -nsecs;
2176 sign = -1;
2177 }
2178 if ( nsecs >= (int)SECS_PER_DAY ) {
2179 dd += sign*(nsecs/SECS_PER_DAY);
2180 nsecs %= SECS_PER_DAY;
2181 }
2182 tt += sign*nsecs*1000;
2183 if ( tt < 0 ) {
2184 tt = MSECS_PER_DAY - tt - 1;
2185 dd -= tt / MSECS_PER_DAY;
2186 tt = tt % MSECS_PER_DAY;
2187 tt = MSECS_PER_DAY - tt - 1;
2188 } else if ( tt >= (int)MSECS_PER_DAY ) {
2189 dd += ( tt / MSECS_PER_DAY );
2190 tt = tt % MSECS_PER_DAY;
2191 }
2192 QDateTime ret;
2193 ret.t.ds = tt;
2194 ret.d.jd = dd;
2195 return ret;
2196}
2197
2198/*!
2199 Returns the number of days from this datetime to \a dt (which is
2200 negative if \a dt is earlier than this datetime).
2201
2202 \sa addDays(), secsTo()
2203*/
2204
2205int QDateTime::daysTo( const QDateTime &dt ) const
2206{
2207 return d.daysTo( dt.d );
2208}
2209
2210/*!
2211 Returns the number of seconds from this datetime to \a dt (which
2212 is negative if \a dt is earlier than this datetime).
2213
2214 Example:
2215 \code
2216 QDateTime dt = QDateTime::currentDateTime();
2217 QDateTime xmas( QDate(dt.year(),12,24), QTime(17,00) );
2218 qDebug( "There are %d seconds to Christmas", dt.secsTo(xmas) );
2219 \endcode
2220
2221 \sa addSecs(), daysTo(), QTime::secsTo()
2222*/
2223
2224int QDateTime::secsTo( const QDateTime &dt ) const
2225{
2226 return t.secsTo(dt.t) + d.daysTo(dt.d)*SECS_PER_DAY;
2227}
2228
2229
2230/*!
2231 Returns TRUE if this datetime is equal to \a dt; otherwise returns FALSE.
2232
2233 \sa operator!=()
2234*/
2235
2236bool QDateTime::operator==( const QDateTime &dt ) const
2237{
2238 return t == dt.t && d == dt.d;
2239}
2240
2241/*!
2242 Returns TRUE if this datetime is different from \a dt; otherwise
2243 returns FALSE.
2244
2245 \sa operator==()
2246*/
2247
2248bool QDateTime::operator!=( const QDateTime &dt ) const
2249{
2250 return t != dt.t || d != dt.d;
2251}
2252
2253/*!
2254 Returns TRUE if this datetime is earlier than \a dt; otherwise
2255 returns FALSE.
2256*/
2257
2258bool QDateTime::operator<( const QDateTime &dt ) const
2259{
2260 if ( d < dt.d )
2261 return TRUE;
2262 return d == dt.d ? t < dt.t : FALSE;
2263}
2264
2265/*!
2266 Returns TRUE if this datetime is earlier than or equal to \a dt;
2267 otherwise returns FALSE.
2268*/
2269
2270bool QDateTime::operator<=( const QDateTime &dt ) const
2271{
2272 if ( d < dt.d )
2273 return TRUE;
2274 return d == dt.d ? t <= dt.t : FALSE;
2275}
2276
2277/*!
2278 Returns TRUE if this datetime is later than \a dt; otherwise
2279 returns FALSE.
2280*/
2281
2282bool QDateTime::operator>( const QDateTime &dt ) const
2283{
2284 if ( d > dt.d )
2285 return TRUE;
2286 return d == dt.d ? t > dt.t : FALSE;
2287}
2288
2289/*!
2290 Returns TRUE if this datetime is later than or equal to \a dt;
2291 otherwise returns FALSE.
2292*/
2293
2294bool QDateTime::operator>=( const QDateTime &dt ) const
2295{
2296 if ( d > dt.d )
2297 return TRUE;
2298 return d == dt.d ? t >= dt.t : FALSE;
2299}
2300
2301/*!
2302 \overload
2303
2304 Returns the current datetime, as reported by the system clock.
2305
2306 \sa QDate::currentDate(), QTime::currentTime()
2307*/
2308
2309QDateTime QDateTime::currentDateTime()
2310{
2311 return currentDateTime( Qt::LocalTime );
2312}
2313
2314/*!
2315 Returns the current datetime, as reported by the system clock, for the
2316 TimeSpec \a ts. The default TimeSpec is LocalTime.
2317
2318 \sa QDate::currentDate(), QTime::currentTime(), Qt::TimeSpec
2319*/
2320
2321QDateTime QDateTime::currentDateTime( Qt::TimeSpec ts )
2322{
2323 QDateTime dt;
2324 QTime t;
2325 dt.setDate( QDate::currentDate(ts) );
2326 if ( QTime::currentTime(&t, ts) ) // midnight or right after?
2327 dt.setDate( QDate::currentDate(ts) ); // fetch date again
2328 dt.setTime( t );
2329 return dt;
2330}
2331
2332#ifndef QT_NO_DATESTRING
2333/*!
2334 Returns the QDateTime represented by the string \a s, using the
2335 format \a f, or an invalid datetime if this is not possible.
2336
2337 Note for \c Qt::TextDate: It is recommended that you use the
2338 English short month names (e.g. "Jan"). Although localized month
2339 names can also be used, they depend on the user's locale settings.
2340
2341 \warning Note that \c Qt::LocalDate cannot be used here.
2342*/
2343QDateTime QDateTime::fromString( const QString& s, Qt::DateFormat f )
2344{
2345 if ( ( s.isEmpty() ) || ( f == Qt::LocalDate ) ) {
2346#if defined(QT_CHECK_RANGE)
2347 qWarning( "QDateTime::fromString: Parameter out of range" );
2348#endif
2349 return QDateTime();
2350 }
2351 if ( f == Qt::ISODate ) {
2352 return QDateTime( QDate::fromString( s.mid(0,10), Qt::ISODate ),
2353 QTime::fromString( s.mid(11), Qt::ISODate ) );
2354 }
2355#if !defined(QT_NO_REGEXP) && !defined(QT_NO_TEXTDATE)
2356 else if ( f == Qt::TextDate ) {
2357 QString monthName( s.mid( 4, 3 ) );
2358 int month = -1;
2359 // Assume that English monthnames are the default
2360 for ( int i = 0; i < 12; ++i ) {
2361 if ( monthName == qt_shortMonthNames[i] ) {
2362 month = i + 1;
2363 break;
2364 }
2365 }
2366 // If English names can't be found, search the localized ones
2367 if ( month == -1 ) {
2368 for ( int i = 1; i <= 12; ++i ) {
2369 if ( monthName == QDate::shortMonthName( i ) ) {
2370 month = i;
2371 break;
2372 }
2373 }
2374 }
2375#if defined(QT_CHECK_RANGE)
2376 if ( month < 1 || month > 12 ) {
2377 qWarning( "QDateTime::fromString: Parameter out of range." );
2378 month = 1;
2379 }
2380#endif
2381 int day = s.mid( 8, 2 ).simplifyWhiteSpace().toInt();
2382 int year = s.right( 4 ).toInt();
2383 QDate date( year, month, day );
2384 QTime time;
2385 int hour, minute, second;
2386 int pivot = s.find( QRegExp("[0-9][0-9]:[0-9][0-9]:[0-9][0-9]") );
2387 if ( pivot != -1 ) {
2388 hour = s.mid( pivot, 2 ).toInt();
2389 minute = s.mid( pivot+3, 2 ).toInt();
2390 second = s.mid( pivot+6, 2 ).toInt();
2391 time.setHMS( hour, minute, second );
2392 }
2393 return QDateTime( date, time );
2394 }
2395#endif //QT_NO_REGEXP
2396 return QDateTime();
2397}
2398#endif //QT_NO_DATESTRING
2399
2400
2401/*****************************************************************************
2402 Date/time stream functions
2403 *****************************************************************************/
2404
2405#ifndef QT_NO_DATASTREAM
2406/*!
2407 \relates QDate
2408
2409 Writes the date, \a d, to the data stream, \a s.
2410
2411 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
2412*/
2413
2414QDataStream &operator<<( QDataStream &s, const QDate &d )
2415{
2416 return s << (Q_UINT32)(d.jd);
2417}
2418
2419/*!
2420 \relates QDate
2421
2422 Reads a date from the stream \a s into \a d.
2423
2424 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
2425*/
2426
2427QDataStream &operator>>( QDataStream &s, QDate &d )
2428{
2429 Q_UINT32 jd;
2430 s >> jd;
2431 d.jd = jd;
2432 return s;
2433}
2434
2435/*!
2436 \relates QTime
2437
2438 Writes time \a t to the stream \a s.
2439
2440 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
2441*/
2442
2443QDataStream &operator<<( QDataStream &s, const QTime &t )
2444{
2445 return s << (Q_UINT32)(t.ds);
2446}
2447
2448/*!
2449 \relates QTime
2450
2451 Reads a time from the stream \a s into \a t.
2452
2453 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
2454*/
2455
2456QDataStream &operator>>( QDataStream &s, QTime &t )
2457{
2458 Q_UINT32 ds;
2459 s >> ds;
2460 t.ds = ds;
2461 return s;
2462}
2463
2464/*!
2465 \relates QDateTime
2466
2467 Writes the datetime \a dt to the stream \a s.
2468
2469 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
2470*/
2471
2472QDataStream &operator<<( QDataStream &s, const QDateTime &dt )
2473{
2474 return s << dt.d << dt.t;
2475}
2476
2477/*!
2478 \relates QDateTime
2479
2480 Reads a datetime from the stream \a s into \a dt.
2481
2482 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
2483*/
2484
2485QDataStream &operator>>( QDataStream &s, QDateTime &dt )
2486{
2487 s >> dt.d >> dt.t;
2488 return s;
2489}
2490#endif //QT_NO_DATASTREAM