📄 qdatetime.cpp
字号:
This function is guaranteed to be atomic, and is thus very handy for repeated measurements: call start() to start the first measurement, then restart() for each later measurement. Note that the counter wraps to zero 24 hours after the last call to start() or restart(). \warning If the system's clock setting has been changed since the last time start() or restart() was called, the result is undefined. This can happen e.g. when daylight saving is turned on or off. \sa start(), elapsed(), currentTime()*/int QTime::restart(){ QTime t = currentTime(); int n = msecsTo( t ); if ( n < 0 ) // passed midnight n += 86400*1000; *this = t; return n;}/*! Returns the number of milliseconds that have elapsed since the last time start() or restart() was called. Note that the counter wraps to zero 24 hours after the last call to start() or restart. Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy. \warning If the system's clock setting has been changed since the last time start() or restart() was called, the result is undefined. This can happen e.g. when daylight saving is turned on or off. \sa start(), restart()*/int QTime::elapsed(){ int n = msecsTo( currentTime() ); if ( n < 0 ) // passed midnight n += 86400*1000; return n;}/***************************************************************************** QDateTime member functions *****************************************************************************//*! \class QDateTime qdatetime.h \brief The QDateTime class provides date and time functions. \ingroup time 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. \sa QDate, QTime*//*! \fn QDateTime::QDateTime() Constructs a null datetime (i.e. null date and null time). A null datetime is invalid, since the date is invalid. \sa isValid()*//*! Constructs a datetime with date \a date and null time (00:00:00.000).*/QDateTime::QDateTime( const QDate &date ) : d(date){}/*! Constructs a datetime with date \a date and time \a time.*/QDateTime::QDateTime( const QDate &date, const QTime &time ) : d(date), t(time){}/*! \fn bool QDateTime::isNull() const Returns TRUE if both the date and the time are null. A null date is invalid. \sa QDate::isNull(), QTime::isNull()*//*! \fn bool QDateTime::isValid() const Returns TRUE if both the date and the time are valid. \sa QDate::isValid(), QTime::isValid()*//*! \fn QDate QDateTime::date() const Returns the date part of this datetime. \sa setDate(), time()*//*! \fn QTime QDateTime::time() const Returns the time part of this datetime. \sa setTime(), date()*//*! \fn void QDateTime::setDate( const QDate &date ) Sets the date part of this datetime. \sa date(), setTime()*//*! \fn void QDateTime::setTime( const QTime &time ) Sets the time part of this datetime. \sa time(), setDate()*//*! 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 \a secsSince1Jan1970UTC.*/void QDateTime::setTime_t( uint secsSince1Jan1970UTC ){ time_t tmp = (time_t) secsSince1Jan1970UTC; tm *tM = localtime( &tmp ); if ( !tM ) { tM = gmtime( &tmp ); if ( !tM ) { d.jd = QDate::greg2jul( 1970, 1, 1 ); t.ds = 0; return; } } d.jd = QDate::greg2jul( tM->tm_year + 1900, tM->tm_mon + 1, tM->tm_mday ); t.ds = MSECS_PER_HOUR*tM->tm_hour + MSECS_PER_MIN*tM->tm_min + 1000*tM->tm_sec;}/*! 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.*/QString QDateTime::toString() const{ QString buf = d.dayName(d.dayOfWeek()); buf += ' '; buf += d.monthName(d.month()); buf += ' '; buf += QString().setNum(d.day()); buf += ' '; buf += t.toString(); buf += ' '; buf += QString().setNum(d.year()); return buf;}/*! Returns a QDateTime object containing a datetime \a ndays days later than the datetime of this object (or earlier if \a ndays is negative). \sa daysTo(), addSecs()*/QDateTime QDateTime::addDays( int ndays ) const{ return QDateTime( d.addDays(ndays), t );}/*! Returns a QDateTime object containing a datetime \a nsecs seconds later than the datetime of this object (or earlier if \a nsecs is negative). \sa secsTo(), addDays()*/QDateTime QDateTime::addSecs( int nsecs ) const{ uint dd = d.jd; int tt = t.ds; int sign = 1; if ( nsecs < 0 ) { nsecs = -nsecs; sign = -1; } if ( nsecs >= (int)SECS_PER_DAY ) { dd += sign*(nsecs/SECS_PER_DAY); nsecs %= SECS_PER_DAY; } tt += sign*nsecs*1000; if ( tt < 0 ) { tt = MSECS_PER_DAY - tt - 1; dd -= tt / MSECS_PER_DAY; tt = tt % MSECS_PER_DAY; tt = MSECS_PER_DAY - tt - 1; } else if ( tt >= (int)MSECS_PER_DAY ) { dd += ( tt / MSECS_PER_DAY ); tt = tt % MSECS_PER_DAY; } QDateTime ret; ret.t.ds = tt; ret.d.jd = dd; return ret;}/*! Returns the number of days from this datetime to \a dt (which is negative if \a dt is earlier than this datetime). \sa addDays(), secsTo()*/int QDateTime::daysTo( const QDateTime &dt ) const{ return d.daysTo( dt.d );}/*! Returns the number of seconds from this datetime to \a dt (which is negative if \a dt is earlier than this datetime). Example: \code QDateTime dt = QDateTime::currentDateTime(); QDateTime x( QDate(dt.year(),12,24), QTime(17,00) ); qDebug( "There are %d seconds to Christmas", dt.secsTo(x) ); \endcode \sa addSecs(), daysTo(), QTime::secsTo()*/int QDateTime::secsTo( const QDateTime &dt ) const{ return t.secsTo(dt.t) + d.daysTo(dt.d)*SECS_PER_DAY;}/*! Returns TRUE if this datetime is equal to \a dt, or FALSE if they are different. \sa operator!=()*/bool QDateTime::operator==( const QDateTime &dt ) const{ return t == dt.t && d == dt.d;}/*! Returns TRUE if this datetime is different from \a dt, or FALSE if they are equal. \sa operator==()*/bool QDateTime::operator!=( const QDateTime &dt ) const{ return t != dt.t || d != dt.d;}/*! Returns TRUE if this datetime is earlier than \a dt, otherwise FALSE.*/bool QDateTime::operator<( const QDateTime &dt ) const{ if ( d < dt.d ) return TRUE; return d == dt.d ? t < dt.t : FALSE;}/*! Returns TRUE if this datetime is earlier than or equal to \a dt, otherwise FALSE.*/bool QDateTime::operator<=( const QDateTime &dt ) const{ if ( d < dt.d ) return TRUE; return d == dt.d ? t <= dt.t : FALSE;}/*! Returns TRUE if this datetime is later than \a dt, otherwise FALSE.*/bool QDateTime::operator>( const QDateTime &dt ) const{ if ( d > dt.d ) return TRUE; return d == dt.d ? t > dt.t : FALSE;}/*! Returns TRUE if this datetime is later than or equal to \a dt, otherwise FALSE.*/bool QDateTime::operator>=( const QDateTime &dt ) const{ if ( d > dt.d ) return TRUE; return d == dt.d ? t >= dt.t : FALSE;}/*! Returns the current datetime, as reported by the system clock. \sa QDate::currentDate(), QTime::currentTime()*/QDateTime QDateTime::currentDateTime(){ QDate cd = QDate::currentDate(); QTime ct; if ( QTime::currentTime(&ct) ) // too close to midnight? cd = QDate::currentDate(); // YES! time for some midnight // voodoo, fetch date again return QDateTime( cd, ct );}/***************************************************************************** Date/time stream functions *****************************************************************************/#ifndef QT_NO_DATASTREAM/*! \relates QDate Writes the date to the stream. \sa \link datastreamformat.html Format of the QDataStream operators \endlink*/QDataStream &operator<<( QDataStream &s, const QDate &d ){ return s << (Q_UINT32)(d.jd);}/*! \relates QDate Reads a date from the stream. \sa \link datastreamformat.html Format of the QDataStream operators \endlink*/QDataStream &operator>>( QDataStream &s, QDate &d ){ Q_UINT32 jd; s >> jd; d.jd = jd; return s;}/*! \relates QTime Writes a time to the stream. \sa \link datastreamformat.html Format of the QDataStream operators \endlink*/QDataStream &operator<<( QDataStream &s, const QTime &t ){ return s << (Q_UINT32)(t.ds);}/*! \relates QTime Reads a time from the stream. \sa \link datastreamformat.html Format of the QDataStream operators \endlink*/QDataStream &operator>>( QDataStream &s, QTime &t ){ Q_UINT32 ds; s >> ds; t.ds = ds; return s;}/*! \relates QDateTime Writes a datetime to the stream. \sa \link datastreamformat.html Format of the QDataStream operators \endlink*/QDataStream &operator<<( QDataStream &s, const QDateTime &dt ){ return s << dt.d << dt.t;}/*! \relates QDateTime Reads a datetime from the stream. \sa \link datastreamformat.html Format of the QDataStream operators \endlink*/QDataStream &operator>>( QDataStream &s, QDateTime &dt ){ s >> dt.d >> dt.t; return s;}#endif //QT_NO_DATASTREAM
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -