CopperSpice API  1.9.1
QTime Class Reference

Stores time information. More...

Public Methods

constexpr QTime ()
 
 QTime (int h, int m, int s=0, int ms=0)
 
QTime addMSecs (int msecs) const
 
QTime addSecs (int seconds) 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

QDataStreamoperator<< (QDataStream &stream, const QTime &time)
 
QDataStreamoperator>> (QDataStream &stream, QTime &time)
 
class QDateTime
 

Detailed Description

The QTime class stores time information which is defined as the number of hours, minutes, seconds, and milliseconds since midnight. This class retrieves the current time from the operating system internal clock. A variety of methods are provided to calculate a time based on some offset or compare two existing times.

A QTime is typically created by specifying the number of hours, minutes, seconds, and milliseconds or using the static method currentTime() which returns a new QTime based on the system clock.

QTime uses a 24 hour clock format and has no concept of AM/PM. This class does not store any information about time zones or daylight saving time (DST).

Methods

The hour(), minute(), second(), and msec() methods provide access to the number of hours, minutes, seconds, and milliseconds of the given QTime. The same information is provided in a text format by calling one of the toString() methods.

There are additional methods which can be used to calculate a span of elapsed time using start(), restart(), and elapsed(). The number of seconds or milliseconds between two times can be found using secsTo() or msecsTo().

See also
QDate, QDateTime

Constructor & Destructor Documentation

constexpr QTime::QTime ( )
inlineconstexpr

Constructs an invalid null QTime.

See also
isNull(), isValid()
QTime::QTime ( int  h,
int  m,
int  s = 0,
int  ms = 0 
)

Constructs a new QTime 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.

See also
isValid()

Method Documentation

QTime QTime::addMSecs ( int  msecs) const
nodiscard

Adds the given number of msecs milliseconds to the current QTime and returns the result. If msecs is negative the value for msecs is subtracted from this QTime. The value for QTime will wrap if it crosses midnight.

See also
addSecs(), msecsTo(), QDateTime::addMSecs()
QTime QTime::addSecs ( int  seconds) const
nodiscard

Adds the given number of seconds to the current QTime and returns the result. If seconds is negative the value for seconds is subtracted from this QTime. The value for QTime will wrap if it crosses midnight.

QTime n(14, 0, 0); // n == 14:00:00
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
See also
addMSecs(), secsTo(), QDateTime::addSecs()
QTime QTime::currentTime ( )
static

Returns the current QTime as reported by the system clock. The accuracy depends on the underlying operating system and not all operating systems provide 1 millisecond accuracy.

int QTime::elapsed ( ) const

Returns the number of milliseconds which has elapsed since the last time start() or restart() was called. The count is reset to zero every 24 hours. If the system clock is modified while this method is tracking a count, the result will be undefined. A problem can also occur if daylight saving time is turned on or off.

QTime QTime::fromMSecsSinceStartOfDay ( int  msecs)
inlinestatic

Returns a new QTime with the time set to the number of milliseconds, msecs since midnight. If msecs falls outside the valid range an invalid QTime will be returned.

See also
msecsSinceStartOfDay()
QTime QTime::fromString ( const QString str,
const QString format 
)
static

Returns the QTime represented by the str using the format given. If the string does not represent a valid time using the given format, then QTime will set to an invalid time.

The following expressions can be used as part of the format. If any other characters are used in the format they will be treated as literal text. Any sequence of characters which are enclosed in single quotes will also be treated as literal text.

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".
QTime time = QTime::fromString("1mm12car00", "m'mm'hcarss"); // time is 12:01.00

Expressions which do not expect leading zeroes (h, m, s and z) are greedy. This means they will consume two digits even if this produces a number which is outside the range of accepted values. For example, the following string does not mean 00:07:10, because the m will consume two digits, resulting in an invalid time.

QTime time = QTime::fromString("00:710", "hh:ms"); // invalid

Any field which is not represented in the given format will be set to zero.

QTime time = QTime::fromString("1.30", "m.s"); // time is 00:01:30.000
See also
QDate::fromString(), QDate::toString(), QDateTime::fromString()
QTime QTime::fromString ( const QString str,
Qt::DateFormat  format = Qt::TextDate 
)
static

Returns the QTime represented by the str using the format given. If the string does not represent a valid time then QTime will set to an invalid time. This method uses a "C" locale encoded string to convert milliseconds to a float.

int QTime::hour ( ) const

Returns the hour part (0 to 23) of this QTime.

See also
minute(), msec(), second()
constexpr bool QTime::isNull ( ) const
inlineconstexpr

Returns true if this QTime was default constructed, otherwise returns false. A null time is an invalid time.

See also
isValid()
bool QTime::isValid ( ) const

Returns true if the QTime is valid, otherwise returns false. For example, 24:12:30 is not a valid time.

See also
isNull()
bool QTime::isValid ( int  h,
int  m,
int  s,
int  ms = 0 
)
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.

QTime::isValid(21, 10, 30); // returns true
QTime::isValid(22, 5, 65); // returns false
int QTime::minute ( ) const

Returns the minute part (0 to 59) of this QTime.

See also
hour(), msec(), second()
int QTime::msec ( ) const

Returns the millisecond part (0 to 999) of this QTime.

See also
hour(), minute(), second()
int QTime::msecsSinceStartOfDay ( ) const
inline

Returns the number of milliseconds since the start of the day, which is 00:00:00.

See also
fromMSecsSinceStartOfDay()
int QTime::msecsTo ( const QTime &  value) const

Returns the number of milliseconds between this QTime and the given value. If value is earlier than this QTime the number of milliseconds returned is negative.

See also
addMSecs(), QDateTime::msecsTo(), secsTo()
bool QTime::operator!= ( const QTime &  value) const
inline

Returns true if this QTime is different from value, otherwise returns false.

bool QTime::operator< ( const QTime &  value) const
inline

Returns true if this QTime is earlier than value, otherwise returns false.

bool QTime::operator<= ( const QTime &  value) const
inline

Returns true if this QTime is earlier than or equal to value, otherwise returns false.

bool QTime::operator== ( const QTime &  value) const
inline

Returns true if this QTime is equal to value, otherwise returns false.

bool QTime::operator> ( const QTime &  value) const
inline

Returns true if this QTime is later than value, otherwise returns false.

bool QTime::operator>= ( const QTime &  value) const
inline

Returns true if this QTime is later than or equal to value, otherwise returns false.

int QTime::restart ( )

Returns the number of milliseconds which has elapsed since the last time start() or restart() was called. The count is reset to zero every 24 hours. If the system clock is modified while this method is tracking a count, the result will be undefined. A problem can also occur if daylight saving time is turned on or off.

Sets this QTime to the current system time.

This method is useful for repeated measurements. Simply call start() to find the first measurement and then restart() for each later measurement.

See also
currentTime(), elapsed()
int QTime::second ( ) const

Returns the second part (0 to 59) of this QTime.

See also
hour(), minute(), msec()
int QTime::secsTo ( const QTime &  value) const

Returns the number of seconds between this QTime and the given value. If value is earlier than this QTime the number of seconds returned is negative.

See also
addSecs(), QDateTime::secsTo()
bool QTime::setHMS ( int  h,
int  m,
int  s,
int  ms = 0 
)

Sets this QTime 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.

See also
isValid()
void QTime::start ( )

Sets this QTime to the current system time.

t.start();
doSomeTask();
qDebug("Time elapsed: %d ms", t.elapsed());
See also
currentTime(), elapsed(), restart()
QString QTime::toString ( const QString format) const

Returns this QTime as a string. The format determines how the result will be formatted. If the QTime is invalid an empty string will be returned. If format is empty the default format of "hh:mm:" will be is used.

The following expressions can be used as part of the format.

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 singleq uotes ("''") are replaced by a single quote 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
See also
QDate::toString(), QDateTime::toString()
QString QTime::toString ( Qt::DateFormat  format = Qt::TextDate) const

Returns this QTime as a string. The format determines how the result will be formatted. If the QTime is invalid an empty string will be returned.

Qt::TextDate
String format is HH:MM:SS
Qt::ISODate
String format is HH:mm:ss
Qt::SystemLocaleShortDate or Qt::SystemLocaleLongDate
String format depends on the locale settings of the system. Equivalent to calling one of the following.
//
QLocale::system().toString(time, QLocale::ShortFormat)
//
QLocale::system().toString(time, QLocale::LongFormat)
Qt::DefaultLocaleShortDate or Qt::DefaultLocaleLongDate
String format depends on the default application locale which is the locale set with QLocale::setDefault() or the system locale if no default has been set. Using this format is equivalent to calling one of the following.
//
QLocale().toString(time, QLocale::ShortFormat)
//
QLocale().toString(time, QLocale::LongFormat)

Friends And Related Function Documentation

QDataStream & operator<< ( QDataStream stream,
const QTime &  time 
)
friend

Writes the given time to the stream. Returns a reference to the stream.

Refer to Serializing Data Types for additional information.

QDataStream & operator>> ( QDataStream stream,
QTime &  time 
)
friend

Reads from the stream into the given time. Returns a reference to the stream.

Refer to Serializing Data Types for additional information.