QTime 類

QTime 類提供時鍾時間功能。 更多...

頭: #include <QTime>
qmake: QT += core

注意: 此類的所有函數 可重入 .

公共函數

QTime (int h , int m , int s = 0, int ms = 0)
QTime ()
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 (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)
QString toString (const QString & format ) const
QString toString (Qt::DateFormat format = Qt::TextDate) const
QString toString (QStringView format ) 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 )

詳細描述

QTime 對象包含時鍾時間,可以以從午夜起的小時、分鍾、秒及毫秒數形式錶達。它提供比較時間,和通過添加毫秒數操縱時間的函數。

QTime 使用 24 小時時鍾格式;它沒有 AM/PM 的概念。不像 QDateTime ,QTime 對時區或 DST (夏令時) 一無所知。

通常,QTime 對象的創建是通過明確給齣小時、分鍾、秒及毫秒數,或通過使用靜態函數 currentTime (),創建錶示係統區域設置時間的 QTime 對象。

The hour (), minute (), second (),和 msec () 函數提供對時間的小時、分鍾、秒及毫秒數的訪問。以正文格式提供相同信息是通過 toString () 函數。

The addSecs () 和 addMSecs () 函數提供晚於給定時間的給定秒 (或毫秒) 數的時間。相應地,查找 2 時間之間的秒 (或毫秒) 數可以使用 secsTo () 或 msecsTo ().

QTime 提供瞭比較兩 QTime 對象的一組完整運算符;認為較早時間小於稍後時間;若 A. msecsTo (B) 為正值,那麼 A < B。

另請參閱 QDate and QDateTime .

成員函數文檔編製

QString QTime:: toString ( QStringView format ) const

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 an upper-case version of either QLocale::amText () 或 QLocale::pmText ().
ap or a Use am/pm display. a/ap will be replaced by a lower-case version of either QLocale::amText () 或 QLocale::pmText ().
t The timezone (for example "CEST")

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

格式字符串範例 (假定 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.

注意: If localized forms of am or pm (the AP, ap, A or a formats) are desired, please switch to using QLocale::system (). toString () 作為 QTime methods shall change to use English (C locale) at Qt 6.

另請參閱 fromString (), QDate::toString (), QDateTime::toString (),和 QLocale::toString ().

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

構造 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:: 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.

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

注意: Support for localized dates, including the format options Qt::SystemLocaleDate , Qt::SystemLocaleShortDate , Qt::SystemLocaleLongDate , Qt::LocaleDate , Qt::DefaultLocaleShortDate ,和 Qt::DefaultLocaleLongDate ,在 Qt 6 被移除。使用 QLocale::toTime () 代替。

另請參閱 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)
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 Interpret as an AM/PM time. A/AP will match an upper-case version of either QLocale::amText () 或 QLocale::pmText ().
ap or a Interpret as an am/pm time. a/ap will match a lower-case version of either QLocale::amText () 或 QLocale::pmText ().

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 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, A or a formats) are used, please switch to using QLocale::system ().toTime() as QTime methods shall change to only recognize English (C locale) at Qt 6.

另請參閱 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 ().

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.

The format options Qt::SystemLocaleDate :, Qt::SystemLocaleShortDate and Qt::SystemLocaleLongDate shall be removed in Qt 6. Their use should be replaced with: QLocale::system().toString(time, QLocale::ShortFormat) or QLocale::system().toString(time, QLocale::LongFormat) .

The format options Qt::LocaleDate , Qt::DefaultLocaleShortDate and Qt::DefaultLocaleLongDate shall be removed in Qt 6. Their use should be replaced with: 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 數據類型 .