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