Public Member Functions | Static Public Member Functions | Static Protected Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
QDate Class Reference

The QDate class provides date functions. More...

#include <qdatetime.h>

Public Member Functions

 QDate ()
 
 QDate (int y, int m, int d)
 
virtual ~QDate ()
 
bool isNull () const
 
bool isValid () const
 
int year () const
 
int month () const
 
int day () const
 
int dayOfWeek () const
 
int dayOfYear () const
 
int daysInMonth () const
 
int daysInYear () const
 
virtual QString monthName (int month) const
 
virtual QString dayName (int weekday) const
 
QString toString () const
 
bool setYMD (int y, int m, int d)
 
QDate addDays (int days) const
 
int daysTo (const QDate &) const
 
bool operator== (const QDate &d) const
 
bool operator!= (const QDate &d) const
 
bool operator< (const QDate &d) const
 
bool operator<= (const QDate &d) const
 
bool operator> (const QDate &d) const
 
bool operator>= (const QDate &d) const
 

Static Public Member Functions

static QDate currentDate ()
 
static bool isValid (int y, int m, int d)
 
static bool leapYear (int year)
 

Static Protected Member Functions

static uint greg2jul (int y, int m, int d)
 
static void jul2greg (uint jd, int &y, int &m, int &d)
 

Private Attributes

uint jd
 

Static Private Attributes

static const char *const monthNames []
 
static const char *const weekdayNames []
 

Friends

class QDateTime
 
Q_EXPORT QDataStreamoperator<< (QDataStream &, const QDate &)
 
Q_EXPORT QDataStreamoperator>> (QDataStream &, QDate &)
 

Detailed Description

The QDate class provides date functions.

A QDate object contains a calendar date, i.e. year, month, and day numbers in the modern western (Gregorian) calendar. It can read the current date from the system clock. It provides functions for comparing dates and for manipulating a date by adding a number of days.

A QDate object is typically created either by giving the year, month and day numbers explicitly, or by using the static function currentDate(), which makes a QDate object which contains the system's clock date. An explicit date can also be set using setYMD().

The year(), month(), and day() functions provide access to the year, month, and day numbers. Also, dayOfWeek() and dayOfYear() functions are provided. The same information is provided in textual format by the toString(), dayName(), and monthName() functions.

QDate provides a full set of operators to compare two QDate objects. A date is considered smaller than another if it is earlier than the other.

The date a given number of days later than a given date can be found using the addDays() function. Correspondingly, the number of days between two dates can be found using the daysTo() function.

The daysInMonth() and daysInYear() functions tell how many days there are in this date's month and year, respectively. The isLeapYear() function tells whether this date is in a leap year.

Note that QDate may not be used for date calculations for dates in the remote past, i.e. prior to the introduction of the Gregorian calendar. This calendar was adopted by England Sep. 14. 1752 (hence this is the earliest valid QDate), and subsequently by most other western countries, until 1923.

The end of time is reached around 8000AD, by which time we expect Qt to be obsolete.

See also
QTime, QDateTime

Definition at line 50 of file qdatetime.h.

Constructor & Destructor Documentation

QDate::QDate ( )
inline

Constructs a null date. Null dates are invalid.

See also
isNull(), isValid()

Definition at line 53 of file qdatetime.h.

53 { jd=0; } // set null date
uint jd
Definition: qdatetime.h:95
QDate::QDate ( int  y,
int  m,
int  d 
)

Constructs a date with the year y, month m and day d.

y must be in the range 1752-ca. 8000, m must be in the range 1-12, and d must be in the range 1-31. Exception: if y is in the range 0-99, it is interpreted as 1900-1999.

See also
isValid()

Definition at line 155 of file qdatetime.cpp.

156 {
157  jd = 0;
158  setYMD( y, m, d );
159 }
bool setYMD(int y, int m, int d)
Definition: qdatetime.cpp:348
uint jd
Definition: qdatetime.h:95
virtual QDate::~QDate ( )
inlinevirtual

Definition at line 55 of file qdatetime.h.

55 {}

Member Function Documentation

QDate QDate::addDays ( int  ndays) const

Returns a QDate object containing a date ndays later than the date of this object (or earlier if ndays is negative).

See also
daysTo()

Definition at line 370 of file qdatetime.cpp.

371 {
372  QDate d;
373  d.jd = jd + ndays;
374  return d;
375 }
The QDate class provides date functions.
Definition: qdatetime.h:50
uint jd
Definition: qdatetime.h:95
QDate QDate::currentDate ( )
static

Returns the current date, as reported by the system clock.

See also
QTime::currentTime(), QDateTime::currentDateTime()

Definition at line 437 of file qdatetime.cpp.

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( &ltime );
451  tm *t = localtime( &ltime );
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 }
The QDate class provides date functions.
Definition: qdatetime.h:50
static uint greg2jul(int y, int m, int d)
Definition: qdatetime.cpp:504
tm
Definition: demo.py:21
uint jd
Definition: qdatetime.h:95
int QDate::day ( ) const

Returns the day of the month (1..31) of this date.

See also
year(), month(), dayOfWeek()

Definition at line 215 of file qdatetime.cpp.

216 {
217  int y, m, d;
218  jul2greg( jd, y, m, d );
219  return d;
220 }
static void jul2greg(uint jd, int &y, int &m, int &d)
Definition: qdatetime.cpp:528
uint jd
Definition: qdatetime.h:95
QString QDate::dayName ( int  weekday) const
virtual

Returns the name of the weekday.

Weekday 1 == "Mon", day 2 == "Tue" etc.

See also
toString(), monthName()

Definition at line 302 of file qdatetime.cpp.

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 }
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
static QString fromLatin1(const char *, int len=-1)
Definition: qstring.cpp:14539
static const char *const weekdayNames[]
Definition: qdatetime.h:94
int QDate::dayOfWeek ( ) const

Returns the weekday (Monday=1 .. Sunday=7) for this date.

See also
day(), dayOfYear()

Definition at line 228 of file qdatetime.cpp.

229 {
230  return (((jd+1) % 7) + 6)%7 + 1;
231 }
uint jd
Definition: qdatetime.h:95
int QDate::dayOfYear ( ) const

Returns the day of the year (1..365) for this date.

See also
day(), dayOfWeek()

Definition at line 239 of file qdatetime.cpp.

240 {
241  return jd - greg2jul(year(), 1, 1) + 1;
242 }
static uint greg2jul(int y, int m, int d)
Definition: qdatetime.cpp:504
int year() const
Definition: qdatetime.cpp:189
uint jd
Definition: qdatetime.h:95
int QDate::daysInMonth ( ) const

Returns the number of days in the month (28..31) for this date.

See also
day(), daysInYear()

Definition at line 250 of file qdatetime.cpp.

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 }
static void jul2greg(uint jd, int &y, int &m, int &d)
Definition: qdatetime.cpp:528
static const short monthDays[]
Definition: qdatetime.cpp:71
static bool leapYear(int year)
Definition: qdatetime.cpp:492
uint jd
Definition: qdatetime.h:95
int QDate::daysInYear ( ) const

Returns the number of days in the year (365 or 366) for this date.

See also
day(), daysInMonth()

Definition at line 266 of file qdatetime.cpp.

267 {
268  int y, m, d;
269  jul2greg( jd, y, m, d );
270  return leapYear(y) ? 366 : 365;
271 }
static void jul2greg(uint jd, int &y, int &m, int &d)
Definition: qdatetime.cpp:528
static bool leapYear(int year)
Definition: qdatetime.cpp:492
uint jd
Definition: qdatetime.h:95
int QDate::daysTo ( const QDate d) const

Returns the number of days from this date to d (which is negative if d is earlier than this date).

Example:

QDate d1( 1995, 5, 17 ); // May 17th 1995
QDate d2( 1995, 5, 20 ); // May 20th 1995
d1.daysTo( d2 ); // returns 3
d2.daysTo( d1 ); // returns -3
See also
addDays()

Definition at line 392 of file qdatetime.cpp.

393 {
394  return d.jd - jd;
395 }
uint jd
Definition: qdatetime.h:95
uint QDate::greg2jul ( int  y,
int  m,
int  d 
)
staticprotected

