📄 qdatetime.cpp
字号:
the format of the result string. These expressions may be used: \table \header \i Expression \i Output \row \i h \i the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) \row \i hh \i the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) \row \i H \i the hour without a leading zero (0 to 23, even with AM/PM display) \row \i HH \i the hour with a leading zero (00 to 23, even with AM/PM display) \row \i m \i the minute without a leading zero (0 to 59) \row \i mm \i the minute with a leading zero (00 to 59) \row \i s \i the second without a leading zero (0 to 59) \row \i ss \i the second with a leading zero (00 to 59) \row \i z \i the milliseconds without leading zeroes (0 to 999) \row \i zzz \i the milliseconds with leading zeroes (000 to 999) \row \i AP or A \i use AM/PM display. \e AP will be replaced by either "AM" or "PM". \row \i ap or a \i use am/pm display. \e ap will be replaced by either "am" or "pm". \endtable All other input characters will be ignored. Any sequence of characters that are enclosed in singlequotes will be treated as text and not be used as an expression. Two consecutive singlequotes ("''") are replaced by a singlequote in the output. Example format strings (assuming that the QTime is 14:13:09.042) \table \header \i Format \i Result \row \i hh:mm:ss.zzz \i 14:13:09.042 \row \i h:m:s ap \i 2:13:9 pm \row \i H:m:s a \i 14:13:9 pm \endtable If the datetime is invalid, an empty string will be returned. \sa QDate::toString() QDateTime::toString()*/QString QTime::toString(const QString& format) const{ return fmtDateTime(format, this, 0);}#endif //QT_NO_DATESTRING/*! Sets the time to hour \a h, minute \a m, seconds \a s and milliseconds \a ms. \a h must be in the range 0 to 23, \a m and \a s must be in the range 0 to 59, and \a ms must be in the range 0 to 999. Returns true if the set time is valid; otherwise returns false. \sa isValid()*/bool QTime::setHMS(int h, int m, int s, int ms){ if (!isValid(h,m,s,ms)) { mds = NullTime; // make this invalid return false; } mds = (h*SECS_PER_HOUR + m*SECS_PER_MIN + s)*1000 + ms; return true;}/*! Returns a QTime object containing a time \a s seconds later than the time of this object (or earlier if \a s is negative). Note that the time will wrap if it passes midnight. Example: \code QTime n(14, 0, 0); // n == 14:00:00 QTime t; t = n.addSecs(70); // t == 14:01:10 t = n.addSecs(-70); // t == 13:58:50 t = n.addSecs(10 * 60 * 60 + 5); // t == 00:00:05 t = n.addSecs(-15 * 60 * 60); // t == 23:00:00 \endcode \sa addMSecs(), secsTo(), QDateTime::addSecs()*/QTime QTime::addSecs(int s) const{ return addMSecs(s * 1000);}/*! Returns the number of seconds from this time to \a t. If \a t is earlier than this time, the number of seconds returned is negative. Because QTime measures time within a day and there are 86400 seconds in a day, the result is always between -86400 and 86400. \sa addSecs(), QDateTime::secsTo()*/int QTime::secsTo(const QTime &t) const{ return (t.ds() - ds()) / 1000;}/*! Returns a QTime object containing a time \a ms milliseconds later than the time of this object (or earlier if \a ms is negative). 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.mds = (ds() + ms + negdays * MSECS_PER_DAY) % MSECS_PER_DAY; } else { t.mds = (ds() + ms) % MSECS_PER_DAY; } return t;}/*! Returns the number of milliseconds from this time to \a t. If \a t is earlier than this time, the number of milliseconds returned is negative. Because QTime measures time within a day and there are 86400 seconds in a day, the result is always between -86400000 and 86400000 ms. \sa secsTo(), addMSecs()*/int QTime::msecsTo(const QTime &t) const{ return t.ds() - 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(){ QTime ct;#if defined(Q_OS_WIN) SYSTEMTIME st; memset(&st, 0, sizeof(SYSTEMTIME)); GetLocalTime(&st); ct.mds = MSECS_PER_HOUR * st.wHour + MSECS_PER_MIN * st.wMinute + 1000 * st.wSecond + st.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_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) // use the reentrant version of localtime() where available tm res; t = localtime_r(<ime, &res);#else t = localtime(<ime);#endif ct.mds = 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; localtime(<ime); ct.mds = MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + 1000 * t->tm_sec;#endif return ct;}#ifndef QT_NO_DATESTRING/*! \fn QTime QTime::fromString(const QString &string, Qt::DateFormat format) Returns the time represented in the \a string as a QTime using the \a format given, or an invalid time if this is not possible. \warning Note that Qt::LocalDate cannot be used here.*/QTime QTime::fromString(const QString& s, Qt::DateFormat f){ if (s.isEmpty() || f == Qt::LocalDate) { qWarning("QTime::fromString: Parameter out of range"); QTime t; t.mds = NullTime; return t; } const int hour(s.mid(0, 2).toInt()); const int minute(s.mid(3, 2).toInt()); const int second(s.mid(6, 2).toInt()); const QString msec_s(QLatin1String("0.") + s.mid(9, 4)); const float msec(msec_s.toFloat()); return QTime(hour, minute, second, qMin(qRound(msec * 1000.0), 999));}/*! \fn QTime::fromString(const QString &string, const QString &format) Returns the QTime represented by the \a string, using the \a format given, or an invalid time if the string cannot be parsed. These expressions may be used for the format: \table \header \i Expression \i Output \row \i h \i the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) \row \i hh \i the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) \row \i m \i the minute without a leading zero (0 to 59) \row \i mm \i the minute with a leading zero (00 to 59) \row \i s \i the second without a leading zero (0 to 59) \row \i ss \i the second with a leading zero (00 to 59) \row \i z \i the milliseconds without leading zeroes (0 to 999) \row \i zzz \i the milliseconds with leading zeroes (000 to 999) \row \i AP \i interpret as an AM/PM time. \e AP must be either "AM" or "PM". \row \i ap \i Interpret as an AM/PM time. \e ap must be either "am" or "pm". \endtable All other input characters will be treated as text. Any sequence of characters that are enclosed in single quotes will also be treated as text and not be used as an expression. \code QTime time = QTime::fromString("1mm12car00", "m'mm'hcarss"); // time is 12:01.00 \endcode If the format is not satisfied an invalid QTime is returned. Expressions that do not expect leading zeroes to be given (h, m, s and z) are greedy. This means that they will use two digits even if this puts them outside the range of accepted values and leaves too few digits for other sections. For example, the following string could have meant 00:07:10, but the m will grab two digits, resulting in an invalid time: \code QTime time = QTime::fromString("00:710", "hh:ms"); // invalid \endcode Any field that is not represented in the format will be set to zero. For example: \code QTime time = QTime::fromString("1.30", "m.s"); // time is 00:01:30.000 \endcode \sa QDateTime::fromString() QDate::fromString() QDate::toString() QDateTime::toString() QTime::toString()*/QTime QTime::fromString(const QString &string, const QString &format){ QTime time;#ifndef QT_BOOTSTRAPPED QDateTimeParser dt(QVariant::Time); if (dt.parseFormat(format)) dt.fromString(string, 0, &time);#else Q_UNUSED(string); Q_UNUSED(format);#endif return time;}#endif // QT_NO_DATESTRING/*! \overload Returns true if the specified time is valid; otherwise returns false. The time is valid if \a h is in the range 0 to 23, \a m and \a s are in the range 0 to 59, and \a ms is in the range 0 to 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 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 \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() that 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 that takes a POSIX-standard "number of seconds since 00:00:00 on January 1, 1970" value. The fromString()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -