Qt 4.8
Public Functions | Static Public Functions | Private Types | Private Functions | Properties | Friends | List of all members
QTime Class Reference

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 QDataStreamoperator<< (QDataStream &, const QTime &)
 Writes time to stream out. More...
 
Q_CORE_EXPORT QDataStreamoperator>> (QDataStream &, QTime &)
 Reads a time from stream in into the given time. More...
 
class QDateTime
 
class QDateTimePrivate
 

Detailed Description

The QTime class provides clock time functions.

Note
This class or function is reentrant.

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.

See also
QDate, QDateTime

Definition at line 148 of file qdatetime.h.

Enumerations

◆ TimeFlag

enum QTime::TimeFlag
private
Enumerator
NullTime 

Definition at line 198 of file qdatetime.h.

198 { NullTime = -1 };

Constructors and Destructors

◆ QTime() [1/2]

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

See also
isNull(), isValid()

Definition at line 151 of file qdatetime.h.

151  : mds(NullTime)
152 #if defined(Q_OS_WINCE)
154 #endif
155  {}
int startTick
Definition: qdatetime.h:202
int mds
Definition: qdatetime.h:200

◆ QTime() [2/2]

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

Definition at line 1540 of file qdatetime.cpp.

1541 {
1542  setHMS(h, m, s, ms);
1543 }
bool setHMS(int h, int m, int s, int ms=0)
Sets the time to hour h, minute m, seconds s and milliseconds ms.
Definition: qdatetime.cpp:1744

Functions

◆ addMSecs()

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.

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

Definition at line 1803 of file qdatetime.cpp.

Referenced by fromTime(), fromTimeStamp(), QTDSResult::gotoNext(), operator+(), operator-(), QSvgTinyDocument::setCurrentFrame(), and QDateTime::setMSecsSinceEpoch().

1804 {
1805  QTime t;
1806  if (ms < 0) {
1807  // % not well-defined for -ve, but / is.
1808  int negdays = (MSECS_PER_DAY - ms) / MSECS_PER_DAY;
1809  t.mds = (ds() + ms + negdays * MSECS_PER_DAY) % MSECS_PER_DAY;
1810  } else {
1811  t.mds = (ds() + ms) % MSECS_PER_DAY;
1812  }
1813 #if defined(Q_OS_WINCE)
1814  if (startTick > NullTime)
1815  t.startTick = (startTick + ms) % MSECS_PER_DAY;
1816 #endif
1817  return t;
1818 }
int ds() const
Definition: qdatetime.h:199
The QTime class provides clock time functions.
Definition: qdatetime.h:148
int startTick
Definition: qdatetime.h:202
int mds
Definition: qdatetime.h:200

◆ addSecs()

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:

QTime n(14, 0, 0); // n == 14:00:00
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(), QDateTime::addSecs()

Definition at line 1770 of file qdatetime.cpp.

Referenced by QDateTime::setTime_t().

