Qt 4.8
|
The QTime class provides clock time functions. More...
#include <qdatetime.h>
Public Functions | |
QTime | addMSecs (int ms) const |
Returns a QTime object containing a time ms milliseconds later than the time of this object (or earlier if ms is negative). More... | |
QTime | addSecs (int secs) const |
Returns a QTime object containing a time s seconds later than the time of this object (or earlier if s is negative). More... | |
int | elapsed () const |
Returns the number of milliseconds that have elapsed since the last time start() or restart() was called. More... | |
int | hour () const |
Returns the hour part (0 to 23) of the time. More... | |
bool | isNull () const |
Returns true if the time is null (i. More... | |
bool | isValid () const |
Returns true if the time is valid; otherwise returns false. More... | |
int | minute () const |
Returns the minute part (0 to 59) of the time. More... | |
int | msec () const |
Returns the millisecond part (0 to 999) of the time. More... | |
int | msecsTo (const QTime &) const |
Returns the number of milliseconds from this time to t. More... | |
bool | operator!= (const QTime &other) const |
Returns true if this time is different from t; otherwise returns false. More... | |
bool | operator< (const QTime &other) const |
Returns true if this time is earlier than t; otherwise returns false. More... | |
bool | operator<= (const QTime &other) const |
Returns true if this time is earlier than or equal to t; otherwise returns false. More... | |
bool | operator== (const QTime &other) const |
Returns true if this time is equal to t; otherwise returns false. More... | |
bool | operator> (const QTime &other) const |
Returns true if this time is later than t; otherwise returns false. More... | |
bool | operator>= (const QTime &other) const |
Returns true if this time is later than or equal to t; otherwise returns false. More... | |
QTime () | |
Constructs a null time object. More... | |
QTime (int h, int m, int s=0, int ms=0) | |
Constructs a time with hour h, minute m, seconds s and milliseconds ms. More... | |
int | restart () |
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. More... | |
int | second () const |
Returns the second part (0 to 59) of the time. More... | |
int | secsTo (const QTime &) const |
Returns the number of seconds from this time to t. More... | |
bool | setHMS (int h, int m, int s, int ms=0) |
Sets the time to hour h, minute m, seconds s and milliseconds ms. More... | |
void | start () |
Sets this time to the current time. More... | |
QString | toString (Qt::DateFormat f=Qt::TextDate) const |
Returns the time as a string. More... | |
QString | toString (const QString &format) const |
Returns the time as a string. More... | |
Static Public Functions | |
static QTime | currentTime () |
Returns the current time as reported by the system clock. More... | |
static QTime | fromString (const QString &s, Qt::DateFormat f=Qt::TextDate) |
Returns the time represented in the string as a QTime using the format given, or an invalid time if this is not possible. More... | |
static QTime | fromString (const QString &s, const QString &format) |
Returns the QTime represented by the string, using the format given, or an invalid time if the string cannot be parsed. More... | |
static bool | isValid (int h, int m, int s, int ms=0) |
Returns true if the specified time is valid; otherwise returns false. More... | |
Private Types | |
enum | TimeFlag { NullTime = -1 } |
Private Functions | |
int | ds () const |
Properties | |
int | mds |
int | startTick |
Friends | |
Q_CORE_EXPORT QDataStream & | operator<< (QDataStream &, const QTime &) |
Writes time to stream out. More... | |
Q_CORE_EXPORT QDataStream & | operator>> (QDataStream &, QTime &) |
Reads a time from stream in into the given time. More... | |
class | QDateTime |
class | QDateTimePrivate |
The QTime class provides clock time functions.
A QTime object contains a clock time, i.e. the number of hours, minutes, seconds, and milliseconds since midnight. It can read the current time from the system clock and measure a span of elapsed time. It provides functions for comparing times and for manipulating a time by adding a number of milliseconds.
QTime uses the 24-hour clock format; it has no concept of AM/PM. Unlike QDateTime, QTime knows nothing about time zones or daylight savings time (DST).
A QTime object is typically created either by giving the number of hours, minutes, seconds, and milliseconds explicitly, or by using the static function currentTime(), which creates a QTime object that contains the system's local time. Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.
The hour(), minute(), second(), and msec() functions provide access to the number of hours, minutes, seconds, and milliseconds of the time. The same information is provided in textual format by the toString() function.
QTime provides a full set of operators to compare two QTime objects. One time is considered smaller than another if it is earlier than the other.
The time a given number of seconds or milliseconds later than a given time can be found using the addSecs() or addMSecs() functions. Correspondingly, the number of seconds or milliseconds between two times can be found using secsTo() or msecsTo().
QTime can be used to measure a span of elapsed time using the start(), restart(), and elapsed() functions.
Definition at line 148 of file qdatetime.h.
|
private |
|
inline |
Constructs a null time object.
A null time can be a QTime(0, 0, 0, 0) (i.e., midnight) object, except that isNull() returns true and isValid() returns false.
Definition at line 151 of file qdatetime.h.
QTime::QTime | ( | int | h, |
int | m, | ||
int | s = 0 , |
||
int | ms = 0 |
||
) |
Constructs a time with hour h, minute m, seconds s and milliseconds ms.
h must be in the range 0 to 23, m and s must be in the range 0 to 59, and ms must be in the range 0 to 999.
Definition at line 1540 of file qdatetime.cpp.
QTime QTime::addMSecs | ( | int | ms | ) | const |
Returns a QTime object containing a time ms milliseconds later than the time of this object (or earlier if ms is negative).
Note that the time will wrap if it passes midnight. See addSecs() for an example.
Definition at line 1803 of file qdatetime.cpp.
Referenced by fromTime(), fromTimeStamp(), QTDSResult::gotoNext(), operator+(), operator-(), QSvgTinyDocument::setCurrentFrame(), and QDateTime::setMSecsSinceEpoch().
QTime QTime::addSecs | ( | int | s | ) | const |
Returns a QTime object containing a time s seconds later than the time of this object (or earlier if s is negative).
Note that the time will wrap if it passes midnight.
Example:
Definition at line 1770 of file qdatetime.cpp.
Referenced by QDateTime::setTime_t().
|
static |
Returns the current time as reported by the system clock.
Returns the current time for the given specification.
Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.
To replace uses of this function where the specification is Qt::LocalTime, use the currentDate() overload that takes no parameters instead; otherwise, use QDateTime::currentDateTime() and convert the result to a UTC measurement.
QTime localTime = QTime::currentTime(Qt::LocalTime); QTime utcTime = QTime::currentTime(Qt::UTC); QTime localTime = QTime::currentTime(); QTime utcTime = QTimeTime::currentDateTime().toUTC().time();
Definition at line 3125 of file qdatetime.cpp.
Referenced by QDateTime::currentMSecsSinceEpoch(), QWindowsVistaStyle::drawComplexControl(), QWindowsVistaStyle::drawControl(), QWindowsVistaStyle::drawPrimitive(), QSslSocketPrivate::ensureLibraryLoaded(), QScreen::exposeRegion(), QAudioInputPrivate::feedback(), QAudioOutputPrivate::freeBlocks(), QAudioInputPrivate::freeBlocks(), QAudioInputPrivate::open(), QAudioOutputPrivate::open(), QWindowsVistaTransition::paint(), QWindowsVistaPulse::paint(), qEncodeNtlmv2Response(), QHttpMultiPartPrivate::QHttpMultiPartPrivate(), qt_flush(), QAudioOutputPrivate::updateAvailable(), QAudioOutputPrivate::userFeed(), and QAudioInputPrivate::userFeed().
|
inlineprivate |
Definition at line 199 of file qdatetime.h.
Referenced by QDateTimePrivate::addMSecs(), msecsTo(), secsTo(), and QDateTime::toMSecsSinceEpoch().
int QTime::elapsed | ( | ) | const |
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.
Definition at line 2123 of file qdatetime.cpp.
Referenced by QIBaseDriver::close(), QSvgTinyDocument::currentFrame(), QAudioInputPrivate::feedback(), QAudioOutputPrivate::freeBlocks(), QWindowSystemInterface::handleExtendedKeyEvent(), QWindowSystemInterface::handleKeyEvent(), QWindowSystemInterface::handleMouseEvent(), QWindowSystemInterface::handleTouchEvent(), QWindowSystemInterface::handleWheelEvent(), QIncrementalSleepTimer::hasTimedOut(), QmlJSDebugger::LiveSelectionTool::mouseMoveEvent(), QMoviePrivate::next(), QDeclarativeView::paintEvent(), qt_flush(), QWSServerPrivate::sendMouseEventUnfiltered(), QSvgTinyDocument::setCurrentFrame(), QIncrementalSleepTimer::timeLeft(), QUnixSocket::waitForBytesWritten(), and QUnixSocket::waitForReadyRead().
|
static |
Returns the time represented in the string as a QTime using the format given, or an invalid time if this is not possible.
Note that fromString() uses a "C" locale encoded string to convert milliseconds to a float value. If the default locale is not "C", this may result in two conversion attempts (if the conversion fails for the default locale). This should be considered an implementation detail.
Definition at line 1928 of file qdatetime.cpp.
Referenced by convert(), QPSQLResult::data(), QDateTime::fromString(), QTDSResult::gotoNext(), qTimeFromString(), and QDeclarativeStringConverters::timeFromString().
Returns the QTime represented by the string, using the format given, or an invalid time if the string cannot be parsed.
These expressions may be used for the format:
Expression | Output |
h | the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) |
hh | the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) |
m | the minute without a leading zero (0 to 59) |
mm | the minute with a leading zero (00 to 59) |
s | the second without a leading zero (0 to 59) |
ss | the second with a leading zero (00 to 59) |
z | the milliseconds without leading zeroes (0 to 999) |
zzz | the milliseconds with leading zeroes (000 to 999) |
AP | interpret as an AM/PM time. AP must be either "AM" or "PM". |
ap | Interpret as an AM/PM time. ap must be either "am" or "pm". |
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.
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:
Any field that is not represented in the format will be set to zero. For example:
Definition at line 2022 of file qdatetime.cpp.
int QTime::hour | ( | ) | const |
Returns the hour part (0 to 23) of the time.
Definition at line 1578 of file qdatetime.cpp.
Referenced by QScript::DateTimeToMs(), QLocalePrivate::dateTimeToString(), QDB2Result::exec(), QODBCResult::exec(), QPatternist::HoursFromAbstractDateTimeFN::extract(), QDB2Driver::formatValue(), QIBaseDriver::formatValue(), QOCIDriver::formatValue(), QDeclarativeCompiler::genLiteralAssignment(), QDateTimeParser::getDigit(), localToUtc(), macTimeToString(), operator<<(), QDateTimeParser::parse(), QDateTimeToDATE(), qMakeOraDate(), QDateTimeParser::setDigit(), QWSDisplay::setSelectionOwner(), QDateTimeEdit::stepBy(), QSystemLocalePrivate::toString(), QMYSQLResult::virtual_hook(), QPdfEnginePrivate::writeInfo(), and writeMSDosDate().
|
inline |
Returns true if the time is null (i.
e., the QTime object was constructed using the default constructor); otherwise returns false. A null time is also an invalid time.
Definition at line 158 of file qdatetime.h.
Referenced by QSvgTinyDocument::draw(), QDateTime::isNull(), parseDateString(), and QDateTimeToDATE().
bool QTime::isValid | ( | ) | const |
Returns true if the time is valid; otherwise returns false.
For example, the time 23:30:55.746 is valid, but 24:12:30 is invalid.
Definition at line 1566 of file qdatetime.cpp.
Referenced by convert(), QPatternist::AbstractDateTime::create(), QLocalePrivate::dateTimeToString(), QDateTimeEditPrivate::emitSignals(), fmtDateTime(), QSqlDriver::formatValue(), QIBaseDriver::formatValue(), QPSQLDriver::formatValue(), QDateTimeParser::fromString(), QDateTime::isValid(), operator<<(), QDateTime::QDateTime(), QDateTimeEdit::QDateTimeEdit(), QDateTimeToDATE(), QDateTime::setDate(), QDateTimeParser::setDigit(), QDateTimeEdit::setMaximumTime(), QDateTimeEdit::setMinimumTime(), QDateTimeEdit::setTime(), QDateTimeEdit::setTimeRange(), QDeclarativeStringConverters::timeFromString(), QTest::toString(), and QLocale::toString().
|
static |
Returns true if the specified time is valid; otherwise returns false.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
The time is valid if h is in the range 0 to 23, m and s are in the range 0 to 59, and ms is in the range 0 to 999.
Example:
Definition at line 2056 of file qdatetime.cpp.
int QTime::minute | ( | ) | const |
Returns the minute part (0 to 59) of the time.
Definition at line 1589 of file qdatetime.cpp.
Referenced by QScript::DateTimeToMs(), QLocalePrivate::dateTimeToString(), QDB2Result::exec(), QODBCResult::exec(), QPatternist::MinutesFromAbstractDateTimeFN::extract(), QDB2Driver::formatValue(), QIBaseDriver::formatValue(), QOCIDriver::formatValue(), QDeclarativeCompiler::genLiteralAssignment(), QDateTimeParser::getDigit(), localToUtc(), macTimeToString(), operator<<(), QDateTimeParser::parse(), QDateTimeToDATE(), qMakeOraDate(), QDateTimeParser::setDigit(), QWSDisplay::setSelectionOwner(), QDateTimeEdit::stepBy(), QSystemLocalePrivate::toString(), QMYSQLResult::virtual_hook(), QPdfEnginePrivate::writeInfo(), and writeMSDosDate().
int QTime::msec | ( | ) | const |
Returns the millisecond part (0 to 999) of the time.
Definition at line 1611 of file qdatetime.cpp.
Referenced by QScript::DateTimeToMs(), QLocalePrivate::dateTimeToString(), QSslSocketPrivate::ensureLibraryLoaded(), QDB2Result::exec(), QODBCResult::exec(), QAudioInputPrivate::feedback(), QDB2Driver::formatValue(), QIBaseDriver::formatValue(), QPSQLDriver::formatValue(), QAudioOutputPrivate::freeBlocks(), QAudioInputPrivate::freeBlocks(), QDeclarativeCompiler::genLiteralAssignment(), QDateTimeParser::getDigit(), localToUtc(), QAudioInputPrivate::open(), QAudioOutputPrivate::open(), operator-(), operator<<(), QDateTimeParser::parse(), QDateTimeToDATE(), QDateTimeParser::setDigit(), QWSDisplay::setSelectionOwner(), QPatternist::AbstractDateTime::timeToString(), QAudioOutputPrivate::updateAvailable(), QAudioOutputPrivate::userFeed(), QAudioInputPrivate::userFeed(), utcToLocal(), and QMYSQLResult::virtual_hook().
int QTime::msecsTo | ( | const QTime & | t | ) | const |
Returns the number of milliseconds from this time to t.
If 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.
Definition at line 1832 of file qdatetime.cpp.
Referenced by localToUtc(), QDateTime::msecsTo(), QWindowsVistaTransition::paint(), QWindowsVistaPulse::paint(), QDateTimeParser::parse(), toTime(), toTimeStamp(), and utcToLocal().
|
inline |
Returns true if this time is different from t; otherwise returns false.
Definition at line 177 of file qdatetime.h.
|
inline |
Returns true if this time is earlier than t; otherwise returns false.
Definition at line 178 of file qdatetime.h.
|
inline |
Returns true if this time is earlier than or equal to t; otherwise returns false.
Definition at line 179 of file qdatetime.h.
|
inline |
Returns true if this time is equal to t; otherwise returns false.
Definition at line 176 of file qdatetime.h.
|
inline |
Returns true if this time is later than t; otherwise returns false.
Definition at line 180 of file qdatetime.h.
|
inline |
Returns true if this time is later than or equal to t; otherwise returns false.
Definition at line 181 of file qdatetime.h.
int QTime::restart | ( | ) |
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().
Definition at line 2095 of file qdatetime.cpp.
Referenced by QAudioOutputPrivate::freeBlocks(), QAudioInputPrivate::freeBlocks(), qt_flush(), and QSvgTinyDocument::restartAnimation().
int QTime::second | ( | ) | const |
Returns the second part (0 to 59) of the time.
Definition at line 1600 of file qdatetime.cpp.
Referenced by QScript::DateTimeToMs(), QLocalePrivate::dateTimeToString(), QSslSocketPrivate::ensureLibraryLoaded(), QDB2Result::exec(), QODBCResult::exec(), QAudioInputPrivate::feedback(), QDB2Driver::formatValue(), QIBaseDriver::formatValue(), QOCIDriver::formatValue(), QAudioOutputPrivate::freeBlocks(), QAudioInputPrivate::freeBlocks(), QDeclarativeCompiler::genLiteralAssignment(), QDateTimeParser::getDigit(), localToUtc(), macTimeToString(), QAudioInputPrivate::open(), QAudioOutputPrivate::open(), operator<<(), QDateTimeParser::parse(), QDateTimeToDATE(), qMakeOraDate(), QDateTimeParser::setDigit(), QWSDisplay::setSelectionOwner(), QSystemLocalePrivate::toString(), QAudioOutputPrivate::updateAvailable(), QAudioOutputPrivate::userFeed(), QAudioInputPrivate::userFeed(), QMYSQLResult::virtual_hook(), QPdfEnginePrivate::writeInfo(), and writeMSDosDate().
int QTime::secsTo | ( | const QTime & | t | ) | const |
Returns the number of seconds from this time to t.
If 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.
secsTo() does not take into account any milliseconds.
Definition at line 1788 of file qdatetime.cpp.
Referenced by QDateTime::secsTo().
bool QTime::setHMS | ( | int | h, |
int | m, | ||
int | s, | ||
int | ms = 0 |
||
) |
Sets the time to hour h, minute m, seconds s and milliseconds ms.
h must be in the range 0 to 23, m and s must be in the range 0 to 59, and ms must be in the range 0 to 999. Returns true if the set time is valid; otherwise returns false.
Definition at line 1744 of file qdatetime.cpp.
Referenced by operator>>().
void QTime::start | ( | ) |
Sets this time to the current time.
This is practical for timing:
Definition at line 2070 of file qdatetime.cpp.
Referenced by QIBaseDriver::close(), QSvgTinyDocument::draw(), QmlJSDebugger::LiveSelectionTool::mousePressEvent(), QMoviePrivate::next(), QDeclarativeView::paintEvent(), QIncrementalSleepTimer::QIncrementalSleepTimer(), QUnixSocket::waitForBytesWritten(), and QUnixSocket::waitForReadyRead().
QString QTime::toString | ( | Qt::DateFormat | format = Qt::TextDate | ) | const |
Returns the time as a string.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Milliseconds are not included. The format parameter determines the format of the string.
If format is Qt::TextDate, the string format is HH:MM:SS; e.g. 1 second before midnight would be "23:59:59".
If format is Qt::ISODate, the string format corresponds to the ISO 8601 extended specification for representations of dates, which is also HH:mm:ss. (However, contrary to ISO 8601, dates before 15 October 1582 are handled as Julian dates, not Gregorian dates. See QDate G and J {Use of Gregorian and Julian Calendars}. This might change in a future version of Qt.)
If the format is Qt::SystemLocaleShortDate or Qt::SystemLocaleLongDate, the string format depends on the locale settings of the system. Identical to calling QLocale::system().toString(time, QLocale::ShortFormat) or QLocale::system().toString(time, QLocale::LongFormat).
If the format is Qt::DefaultLocaleShortDate or Qt::DefaultLocaleLongDate, the string format depends on the default application locale. This is the locale set with QLocale::setDefault(), or the system locale if no default locale has been set. Identical to calling QLocale().toString(time, QLocale::ShortFormat) or QLocale().toString(time, QLocale::LongFormat).
If the time is invalid, an empty string will be returned.
Definition at line 1653 of file qdatetime.cpp.
Referenced by QDeclarativeEnginePrivate::formatTime(), QSqlDriver::formatValue(), QODBCDriver::formatValue(), QPSQLDriver::formatValue(), operator<<(), QTest::toString(), and QDateTime::toString().
Returns the time as a string.
The format parameter determines the format of the result string.
These expressions may be used:
Expression | Output |
h | the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) |
hh | the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) |
H | the hour without a leading zero (0 to 23, even with AM/PM display) |
HH | the hour with a leading zero (00 to 23, even with AM/PM display) |
m | the minute without a leading zero (0 to 59) |
mm | the minute with a leading zero (00 to 59) |
s | the second without a leading zero (0 to 59) |
ss | the second with a leading zero (00 to 59) |
z | the milliseconds without leading zeroes (0 to 999) |
zzz | the milliseconds with leading zeroes (000 to 999) |
AP or A | use AM/PM display. AP will be replaced by either "AM" or "PM". |
ap or a | use am/pm display. ap will be replaced by either "am" or "pm". |
t | the timezone (for example "CEST") |
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)
Format | Result |
hh:mm:ss.zzz | 14:13:09.042 |
h:m:s ap | 2:13:9 pm |
H:m:s a | 14:13:9 pm |
If the datetime is invalid, an empty string will be returned. If format is empty, the default format "hh:mm:ss" is used.
Definition at line 1728 of file qdatetime.cpp.
|
friend |
|
friend |
Definition at line 205 of file qdatetime.h.
|
friend |
Definition at line 206 of file qdatetime.h.
|
private |
Definition at line 200 of file qdatetime.h.
Referenced by QDateTimePrivate::addMSecs(), addMSecs(), QDateTime::currentDateTime(), QDateTime::currentDateTimeUtc(), QDateTime::currentMSecsSinceEpoch(), currentTime(), fromString(), operator!=(), operator<(), operator<=(), operator==(), operator>(), and operator>=().
|
private |
Definition at line 202 of file qdatetime.h.
Referenced by addMSecs(), currentTime(), and msecsTo().