6.0.0-rc4Source

QTime Class

The QTime class provides clock time functions. More…

Header#include <QTime>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Functions

QTime()
QTime(int h, int m, int s = 0, int ms = 0)
boolisNull() const
boolisValid() const
inthour() const
intminute() const
intsecond() const
intmsec() const
boolsetHMS(int h, int m, int s, int msecs = 0)
QTimeaddSecs(int secs) const
QTimeaddMSecs(int msecs) const
intsecsTo(QTime t) const
intmsecsTo(QTime t) const
intmsecsSinceStartOfDay() const
QStringtoString(Qt::DateFormat format = Qt::TextDate) const
QStringtoString(const QString &format) const

Static Public Members

QTimecurrentTime()
QTimefromMSecsSinceStartOfDay(int msecs)
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)

Detailed Description

A QTime represents a time of day, with millisecond resolution, from 00:00:00.000 to 23:59:59.999. It stores no date, time zone or daylight saving information. A default-constructed QTime is null; a QTime built from out-of-range values is invalid.

QTime t(13, 45, 30);
t.addSecs(3600).toString("hh:mm:ss"); // "14:45:30"

See also QDate, QDateTime and QElapsedTimer.

Differences from Qt

The start(), restart() and elapsed() functions that Qt 5 provided are not available; use QElapsedTimer instead. Text formats use English text and the format string supports the h, H, m, s, z, AP and ap tokens, and single-quoted literal text. fromString() with a Qt::DateFormat accepts only HH:mm[:ss[.zzz]], whichever format is given.

Member Function Documentation

[constexpr] QTime::QTime()

Constructs a null time. A null time is invalid.

See also isNull() and isValid().

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

Constructs a time with hour h, minute m, seconds s and milliseconds ms. If the time is not valid, the QTime is invalid but not null.

See also isValid().

QTime QTime::addMSecs(int msecs) const

Returns a QTime that is msecs milliseconds after this time, or before it if msecs is negative. The result wraps around midnight. Returns a null QTime if this time is invalid.

See also addSecs() and msecsTo().

QTime QTime::addSecs(int secs) const

Returns a QTime that is secs seconds after this time, or before it if secs is negative. The result wraps around midnight. Returns a null QTime if this time is invalid.

See also addMSecs() and secsTo().

[static] QTime QTime::currentTime()

Returns the current time, as reported by the system clock in local time.

See also QDateTime::currentDateTime() and QDate::currentDate().

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

Returns the time that is msecs milliseconds after midnight. Returns an invalid QTime if msecs is negative or not less than 86400000.

See also msecsSinceStartOfDay().

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

Parses string as HH:mm[:ss[.zzz]] and returns the time. Returns an invalid QTime if string does not match. The format is ignored.

See also toString().

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

This is an overloaded function.

Parses string using format, which uses the same expressions as toString(const QString &) const, and returns the time. Returns an invalid QTime if string does not match format.

See also toString().

int QTime::hour() const

Returns the hour, from 0 to 23, or -1 if the time is invalid.

See also minute(), second() and msec().

[constexpr] bool QTime::isNull() const

Returns true if the time is null, which means it was default constructed and not set; otherwise returns false. A null time is also invalid.

See also isValid().

[constexpr] bool QTime::isValid() const

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

See also isNull().

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

This is an overloaded function.

Returns true if the specified time (h, m, s, ms) is valid; otherwise returns false. The hour must be 0 to 23, the minute and second 0 to 59, and the milliseconds 0 to 999.

int QTime::minute() const

Returns the minute, from 0 to 59, or -1 if the time is invalid.

See also hour(), second() and msec().

int QTime::msec() const

Returns the millisecond, from 0 to 999, or -1 if the time is invalid.

See also hour(), minute() and second().

[constexpr] int QTime::msecsSinceStartOfDay() const

Returns the number of milliseconds since midnight, or 0 if the time is invalid.

See also fromMSecsSinceStartOfDay().

int QTime::msecsTo(QTime t) const

Returns the number of milliseconds from this time to t, which is negative if t is earlier. The result does not wrap around midnight. Returns 0 if either time is invalid.

See also addMSecs() and secsTo().

int QTime::second() const

Returns the second, from 0 to 59, or -1 if the time is invalid.

See also hour(), minute() and msec().

int QTime::secsTo(QTime t) const

Returns the number of whole seconds from this time to t, which is negative if t is earlier. The result does not wrap around midnight. Returns 0 if either time is invalid.

See also addSecs() and msecsTo().

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

Sets the time to hour h, minute m, seconds s and milliseconds msecs. Returns true if the time is valid; otherwise makes the QTime invalid and returns false.

See also isValid().

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

Returns the time as text in the given format, or an empty string if the time is invalid. ISODateWithMs gives HH:mm:ss.zzz; every other format gives HH:mm:ss.

See also fromString().

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

This is an overloaded function.

Returns the time as text using format, or an empty string if the time is invalid. The expressions are h and hh (hour as 0 to 23, or 1 to 12 if the format has an AM/PM expression), H and HH (hour as 0 to 23), m and mm (minute), s and ss (second), z (milliseconds without trailing zeros), zzz (milliseconds as three digits), AP and A (upper-case AM or PM), ap and a (lower-case am or pm). Text in single quotes is copied as is.

See also fromString().