1771 {
1772  return addMSecs(s * 1000);
1773 }
QTime addMSecs(int ms) const
Returns a QTime object containing a time ms milliseconds later than the time of this object (or earli...
Definition: qdatetime.cpp:1803

◆ currentTime()

QTime QTime::currentTime ( )
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();

See also
QDateTime::toUTC()

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

3126 {
3127  QTime ct;
3128  SYSTEMTIME st;
3129  memset(&st, 0, sizeof(SYSTEMTIME));
3130  GetLocalTime(&st);
3131  ct.mds = msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
3132 #if defined(Q_OS_WINCE)
3133  ct.startTick = GetTickCount() % MSECS_PER_DAY;
3134 #endif
3135  return ct;
3136 }
static uint msecsFromDecomposed(int hour, int minute, int sec, int msec=0)
Definition: qdatetime.cpp:3109
The QTime class provides clock time functions.
Definition: qdatetime.h:148
int startTick
Definition: qdatetime.h:202
int mds
Definition: qdatetime.h:200
#define st(var, type, card)

◆ ds()

int QTime::ds ( ) const
inlineprivate

Definition at line 199 of file qdatetime.h.

Referenced by QDateTimePrivate::addMSecs(), msecsTo(), secsTo(), and QDateTime::toMSecsSinceEpoch().

199 { return mds == -1 ? 0 : mds; }
int mds
Definition: qdatetime.h:200

◆ elapsed()

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 savings time is turned on or off.
See also
start(), restart()

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

2124 {
2125  int n = msecsTo(currentTime());
2126  if (n < 0) // passed midnight
2127  n += 86400 * 1000;
2128  return n;
2129 }
int msecsTo(const QTime &) const
Returns the number of milliseconds from this time to t.
Definition: qdatetime.cpp:1832
static QTime currentTime()
Returns the current time as reported by the system clock.
Definition: qdatetime.cpp:3125

◆ fromString() [1/2]

QTime QTime::fromString ( const QString string,
Qt::DateFormat  format = Qt::TextDate 
)
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().

1929 {
1930  if (s.isEmpty()) {
1931  QTime t;
1932  t.mds = NullTime;
1933  return t;
1934  }
1935 
1936  switch (f) {
1937  case Qt::SystemLocaleDate:
1942  case Qt::LocaleDate:
1945  return fromString(s, QLocale().timeFormat(f == Qt::DefaultLocaleLongDate ? QLocale::LongFormat
1947  default:
1948  {
1949  bool ok = true;
1950  const int hour(s.mid(0, 2).toInt(&ok));
1951  if (!ok)
1952  return QTime();
1953  const int minute(s.mid(3, 2).toInt(&ok));
1954  if (!ok)
1955  return QTime();
1956  const int second(s.mid(6, 2).toInt(&ok));
1957  if (!ok)
1958  return QTime();
1959  const QString msec_s(QLatin1String("0.") + s.mid(9, 4));
1960  const float msec(msec_s.toFloat(&ok));
1961  if (!ok)
1962  return QTime(hour, minute, second, 0);
1963  return QTime(hour, minute, second, qMin(qRound(msec * 1000.0), 999));
1964  }
1965  }
1966 }
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int msec() const
Returns the millisecond part (0 to 999) of the time.
Definition: qdatetime.cpp:1611
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QLocale system()
Returns a QLocale object initialized to the system locale.
Definition: qlocale.cpp:1917
The QTime class provides clock time functions.
Definition: qdatetime.h:148
QTime()
Constructs a null time object.
Definition: qdatetime.h:151
int second() const
Returns the second part (0 to 59) of the time.
Definition: qdatetime.cpp:1600
int minute() const
Returns the minute part (0 to 59) of the time.
Definition: qdatetime.cpp:1589
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 t...
Definition: qdatetime.cpp:1928
int mds
Definition: qdatetime.h:200
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
int hour() const
Returns the hour part (0 to 23) of the time.
Definition: qdatetime.cpp:1578

◆ fromString() [2/2]

QTime QTime::fromString ( const QString string,
const QString format 
)
static

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.

QTime time = QTime::fromString("1mm12car00", "m'mm'hcarss");
// time is 12:01.00

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:

QTime time = QTime::fromString("00:710", "hh:ms"); // invalid

Any field that is not represented in the format will be set to zero. For example:

QTime time = QTime::fromString("1.30", "m.s");
// time is 00:01:30.000
See also
QDateTime::fromString() QDate::fromString() QDate::toString() QDateTime::toString() QTime::toString()

Definition at line 2022 of file qdatetime.cpp.

2023 {
2024  QTime time;
2025 #ifndef QT_BOOTSTRAPPED
2027  if (dt.parseFormat(format))
2028  dt.fromString(string, 0, &time);
2029 #else
2030  Q_UNUSED(string);
2031  Q_UNUSED(format);
2032 #endif
2033  return time;
2034 }
The QTime class provides clock time functions.
Definition: qdatetime.h:148
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ hour()

int QTime::hour ( ) const

◆ isNull()

bool QTime::isNull ( ) const
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.

See also
isValid()

Definition at line 158 of file qdatetime.h.

Referenced by QSvgTinyDocument::draw(), QDateTime::isNull(), parseDateString(), and QDateTimeToDATE().

158 { return mds == NullTime; }
int mds
Definition: qdatetime.h:200

◆ isValid() [1/2]

bool QTime::isValid ( ) const

◆ isValid() [2/2]

bool QTime::isValid ( int  h,
int  m,
int  s,
int  ms = 0 
)
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:

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

Definition at line 2056 of file qdatetime.cpp.

2057 {
2058  return (uint)h < 24 && (uint)m < 60 && (uint)s < 60 && (uint)ms < 1000;
2059 }
unsigned int uint
Definition: qglobal.h:996

◆ minute()

int QTime::minute ( ) const

◆ msec()

int QTime::msec ( ) const

◆ msecsTo()

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.

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

Definition at line 1832 of file qdatetime.cpp.

Referenced by localToUtc(), QDateTime::msecsTo(), QWindowsVistaTransition::paint(), QWindowsVistaPulse::paint(), QDateTimeParser::parse(), toTime(), toTimeStamp(), and utcToLocal().

1833 {
1834 #if defined(Q_OS_WINCE)
1835  // GetLocalTime() for Windows CE has no milliseconds resolution
1836  if (t.startTick > NullTime && startTick > NullTime)
1837  return t.startTick - startTick;
1838  else
1839 #endif
1840  return t.ds() - ds();
1841 }
int ds() const
Definition: qdatetime.h:199
int startTick
Definition: qdatetime.h:202

◆ operator!=()

bool QTime::operator!= ( const QTime other) const
inline

Returns true if this time is different from t; otherwise returns false.

Definition at line 177 of file qdatetime.h.

177 { return mds != other.mds; }
int mds
Definition: qdatetime.h:200

◆ operator<()

bool QTime::operator< ( const QTime other) const
inline

Returns true if this time is earlier than t; otherwise returns false.

Definition at line 178 of file qdatetime.h.

178 { return mds < other.mds; }
int mds
Definition: qdatetime.h:200

◆ operator<=()

bool QTime::operator<= ( const QTime other) const
inline

Returns true if this time is earlier than or equal to t; otherwise returns false.

Definition at line 179 of file qdatetime.h.

179 { return mds <= other.mds; }
int mds
Definition: qdatetime.h:200

◆ operator==()

bool QTime::operator== ( const QTime other) const
inline

Returns true if this time is equal to t; otherwise returns false.

Definition at line 176 of file qdatetime.h.

176 { return mds == other.mds; }
int mds
Definition: qdatetime.h:200

◆ operator>()

bool QTime::operator> ( const QTime other) const
inline

Returns true if this time is later than t; otherwise returns false.

Definition at line 180 of file qdatetime.h.

180 { return mds > other.mds; }
int mds
Definition: qdatetime.h:200

◆ operator>=()

bool QTime::operator>= ( const QTime other) const
inline

Returns true if this time is later than or equal to t; otherwise returns false.

Definition at line 181 of file qdatetime.h.

181 { return mds >= other.mds; }
int mds
Definition: qdatetime.h:200

◆ restart()

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 savings time is turned on or off.
See also
start(), elapsed(), currentTime()

Definition at line 2095 of file qdatetime.cpp.

Referenced by QAudioOutputPrivate::freeBlocks(), QAudioInputPrivate::freeBlocks(), qt_flush(), and QSvgTinyDocument::restartAnimation().

2096 {
2097  QTime t = currentTime();
2098  int n = msecsTo(t);
2099  if (n < 0) // passed midnight
2100  n += 86400*1000;
2101  *this = t;
2102  return n;
2103 }
int msecsTo(const QTime &) const
Returns the number of milliseconds from this time to t.
Definition: qdatetime.cpp:1832
The QTime class provides clock time functions.
Definition: qdatetime.h:148
static QTime currentTime()
Returns the current time as reported by the system clock.
Definition: qdatetime.cpp:3125

◆ second()

int QTime::second ( ) const

◆ secsTo()

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.

See also
addSecs(), QDateTime::secsTo()

Definition at line 1788 of file qdatetime.cpp.

Referenced by QDateTime::secsTo().

1789 {
1790  return (t.ds() - ds()) / 1000;
1791 }
int ds() const
Definition: qdatetime.h:199

◆ setHMS()

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

Definition at line 1744 of file qdatetime.cpp.

Referenced by operator>>().

1745 {
1746 #if defined(Q_OS_WINCE)
1747  startTick = NullTime;
1748 #endif
1749  if (!isValid(h,m,s,ms)) {
1750  mds = NullTime; // make this invalid
1751  return false;
1752  }
1753  mds = (h*SECS_PER_HOUR + m*SECS_PER_MIN + s)*1000 + ms;
1754  return true;
1755 }
int startTick
Definition: qdatetime.h:202
int mds
Definition: qdatetime.h:200
bool isValid() const
Returns true if the time is valid; otherwise returns false.
Definition: qdatetime.cpp:1566

◆ start()

void QTime::start ( )

Sets this time to the current time.

This is practical for timing:

t.start();
some_lengthy_task();
qDebug("Time elapsed: %d ms", t.elapsed());
See also
restart(), elapsed(), currentTime()

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

2071 {
2072  *this = currentTime();
2073 }
static QTime currentTime()
Returns the current time as reported by the system clock.
Definition: qdatetime.cpp:3125

◆ toString() [1/2]

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

1654 {
1655  if (!isValid())
1656  return QString();
1657 
1658  switch (format) {
1659  case Qt::SystemLocaleDate:
1664  case Qt::LocaleDate:
1668  : QLocale::ShortFormat);
1669 
1670  default:
1671  case Qt::ISODate:
1672  case Qt::TextDate:
1673  return QString::fromLatin1("%1:%2:%3")
1674  .arg(hour(), 2, 10, QLatin1Char('0'))
1675  .arg(minute(), 2, 10, QLatin1Char('0'))
1676  .arg(second(), 2, 10, QLatin1Char('0'));
1677  }
1678 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString toString(qlonglong i) const
Returns a localized string representation of i.
Definition: qlocale.cpp:1295
static QLocale system()
Returns a QLocale object initialized to the system locale.
Definition: qlocale.cpp:1917
int second() const
Returns the second part (0 to 59) of the time.
Definition: qdatetime.cpp:1600
int minute() const
Returns the minute part (0 to 59) of the time.
Definition: qdatetime.cpp:1589
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
bool isValid() const
Returns true if the time is valid; otherwise returns false.
Definition: qdatetime.cpp:1566
int hour() const
Returns the hour part (0 to 23) of the time.
Definition: qdatetime.cpp:1578

◆ toString() [2/2]

QString QTime::toString ( const QString format) const

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.

See also
QDate::toString() QDateTime::toString()

Definition at line 1728 of file qdatetime.cpp.

1729 {
1730  return fmtDateTime(format, this, 0);
1731 }
static QString fmtDateTime(const QString &f, const QTime *dt=0, const QDate *dd=0)
Definition: qdatetime.cpp:4133

Friends and Related Functions

◆ operator<<

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

Writes time to stream out.

See also
{Serializing Qt Data Types}

Definition at line 3847 of file qdatetime.cpp.

3848 {
3849  return out << quint32(time.mds);
3850 }
int mds
Definition: qdatetime.h:200
unsigned int quint32
Definition: qglobal.h:938

◆ operator>>

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

Reads a time from stream in into the given time.

See also
{Serializing Qt Data Types}

Definition at line 3863 of file qdatetime.cpp.

3864 {
3865  quint32 ds;
3866  in >> ds;
3867  time.mds = int(ds);
3868  return in;
3869 }
int ds() const
Definition: qdatetime.h:199
int mds
Definition: qdatetime.h:200
unsigned int quint32
Definition: qglobal.h:938

◆ QDateTime

friend class QDateTime
friend

Definition at line 205 of file qdatetime.h.

◆ QDateTimePrivate

friend class QDateTimePrivate
friend

Definition at line 206 of file qdatetime.h.

Properties

◆ mds

int QTime::mds
private

◆ startTick

int QTime::startTick
private

Definition at line 202 of file qdatetime.h.

Referenced by addMSecs(), currentTime(), and msecsTo().


The documentation for this class was generated from the following files: