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

The QDateTime class provides date and time functions. More...

#include <qdatetime.h>

Public Member Functions

 QDateTime ()
 
 QDateTime (const QDate &)
 
 QDateTime (const QDate &, const QTime &)
 
bool isNull () const
 
bool isValid () const
 
QDate date () const
 
QTime time () const
 
void setDate (const QDate &date)
 
void setTime (const QTime &time)
 
void setTime_t (uint secsSince1Jan1970UTC)
 
QString toString () const
 
QDateTime addDays (int days) const
 
QDateTime addSecs (int secs) const
 
int daysTo (const QDateTime &) const
 
int secsTo (const QDateTime &) const
 
bool operator== (const QDateTime &dt) const
 
bool operator!= (const QDateTime &dt) const
 
bool operator< (const QDateTime &dt) const
 
bool operator<= (const QDateTime &dt) const
 
bool operator> (const QDateTime &dt) const
 
bool operator>= (const QDateTime &dt) const
 

Static Public Member Functions

static QDateTime currentDateTime ()
 

Private Attributes

QDate d
 
QTime t
 

Friends

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

Detailed Description

The QDateTime class provides date and time functions.

A QDateTime object contains a calendar date and a clock time (a "datetime"). It is a combination of the QDate and QTime classes. It can read the current datetime from the system clock. It provides functions for comparing datetimes and for manipulating a datetime by adding a number of seconds or days.

A QDateTime object is typically created either by giving a date and time explicitly, or by using the static function currentTime(), which makes a QDateTime object which contains the system's clock time.

The date() and time() functions provide access to the date and time parts of the datetime. The same information is provided in textual format by the toString() function.

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

The datetime a given number of days or seconds later than a given datetime can be found using the addDays() and addSecs() functions. Correspondingly, the number of days or seconds between two times can be found using the daysTo() or secsTo() functions.

A datetime can also be set using the setTime_t() function, which takes a POSIX-standard "number of seconds since 00:00:00 on January 1, 1970" value.

The limitations regarding range and resolution mentioned in the QDate and QTime documentation apply for QDateTime also.

See also
QDate, QTime

Definition at line 161 of file qdatetime.h.

Constructor & Destructor Documentation

QDateTime::QDateTime ( )
inline

Constructs a null datetime (i.e. null date and null time). A null datetime is invalid, since the date is invalid.

See also
isValid()

Definition at line 164 of file qdatetime.h.

164 {} // set null date and null time
QDateTime::QDateTime ( const QDate date)

Constructs a datetime with date date and null time (00:00:00.000).

Definition at line 1068 of file qdatetime.cpp.

1069  : d(date)
1070 {
1071 }
QDate d
Definition: qdatetime.h:194
QDateTime::QDateTime ( const QDate date,
const QTime time 
)

Constructs a datetime with date date and time time.

Definition at line 1077 of file qdatetime.cpp.

1078  : d(date), t(time)
1079 {
1080 }
QDate d
Definition: qdatetime.h:194
QTime t
Definition: qdatetime.h:195

Member Function Documentation

QDateTime QDateTime::addDays ( int  ndays) const

Returns a QDateTime object containing a datetime ndays days later than the datetime of this object (or earlier if ndays is negative).

See also
daysTo(), addSecs()

Definition at line 1192 of file qdatetime.cpp.

1193 {
1194  return QDateTime( d.addDays(ndays), t );
1195 }
QDate d
Definition: qdatetime.h:194
QTime t
Definition: qdatetime.h:195
QDate addDays(int days) const
Definition: qdatetime.cpp:370
QDateTime QDateTime::addSecs ( int  nsecs) const

Returns a QDateTime object containing a datetime nsecs seconds later than the datetime of this object (or earlier if nsecs is negative).

See also
secsTo(), addDays()

Definition at line 1205 of file qdatetime.cpp.

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 }
QDate d
Definition: qdatetime.h:194
QTime t
Definition: qdatetime.h:195
uint ds
Definition: qdatetime.h:148
static const uint SECS_PER_DAY
Definition: qdatetime.cpp:64
Definition: type_traits.h:61
The QDateTime class provides date and time functions.
Definition: qdatetime.h:161
int sign(double val)
Definition: UtilFunc.cxx:104
uint jd
Definition: qdatetime.h:95
static const uint MSECS_PER_DAY
Definition: qdatetime.cpp:65
unsigned uint
Definition: qglobal.h:351
QDateTime QDateTime::currentDateTime ( )
static

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

See also
QDate::currentDate(), QTime::currentTime()

