QtBase
v6.3.1
|
The QTime class provides clock time functions. More...
#include <qdatetime.h>
Public Member Functions | |
constexpr | QTime () |
QTime (int h, int m, int s=0, int ms=0) | |
constexpr bool | isNull () const |
bool | isValid () const |
int | hour () const |
int | minute () const |
int | second () const |
int | msec () const |
bool | setHMS (int h, int m, int s, int ms=0) |
QTime | addSecs (int secs) const |
int | secsTo (QTime t) const |
QTime | addMSecs (int ms) const |
int | msecsTo (QTime t) const |
constexpr int | msecsSinceStartOfDay () const |
Static Public Member Functions | |
static constexpr QTime | fromMSecsSinceStartOfDay (int msecs) |
static QTime | currentTime () |
static bool | isValid (int h, int m, int s, int ms=0) |
Friends | |
class | QDateTime |
class | QDateTimePrivate |
constexpr friend bool | operator== (QTime lhs, QTime rhs) |
constexpr friend bool | operator!= (QTime lhs, QTime rhs) |
constexpr friend bool | operator< (QTime lhs, QTime rhs) |
constexpr friend bool | operator<= (QTime lhs, QTime rhs) |
constexpr friend bool | operator> (QTime lhs, QTime rhs) |
constexpr friend bool | operator>= (QTime lhs, QTime rhs) |
Q_CORE_EXPORT QDataStream & | operator<< (QDataStream &, QTime) |
Q_CORE_EXPORT QDataStream & | operator>> (QDataStream &, QTime &) |
Related Functions | |
(Note that these are not member functions.) | |
QDataStream & | operator<< (QDataStream &out, QTime time) |
QDataStream & | operator>> (QDataStream &in, QTime &time) |
The QTime class provides clock time functions.
\inmodule QtCore \reentrant
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.
Definition at line 165 of file qdatetime.h.
|
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().
Definition at line 170 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 1730 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.
Returns a null time if this time is invalid.
Definition at line 2024 of file qdatetime.cpp.
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:
Definition at line 1980 of file qdatetime.cpp.
|
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.
|
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.
Definition at line 196 of file qdatetime.h.
int QTime::hour | ( | ) | const |
Returns the hour part (0 to 23) of the time.
Returns -1 if the time is invalid.
Definition at line 1767 of file qdatetime.cpp.
|
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.
Definition at line 174 of file qdatetime.h.
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 1753 of file qdatetime.cpp.
|
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:
Definition at line 2362 of file qdatetime.cpp.
int QTime::minute | ( | ) | const |
Returns the minute part (0 to 59) of the time.
Returns -1 if the time is invalid.
Definition at line 1783 of file qdatetime.cpp.
int QTime::msec | ( | ) | const |
Returns the millisecond part (0 to 999) of the time.
Returns -1 if the time is invalid.
Definition at line 1815 of file qdatetime.cpp.
|
inlineconstexpr |
Returns the number of msecs since the start of the day, i.e. since 00:00:00.
Definition at line 197 of file qdatetime.h.
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.
Definition at line 2046 of file qdatetime.cpp.
int QTime::second | ( | ) | const |
Returns the second part (0 to 59) of the time.
Returns -1 if the time is invalid.
Definition at line 1799 of file qdatetime.cpp.
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.
Definition at line 2001 of file qdatetime.cpp.
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 1954 of file qdatetime.cpp.
|
friend |
Returns true
if lhs is different from rhs; otherwise returns false
.
Definition at line 220 of file qdatetime.h.
Returns true
if lhs is earlier than rhs; otherwise returns false
.
Definition at line 221 of file qdatetime.h.
|
friend |
Writes time to stream out.
Definition at line 5373 of file qdatetime.cpp.
|
related |
Writes time to stream out.
Definition at line 5373 of file qdatetime.cpp.
Returns true
if lhs is earlier than or equal to rhs; otherwise returns false
.
Definition at line 222 of file qdatetime.h.
Returns true
if lhs is equal to rhs; otherwise returns false
.
Definition at line 219 of file qdatetime.h.
Returns true
if lhs is later than rhs; otherwise returns false
.
Definition at line 223 of file qdatetime.h.
Returns true
if lhs is later than or equal to rhs; otherwise returns false
.
Definition at line 224 of file qdatetime.h.
|
friend |
Reads a time from stream in into the given time.
Definition at line 5391 of file qdatetime.cpp.
|
related |
Reads a time from stream in into the given time.
Definition at line 5391 of file qdatetime.cpp.
|
friend |
Definition at line 226 of file qdatetime.h.
|
friend |
Definition at line 227 of file qdatetime.h.