# QTime Class Reference

The QTime class provides time functions 24 hours a day. More...

`#include <qdatetime.h>`

List of all member functions.

## Public Members

- QTime ()
- QTime ( int h, int m, int s=0, int ms=0 )
- bool isNull () const
- bool isValid () const
- int hour () const
- int minute () const
- int second () const
- int msec () const
- QString toString () const
- bool setHMS ( int h, int m, int s, int ms=0 )
- QTime addSecs ( int secs ) const
- int secsTo ( const QTime & ) const
- QTime addMSecs ( int ms ) const
- int msecsTo ( const QTime & ) const
- bool operator== ( const QTime & d ) const
- bool operator!= ( const QTime & d ) const
- bool operator< ( const QTime & d ) const
- bool operator<= ( const QTime & d ) const
- bool operator> ( const QTime & d ) const
- bool operator>= ( const QTime & d ) const
- void start ()
- int restart ()
- int elapsed ()

## Static Public Members

- QTime currentTime ()
- bool isValid ( int h, int m, int s, int ms=0 )

## Related Functions

(Note that these are not member functions.)

- QDataStream & operator<< (QDataStream & s, const QTime & t)
- QDataStream & operator>> (QDataStream & s, QTime & t)

## Detailed Description

The QTime class provides time functions 24 hours a day.

The time resolution of QTime is a millisecond, although the accuracy depends on the underlying operating system. Some operating systems (e.g. Linux and Window NT) support a one-millisecond resolution, while others (MS-DOS and Windows 3.1) support only a 55 millisecond resolution.

See also: QDate and QDateTime.

Examples: tictac/tictac.cpp aclock/aclock.cpp

## Member Function Documentation

### QTime::QTime ()

Constructs a time 00:00:00.000, which is valid.

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

### QTime QTime::addMSecs ( int ms ) const

Returns the time plus ms milliseconds.

### QTime QTime::addSecs ( int nsecs ) const

Returns the time plus nsecs seconds.

See also: secsTo().

### QTime QTime::currentTime () `[static]`

Returns the current time.

### int QTime::elapsed ()

Returns the number of milliseconds that have elapsed since start() or restart() were called.

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

Warning: If the system's local time changes, the result is undefined. This can happen e.g. when daylight saving is turned on or off.

See also: start() and restart().

### int QTime::hour () const

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

Examples: tictac/tictac.cpp

### bool QTime::isNull () const

Returns TRUE if the time is equal to 00:00:00.000. A null time is valid.

See also: isValid().

### bool QTime::isValid () const

Returns TRUE if the time is valid, or FALSE if the time is invalid. The time 23:30:55.746 is valid, while 24:12:30 is invalid.

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

Example:

        QTime::isValid(21, 10, 30);         // returns TRUE
        QTime::isValid(22, 5,  62);         // returns FALSE

### int QTime::minute () const

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

Examples: tictac/tictac.cpp aclock/aclock.cpp

### int QTime::msec () const

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

### int QTime::msecsTo ( const QTime & t ) const

Returns the number of milliseconds between this time and t.

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

Returns TRUE if this time is different from t, or FALSE if they are equal.

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

Returns TRUE if this time is before t, otherwise FALSE.

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

Returns TRUE if this time is before or equal to t, otherwise FALSE.

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

Returns TRUE if this time is equal to t, or FALSE if they are different.

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

Returns TRUE if this time is after t, otherwise FALSE.

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

Returns TRUE if this time is equal to or after t, otherwise FALSE.

### int QTime::restart ()

Sets *this to the current time, and returns the number of milliseconds that have elapsed since the last start() or restart().

restart is guaranteed to be atomic, and so is very handy for repeated measurements; call start() to start the first measurement, then restart() for each later measurement.

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

Warning: If the system's local time changes, the result is undefined. This can happen e.g. when daylight saving is turned on or off.

See also: start() and elapsed().

### int QTime::second () const

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

Examples: tictac/tictac.cpp

### int QTime::secsTo ( const QTime & t ) const

Returns the number of seconds from this time to t (which is negative if t is in the past).

Since QTime measures time within a day and there are 86400 seconds in a day, the result is between -86400 and 86400.

See also: addSecs() and QDateTime::secsTo().

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

Sets the hour h, minute m, seconds s and milliseconds ms. Returns TRUE if the time is valid, otherwise FALSE.

### void QTime::start ()

Sets the time to the current time, e.g. for timing:

        QTime t;
        t.start();                          // start clock
        ... // some lengthy task
        debug( "%d\n", t.elapsed() );       // prints # msecs elapsed

See also: restart() and elapsed().

### QString QTime::toString () const

Converts the date to a string, which is returned. Milliseconds are not included. The string format is "03:40:13".

## Related Functions

### QDataStream & operator<< (QDataStream & s, const QTime & t)

Writes a time to the stream.

Serialization format: [Q_UINT32], milliseconds since midnight.

### QDataStream & operator>> (QDataStream & s, QTime & t)

Reads a time from the stream.

Search the documentation, FAQ, qt-interest archive and more (uses www.troll.no):

This file is part of the Qt toolkit, copyright © 1995-99 Troll Tech, all rights reserved.

It was generated from the following files:

- qdatetime.h: 1998/08/25
- qdatetime.cpp: 1999/02/15