Definition at line 1340 of file qdatetime.cpp.

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 }
The QDate class provides date functions.
Definition: qdatetime.h:50
The QTime class provides clock time functions.
Definition: qdatetime.h:108
static QDate currentDate()
Definition: qdatetime.cpp:437
static QTime currentTime()
Definition: qdatetime.cpp:848
QDate QDateTime::date ( ) const
inline

Returns the date part of this datetime.

See also
setDate(), time()

Definition at line 171 of file qdatetime.h.

171 { return d; }
QDate d
Definition: qdatetime.h:194
int QDateTime::daysTo ( const QDateTime dt) const

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

See also
addDays(), secsTo()

Definition at line 1241 of file qdatetime.cpp.

1242 {
1243  return d.daysTo( dt.d );
1244 }
QDate d
Definition: qdatetime.h:194
int daysTo(const QDate &) const
Definition: qdatetime.cpp:392
bool QDateTime::isNull ( ) const
inline

Returns TRUE if both the date and the time are null. A null date is invalid.

See also
QDate::isNull(), QTime::isNull()

Definition at line 168 of file qdatetime.h.

168 { return d.isNull() && t.isNull(); }
QDate d
Definition: qdatetime.h:194
QTime t
Definition: qdatetime.h:195
bool isNull() const
Definition: qdatetime.h:114
bool isNull() const
Definition: qdatetime.h:57
bool QDateTime::isValid ( ) const
inline

Returns TRUE if both the date and the time are valid.

See also
QDate::isValid(), QTime::isValid()

Definition at line 169 of file qdatetime.h.

169 { return d.isValid() && t.isValid(); }
QDate d
Definition: qdatetime.h:194
QTime t
Definition: qdatetime.h:195
bool isValid() const
Definition: qdatetime.cpp:638
bool isValid() const
Definition: qdatetime.cpp:177
bool QDateTime::operator!= ( const QDateTime dt) const

Returns TRUE if this datetime is different from dt, or FALSE if they are equal.

See also
operator==()

Definition at line 1283 of file qdatetime.cpp.

1284 {
1285  return t != dt.t || d != dt.d;
1286 }
QDate d
Definition: qdatetime.h:194
QTime t
Definition: qdatetime.h:195
bool QDateTime::operator< ( const QDateTime dt) const

Returns TRUE if this datetime is earlier than dt, otherwise FALSE.

Definition at line 1292 of file qdatetime.cpp.

1293 {
1294  if ( d < dt.d )
1295  return TRUE;
1296  return d == dt.d ? t < dt.t : FALSE;
1297 }
QDate d
Definition: qdatetime.h:194
QTime t
Definition: qdatetime.h:195
const bool FALSE
Definition: qglobal.h:370
const bool TRUE
Definition: qglobal.h:371
bool QDateTime::operator<= ( const QDateTime dt) const

Returns TRUE if this datetime is earlier than or equal to dt, otherwise FALSE.

Definition at line 1304 of file qdatetime.cpp.

1305 {
1306  if ( d < dt.d )
1307  return TRUE;
1308  return d == dt.d ? t <= dt.t : FALSE;
1309 }
QDate d
Definition: qdatetime.h:194
QTime t
Definition: qdatetime.h:195
const bool FALSE
Definition: qglobal.h:370
const bool TRUE
Definition: qglobal.h:371
bool QDateTime::operator== ( const QDateTime dt) const

Returns TRUE if this datetime is equal to dt, or FALSE if they are different.

See also
operator!=()

Definition at line 1272 of file qdatetime.cpp.

1273 {
1274  return t == dt.t && d == dt.d;
1275 }
QDate d
Definition: qdatetime.h:194
QTime t
Definition: qdatetime.h:195
bool QDateTime::operator> ( const QDateTime dt) const

Returns TRUE if this datetime is later than dt, otherwise FALSE.

Definition at line 1315 of file qdatetime.cpp.

1316 {
1317  if ( d > dt.d )
1318  return TRUE;
1319  return d == dt.d ? t > dt.t : FALSE;
1320 }
QDate d
Definition: qdatetime.h:194
QTime t
Definition: qdatetime.h:195
const bool FALSE
Definition: qglobal.h:370
const bool TRUE
Definition: qglobal.h:371
bool QDateTime::operator>= ( const QDateTime dt) const

Returns TRUE if this datetime is later than or equal to dt, otherwise FALSE.

Definition at line 1327 of file qdatetime.cpp.

1328 {
1329  if ( d > dt.d )
1330  return TRUE;
1331  return d == dt.d ? t >= dt.t : FALSE;
1332 }
QDate d
Definition: qdatetime.h:194
QTime t
Definition: qdatetime.h:195
const bool FALSE
Definition: qglobal.h:370
const bool TRUE
Definition: qglobal.h:371
int QDateTime::secsTo ( const QDateTime dt) const

