Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QTime Class Reference

\inmodule QtCore \reentrant More...

#include <qdatetime.h>

+ Collaboration diagram for QTime:

Public Member Functions

constexpr QTime ()
 Constructs a null time object.
 
 QTime (int h, int m, int s=0, int ms=0)
 Constructs a time with hour h, minute m, seconds s and milliseconds ms.
 
constexpr bool isNull () const
 Returns true if the time is null (i.e., the QTime object was constructed using the default constructor); otherwise returns false.
 
bool isValid () const
 Returns true if the time is valid; otherwise returns false.
 
int hour () const
 Returns the hour part (0 to 23) of the time.
 
int minute () const
 Returns the minute part (0 to 59) of the time.
 
int second () const
 Returns the second part (0 to 59) of the time.
 
int msec () const
 Returns the millisecond part (0 to 999) of the time.
 
bool setHMS (int h, int m, int s, int ms=0)
 Sets the time to hour h, minute m, seconds s and milliseconds ms.
 
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).
 
int secsTo (QTime t) const
 Returns the number of seconds from this time to t.
 
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).
 
int msecsTo (QTime t) const
 Returns the number of milliseconds from this time to t.
 
constexpr int msecsSinceStartOfDay () const
 Returns the number of msecs since the start of the day, i.e.
 

Static Public Member Functions

static constexpr QTime fromMSecsSinceStartOfDay (int msecs)
 Returns a new QTime instance with the time set to the number of msecs since the start of the day, i.e.
 
static QTime currentTime ()
 Returns the current time as reported by the system clock.
 
static bool isValid (int h, int m, int s, int ms=0)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns true if the specified time is valid; otherwise returns false.
 

Friends

class QDateTime
 
class QDateTimePrivate
 
constexpr bool comparesEqual (const QTime &lhs, const QTime &rhs) noexcept
 
constexpr Qt::strong_ordering compareThreeWay (const QTime &lhs, const QTime &rhs) noexcept
 
Q_CORE_EXPORT QDataStreamoperator<< (QDataStream &, QTime)
 Writes time to stream out.
 
Q_CORE_EXPORT QDataStreamoperator>> (QDataStream &, QTime &)
 Reads a time from stream in into the given time.
 

Related Symbols

(Note that these are not member symbols.)

QDataStreamoperator<< (QDataStream &out, QTime time)
 Writes time to stream out.
 
QDataStreamoperator>> (QDataStream &in, QTime &time)
 Reads a time from stream in into the given time.
 

Detailed Description

\inmodule QtCore \reentrant

The QTime class provides clock time functions.

\compares strong

A QTime object contains a clock time, which it can express as the numbers of hours, minutes, seconds, and milliseconds since midnight. It provides functions for comparing times and for manipulating a time by adding a number of milliseconds. QTime objects should be passed by value rather than by reference to const; they simply package int.

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 represents the system's local time.

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 objects can also be created from a text representation using fromString() and converted to a string representation using toString(). All conversion to and from string formats is done using the C locale. For localized conversions, see QLocale.

See also
QDate, QDateTime

Definition at line 214 of file qdatetime.h.

Constructor & Destructor Documentation

◆ QTime() [1/2]

QTime::QTime ( )
inlineconstexpr

Constructs a null time object.

For a null time, isNull() returns true and isValid() returns false. If you need a zero time, use QTime(0, 0). For the start of a day, see QDate::startOfDay().

See also
isNull(), isValid()

Definition at line 219 of file qdatetime.h.

◆ 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 2017 of file qdatetime.cpp.

References setHMS().

+ Here is the call graph for this function:

Member Function Documentation

◆ 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.

Returns a null time if this time is invalid.

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

Definition at line 2341 of file qdatetime.cpp.

References isValid().

Referenced by addSecs().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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.

Returns a null time if this time is invalid.

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 2297 of file qdatetime.cpp.

References addMSecs(), QtPrivate::DateTimeConstants::MSECS_PER_SEC, and QtPrivate::DateTimeConstants::SECS_PER_DAY.

Referenced by toEarliest(), and toLatest().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ currentTime()

QTime::currentTime ( )
static

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.

Furthermore, currentTime() only increases within each day; it shall drop by 24 hours each time midnight passes; and, beside this, changes in it may not correspond to elapsed time, if a daylight-saving transition intervenes.

See also
QDateTime::currentDateTime(), QDateTime::currentDateTimeUtc()

Referenced by QWindowsVistaStylePrivate::animationTime(), QXcbDrag::drop(), QSGOpenVGRenderLoop::renderWindow(), QSGSoftwareRenderLoop::renderWindow(), and QXcbDrag::timerEvent().

+ Here is the caller graph for this function:

◆ fromMSecsSinceStartOfDay()

QTime QTime::fromMSecsSinceStartOfDay ( int msecs)
inlinestaticconstexpr

Returns a new QTime instance with the time set to the number of msecs since the start of the day, i.e.

since 00:00:00.

If msecs falls outside the valid range an invalid QTime will be returned.

See also
msecsSinceStartOfDay()

Definition at line 243 of file qdatetime.h.

Referenced by getDateTime(), msecsToTime(), setDateTime(), and QMediaMetaData::stringValue().

+ Here is the caller graph for this function:

◆ hour()

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

Definition at line 2054 of file qdatetime.cpp.

References isValid(), and QtPrivate::DateTimeConstants::MSECS_PER_HOUR.

Referenced by QOCIDateTime::QOCIDateTime(), QCalendarBackend::dateTimeToString(), QDB2Result::exec(), QMYSQLResult::exec(), QODBCResult::exec(), QDB2Driver::formatValue(), QIBaseDriver::formatValue(), QOCIDriver::formatValue(), QV4::Date::init(), operator<<(), QDateTimeParser::setDigit(), QDateTimeEdit::stepBy(), toFileTime(), and QSystemLocalePrivate::toString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isNull()

bool QTime::isNull ( ) const
inlineconstexpr

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 223 of file qdatetime.h.

Referenced by parseDateString().

+ Here is the caller graph for this function:

◆ isValid() [1/2]

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

Definition at line 2040 of file qdatetime.cpp.

References QtPrivate::DateTimeConstants::MSECS_PER_DAY.

Referenced by QDateTimeEdit::QDateTimeEdit(), addMSecs(), QIBaseDriver::formatValue(), QSqlDriver::formatValue(), hour(), QV4::Date::init(), minute(), msec(), msecsTo(), msecsTo(), QNmeaPositionInfoSourcePrivate::notifyNewUpdate(), operator<<(), second(), secsTo(), setDateTime(), setHMS(), QDateTimeEdit::setMaximumTime(), QDateTimeEdit::setTime(), QDateTimeEdit::setTimeRange(), timeFromString(), QAsn1Element::toDateTime(), and QLocale::toString().

+ Here is the caller graph for this function:

◆ isValid() [2/2]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.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

Definition at line 2696 of file qdatetime.cpp.

References QtPrivate::DateTimeConstants::MINS_PER_HOUR, QtPrivate::DateTimeConstants::MSECS_PER_SEC, and QtPrivate::DateTimeConstants::SECS_PER_MIN.

◆ minute()

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

Definition at line 2070 of file qdatetime.cpp.

References isValid(), QtPrivate::DateTimeConstants::MSECS_PER_HOUR, and QtPrivate::DateTimeConstants::MSECS_PER_MIN.

Referenced by QOCIDateTime::QOCIDateTime(), QCalendarBackend::dateTimeToString(), QMYSQLResult::exec(), QDB2Driver::formatValue(), QIBaseDriver::formatValue(), QOCIDriver::formatValue(), QV4::Date::init(), operator<<(), QDateTimeParser::setDigit(), QDateTimeEdit::stepBy(), and QSystemLocalePrivate::toString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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(), second()

Definition at line 2102 of file qdatetime.cpp.

References isValid(), and QtPrivate::DateTimeConstants::MSECS_PER_SEC.

Referenced by QOCIDateTime::QOCIDateTime(), QCalendarBackend::dateTimeToString(), QMYSQLResult::exec(), QDB2Driver::formatValue(), QIBaseDriver::formatValue(), QV4::Date::init(), operator<<(), and QDateTimeParser::setDigit().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ msecsSinceStartOfDay()

int QTime::msecsSinceStartOfDay ( ) const
inlineconstexpr

Returns the number of msecs since the start of the day, i.e.

since 00:00:00.

See also
fromMSecsSinceStartOfDay()

Definition at line 244 of file qdatetime.h.

Referenced by Q_LOGGING_CATEGORY(), setDateTime(), timeToMSecs(), toEarliest(), and toLatest().

+ Here is the caller graph for this function:

◆ msecsTo()

int QTime::msecsTo ( 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(), QDateTime::msecsTo()

Definition at line 2363 of file qdatetime.cpp.

References isValid().

Referenced by QWindowsVistaStyle::drawControl(), msecsTo(), QDateTimeParser::parse(), QSGOpenVGRenderLoop::renderWindow(), and QSGSoftwareRenderLoop::renderWindow().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ second()

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

Definition at line 2086 of file qdatetime.cpp.

References isValid(), QtPrivate::DateTimeConstants::MSECS_PER_SEC, and QtPrivate::DateTimeConstants::SECS_PER_MIN.

Referenced by QOCIDateTime::QOCIDateTime(), QCalendarBackend::dateTimeToString(), QMYSQLResult::exec(), QDB2Driver::formatValue(), QIBaseDriver::formatValue(), QOCIDriver::formatValue(), QV4::Date::init(), operator<<(), QDateTimeParser::setDigit(), and QSystemLocalePrivate::toString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ secsTo()

int QTime::secsTo ( 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(), QDateTime::secsTo()

Definition at line 2318 of file qdatetime.cpp.

References isValid(), and QtPrivate::DateTimeConstants::MSECS_PER_SEC.

+ Here is the call graph for this function:

◆ 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 2271 of file qdatetime.cpp.

References isValid(), QtPrivate::DateTimeConstants::MINS_PER_HOUR, QtPrivate::DateTimeConstants::MSECS_PER_DAY, QtPrivate::DateTimeConstants::MSECS_PER_SEC, Q_ASSERT, and QtPrivate::DateTimeConstants::SECS_PER_MIN.

Referenced by QTime(), and operator>>().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ comparesEqual

constexpr bool comparesEqual ( const QTime & lhs,
const QTime & rhs )
friend

Definition at line 264 of file qdatetime.h.

◆ compareThreeWay

constexpr Qt::strong_ordering compareThreeWay ( const QTime & lhs,
const QTime & rhs )
friend

Definition at line 267 of file qdatetime.h.

◆ operator<< [1/2]

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

Writes time to stream out.

See also
{Serializing Qt Data Types}

Definition at line 6014 of file qdatetime.cpp.

◆ operator<<() [2/2]

QDataStream & operator<< ( QDataStream & out,
QTime time )
related

Writes time to stream out.

See also
{Serializing Qt Data Types}

Definition at line 6014 of file qdatetime.cpp.

◆ operator>> [1/2]

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 6032 of file qdatetime.cpp.

◆ operator>>() [2/2]

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

Reads a time from stream in into the given time.

See also
{Serializing Qt Data Types}

Definition at line 6032 of file qdatetime.cpp.

◆ QDateTime

friend class QDateTime
friend

Definition at line 271 of file qdatetime.h.

◆ QDateTimePrivate

friend class QDateTimePrivate
friend

Definition at line 272 of file qdatetime.h.


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