QTime 类

QTime 类提供时钟时间功能。 更多...

头: #include <QTime>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注意: 此类的所有函数 可重入 .

公共函数

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

静态公共成员

QTime currentTime ()
QTime fromMSecsSinceStartOfDay (int msecs )
QTime fromString (const QString & string , Qt::DateFormat format = Qt::TextDate)
QTime fromString (QStringView string , Qt::DateFormat format = Qt::TextDate)
QTime fromString (QStringView string , QStringView format )
QTime fromString (const QString & string , QStringView format )
QTime fromString (const QString & string , const QString & format )
bool isValid (int h , int m , int s , int ms = 0)
bool operator!= (QTime lhs , QTime rhs )
bool operator< (QTime lhs , QTime rhs )
QDataStream & operator<< (QDataStream & out , QTime time )
bool operator<= (QTime lhs , QTime rhs )
bool operator== (QTime lhs , QTime rhs )
bool operator> (QTime lhs , QTime rhs )
bool operator>= (QTime lhs , QTime rhs )
QDataStream & operator>> (QDataStream & in , QTime & time )

详细描述

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 (),和 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 () 函数。

The addSecs () 和 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 () 或 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 .

另请参阅 QDate and QDateTime .

成员函数文档编制

QString QTime:: toString ( QStringView format ) const

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:

表达式 输出
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 whole second, without any leading zero (0 to 59)
ss The whole second, with a leading zero where applicable (00 to 59)
z or zz The fractional part of the second, to go after a decimal point, without trailing zeroes. Thus "s.z" reports the seconds to full available (millisecond) precision without trailing zeroes (0 to 999). For example, "s.z" would produce "0.25" for a time a quarter second into a minute.
zzz The fractional part of the second, to millisecond precision, including trailing zeroes where applicable (000 to 999). For example, "ss.zzz" would produce "00.250" for a time a quarter second into a minute.
AP or A Use AM/PM display. A/AP will be replaced by 'AM' or 'PM'. In localized forms (only relevant to QLocale::toString ()), the locale-appropriate text is converted to upper-case.
ap or a Use am/pm display. a/ap will be replaced by 'am' or 'pm'. In localized forms (only relevant to QLocale::toString ()), the locale-appropriate text is converted to lower-case.
aP or Ap Use AM/PM display (since 6.3). aP/Ap will be replaced by 'AM' or 'PM'. In localized forms (only relevant to QLocale::toString ()), the locale-appropriate text (returned by QLocale::amText () 或 QLocale::pmText ()) is used without change of case.
t The timezone abbreviation (for example "CEST"). Note that time zone abbreviations are not unique. In particular, toString () cannot parse this.
tt The timezone's offset from UTC with no colon between the hours and minutes (for example "+0200").
ttt The timezone's offset from UTC with a colon between the hours and minutes (for example "+02:00").
tttt The timezone name (for example "Europe/Berlin"). Note that this gives no indication of whether the datetime was in daylight-saving time or standard time, which may lead to ambiguity if the datetime falls in an hour repeated by a transition between the two. The name used is the one provided by QTimeZone::displayName () 采用 QTimeZone::LongName type. This may depend on the operating system in use.

Any non-empty sequence of characters enclosed in single quotes will be included verbatim in the output string (stripped of the quotes), even if it contains formatting characters. Two consecutive single quotes ("''") are replaced by a single quote in the output. All other characters in the format string are included verbatim in the output string.

Formats without separators (e.g. "ddMM") are supported but must be used with care, as the resulting strings aren't always reliably readable (e.g. if "dM" produces "212" it could mean either the 2nd of December or the 21st of February).

Example format strings (assuming that the QTime is 14:13:09.042)

格式 结果
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 time is invalid, an empty string will be returned.

注意: To get localized forms of AM or PM (the AP, ap, A, a, aP or Ap formats), use QLocale::system (). toString ().

注意: If a format character is repeated more times than the longest expression in the table above using it, this part of the format will be read as several expressions with no separator between them; the longest above, possibly repeated as many times as there are copies of it, ending with a residue that may be a shorter expression. Thus 'HHHHH' for the time 08:00 will contribute "08088" to the output.

另请参阅 fromString (), QDate::toString (), QDateTime::toString (),和 QLocale::toString ().

[constexpr] QTime:: QTime ()

构造 null 时间对象。对于 null 时间, isNull () 返回 true and isValid () 返回 false . If you need a zero time, use QTime(0, 0). For the start of a day, see QDate::startOfDay ().

另请参阅 isNull () 和 isValid ().

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

构造时间采用小时 h ,分钟 m ,秒 s 和毫秒 ms .

h 必须在范围 0 到 23, m and s 必须在范围 0 到 59,和 ms 必须在范围 0 到 999。

另请参阅 isValid ().

QTime QTime:: addMSecs ( int ms ) const

返回 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 () 范例。

返回 null 时间若此时间无效。

另请参阅 addSecs (), msecsTo (),和 QDateTime::addMSecs ().

QTime QTime:: addSecs ( int s ) const

返回 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.

返回 null 时间若此时间无效。

范例:

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
					

另请参阅 addMSecs (), secsTo (),和 QDateTime::addSecs ().

[static] QTime QTime:: currentTime ()

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.

另请参阅 QDateTime::currentDateTime () 和 QDateTime::currentDateTimeUtc ().

[static constexpr] QTime QTime:: fromMSecsSinceStartOfDay ( int msecs )

返回新的 QTime instance with the time set to the number of msecs since the start of the day, i.e. since 00:00:00.

msecs falls outside the valid range an invalid QTime 将被返回。

另请参阅 msecsSinceStartOfDay ().

[static] QTime QTime:: fromString (const QString & string , Qt::DateFormat format = Qt::TextDate)

Returns the time represented in the string 作为 QTime 使用 format given, or an invalid time if this is not possible.

另请参阅 toString () 和 QLocale::toTime ().

[static, since 6.0] QTime QTime:: fromString ( QStringView string , Qt::DateFormat format = Qt::TextDate)

这是重载函数。

该函数在 Qt 6.0 引入。

[static, since 6.0] QTime QTime:: fromString ( QStringView string , QStringView format )

这是重载函数。

该函数在 Qt 6.0 引入。

[static, since 6.0] QTime QTime:: fromString (const QString & string , QStringView format )

这是重载函数。

该函数在 Qt 6.0 引入。

[static] QTime QTime:: fromString (const QString & string , const QString & format )

返回 QTime represented by the string ,使用 format given, or an invalid time if the string cannot be parsed.

These expressions may be used for the format:

表达式 输出
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 whole second, without any leading zero (0 to 59)
ss The whole second, with a leading zero where applicable (00 to 59)
z or zz The fractional part of the second, as would usually follow a decimal point, without requiring trailing zeroes (0 to 999). Thus "s.z" matches the seconds with up to three digits of fractional part supplying millisecond precision, without needing trailing zeroes. For example, "s.z" would recognize either "00.250" or "0.25" as representing a time a quarter second into its minute.
zzz Three digit fractional part of the second, to millisecond precision, including trailing zeroes where applicable (000 to 999). For example, "ss.zzz" would reject "0.25" but recognize "00.250" as representing a time a quarter second into its minute.
AP, A, ap, a, aP or Ap Either 'AM' indicating a time before 12:00 or 'PM' for later times, matched case-insensitively.

All other input characters will be treated as text. Any non-empty sequence of characters enclosed in single quotes will also be treated (stripped of the quotes) as text and not be interpreted as expressions.

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 (or three, for z) 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
					

注意: If localized forms of am or pm (the AP, ap, Ap, aP, A or a formats) are to be recognized, use QLocale::system ().toTime().

注意: If a format character is repeated more times than the longest expression in the table above using it, this part of the format will be read as several expressions with no separator between them; the longest above, possibly repeated as many times as there are copies of it, ending with a residue that may be a shorter expression. Thus 'HHHHH' would match "08088" or "080808" and set the hour to 8; if the time string contained "070809" it would "match" but produce an inconsistent result, leading to an invalid time.

另请参阅 toString (), QDateTime::fromString (), QDate::fromString (), QLocale::toTime (),和 QLocale::toDateTime ().

int QTime:: hour () const

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

返回 -1,若时间无效。

另请参阅 minute (), second (),和 msec ().

[constexpr] bool QTime:: isNull () const

返回 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.

另请参阅 isValid ().

bool QTime:: isValid () const

返回 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.

另请参阅 isNull ().

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

这是重载函数。

返回 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.

范例:

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.

返回 -1,若时间无效。

另请参阅 hour (), second (),和 msec ().

int QTime:: msec () const

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

返回 -1,若时间无效。

另请参阅 hour (), minute (),和 second ().

[constexpr] int QTime:: msecsSinceStartOfDay () const

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

另请参阅 fromMSecsSinceStartOfDay ().

int QTime:: msecsTo ( QTime t ) const

Returns the number of milliseconds from this time to t 。若 t is earlier than this time, the number of milliseconds returned is negative.

因为 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.

另请参阅 secsTo (), addMSecs (),和 QDateTime::msecsTo ().

int QTime:: second () const

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

返回 -1,若时间无效。

另请参阅 hour (), minute (),和 msec ().

int QTime:: secsTo ( QTime t ) const

Returns the number of seconds from this time to t 。若 t is earlier than this time, the number of seconds returned is negative.

因为 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.

另请参阅 addSecs () 和 QDateTime::secsTo ().

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

Sets the time to hour h ,分钟 m ,秒 s 和毫秒 ms .

h 必须在范围 0 到 23, m and s 必须在范围 0 到 59,和 ms must be in the range 0 to 999. Returns true if the set time is valid; otherwise returns false .

另请参阅 isValid ().

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

这是重载函数。

Returns the time as a string. The format parameter determines the format of the string.

format is Qt::TextDate , the string format is HH:mm:ss; e.g. 1 second before midnight would be "23:59:59".

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.

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.

另请参阅 fromString (), QDate::toString (), QDateTime::toString (),和 QLocale::toString ().

相关非成员

[constexpr] bool operator!= ( QTime lhs , QTime rhs )

返回 true if lhs is different from rhs ;否则返回 false .

[constexpr] bool operator< ( QTime lhs , QTime rhs )

返回 true if lhs is earlier than rhs ;否则返回 false .

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

写入 time 到流 out .

另请参阅 序列化 Qt 数据类型 .

[constexpr] bool operator<= ( QTime lhs , QTime rhs )

返回 true if lhs is earlier than or equal to rhs ;否则返回 false .

[constexpr] bool operator== ( QTime lhs , QTime rhs )

返回 true if lhs 等于 rhs ;否则返回 false .

[constexpr] bool operator> ( QTime lhs , QTime rhs )

返回 true if lhs is later than rhs ;否则返回 false .

[constexpr] bool operator>= ( QTime lhs , QTime rhs )

返回 true if lhs is later than or equal to rhs ;否则返回 false .

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

读取时间从流 in 进给定 time .

另请参阅 序列化 Qt 数据类型 .