QTime Class

The QTime class provides clock time functions. More...

Header: #include <QTime>
qmake: QT += core

Note: All functions in this class are reentrant.

Public Functions

QTime(int h, int m, int s = 0, int ms = 0)
QTime addMSecs(int ms) const
QTime addSecs(int s) const
int elapsed() const
int hour() const
bool isValid() const
int minute() const
int msec() const
int msecsTo(const QTime &t) const
int restart()
int second() const
int secsTo(const QTime &t) const
bool setHMS(int h, int m, int s, int ms = 0)
void start()
QString toString(Qt::DateFormat format = Qt::TextDate) const

Static Public Members

bool isValid(int h, int m, int s, int ms = 0)
QDataStream &operator<<(QDataStream &out, const QTime &time)
QDataStream &operator>>(QDataStream &in, QTime &time)

Detailed Description

A QTime object contains a clock time, which it can express as the numbers 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-saving 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.

The addSecs() and addMSecs() functions provide the time a given number of seconds or milliseconds later than a given time. Correspondingly, the number of seconds or milliseconds between two times can be found using secsTo() or msecsTo().

QTime provides a full set of operators to compare two QTime objects; an earlier time is considered smaller than a later one; if A.msecsTo(B) is positive, then A < B.

QTime can be used to measure a span of elapsed time using the start(), restart(), and elapsed() functions.

See also QDate and QDateTime.

Member Function Documentation

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.

See also isValid().

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.

Returns a null time if this time is invalid.

See also addSecs(), msecsTo(), and QDateTime::addMSecs().

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.

Returns a null time if this time is invalid.

Example:


  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

See also addMSecs(), secsTo(), and QDateTime::addSecs().

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.

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-saving time is turned on or off.

See also start() and restart().

int QTime::hour() const

Returns the hour part (0 to 23) of the time.

Returns -1 if the time is invalid.

See also minute(), second(), and msec().

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.

See also isNull().

[static] bool QTime::isValid(int h, int m, int s, int ms = 0)

This is an overloaded function.

Returns true if the specified time is valid; otherwise returns false.

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:


  QTime::isValid(21, 10, 30); // returns true
  QTime::isValid(22, 5,  62); // returns false

int QTime::minute() const

Returns the minute part (0 to 59) of the time.

Returns -1 if the time is invalid.

See also hour(), second(), and msec().

int QTime::msec() const

Returns the millisecond part (0 to 999) of the time.

Returns -1 if the time is invalid.

See also hour(), minute(), and second().

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.

Returns 0 if either time is invalid.

See also secsTo(), addMSecs(), and QDateTime::msecsTo().

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().

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-saving time is turned on or off.

See also start(), elapsed(), and currentTime().

int QTime::second() const

Returns the second part (0 to 59) of the time.

Returns -1 if the time is invalid.

See also hour(), minute(), and msec().

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.

Returns 0 if either time is invalid.

See also addSecs() and 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.

See also isValid().

void QTime::start()

Sets this time to the current time. This is practical for timing:


  QTime t;
  t.start();
  some_lengthy_task();
  qDebug("Time elapsed: %d ms", t.elapsed());

See also restart(), elapsed(), and currentTime().

QString QTime::toString(Qt::DateFormat format = Qt::TextDate) const

This is an overloaded function.

Returns the time as a string. 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, represented by HH:mm:ss. To include milliseconds in the ISO 8601 date, use the format Qt::ISODateWithMs, which corresponds to HH:mm:ss.zzz.

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 format is Qt::RFC2822Date, the string is formatted in an RFC 2822 compatible way. An example of this formatting is "23:59:20".

If the time is invalid, an empty string will be returned.

See also fromString(), QDate::toString(), QDateTime::toString(), and QLocale::toString().

Related Non-Members

QDataStream &QTime::operator<<(QDataStream &out, const QTime &time)

Writes time to stream out.

See also Serializing Qt Data Types.

QDataStream &QTime::operator>>(QDataStream &in, QTime &time)

Reads a time from stream in into the given time.

See also Serializing Qt Data Types.