Skip to content
Permalink
Newer
Older
100644 1455 lines (1130 sloc) 33.2 KB
Oct 15, 2000
1
/****************************************************************************
Apr 22, 2001
2
**
Oct 15, 2000
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
#define gettimeofday __hide_gettimeofday
39
#include "qdatetime.h"
40
#include "qdatastream.h"
41
#include <stdio.h>
42
#include <time.h>
43
#if defined(_OS_WIN32_)
44
#if defined(_CC_BOOL_DEF_)
45
#undef bool
46
#include <windows.h>
47
#define bool int
48
#else
49
#include <windows.h>
50
#endif
51
#elif defined(_OS_MSDOS_)
52
#include <dos.h>
53
#elif defined(_OS_OS2_)
54
#include <os2.h>
55
#elif defined(_OS_UNIX_) || defined(_OS_MAC_)
Oct 15, 2000
56
#include <sys/time.h>
57
#include <unistd.h>
58
#undef gettimeofday
59
extern "C" int gettimeofday( struct timeval *, struct timezone * );
60
#endif
61
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;
70
71
static const short monthDays[] ={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
72
73
// ##### Localize.
74
75
const char * const QDate::monthNames[] = {
76
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
77
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
78
79
const char * const QDate::weekdayNames[] ={
80
"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
81
82
83
/*****************************************************************************
84
QDate member functions
85
*****************************************************************************/
86
87
// REVISED: aavit
88
89
/*!
90
\class QDate qdatetime.h
91
\brief The QDate class provides date functions.
92
93
\ingroup time
94
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
99
days.
100
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
105
setYMD().
106
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.
111
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
114
than the other.
115
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.
119
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.
123
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.
129
130
The end of time is reached around 8000AD, by which time we expect Qt
131
to be obsolete.
132
133
\sa QTime, QDateTime
134
*/
135
136
137
/*!
138
\fn QDate::QDate()
139
Constructs a null date. Null dates are invalid.
140
141
\sa isNull(), isValid()
142
*/
143
144
145
/*!
146
Constructs a date with the year \a y, month \a m and day \a d.
147
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.
151
152
\sa isValid()
153
*/
154
155
QDate::QDate( int y, int m, int d )
156
{
157
jd = 0;
158
setYMD( y, m, d );
159
}
160
161
162
/*!
163
\fn bool QDate::isNull() const
164
165
Returns TRUE if the date is null. A null date is invalid.
166
167
\sa isValid()
168
*/
169
170
171
/*!
172
Returns TRUE if this date is valid.
173
174
\sa isNull()
175
*/
176
177
bool QDate::isValid() const
178
{
179
return jd >= FIRST_DAY;
180
}
181
182
183
/*!
184
Returns the year (>= 1752) of this date.
185
186
\sa month(), day()
187
*/
188
189
int QDate::year() const
190
{
191
int y, m, d;
192
jul2greg( jd, y, m, d );
193
return y;
194
}
195
196
/*!
197
Returns the month (January=1 .. December=12) of this date.
198
199
\sa year(), day()
200
*/
201
202
int QDate::month() const
203
{
204
int y, m, d;
205
jul2greg( jd, y, m, d );
206
return m;
207
}
208
209
/*!
210
Returns the day of the month (1..31) of this date.
211
212
\sa year(), month(), dayOfWeek()
213
*/
214
215
int QDate::day() const
216
{
217
int y, m, d;
218
jul2greg( jd, y, m, d );
219
return d;
220
}
221
222
/*!
223
Returns the weekday (Monday=1 .. Sunday=7) for this date.
224
225
\sa day(), dayOfYear()
226
*/
227
228
int QDate::dayOfWeek() const
229
{
230
return (((jd+1) % 7) + 6)%7 + 1;
231
}
232
233
/*!
234
Returns the day of the year (1..365) for this date.
235
236
\sa day(), dayOfWeek()
237
*/
238
239
int QDate::dayOfYear() const
240
{
241
return jd - greg2jul(year(), 1, 1) + 1;
242
}
243
244
/*!
245
Returns the number of days in the month (28..31) for this date.
246
247
\sa day(), daysInYear()
248
*/
249
250
int QDate::daysInMonth() const
251
{
252
int y, m, d;
253
jul2greg( jd, y, m, d );
254
if ( m == 2 && leapYear(y) )
255
return 29;
256
else
257
return monthDays[m];
258
}
259
260
/*!
261
Returns the number of days in the year (365 or 366) for this date.
262
263
\sa day(), daysInMonth()
264
*/
265
266
int QDate::daysInYear() const
267
{
268
int y, m, d;
269
jul2greg( jd, y, m, d );
270
return leapYear(y) ? 366 : 365;
271
}
272
273
274
/*!
275
Returns the name of the \a month.
276
277
Month 1 == "Jan", month 2 == "Feb" etc.
278
279
\sa toString(), dayName()
280
*/
281
282
QCString QDate::monthName( int month ) const
Oct 15, 2000
283
{
284
#if defined(CHECK_RANGE)
285
if ( month < 1 || month > 12 ) {
286
qWarning( "QDate::monthName: Parameter out ouf range." );
287
month = 1;
288
}
289
#endif
290
return monthNames[month-1];
Oct 15, 2000
291
}
292
293
/*!
294
Returns the name of the \a weekday.
295
296
Weekday 1 == "Mon", day 2 == "Tue" etc.
297
298
\sa toString(), monthName()
299
*/
300
301
QCString QDate::dayName( int weekday ) const
Oct 15, 2000
302
{
303
#if defined(CHECK_RANGE)
304
if ( weekday < 1 || weekday > 7 ) {
305
qWarning( "QDate::dayName: Parameter out of range." );
306
weekday = 1;
307
}
308
#endif
309
return weekdayNames[weekday-1];
Oct 15, 2000
310
}
311
312
313
/*!
314
Returns the date as a string.
315
316
The string format is "Sat May 20 1995". This function uses the
317
dayName() and monthName() functions to generate the string.
318
319
\sa dayName(), monthName()
320
*/
321
322
QCString QDate::toString() const
Oct 15, 2000
323
{
324
int y, m, d;
325
jul2greg( jd, y, m, d );
326
QCString buf = dayName(dayOfWeek());
Oct 15, 2000
327
buf += ' ';
328
buf += monthName(m);
Oct 15, 2000
330
t.sprintf( " %d %d", d, y);
331
buf += t;
332
return buf;
333
}
334
335
336
/*!
337
Sets the year \a y, month \a m and day \a d.
338
339
\a y must be in the range 1752-ca. 8000, \a m must be in the range
340
1-12, and \a d must be in the range 1-31. Exception: if \a y is in
341
the range 0-99, it is interpreted as 1900-1999.
342
343
Returns TRUE if the date is valid, otherwise FALSE.
344
*/
345
346
bool QDate::setYMD( int y, int m, int d )
347
{
348
if ( !isValid(y,m,d) ) {
349
#if defined(CHECK_RANGE)
350
qWarning( "QDate::setYMD: Invalid date %04d/%02d/%02d", y, m, d );
351
#endif
352
return FALSE;
353
}
354
jd = greg2jul( y, m, d );
355
#if defined(DEBUG)
356
ASSERT( year() == (y > 99 ? y : 1900+y) && month() == m && day() == d );
Oct 15, 2000
357
#endif
358
return TRUE;
359
}
360
361
/*!
362
Returns a QDate object containing a date \a ndays later than the
363
date of this object (or earlier if \a ndays is negative).
364
365
\sa daysTo()
366
*/
367
368
QDate QDate::addDays( int ndays ) const
369
{
370
QDate d;
371
d.jd = jd + ndays;
372
return d;
373
}
374
375
/*!
376
Returns the number of days from this date to \a d (which is negative
377
if \a d is earlier than this date).
378
379
Example:
380
\code
381
QDate d1( 1995, 5, 17 ); // May 17th 1995
382
QDate d2( 1995, 5, 20 ); // May 20th 1995
383
d1.daysTo( d2 ); // returns 3
384
d2.daysTo( d1 ); // returns -3
385
\endcode
386
387
\sa addDays()
388
*/
389
390
int QDate::daysTo( const QDate &d ) const
391
{
392
return d.jd - jd;
393
}
394
395
396
/*!
397
\fn bool QDate::operator==( const QDate &d ) const
398
Returns TRUE if this date is equal to \a d, or FALSE if
399
they are different.
400
*/
401
402
/*!
403
\fn bool QDate::operator!=( const QDate &d ) const
404
Returns TRUE if this date is different from \a d, or FALSE if
405
they are equal.
406
*/
407
408
/*!
409
\fn bool QDate::operator<( const QDate &d ) const
410
Returns TRUE if this date is earlier than \a d, otherwise FALSE.
411
*/
412
413
/*!
414
\fn bool QDate::operator<=( const QDate &d ) const
415
Returns TRUE if this date is earlier than or equal to \a d, otherwise FALSE.
416
*/
417
418
/*!
419
\fn bool QDate::operator>( const QDate &d ) const
420
Returns TRUE if this date is later than \a d, otherwise FALSE.
421
*/
422
423
/*!
424
\fn bool QDate::operator>=( const QDate &d ) const
425
Returns TRUE if this date is later than or equal to \a d, otherwise FALSE.
426
*/
427
428
429
/*!
430
Returns the current date, as reported by the system clock.
431
432
\sa QTime::currentTime(), QDateTime::currentDateTime()
433
*/
434
435
QDate QDate::currentDate()
436
{
437
#if defined(_OS_WIN32_)
438
439
SYSTEMTIME t;
440
GetLocalTime( &t );
441
QDate d;
442
d.jd = greg2jul( t.wYear, t.wMonth, t.wDay );
443
return d;
444
445
#else
446
447
time_t ltime;
448
time( &ltime );
449
tm *t = localtime( &ltime );
450
QDate d;
451
d.jd = greg2jul( t->tm_year + 1900, t->tm_mon + 1, t->tm_mday );
452
return d;
453
454
#endif
455
}
456
457
/*!
458
Returns TRUE if the specified date (year \a y, month \a m and day \a
459
d) is valid.
460
461
Example:
462
\code
463
QDate::isValid( 2002, 5, 17 ); // TRUE; May 17th 2002 is OK.
464
QDate::isValid( 2002, 2, 30 ); // FALSE; Feb 30th does not exist
465
QDate::isValid( 2004, 2, 29 ); // TRUE; 2004 is a leap year
466
QDate::isValid( 1202, 6, 6 ); // FALSE; 1202 is pre-Gregorian
467
\endcode
468
469
Note that a \a y value in the range 00-99 is interpreted as
470
1900-1999.
471
472
\sa isNull(), setYMD()
473
*/
474
475
bool QDate::isValid( int y, int m, int d )
476
{
477
if ( y >= 0 && y <= 99 )
478
y += 1900;
479
else if ( y < FIRST_YEAR || (y == FIRST_YEAR && (m < 9 ||
480
(m == 9 && d < 14))) )
481
return FALSE;
482
return (d > 0 && m > 0 && m <= 12) &&
483
(d <= monthDays[m] || (d == 29 && m == 2 && leapYear(y)));
484
}
485
486
/*!
487
Returns TRUE if the specified year \a y is a leap year.
488
*/
489
490
bool QDate::leapYear( int y )
491
{
Mar 24, 2010
492
return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
Oct 15, 2000
493
}
494
495
/*!
496
\internal
497
Converts a Gregorian date to a Julian day.
498
This algorithm is taken from Communications of the ACM, Vol 6, No 8.
499
\sa jul2greg()
500
*/
501
502
uint QDate::greg2jul( int y, int m, int d )
503
{
504
uint c, ya;
505
if ( y <= 99 )
506
y += 1900;
507
if ( m > 2 ) {
508
m -= 3;
509
} else {
510
m += 9;
511
y--;
512
}
513
c = y; // NOTE: Sym C++ 6.0 bug
514
c /= 100;
515
ya = y - 100*c;
516
return 1721119 + d + (146097*c)/4 + (1461*ya)/4 + (153*m+2)/5;
517
}
518
519
/*!
520
\internal
521
Converts a Julian day to a Gregorian date.
522
This algorithm is taken from Communications of the ACM, Vol 6, No 8.
523
\sa greg2jul()
524
*/
525
526
void QDate::jul2greg( uint jd, int &y, int &m, int &d )
527
{
528
uint x;
529
uint j = jd - 1721119;
530
y = (j*4 - 1)/146097;
531
j = j*4 - 146097*y - 1;
532
x = j/4;
533
j = (x*4 + 3) / 1461;
534
y = 100*y + j;
535
x = (x*4) + 3 - 1461*j;
536
x = (x + 4)/4;
537
m = (5*x - 3)/153;
538
x = 5*x - 3 - 153*m;
539
d = (x + 5)/5;
540
if ( m < 10 ) {
541
m += 3;
542
} else {
543
m -= 9;
544
y++;
545
}
546
}
547
548
549
/*****************************************************************************
550
QTime member functions
551
*****************************************************************************/
552
553
/*!
554
\class QTime qdatetime.h
555
556
\brief The QTime class provides clock time functions.
557
558
\ingroup time
559
560
A QTime object contains a clock time, i.e. a number of hours,
561
minutes, seconds and milliseconds since midnight. It can read the
562
current time from the system clock, and measure a span of elapsed
563
time. It provides functions for comparing times and for manipulating
564
a time by adding a number of (milli)seconds.
565
566
QTime operates with 24-hour clock format; it has no concept of
567
AM/PM. It operates with local time; it does not know anything about
568
time zones or daylight savings time.
569
570
A QTime object is typically created either by giving the number of
571
hours, minutes, seconds, and milliseconds explicitly, or by using
572
the static function currentTime(), which makes a QTime object which
573
contains the system's clock time. Note that the accuracy depends on
574
the accuracy of the underlying operating system; not all systems
575
provide 1-millisecond accuracy.
576
577
The hour(), minute(), second(), and msec() functions provide access
578
to the number of hours, minutes, seconds, and milliseconds of the
579
time. The same information is provided in textual format by the
580
toString() function.
581
582
QTime provides a full set of operators to compare two QTime
583
objects. A time is considered smaller than another if it is earlier
584
than the other.
585
586
The time a given number of seconds or milliseconds later than a
587
given time can be found using the addSecs() or addMSecs()
588
functions. Correspondingly, the number of (milli)seconds between two
589
times can be found using the secsTo() or msecsTo() functions.
590
591
QTime can be used to measure a span of elapsed time using the
592
start(), restart(), and elapsed() functions.
593
594
\sa QDate, QDateTime
595
*/
596
597
/*!
598
\fn QTime::QTime()
599
600
Constructs the time 0 hours, minutes, seconds and milliseconds,
601
i.e. 00:00:00.000 (midnight). This is a valid time.
602
603
\sa isValid()
604
*/
605
606
/*!
607
Constructs a time with hour \a h, minute \a m, seconds \a s and
608
milliseconds \a ms.
609
610
\a h must be in the range 0-23, \a m and \a s must be in the range
611
0-59, and \a ms must be in the range 0-999.
612
613
\sa isValid()
614
*/
615
616
QTime::QTime( int h, int m, int s, int ms )
617
{
618
setHMS( h, m, s, ms );
619
}
620
621
622
/*!
623
\fn bool QTime::isNull() const
624
Returns TRUE if the time is equal to 00:00:00.000. A null time is valid.
625
626
\sa isValid()
627
*/
628
629
/*!
630
Returns TRUE if the time is valid, or FALSE if the time is invalid.
631
The time 23:30:55.746 is valid, while 24:12:30 is invalid.
632
633
\sa isNull()
634
*/
635
636
bool QTime::isValid() const
637
{
638
return ds < MSECS_PER_DAY;
639
}
640
641
642
/*!
643
Returns the hour part (0..23) of the time.
644
*/
645
646
int QTime::hour() const
647
{
648
return ds / MSECS_PER_HOUR;
649
}
650
651
/*!
652
Returns the minute part (0..59) of the time.
653
*/
654
655
int QTime::minute() const
656
{
657
return (ds % MSECS_PER_HOUR)/MSECS_PER_MIN;
658
}
659
660
/*!
661
Returns the second part (0..59) of the time.
662
*/
663
664
int QTime::second() const
665
{
666
return (ds / 1000)%SECS_PER_MIN;
667
}
668
669
/*!
670
Returns the millisecond part (0..999) of the time.
671
*/
672
673
int QTime::msec() const
674
{
675
return ds % 1000;
676
}
677
678
679
/*!
680
Returns the time of this object in a textual format. Milliseconds
681
are not included. The string format is HH:MM:SS, e.g. 1 second
682
before midnight would be "23:59:59".
683
*/
684
685
QCString QTime::toString() const
Oct 15, 2000
686
{
Oct 15, 2000
688
buf.sprintf( "%.2d:%.2d:%.2d", hour(), minute(), second() );
689
return buf;
690
}
691
692
693
/*!
694
Sets the time to hour \a h, minute \a m, seconds \a s and
695
milliseconds \a ms.
696
697
\a h must be in the range 0-23, \a m and \a s must be in the range
698
0-59, and \a ms must be in the range 0-999. Returns TRUE if the set
699
time is valid, otherwise FALSE.
700
701
\sa isValid()
702
*/
703
704
bool QTime::setHMS( int h, int m, int s, int ms )
705
{
706
if ( !isValid(h,m,s,ms) ) {
707
#if defined(CHECK_RANGE)
708
qWarning( "QTime::setHMS Invalid time %02d:%02d:%02d.%03d", h, m, s,
709
ms );
710
#endif
711
ds = MSECS_PER_DAY; // make this invalid
712
return FALSE;
713
}
714
ds = (h*SECS_PER_HOUR + m*SECS_PER_MIN + s)*1000 + ms;
715
return TRUE;
716
}
717
718
/*!
719
Returns a QTime object containing a time \a nsecs seconds later than
720
the time of this object (or earlier if \a ms is negative).
721
722
Note that the time will wrap if it passes midnight.
723
724
Example:
725
\code
726
QTime n( 14, 0, 0 ); // n == 14:00:00
727
QTime t;
728
t = n.addSecs( 70 ); // t == 14:01:10
729
t = n.addSecs( -70 ); // t == 13:58:50
730
t = n.addSecs( 10*60*60 + 5 ); // t == 00:00:05
731
t = n.addSecs( -15*60*60 ); // t == 23:00:00
732
\endcode
733
734
\sa addMSecs(), secsTo(), QDateTime::addSecs()
735
*/
736
737
QTime QTime::addSecs( int nsecs ) const
738
{
739
return addMSecs(nsecs*1000);
740
}
741
742
/*!
743
Returns the number of seconds from this time to \a t (which is
744
negative if \a t is earlier than this time).
745
746
Since QTime measures time within a day and there are 86400 seconds
747
in a day, the result is between -86400 and 86400.
748
749
\sa addSecs() QDateTime::secsTo()
750
*/
751
752
int QTime::secsTo( const QTime &t ) const
753
{
754
return ((int)t.ds - (int)ds)/1000;
755
}
756
757
/*!
758
Returns a QTime object containing a time \a ms milliseconds later than
759
the time of this object (or earlier if \a ms is negative).
760
761
Note that the time will wrap if it passes midnight. See addSecs()
762
for an example.
763
764
\sa addSecs(), msecsTo()
765
*/
766
767
QTime QTime::addMSecs( int ms ) const
768
{
769
QTime t;
770
if ( ms < 0 ) {
771
// % not well-defined for -ve, but / is.
772
int negdays = (MSECS_PER_DAY-ms) / MSECS_PER_DAY;
773
t.ds = ((int)ds + ms + negdays*MSECS_PER_DAY)
774
% MSECS_PER_DAY;
775
} else {
776
t.ds = ((int)ds + ms) % MSECS_PER_DAY;
777
}
778
return t;
779
}
780
781
/*!
782
Returns the number of milliseconds from this time to \a t (which is
783
negative if \a t is earlier than this time).
784
785
Since QTime measures time within a day and there are 86400000
786
milliseconds in a day, the result is between -86400000 and 86400000.
787
788
\sa secsTo()
789
*/
790
791
int QTime::msecsTo( const QTime &t ) const
792
{
793
return (int)t.ds - (int)ds;
794
}
795
796
797
/*!
798
\fn bool QTime::operator==( const QTime &t ) const
799
800
Returns TRUE if this time is equal to \a t, or FALSE if they are
801
different.
802
*/
803
804
/*!
805
\fn bool QTime::operator!=( const QTime &t ) const
806
807
Returns TRUE if this time is different from \a t, or FALSE if they
808
are equal.
809
*/
810
811
/*!
812
\fn bool QTime::operator<( const QTime &t ) const
813
814
Returns TRUE if this time is earlier than \a t, otherwise FALSE.
815
*/
816
817
/*!
818
\fn bool QTime::operator<=( const QTime &t ) const
819
820
Returns TRUE if this time is earlier than or equal to \a t,
821
otherwise FALSE.
822
*/
823
824
/*!
825
\fn bool QTime::operator>( const QTime &t ) const
826
827
Returns TRUE if this time is later than \a t, otherwise FALSE.
828
*/
829
830
/*!
831
\fn bool QTime::operator>=( const QTime &t ) const
832
833
Returns TRUE if this time is later than or equal to \a t, otherwise
834
FALSE.
835
*/
836
837
838
839
/*!
840
Returns the current time, as reported by the system clock.
841
842
Note that the accuracy depends on the accuracy of the underlying
843
operating system; not all systems provide 1-millisecond accuracy.
844
*/
845
846
QTime QTime::currentTime()
847
{
848
QTime ct;
849
currentTime( &ct );
850
return ct;
851
}
852
853
/*!
854
\internal
855
856
Fetches the current time and returns TRUE if the time is within one
857
minute after midnight, otherwise FALSE. The return value is used by
858
QDateTime::currentDateTime() to ensure that the date there is correct.
859
*/
860
861
bool QTime::currentTime( QTime *ct )
862
{
863
if ( !ct ) {
864
#if defined(CHECK_NULL)
865
qWarning( "QTime::currentTime(QTime *): Null pointer not allowed" );
866
#endif
867
return FALSE;
868
}
869
870
#if defined(_OS_WIN32_)
871
872
SYSTEMTIME t;
873
GetLocalTime( &t );
874
ct->ds = MSECS_PER_HOUR*t.wHour + MSECS_PER_MIN*t.wMinute +
875
1000*t.wSecond + t.wMilliseconds;
876
return (t.wHour == 0 && t.wMinute == 0);
877
878
#elif defined(_OS_OS2_)
879
880
DATETIME t;
881
DosGetDateTime( &t );
882
ct->ds = MSECS_PER_HOUR*t.hours + MSECS_PER_MIN*t.minutes +
883
1000*t.seconds + 10*t.hundredths;
884
return (t.hours == 0 && t.minutes == 0);
885
886
#elif defined(_OS_MSDOS_)
887
888
_dostime_t t;
889
_dos_gettime( &t );
890
ct->ds = MSECS_PER_HOUR*t.hour + MSECS_PER_MIN*t.minute +
891
t.second*1000 + t.hsecond*10;
892
return (t.hour== 0 && t.minute == 0);
893
894
#elif defined(_OS_UNIX_) || defined(_OS_MAC_)
Oct 15, 2000
895
896
struct timeval tv;
897
gettimeofday( &tv, 0 );
898
time_t ltime = tv.tv_sec;
899
tm *t = localtime( &ltime );
900
ct->ds = (uint)( MSECS_PER_HOUR*t->tm_hour + MSECS_PER_MIN*t->tm_min +
901
1000*t->tm_sec + tv.tv_usec/1000 );
902
return (t->tm_hour== 0 && t->tm_min == 0);
903
904
#else
905
906
time_t ltime; // no millisecond resolution!!
907
::time( &ltime );
908
tm *t = localtime( &ltime );
909
ct->ds = MSECS_PER_HOUR*t->tm_hour + MSECS_PER_MIN*t->tm_min +
910
1000*t->tm_sec;
911
return (t->tm_hour== 0 && t->tm_min == 0);
912
#endif
913
}
914
915
/*!
916
Returns TRUE if the specified time is valid, otherwise FALSE.
917
918
The time is valid if \a h is in the range 0-23, \a m and \a s are in
919
the range 0-59, and \a ms is in the range 0-999.
920
921
Example:
922
\code
923
QTime::isValid(21, 10, 30); // returns TRUE
924
QTime::isValid(22, 5, 62); // returns FALSE
925
\endcode
926
*/
927
928
bool QTime::isValid( int h, int m, int s, int ms )
929
{
930
return (uint)h < 24 && (uint)m < 60 && (uint)s < 60 && (uint)ms < 1000;
931
}
932
933
934
/*!
935
Sets this time to the current time. This is practical for timing:
936
937
\code
938
QTime t;
939
t.start(); // start clock
940
... // some lengthy task
941
qDebug( "%d\n", t.elapsed() ); // prints # msecs elapsed
942
\endcode
943
944
\sa restart(), elapsed(), currentTime()
945
*/
946
947
void QTime::start()
948
{
949
*this = currentTime();
950
}
951
952
/*!
953
Sets this time to the current time, and returns the number of
954
milliseconds that have elapsed since the last time start() or
955
restart() was called.
956
957
This function is guaranteed to be atomic, and is thus very handy for
958
repeated measurements: call start() to start the first measurement,
959
then restart() for each later measurement.
960
961
Note that the counter wraps to zero 24 hours after the last call to
962
start() or restart().
963
964
\warning If the system's clock setting has been changed since the
965
last time start() or restart() was called, the result is undefined.
966
This can happen e.g. when daylight saving is turned on or off.
967
968
\sa start(), elapsed(), currentTime()
969
*/
970
971
int QTime::restart()
972
{
973
QTime t = currentTime();
974
int n = msecsTo( t );
975
if ( n < 0 ) // passed midnight
976
n += 86400*1000;
977
*this = t;
978
return n;
979
}
980
981
/*!
982
Returns the number of milliseconds that have elapsed since the last
983
time start() or restart() was called.
984
985
Note that the counter wraps to zero 24 hours after the last call to
986
start() or restart.
987
988
Note that the accuracy depends on the accuracy of the underlying
989
operating system; not all systems provide 1-millisecond accuracy.
990
991
\warning If the system's clock setting has been changed since the
992
last time start() or restart() was called, the result is undefined.
993
This can happen e.g. when daylight saving is turned on or off.
994
995
\sa start(), restart()
996
*/
997
998
int QTime::elapsed()
999
{
1000
int n = msecsTo( currentTime() );
1001
if ( n < 0 ) // passed midnight
1002
n += 86400*1000;
1003
return n;
1004
}
1005
1006
1007
/*****************************************************************************
1008
QDateTime member functions
1009
*****************************************************************************/
1010
1011
/*!
1012
\class QDateTime qdatetime.h
1013
\brief The QDateTime class provides date and time functions.
1014
1015
\ingroup time
1016
1017
A QDateTime object contains a calendar date and a clock time (a
1018
"datetime"). It is a combination of the QDate and QTime classes. It
1019
can read the current datetime from the system clock. It provides
1020
functions for comparing datetimes and for manipulating a datetime by
1021
adding a number of seconds or days.
1022
1023
A QDateTime object is typically created either by giving a date and
1024
time explicitly, or by using the static function currentTime(),
1025
which makes a QDateTime object which contains the system's clock
1026
time.
1027
1028
The date() and time() functions provide access to the date and time
1029
parts of the datetime. The same information is provided in textual
1030
format by the toString() function.
1031
1032
QDateTime provides a full set of operators to compare two QDateTime
1033
objects. A datetime is considered smaller than another if it is
1034
earlier than the other.
1035
1036
The datetime a given number of days or seconds later than a given
1037
datetime can be found using the addDays() and addSecs()
1038
functions. Correspondingly, the number of days or seconds between
1039
two times can be found using the daysTo() or secsTo() functions.
1040
1041
A datetime can also be set using the setTime_t() function, which
1042
takes a POSIX-standard "number of seconds since 00:00:00 on January
1043
1, 1970" value.
1044
1045
The limitations regarding range and resolution mentioned in the
1046
QDate and QTime documentation apply for QDateTime also.
1047
1048
\sa QDate, QTime
1049
*/
1050
1051
1052
/*!
1053
\fn QDateTime::QDateTime()
1054
1055
Constructs a null datetime (i.e. null date and null time). A null
1056
datetime is invalid, since the date is invalid.
1057
1058
\sa isValid()
1059
*/
1060
1061
1062
/*!
1063
Constructs a datetime with date \a date and null time (00:00:00.000).
1064
*/
1065
1066
QDateTime::QDateTime( const QDate &date )
1067
: d(date)
1068
{
1069
}
1070
1071
/*!
1072
Constructs a datetime with date \a date and time \a time.
1073
*/
1074
1075
QDateTime::QDateTime( const QDate &date, const QTime &time )
1076
: d(date), t(time)
1077
{
1078
}
1079
1080
1081
/*!
1082
\fn bool QDateTime::isNull() const
1083
1084
Returns TRUE if both the date and the time are null. A null date is invalid.
1085
1086
\sa QDate::isNull(), QTime::isNull()
1087
*/
1088
1089
/*!
1090
\fn bool QDateTime::isValid() const
1091
1092
Returns TRUE if both the date and the time are valid.
1093
1094
\sa QDate::isValid(), QTime::isValid()
1095
*/
1096
1097
/*!
1098
\fn QDate QDateTime::date() const
1099
1100
Returns the date part of this datetime.
1101
1102
\sa setDate(), time()
1103
*/
1104
1105
/*!
1106
\fn QTime QDateTime::time() const
1107
1108
Returns the time part of this datetime.
1109
1110
\sa setTime(), date()
1111
*/
1112
1113
/*!
1114
\fn void QDateTime::setDate( const QDate &date )
1115
1116
Sets the date part of this datetime.
1117
1118
\sa date(), setTime()
1119
*/
1120
1121
/*!
1122
\fn void QDateTime::setTime( const QTime &time )
1123
1124
Sets the time part of this datetime.
1125
1126
\sa time(), setDate()
1127
*/
1128
1129
1130
/*!
1131
Sets the local date and time given the number of seconds that have passed
1132
since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC).
1133
On systems that do not support timezones this function will behave as if
1134
local time were UTC.
1135
1136
Note that Microsoft Windows supports only a limited range of values for
1137
\a secsSince1Jan1970UTC.
1138
*/
1139
1140
void QDateTime::setTime_t( uint secsSince1Jan1970UTC )
1141
{
1142
time_t tmp = (time_t) secsSince1Jan1970UTC;
1143
tm *tM = localtime( &tmp );
1144
if ( !tM ) {
1145
tM = gmtime( &tmp );
1146
if ( !tM ) {
1147
d.jd = QDate::greg2jul( 1970, 1, 1 );
1148
t.ds = 0;
1149
return;
1150
}
1151
}
1152
d.jd = QDate::greg2jul( tM->tm_year + 1900, tM->tm_mon + 1, tM->tm_mday );
1153
t.ds = MSECS_PER_HOUR*tM->tm_hour + MSECS_PER_MIN*tM->tm_min +
1154
1000*tM->tm_sec;
1155
}
1156
1157
1158
/*!
1159
Sets the UTC date and time given the number of seconds that have passed
1160
since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC).
1161
1162
Note that Microsoft Windows supports only a limited range of values for
1163
\a secsSince1Jan1970UTC.
1164
*/
1165
1166
void QDateTime::setTimeUtc_t( uint secsSince1Jan1970UTC )
1167
{
1168
time_t tmp = (time_t) secsSince1Jan1970UTC;
1169
tm *tM = gmtime( &tmp );
1170
if ( !tM ) {
1171
d.jd = QDate::greg2jul( 1970, 1, 1 );
1172
t.ds = 0;
1173
return;
1174
}
1175
d.jd = QDate::greg2jul( tM->tm_year + 1900, tM->tm_mon + 1, tM->tm_mday );
1176
t.ds = MSECS_PER_HOUR*tM->tm_hour + MSECS_PER_MIN*tM->tm_min +
1177
1000*tM->tm_sec;
1178
}
1179
1180
Oct 15, 2000
1181
/*!
1182
Returns the datetime as a string.
1183
1184
The string format is "Sat May 20 03:40:13 1998".
1185
1186
This function uses QDate::dayName(), QDate::monthName(), and
1187
QTime::toString() to generate the string.
1188
1189
*/
1190
1191
QCString QDateTime::toString() const
Oct 15, 2000
1192
{
1193
QCString buf = d.dayName(d.dayOfWeek());
Oct 15, 2000
1194
buf += ' ';
1195
buf += d.monthName(d.month());
1196
buf += ' ';
1197
buf += QCString().setNum(d.day());
Oct 15, 2000
1198
buf += ' ';
1199
buf += t.toString();
1200
buf += ' ';
1201
buf += QCString().setNum(d.year());
Oct 15, 2000
1202
return buf;
1203
}
1204
1205
/*!
1206
Returns a QDateTime object containing a datetime \a ndays days later
1207
than the datetime of this object (or earlier if \a ndays is
1208
negative).
1209
1210
\sa daysTo(), addSecs()
1211
*/
1212
1213
QDateTime QDateTime::addDays( int ndays ) const
1214
{
1215
return QDateTime( d.addDays(ndays), t );
1216
}
1217
1218
/*!
1219
Returns a QDateTime object containing a datetime \a nsecs seconds
1220
later than the datetime of this object (or earlier if \a nsecs is
1221
negative).
1222
1223
\sa secsTo(), addDays()
1224
*/
1225
1226
QDateTime QDateTime::addSecs( int nsecs ) const
1227
{
1228
uint dd = d.jd;
1229
int tt = t.ds;
1230
int sign = 1;
1231
if ( nsecs < 0 ) {
1232
nsecs = -nsecs;
1233
sign = -1;
1234
}
1235
if ( nsecs >= (int)SECS_PER_DAY ) {
1236
dd += sign*(nsecs/SECS_PER_DAY);
1237
nsecs %= SECS_PER_DAY;
1238
}
1239
tt += sign*nsecs*1000;
1240
if ( tt < 0 ) {
1241
tt = MSECS_PER_DAY - tt - 1;
1242
dd -= tt / MSECS_PER_DAY;
1243
tt = tt % MSECS_PER_DAY;
1244
tt = MSECS_PER_DAY - tt - 1;
1245
} else if ( tt >= (int)MSECS_PER_DAY ) {
1246
dd += ( tt / MSECS_PER_DAY );
1247
tt = tt % MSECS_PER_DAY;
1248
}
1249
QDateTime ret;
1250
ret.t.ds = tt;
1251
ret.d.jd = dd;
1252
return ret;
1253
}
1254
1255
/*!
1256
Returns the number of days from this datetime to \a dt (which is
1257
negative if \a dt is earlier than this datetime).
1258
1259
\sa addDays(), secsTo()
1260
*/
1261
1262
int QDateTime::daysTo( const QDateTime &dt ) const
1263
{
1264
return d.daysTo( dt.d );
1265
}
1266
1267
/*!
1268
Returns the number of seconds from this datetime to \a dt (which is
1269
negative if \a dt is earlier than this datetime).
1270
1271
Example:
1272
\code
1273
QDateTime dt = QDateTime::currentDateTime();
1274
QDateTime x( QDate(dt.year(),12,24), QTime(17,00) );
1275
qDebug( "There are %d seconds to Christmas", dt.secsTo(x) );
1276
\endcode
1277
1278
\sa addSecs(), daysTo(), QTime::secsTo()
1279
*/
1280
1281
int QDateTime::secsTo( const QDateTime &dt ) const
1282
{
1283
return t.secsTo(dt.t) + d.daysTo(dt.d)*SECS_PER_DAY;
1284
}
1285
1286
1287
/*!
1288
Returns TRUE if this datetime is equal to \a dt, or FALSE if
1289
they are different.
1290
\sa operator!=()
1291
*/
1292
1293
bool QDateTime::operator==( const QDateTime &dt ) const
1294
{
1295
return t == dt.t && d == dt.d;
1296
}
1297
1298
/*!
1299
Returns TRUE if this datetime is different from \a dt, or FALSE if
1300
they are equal.
1301
\sa operator==()
1302
*/
1303
1304
bool QDateTime::operator!=( const QDateTime &dt ) const
1305
{
1306
return t != dt.t || d != dt.d;
1307
}
1308
1309
/*!
1310
Returns TRUE if this datetime is earlier than \a dt, otherwise FALSE.
1311
*/
1312
1313
bool QDateTime::operator<( const QDateTime &dt ) const
1314
{
1315
if ( d < dt.d )
1316
return TRUE;
1317
return d == dt.d ? t < dt.t : FALSE;
1318
}
1319
1320
/*!
1321
Returns TRUE if this datetime is earlier than or equal to \a dt,
1322
otherwise FALSE.
1323
*/
1324
1325
bool QDateTime::operator<=( const QDateTime &dt ) const
1326
{
1327
if ( d < dt.d )
1328
return TRUE;
1329
return d == dt.d ? t <= dt.t : FALSE;
1330
}
1331
1332
/*!
1333
Returns TRUE if this datetime is later than \a dt, otherwise FALSE.
1334
*/
1335
1336
bool QDateTime::operator>( const QDateTime &dt ) const
1337
{
1338
if ( d > dt.d )
1339
return TRUE;
1340
return d == dt.d ? t > dt.t : FALSE;
1341
}
1342
1343
/*!
1344
Returns TRUE if this datetime is later than or equal to \a dt,
1345
otherwise FALSE.
1346
*/
1347
1348
bool QDateTime::operator>=( const QDateTime &dt ) const
1349
{
1350
if ( d > dt.d )
1351
return TRUE;
1352
return d == dt.d ? t >= dt.t : FALSE;
1353
}
1354
1355
/*!
1356
Returns the current datetime, as reported by the system clock.
1357
1358
\sa QDate::currentDate(), QTime::currentTime()
1359
*/
1360
1361
QDateTime QDateTime::currentDateTime()
1362
{
1363
QDate cd = QDate::currentDate();
1364
QTime ct;
1365
if ( QTime::currentTime(&ct) ) // too close to midnight?
1366
cd = QDate::currentDate(); // YES! time for some midnight
1367
// voodoo, fetch date again
1368
return QDateTime( cd, ct );
1369
}
1370
1371
1372
/*****************************************************************************
1373
Date/time stream functions
1374
*****************************************************************************/
1375
1376
#ifndef QT_NO_DATASTREAM
1377
/*!
1378
\relates QDate
1379
Writes the date to the stream.
1380
1381
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
1382
*/
1383
1384
QDataStream &operator<<( QDataStream &s, const QDate &d )
1385
{
1386
return s << (Q_UINT32)(d.jd);
1387
}
1388
1389
/*!
1390
\relates QDate
1391
Reads a date from the stream.
1392
1393
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
1394
*/
1395
1396
QDataStream &operator>>( QDataStream &s, QDate &d )
1397
{
1398
Q_UINT32 jd;
1399
s >> jd;
1400
d.jd = jd;
1401
return s;
1402
}
1403
1404
/*!
1405
\relates QTime
1406
Writes a time to the stream.
1407
1408
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
1409
*/
1410
1411
QDataStream &operator<<( QDataStream &s, const QTime &t )
1412
{
1413
return s << (Q_UINT32)(t.ds);
1414
}
1415
1416
/*!
1417
\relates QTime
1418
Reads a time from the stream.
1419
1420
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
1421
*/
1422
1423
QDataStream &operator>>( QDataStream &s, QTime &t )
1424
{
1425
Q_UINT32 ds;
1426
s >> ds;
1427
t.ds = ds;
1428
return s;
1429
}
1430
1431
/*!
1432
\relates QDateTime
1433
Writes a datetime to the stream.
1434
1435
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
1436
*/
1437
1438
QDataStream &operator<<( QDataStream &s, const QDateTime &dt )
1439
{
1440
return s << dt.d << dt.t;
1441
}
1442
1443
/*!
1444
\relates QDateTime
1445
Reads a datetime from the stream.
1446
1447
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
1448
*/
1449
1450
QDataStream &operator>>( QDataStream &s, QDateTime &dt )
1451
{
1452
s >> dt.d >> dt.t;
1453
return s;
1454
}
1455
#endif //QT_NO_DATASTREAM