Definition at line 504 of file qdatetime.cpp.

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 }
unsigned uint
Definition: qglobal.h:351
bool QDate::isNull ( ) const
inline

Returns TRUE if the date is null. A null date is invalid.

See also
isValid()

Definition at line 57 of file qdatetime.h.

57 { return jd == 0; }
uint jd
Definition: qdatetime.h:95
bool QDate::isValid ( ) const

Returns TRUE if this date is valid.

See also
isNull()

Definition at line 177 of file qdatetime.cpp.

178 {
179  return jd >= FIRST_DAY;
180 }
static const uint FIRST_DAY
Definition: qdatetime.cpp:62
uint jd
Definition: qdatetime.h:95
bool QDate::isValid ( int  y,
int  m,
int  d 
)
static

Returns TRUE if the specified date (year y, month m and day d) is valid.

Example:

QDate::isValid( 2002, 5, 17 ); // TRUE; May 17th 2002 is OK.
QDate::isValid( 2002, 2, 30 ); // FALSE; Feb 30th does not exist
QDate::isValid( 2004, 2, 29 ); // TRUE; 2004 is a leap year
QDate::isValid( 1202, 6, 6 ); // FALSE; 1202 is pre-Gregorian

Note that a y value in the range 00-99 is interpreted as 1900-1999.

See also
isNull(), setYMD()

Definition at line 477 of file qdatetime.cpp.

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 }
const bool FALSE
Definition: qglobal.h:370
static const short monthDays[]
Definition: qdatetime.cpp:71
static const int FIRST_YEAR
Definition: qdatetime.cpp:63
static bool leapYear(int year)
Definition: qdatetime.cpp:492
void QDate::jul2greg ( uint  jd,
int &  y,
int &  m,
int &  d 
)
staticprotected

Definition at line 528 of file qdatetime.cpp.

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 }
list x
Definition: train.py:276
uint jd
Definition: qdatetime.h:95
unsigned uint
Definition: qglobal.h:351
bool QDate::leapYear ( int  y)
static

Returns TRUE if the specified year y is a leap year.

Definition at line 492 of file qdatetime.cpp.

493 {
494  return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
495 }
int QDate::month ( ) const

Returns the month (January=1 .. December=12) of this date.

See also
year(), day()

Definition at line 202 of file qdatetime.cpp.

203 {
204  int y, m, d;
205  jul2greg( jd, y, m, d );
206  return m;
207 }
static void jul2greg(uint jd, int &y, int &m, int &d)
Definition: qdatetime.cpp:528
uint jd
Definition: qdatetime.h:95
QString QDate::monthName ( int  month) const
virtual

Returns the name of the month.

Month 1 == "Jan", month 2 == "Feb" etc.

See also
toString(), dayName()

Definition at line 282 of file qdatetime.cpp.

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
292 }
static const char *const monthNames[]
Definition: qdatetime.h:93
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
static QString fromLatin1(const char *, int len=-1)
Definition: qstring.cpp:14539
int month() const
Definition: qdatetime.cpp:202
bool QDate::operator!= ( const QDate d) const
inline

Returns TRUE if this date is different from d, or FALSE if they are equal.

Definition at line 79 of file qdatetime.h.

79 { return jd != d.jd; }
uint jd
Definition: qdatetime.h:95
bool QDate::operator< ( const QDate d) const
inline

Returns TRUE if this date is earlier than d, otherwise FALSE.

Definition at line 80 of file qdatetime.h.

80 { return jd < d.jd; }
uint jd
Definition: qdatetime.h:95
bool QDate::operator<= ( const QDate d) const
inline

Returns TRUE if this date is earlier than or equal to d, otherwise FALSE.

Definition at line 81 of file qdatetime.h.

81 { return jd <= d.jd; }
uint jd
Definition: qdatetime.h:95
bool QDate::operator== ( const QDate d) const
inline

Returns TRUE if this date is equal to d, or FALSE if they are different.

Definition at line 78 of file qdatetime.h.