Returns the number of seconds from this datetime to dt (which is negative if dt is earlier than this datetime).

Example:

QDateTime x( QDate(dt.year(),12,24), QTime(17,00) );
qDebug( "There are %d seconds to Christmas", dt.secsTo(x) );
See also
addSecs(), daysTo(), QTime::secsTo()

Definition at line 1260 of file qdatetime.cpp.

1261 {
1262  return t.secsTo(dt.t) + d.daysTo(dt.d)*SECS_PER_DAY;
1263 }
QDate d
Definition: qdatetime.h:194
QTime t
Definition: qdatetime.h:195
int daysTo(const QDate &) const
Definition: qdatetime.cpp:392
static const uint SECS_PER_DAY
Definition: qdatetime.cpp:64
int secsTo(const QTime &) const
Definition: qdatetime.cpp:754
void QDateTime::setDate ( const QDate date)
inline

Sets the date part of this datetime.

See also
date(), setTime()

Definition at line 173 of file qdatetime.h.

173 { d=date; }
QDate d
Definition: qdatetime.h:194
QDate date() const
Definition: qdatetime.h:171
void QDateTime::setTime ( const QTime time)
inline

Sets the time part of this datetime.

See also
time(), setDate()

Definition at line 174 of file qdatetime.h.

174 { t=time; }
QTime t
Definition: qdatetime.h:195
QTime time() const
Definition: qdatetime.h:172
void QDateTime::setTime_t ( uint  secsSince1Jan1970UTC)

Sets the local date and time given the number of seconds that have passed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC). On systems that do not support timezones this function will behave as if local time were UTC.

Note that Microsoft Windows supports only a limited range of values for secsSince1Jan1970UTC.

Definition at line 1142 of file qdatetime.cpp.

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 }
QDate d
Definition: qdatetime.h:194
static const uint MSECS_PER_HOUR
Definition: qdatetime.cpp:67
QTime t
Definition: qdatetime.h:195
uint ds
Definition: qdatetime.h:148
static uint greg2jul(int y, int m, int d)
Definition: qdatetime.cpp:504
tm
Definition: demo.py:21
string tmp
Definition: languages.py:63
static const uint MSECS_PER_MIN
Definition: qdatetime.cpp:69
uint jd
Definition: qdatetime.h:95
QTime QDateTime::time ( ) const
inline

Returns the time part of this datetime.

See also
setTime(), date()

Definition at line 172 of file qdatetime.h.

172 { return t; }
QTime t
Definition: qdatetime.h:195
QString QDateTime::toString ( ) const

Returns the datetime as a string.

The string format is "Sat May 20 03:40:13 1998".

This function uses QDate::dayName(), QDate::monthName(), and QTime::toString() to generate the string.

Definition at line 1170 of file qdatetime.cpp.

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 }
QDate d
Definition: qdatetime.h:194
virtual QString monthName(int month) const
Definition: qdatetime.cpp:282
QTime t
Definition: qdatetime.h:195
QString toString() const
Definition: qdatetime.cpp:687
The QString class provides an abstraction of Unicode text and the classic C null-terminated char arra...
Definition: qstring.h:350
QString & setNum(short, int base=10)
Definition: qstring.h:706
virtual QString dayName(int weekday) const
Definition: qdatetime.cpp:302
int dayOfWeek() const
Definition: qdatetime.cpp:228
int month() const
Definition: qdatetime.cpp:202
int year() const
Definition: qdatetime.cpp:189
int day() const
Definition: qdatetime.cpp:215

Friends And Related Function Documentation

QDataStream & operator<< ( QDataStream s,
const QDateTime dt 
)
friend

Writes a datetime to the stream.

See also
Format of the QDataStream operators

Definition at line 1417 of file qdatetime.cpp.

1418 {
1419  return s << dt.d << dt.t;
1420 }
QDate d
Definition: qdatetime.h:194
QTime t
Definition: qdatetime.h:195
QDataStream & operator>> ( QDataStream s,
QDateTime dt 
)
friend

Reads a datetime from the stream.

See also
Format of the QDataStream operators

Definition at line 1429 of file qdatetime.cpp.

1430 {
1431  s >> dt.d >> dt.t;
1432  return s;
1433 }
QDate d
Definition: qdatetime.h:194
QTime t
Definition: qdatetime.h:195
static QCString * s
Definition: config.cpp:1042

Member Data Documentation

QDate QDateTime::d
private

Definition at line 194 of file qdatetime.h.

QTime QDateTime::t
private

Definition at line 195 of file qdatetime.h.


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