QTime 类

The QTime class provides clock time functions. 更多...

头: #include <QTime>
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 elapsed () const
int hour () const
bool isNull () const
bool isValid () const
int minute () const
int msec () const
int msecsSinceStartOfDay () const
int msecsTo (const QTime & t ) const
int restart ()
int second () const
int secsTo (const QTime & t ) 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
bool operator!= (const QTime & t ) const
bool operator< (const QTime & t ) const
bool operator<= (const QTime & t ) const
bool operator== (const QTime & t ) const
bool operator> (const QTime & t ) const
bool operator>= (const QTime & t ) const

静态公共成员

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

详细描述

The QTime class provides clock time functions.

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-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 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 (),和 msec () 函数提供对时间的小时、分钟、秒及毫秒数的访问。以正文格式提供相同信息是通过 toString () 函数。

QTime provides a full set of operators to compare two QTime 对象。 QTime A is considered smaller than QTime B if A is earlier than B.

The addSecs () 和 addMSecs () 函数提供晚于给定时间的给定秒 (或毫秒) 数的时间。相应地,查找 2 时间之间的秒 (或毫秒) 数可以使用 secsTo () 或 msecsTo ().

QTime can be used to measure a span of elapsed time using the start (), restart (),和 elapsed () 函数。

另请参阅 QDate and QDateTime .

成员函数文档编制

QTime:: QTime ()

Constructs a null time object. A null time can be a QTime (0, 0, 0, 0) (i.e., midnight) object, except that isNull () 返回 true and isValid () 返回 false .

另请参阅 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).

注意,时间将卷绕,若过了午夜。

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

int QTime:: elapsed () const

Returns the number of milliseconds that have elapsed since the last time start () 或 restart () 被调用。

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.

警告: If the system's clock setting has been changed since the last time start () 或 restart () was called, the result is undefined. This can happen when daylight-saving time is turned on or off.

另请参阅 start () 和 restart ().

[static] 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.

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.

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

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

返回 QTime 表示通过 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)
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 the fractional part of the second, to go after a decimal point, without trailing zeroes (0 to 999). Thus " s.z " reports the seconds to full available (millisecond) precision without trailing zeroes.
zzz the fractional part of the second, to millisecond precision, including trailing zeroes where applicable (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
					

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

int QTime:: hour () const

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

返回 -1,若时间无效。

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

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

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

返回 0 若时间无效。

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

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

警告: 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-saving time is turned on or off.

另请参阅 start (), elapsed (),和 currentTime ().

int QTime:: second () const

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

返回 -1,若时间无效。

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

int QTime:: secsTo (const 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.

返回 0 若时间无效。

另请参阅 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 ().

void QTime:: start ()

Sets this time to the current time. This is practical for timing:

QTime t;
t.start();
some_lengthy_task();
qDebug("Time elapsed: %d ms", t.elapsed());
					

另请参阅 restart (), elapsed (),和 currentTime ().

QString QTime:: toString (const QString & format ) const

以字符串形式返回时间。 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 the fractional part of the second, to go after a decimal point, without trailing zeroes (0 to 999). Thus " s.z " reports the seconds to full available (millisecond) precision without trailing zeroes.
zzz the fractional part of the second, to millisecond precision, including trailing zeroes where applicable (000 to 999).
AP or A use AM/PM display. A/AP will be replaced by either QLocale::amText () 或 QLocale::pmText ().
ap or a use am/pm display. a/ap will be replaced by a lower-case version of QLocale::amText () 或 QLocale::pmText ().
t the timezone (for example "CEST")

All other input characters will be ignored. Any sequence of characters that are enclosed in single quotes will be treated as text and not be used as an expression. Two consecutive single quotes ("''") are replaced by a singlequote in the output. Formats without separators (e.g. "HHmm") are currently not supported.

格式字符串范例 (假定 QTime 是 14:13:09.042 且系统区域设置为 en_US )

格式 结果
hh:mm:ss.zzz 14:13:09.042
h:m:s ap 2:13:9 pm
H:m:s a 14:13:9 pm

若时间无效,返回空字符串。若 format is empty, the default format "hh:mm:ss" is used.

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

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

这是重载函数。

以字符串形式返回时间。 format 参数确定字符串的格式。

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::SystemLocaleShortDate or Qt::SystemLocaleLongDate , the string format depends on the locale settings of the system. Identical to calling QLocale::system (). toString (time, QLocale::ShortFormat ) 或 QLocale::system (). toString (time, QLocale::LongFormat ).

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

format is Qt::RFC2822Date , the string is formatted in an RFC 2822 compatible way. An example of this formatting is "23:59:20".

若时间无效,返回空字符串。

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

bool QTime:: operator!= (const QTime & t ) const

返回 true if this time is different from t ;否则返回 false .

bool QTime:: operator< (const QTime & t ) const

返回 true 若此时间早于 t ;否则返回 false .

bool QTime:: operator<= (const QTime & t ) const

返回 true 若此时间早于或等于 t ;否则返回 false .

bool QTime:: operator== (const QTime & t ) const

返回 true 若此时间等于 t ;否则返回 false .

bool QTime:: operator> (const QTime & t ) const

返回 true 若此时间晚于 t ;否则返回 false .

bool QTime:: operator>= (const QTime & t ) const

返回 true 若此时间 >= t ;否则返回 false .

相关非成员

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

写入 time 到流 out .

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

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

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

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