78 { return jd == d.jd; }
uint jd
Definition: qdatetime.h:95
bool QDate::operator> ( const QDate d) const
inline

Returns TRUE if this date is later than d, otherwise FALSE.

Definition at line 82 of file qdatetime.h.

82 { return jd > d.jd; }
uint jd
Definition: qdatetime.h:95
bool QDate::operator>= ( const QDate d) const
inline

Returns TRUE if this date is later than or equal to d, otherwise FALSE.

Definition at line 83 of file qdatetime.h.

83 { return jd >= d.jd; }
uint jd
Definition: qdatetime.h:95
bool QDate::setYMD ( int  y,
int  m,
int  d 
)

Sets the year y, month m and day d.

y must be in the range 1752-ca. 8000, m must be in the range 1-12, and d must be in the range 1-31. Exception: if y is in the range 0-99, it is interpreted as 1900-1999.

Returns TRUE if the date is valid, otherwise FALSE.

Definition at line 348 of file qdatetime.cpp.

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 }
const bool FALSE
Definition: qglobal.h:370
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
static uint greg2jul(int y, int m, int d)
Definition: qdatetime.cpp:504
int month() const
Definition: qdatetime.cpp:202
int year() const
Definition: qdatetime.cpp:189
uint jd
Definition: qdatetime.h:95
bool isValid() const
Definition: qdatetime.cpp:177
int day() const
Definition: qdatetime.cpp:215
const bool TRUE
Definition: qglobal.h:371
#define ASSERT(x)
Definition: qglobal.h:590
QString QDate::toString ( ) const

Returns the date as a string.

The string format is "Sat May 20 1995". This function uses the dayName() and monthName() functions to generate the string.

See also
dayName(), monthName()

Definition at line 324 of file qdatetime.cpp.

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 }
virtual QString monthName(int month) const
Definition: qdatetime.cpp:282
QString & sprintf(const char *format,...)
Definition: qstring.cpp:12719
static void jul2greg(uint jd, int &y, int &m, int &d)
Definition: qdatetime.cpp:528
The QString class provides an abstraction of Unicode text and the classic C null-terminated char arra...
Definition: qstring.h:350
virtual QString dayName(int weekday) const
Definition: qdatetime.cpp:302
int dayOfWeek() const
Definition: qdatetime.cpp:228
uint jd
Definition: qdatetime.h:95
int QDate::year ( ) const

Returns the year (>= 1752) of this date.

See also
month(), day()

Definition at line 189 of file qdatetime.cpp.

190 {
191  int y, m, d;
192  jul2greg( jd, y, m, d );
193  return y;
194 }
static void jul2greg(uint jd, int &y, int &m, int &d)
Definition: qdatetime.cpp:528
uint jd
Definition: qdatetime.h:95

Friends And Related Function Documentation

QDataStream & operator<< ( QDataStream s,
const QDate d 
)
friend

Writes the date to the stream.

See also
Format of the QDataStream operators

Definition at line 1363 of file qdatetime.cpp.

1364 {
1365  return s << (Q_UINT32)(d.jd);
1366 }
unsigned int Q_UINT32
Definition: qglobal.h:420
uint jd
Definition: qdatetime.h:95
QDataStream & operator>> ( QDataStream s,
QDate d 
)
friend

Reads a date from the stream.

See also
Format of the QDataStream operators

Definition at line 1375 of file qdatetime.cpp.

1376 {
1377  Q_UINT32 jd;
1378  s >> jd;
1379  d.jd = jd;
1380  return s;
1381 }
unsigned int Q_UINT32
Definition: qglobal.h:420
uint jd
Definition: qdatetime.h:95
static QCString * s
Definition: config.cpp:1042
friend class QDateTime
friend

Definition at line 96 of file qdatetime.h.

Member Data Documentation

uint QDate::jd
private

Definition at line 95 of file qdatetime.h.

const char *const QDate::monthNames
staticprivate
Initial value:
= {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }

Definition at line 93 of file qdatetime.h.

const char *const QDate::weekdayNames
staticprivate
Initial value:
={
"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }

Definition at line 94 of file qdatetime.h.


The documentation for this class was generated from the following files: