Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QTime Class

TheQTime class provides clock time functions.More...

Header:#include <QTime>

Note: All functions in this class arereentrant.

Public Functions

QTime()
QTime(int h, int m, int s = 0, int ms = 0)
QTimeaddMSecs(int ms) const
QTimeaddSecs(int s) const
intelapsed() const
inthour() const
boolisNull() const
boolisValid() const
intminute() const
intmsec() const
intmsecsTo(const QTime & t) const
intrestart()
intsecond() const
intsecsTo(const QTime & t) const
boolsetHMS(int h, int m, int s, int ms = 0)
voidstart()
QStringtoString(const QString & format) const
QStringtoString(Qt::DateFormat format = Qt::TextDate) const
booloperator!=(const QTime & t) const
booloperator<(const QTime & t) const
booloperator<=(const QTime & t) const
booloperator==(const QTime & t) const
booloperator>(const QTime & t) const
booloperator>=(const QTime & t) const

Static Public Members

QTimecurrentTime()
QTimefromString(const QString & string, Qt::DateFormat format = Qt::TextDate)
QTimefromString(const QString & string, const QString & format)
boolisValid(int h, int m, int s, int ms = 0)

Related Non-Members

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

Detailed Description

TheQTime class provides clock time functions.

AQTime 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. UnlikeQDateTime,QTime knows nothing about time zones or daylight savings time (DST).

AQTime object is typically created either by giving the number of hours, minutes, seconds, and milliseconds explicitly, or by using the static functioncurrentTime(), which creates aQTime 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.

Thehour(),minute(),second(), andmsec() functions provide access to the number of hours, minutes, seconds, and milliseconds of the time. The same information is provided in textual format by thetoString() function.

QTime provides a full set of operators to compare twoQTime 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 theaddSecs() oraddMSecs() functions. Correspondingly, the number of seconds or milliseconds between two times can be found usingsecsTo() ormsecsTo().

QTime can be used to measure a span of elapsed time using thestart(),restart(), andelapsed() functions.

See alsoQDate andQDateTime.

Member Function Documentation

QTime::QTime()

Constructs a null time object. A null time can be aQTime(0, 0, 0, 0) (i.e., midnight) object, except thatisNull() returns true andisValid() returns false.

See alsoisNull() andisValid().

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

Constructs a time with hourh, minutem, secondss and millisecondsms.

h must be in the range 0 to 23,m ands must be in the range 0 to 59, andms must be in the range 0 to 999.

See alsoisValid().

QTime QTime::addMSecs(int ms) const

Returns aQTime object containing a timems milliseconds later than the time of this object (or earlier ifms is negative).

Note that the time will wrap if it passes midnight. SeeaddSecs() for an example.

See alsoaddSecs(),msecsTo(), andQDateTime::addMSecs().

QTime QTime::addSecs(int s) const

Returns aQTime object containing a times seconds later than the time of this object (or earlier ifs is negative).

Note that the time will wrap if it passes midnight.

Example:

QTime n(14,0,0);// n == 14:00:00QTime t;t= n.addSecs(70);// t == 14:01:10t= n.addSecs(-70);// t == 13:58:50t= n.addSecs(10*60*60+5);// t == 00:00:05t= n.addSecs(-15*60*60);// t == 23:00:00

See alsoaddMSecs(),secsTo(), andQDateTime::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 timestart() orrestart() was called.

Note that the counter wraps to zero 24 hours after the last call tostart() or restart.

Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.

Warning: If the system's clock setting has been changed since the last timestart() orrestart() was called, the result is undefined. This can happen when daylight savings time is turned on or off.

See alsostart() andrestart().

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

Returns the time represented in thestring as aQTime using theformat 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.

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

Returns theQTime represented by thestring, using theformat given, or an invalid time if the string cannot be parsed.

These expressions may be used for the format:

ExpressionOutput
hthe hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hhthe hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
mthe minute without a leading zero (0 to 59)
mmthe minute with a leading zero (00 to 59)
sthe second without a leading zero (0 to 59)
ssthe second with a leading zero (00 to 59)
zthe milliseconds without leading zeroes (0 to 999)
zzzthe milliseconds with leading zeroes (000 to 999)
APinterpret as an AM/PM time.AP must be either "AM" or "PM".
apInterpret 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 invalidQTime 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

QDateTime::toString()QTime::toString()

See alsoQDateTime::fromString(),QDate::fromString(), andQDate::toString().

int QTime::hour() const

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

See alsominute(),second(), andmsec().

bool QTime::isNull() const

Returns true if the time is null (i.e., theQTime object was constructed using the default constructor); otherwise returns false. A null time is also an invalid time.

See alsoisValid().

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.

See alsoisNull().

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

This is an overloaded function.

Returns true if the specified time is valid; otherwise returns false.

The time is valid ifh is in the range 0 to 23,m ands are in the range 0 to 59, andms is in the range 0 to 999.

Example:

QTime::isValid(21,10,30);// returns trueQTime::isValid(22,5,62);// returns false

int QTime::minute() const

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

See alsohour(),second(), andmsec().

int QTime::msec() const

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

See alsohour(),minute(), andsecond().

int QTime::msecsTo(constQTime & t) const

Returns the number of milliseconds from this time tot. Ift is earlier than this time, the number of milliseconds returned is negative.

BecauseQTime measures time within a day and there are 86400 seconds in a day, the result is always between -86400000 and 86400000 ms.

See alsosecsTo(),addMSecs(), andQDateTime::msecsTo().

int QTime::restart()

Sets this time to the current time and returns the number of milliseconds that have elapsed since the last timestart() or restart() was called.

This function is guaranteed to be atomic and is thus very handy for repeated measurements. Callstart() to start the first measurement, and restart() for each later measurement.

Note that the counter wraps to zero 24 hours after the last call tostart() or restart().

Warning: If the system's clock setting has been changed since the last timestart() or restart() was called, the result is undefined. This can happen when daylight savings time is turned on or off.

See alsostart(),elapsed(), andcurrentTime().

int QTime::second() const

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

See alsohour(),minute(), andmsec().

int QTime::secsTo(constQTime & t) const

Returns the number of seconds from this time tot. Ift is earlier than this time, the number of seconds returned is negative.

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

See alsoaddSecs() andQDateTime::secsTo().

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

Sets the time to hourh, minutem, secondss and millisecondsms.

h must be in the range 0 to 23,m ands must be in the range 0 to 59, andms must be in the range 0 to 999. Returns true if the set time is valid; otherwise returns false.

See alsoisValid().

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

See alsorestart(),elapsed(), andcurrentTime().

QString QTime::toString(constQString & format) const

Returns the time as a string. Theformat parameter determines the format of the result string.

These expressions may be used:

ExpressionOutput
hthe hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hhthe hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
Hthe hour without a leading zero (0 to 23, even with AM/PM display)
HHthe hour with a leading zero (00 to 23, even with AM/PM display)
mthe minute without a leading zero (0 to 59)
mmthe minute with a leading zero (00 to 59)
sthe second without a leading zero (0 to 59)
ssthe second with a leading zero (00 to 59)
zthe milliseconds without leading zeroes (0 to 999)
zzzthe milliseconds with leading zeroes (000 to 999)
AP or Ause AM/PM display.AP will be replaced by either "AM" or "PM".
ap or ause am/pm display.ap will be replaced by either "am" or "pm".
tthe 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.

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

FormatResult
hh:mm:ss.zzz14:13:09.042
h:m:s ap2:13:9 pm
H:m:s a14:13:9 pm

If the datetime is invalid, an empty string will be returned. Ifformat is empty, the default format "hh:mm:ss" is used.

See alsoQDate::toString() andQDateTime::toString().

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

This is an overloaded function.

Returns the time as a string. Milliseconds are not included. Theformat parameter determines the format of the string.

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

Ifformat isQt::ISODate, the string format corresponds to the ISO 8601 extended specification for representations of dates, which is also HH:mm:ss. (However, contrary to ISO 8601, dates before 15 October 1582 are handled as Julian dates, not Gregorian dates. SeeUse of Gregorian and Julian Calendars. This might change in a future version of Qt.)

If theformat isQt::SystemLocaleShortDate orQt::SystemLocaleLongDate, the string format depends on the locale settings of the system. Identical to callingQLocale::system().toString(time,QLocale::ShortFormat) orQLocale::system().toString(time,QLocale::LongFormat).

If theformat isQt::DefaultLocaleShortDate orQt::DefaultLocaleLongDate, the string format depends on the default application locale. This is the locale set withQLocale::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).

If the time is invalid, an empty string will be returned.

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

Returns true if this time is different fromt; otherwise returns false.

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

Returns true if this time is earlier thant; otherwise returns false.

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

Returns true if this time is earlier than or equal tot; otherwise returns false.

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

Returns true if this time is equal tot; otherwise returns false.

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

Returns true if this time is later thant; otherwise returns false.

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

Returns true if this time is later than or equal tot; otherwise returns false.

Related Non-Members

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

Writestime to streamout.

See alsoSerializing Qt Data Types.

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

Reads a time from streamin into the giventime.

See alsoSerializing Qt Data Types.

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp