📄 qdatetime.cpp
字号:
Note that the time will wrap if it passes midnight. See addSecs() for an example. \sa addSecs(), msecsTo()*/QTime QTime::addMSecs( int ms ) const{ QTime t; if ( ms < 0 ) { // % not well-defined for -ve, but / is. int negdays = (MSECS_PER_DAY-ms) / MSECS_PER_DAY; t.ds = ((int)ds + ms + negdays*MSECS_PER_DAY) % MSECS_PER_DAY; } else { t.ds = ((int)ds + ms) % MSECS_PER_DAY; } return t;}/*! Returns the number of milliseconds from this time to \a t (which is negative if \a t is earlier than this time). Because QTime measures time within a day and there are 86400 seconds in a day, the result is always between -86400 and 86400s. \sa secsTo()*/int QTime::msecsTo( const QTime &t ) const{ return (int)t.ds - (int)ds;}/*! \fn bool QTime::operator==( const QTime &t ) const Returns TRUE if this time is equal to \a t; otherwise returns FALSE.*//*! \fn bool QTime::operator!=( const QTime &t ) const Returns TRUE if this time is different from \a t; otherwise returns FALSE.*//*! \fn bool QTime::operator<( const QTime &t ) const Returns TRUE if this time is earlier than \a t; otherwise returns FALSE.*//*! \fn bool QTime::operator<=( const QTime &t ) const Returns TRUE if this time is earlier than or equal to \a t; otherwise returns FALSE.*//*! \fn bool QTime::operator>( const QTime &t ) const Returns TRUE if this time is later than \a t; otherwise returns FALSE.*//*! \fn bool QTime::operator>=( const QTime &t ) const Returns TRUE if this time is later than or equal to \a t; otherwise returns FALSE.*//*! \overload Returns the current time as reported by the system clock. Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.*/QTime QTime::currentTime(){ return currentTime( Qt::LocalTime );}/*! Returns the current time as reported by the system clock, for the TimeSpec \a ts. The default TimeSpec is LocalTime. Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy. \sa Qt::TimeSpec*/QTime QTime::currentTime( Qt::TimeSpec ts ){ QTime t; currentTime( &t, ts ); return t;}#ifndef QT_NO_DATESTRING/*! Returns the representation \a s as a QTime using the format \a f, or an invalid time if this is not possible. \warning Note that \c Qt::LocalDate cannot be used here.*/QTime QTime::fromString( const QString& s, Qt::DateFormat f ){ if ( ( s.isEmpty() ) || ( f == Qt::LocalDate ) ) {#if defined(QT_CHECK_RANGE) qWarning( "QTime::fromString: Parameter out of range" );#endif QTime t; t.ds = MSECS_PER_DAY; return t; } int hour( s.mid( 0, 2 ).toInt() ); int minute( s.mid( 3, 2 ).toInt() ); int second( s.mid( 6, 2 ).toInt() ); int msec( s.mid( 9, 3 ).toInt() ); return QTime( hour, minute, second, msec );}#endif/*! \internal \obsolete Fetches the current time and returns TRUE if the time is within one minute after midnight, otherwise FALSE. The return value is used by QDateTime::currentDateTime() to ensure that the date there is correct.*/bool QTime::currentTime( QTime *ct ){ return currentTime( ct, Qt::LocalTime );}/*! \internal Fetches the current time, for the TimeSpec \a ts, and returns TRUE if the time is within one minute after midnight, otherwise FALSE. The return value is used by QDateTime::currentDateTime() to ensure that the date there is correct. The default TimeSpec is LocalTime. \sa Qt::TimeSpec*/bool QTime::currentTime( QTime *ct, Qt::TimeSpec ts ){ if ( !ct ) {#if defined(QT_CHECK_NULL) qWarning( "QTime::currentTime(QTime *): Null pointer not allowed" );#endif return FALSE; }#if defined(Q_OS_WIN32) SYSTEMTIME t; if ( ts == Qt::LocalTime ) { GetLocalTime( &t ); } else { GetSystemTime( &t ); } ct->ds = (uint)( MSECS_PER_HOUR*t.wHour + MSECS_PER_MIN*t.wMinute + 1000*t.wSecond + t.wMilliseconds );#elif defined(Q_OS_UNIX) // posix compliant system struct timeval tv; gettimeofday( &tv, 0 ); time_t ltime = tv.tv_sec; tm *t;# if defined(QT_THREAD_SUPPORT) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) // use the reentrant versions of localtime() and gmtime() where available tm res; if ( ts == Qt::LocalTime ) t = localtime_r( <ime, &res ); else t = gmtime_r( <ime, &res );# else if ( ts == Qt::LocalTime ) t = localtime( <ime ); else t = gmtime( <ime );# endif // QT_THREAD_SUPPORT && _POSIX_THREAD_SAFE_FUNCTIONS ct->ds = (uint)( MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + 1000 * t->tm_sec + tv.tv_usec / 1000 );#else time_t ltime; // no millisecond resolution ::time( <ime ); tm *t; if ( ts == Qt::LocalTime ) localtime( <ime ); else gmtime( <ime ); ct->ds = (uint) ( MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + 1000 * t->tm_sec );#endif // 00:00.00 to 00:00.59.999 is considered as "midnight or right after" return ct->ds < (uint) MSECS_PER_MIN;}/*! \overload Returns TRUE if the specified time is valid; otherwise returns FALSE. The time is valid if \a h is in the range 0..23, \a m and \a s are in the range 0..59, and \a ms is in the range 0..999. Example: \code QTime::isValid(21, 10, 30); // returns TRUE QTime::isValid(22, 5, 62); // returns FALSE \endcode*/bool QTime::isValid( int h, int m, int s, int ms ){ return (uint)h < 24 && (uint)m < 60 && (uint)s < 60 && (uint)ms < 1000;}/*! Sets this time to the current time. This is practical for timing: \code QTime t; t.start(); some_lengthy_task(); qDebug( "Time elapsed: %d ms", t.elapsed() ); \endcode \sa restart(), elapsed(), currentTime()*/void QTime::start(){ *this = currentTime();}/*! Sets this time to the current time and returns the number of milliseconds that have elapsed since the last time start() or restart() was called. This function is guaranteed to be atomic and is thus very handy for repeated measurements. Call start() to start the first measurement and 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 when daylight savings time 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 when daylight savings time is turned on or off. \sa start(), restart()*/int QTime::elapsed() const{ int n = msecsTo( currentTime() ); if ( n < 0 ) // passed midnight n += 86400*1000; return n;}/***************************************************************************** QDateTime member functions *****************************************************************************//*! \class QDateTime qdatetime.h \reentrant \brief The QDateTime class provides date and time functions. \ingroup time \mainclass 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, days, months or years. A QDateTime object is typically created either by giving a date and time explicitly in the constructor, or by using the static function currentDateTime(), which returns a QDateTime object set to the system clock's time. The date and time can be changed with setDate() and setTime(). 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 fromString() function returns a QDateTime given a string and a date format which is used to interpret the date within the string. 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 where smaller means earlier and larger means later. You can increment (or decrement) a datetime by a given number of seconds using addSecs() or days using addDays(). Similarly you can use addMonths() and addYears(). The daysTo() function returns the number of days between two datetimes, and secsTo() returns the number of seconds between two datetimes. The range of a datetime object is constrained to the ranges of the QDate and QTime objects which it embodies. \sa QDate QTime QDateTimeEdit*//*! \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 (but valid) 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; otherwise returns FALSE. A null datetime is invalid. \sa QDate::isNull(), QTime::isNull()*//*! \fn bool QDateTime::isValid() const Returns TRUE if both the date and the time are valid; otherwise returns FALSE. \sa QDate::isValid(), QTime::isValid()*//*! \fn QDate QDateTime::date() const Returns the date part of the datetime. \sa setDate(), time()*//*! \fn QTime QDateTime::time() const Returns the time part of the datetime. \sa setTime(), date()*//*! \fn void QDateTime::setDate( const QDate &date ) Sets the date part of this datetime to \a date. \sa date(), setTime()*//*! \fn void QDateTime::setTime( const QTime &time ) Sets the time part of this datetime to \a time. \sa time(), setDate()*//*! Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (UTC). On systems that do not support timezones, this function will behave as if local time were UTC. \sa setTime_t()*/uint QDateTime::toTime_t() const{ tm brokenDown; brokenDown.tm_sec = t.second(); brokenDown.tm_min = t.minute(); brokenDown.tm_hour = t.hour(); brokenDown.tm_mday = d.day(); brokenDown.tm_mon = d.month() - 1; brokenDown.tm_year = d.year() - 1900; brokenDown.tm_isdst = -1; int secsSince1Jan1970UTC = (int) mktime( &brokenDown ); if ( secsSince1Jan1970UTC < -1 ) secsSince1Jan1970UTC = -1; return (uint) secsSince1Jan1970UTC;}/*! \overload Convenience function that sets the date and time to local time based on the given UTC time.*/void QDateTime::setTime_t( uint secsSince1Jan1970UTC ){ setTime_t( secsSince1Jan1970UTC, Qt::LocalTime );}/*! Sets the date and time to \a ts time (\c Qt::LocalTime or \c Qt::UTC) given the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (UTC). On systems that do not support timezones this function will behave as if local time were UTC. On Windows, only a subset of \a secsSince1Jan1970UTC values are supported, as Windows starts counting from 1980. \sa toTime_t()*/void QDateTime::setTime_t( uint secsSince1Jan1970UTC, Qt::TimeSpec ts ){ time_t tmp = (time_t) secsSince1Jan1970UTC; tm *brokenDown = 0;#if defined(Q_OS_UNIX) && defined(QT_THREAD_SUPPORT) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) // posix compliant system // use the reentrant versions of localtime() and gmtime() where available tm res; if ( ts == Qt::LocalTime ) brokenDown = localtime_r( &tmp, &res ); if ( !brokenDown ) { brokenDown = gmtime_r( &tmp, &res ); if ( !brokenDown ) { d.jd = QDate::gregorianToJulian( 1970, 1, 1 ); t.ds = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -