![]() |
CopperSpice API
1.8.1
|
The QTime class stores clock time information. More...
Public Methods | |
constexpr | QTime () |
QTime (int h, int m, int s=0, int ms=0) | |
QTime | addMSecs (int ms) const |
QTime | addSecs (int secs) const |
int | elapsed () const |
int | hour () const |
constexpr bool | isNull () const |
bool | isValid () const |
int | minute () const |
int | msec () const |
int | msecsSinceStartOfDay () const |
int | msecsTo (const QTime &value) const |
bool | operator!= (const QTime &value) const |
bool | operator< (const QTime &value) const |
bool | operator<= (const QTime &value) const |
bool | operator== (const QTime &value) const |
bool | operator> (const QTime &value) const |
bool | operator>= (const QTime &value) const |
int | restart () |
int | second () const |
int | secsTo (const QTime &value) const |
bool | setHMS (int h, int m, int s, int ms=0) |
void | start () |
QString | toString (const QString &format) const |
QString | toString (Qt::DateFormat format=Qt::TextDate) const |
Static Public Methods | |
static QTime | currentTime () |
static QTime | fromMSecsSinceStartOfDay (int msecs) |
static QTime | fromString (const QString &str, const QString &format) |
static QTime | fromString (const QString &str, Qt::DateFormat format=Qt::TextDate) |
static bool | isValid (int h, int m, int s, int ms=0) |
Friends | |
QDataStream & | operator<< (QDataStream &stream, const QTime &time) |
QDataStream & | operator>> (QDataStream &stream, QTime &time) |
class | QDateTime |
The QTime class stores clock time information. A QTime object contains a clock time which is defined as 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. This class provides methods for comparing times and for adjusting a time by a given number of milliseconds.
QTime uses the 24-hour clock format and 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 method currentTime(), which creates a QTime object that contains the system's local time. The accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.
The hour(), minute(), second(), and msec() methods 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() method.
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() methods. 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() methods.
|
inlineconstexpr |
Constructs a null QTime.
The isNull() method will return true, isValid() will return false, and the time will be set to midnight which is QTime(0, 0, 0, 0).
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. The value for 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.
|
nodiscard |
Returns a QTime containing the time plus ms milliseconds. If ms is negative the result is the QTime value minus the given ms value. The time will wrap if it crosses midnight.
|
nodiscard |
Returns a QTime containing the time plus secs seconds. If ms is negative the result is the QTime value minus the given s value. The time will wrap if it crosses midnight.
|
static |
Returns the current time as reported by the system clock. The accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.
int QTime::elapsed | ( | ) | const |
Returns the number of milliseconds that have elapsed since the last time start() or restart() was called. The counter wraps to zero 24 hours after the last call to start() or restart. The accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.
|
inlinestatic |
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.
Returns the QTime represented by the str using the given format. Returns an invalid time if the string can not be parsed. These expressions may be used for the format parameter.
Expression | Output |
---|---|
h | hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) |
hh | hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) |
m | minute without a leading zero (0 to 59) |
mm | minute with a leading zero (00 to 59) |
s | second without a leading zero (0 to 59) |
ss | second with a leading zero (00 to 59) |
z | milliseconds without leading zeroes (0 to 999) |
zzz | milliseconds with leading zeroes (000 to 999) |
AP | AM/PM time, AP must be either "AM" or "PM". |
ap | 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.
|
static |
Returns the time represented in the str as a QTime using the format given or an invalid time if this is not possible. This method uses a "C" locale encoded string to convert milliseconds to a float.
int QTime::hour | ( | ) | 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.
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.
|
static |
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.
int QTime::minute | ( | ) | const |
int QTime::msec | ( | ) | const |
|
inline |
Returns the number of msecs since the start of the day which is 00:00:00.
int QTime::msecsTo | ( | const QTime & | value | ) | const |
Returns the number of milliseconds from this time to value. If value 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.
|
inline |
Returns true if this time is different from value, otherwise returns false.
|
inline |
Returns true if this time is earlier than value, otherwise returns false.
|
inline |
Returns true if this time is earlier than or equal to value, otherwise returns false.
|
inline |
Returns true if this time is equal to value, otherwise returns false.
|
inline |
Returns true if this time is later than value, otherwise returns false.
|
inline |
Returns true if this time is later than or equal to value, otherwise returns false.
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 method is useful repeated measurements. Call start() to start the first measurement, and then restart() for each later measurement.
int QTime::second | ( | ) | const |
int QTime::secsTo | ( | const QTime & | value | ) | const |
Returns the number of seconds from this time to value. If value 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.
This method does not take into account any milliseconds.
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. Returns true if the set time is valid, otherwise returns false.
The value for 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.
void QTime::start | ( | ) |
Returns the time as a string. The format parameter determines the format of the result string. If the time is invalid an empty string will be returned. If format is empty the default format "hh:mm:ss" is used.
The following expressions may be used.
Expression | Output |
---|---|
h | hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) |
hh | hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) |
H | hour without a leading zero (0 to 23, even with AM/PM display) |
HH | hour with a leading zero (00 to 23, even with AM/PM display) |
m | minute without a leading zero (0 to 59) |
mm | minute with a leading zero (00 to 59) |
s | second without a leading zero (0 to 59) |
ss | second with a leading zero (00 to 59) |
z | milliseconds without leading zeroes (0 to 999) |
zzz | milliseconds with leading zeroes (000 to 999) |
AP or A | AM/PM display, AP will be replaced by either "AM" or "PM". |
ap or a | am/pm display, ap will be replaced by either "am" or "pm". |
t | 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.
These are 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 |
QString QTime::toString | ( | Qt::DateFormat | format = Qt::TextDate | ) | const |
Returns the time as a string. If the time is invalid an empty string will be returned. Milliseconds are not included. The format parameter determines the format of the string.
|
friend |
Writes the given time to the stream. Returns a reference to the stream.
Refer to Serializing Data Types for additional information.
|
friend |
Reads from the stream into the given time. Returns a reference to the stream.
Refer to Serializing Data Types for additional information.