Public Member Functions | Static Public Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
QTime Class Reference

The QTime class provides clock time functions. More...

#include <qdatetime.h>

Public Member Functions

 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 Member Functions

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

Static Private Member Functions

static bool currentTime (QTime *)
 

Private Attributes

uint ds
 

Friends

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

Detailed Description

The QTime class provides clock time functions.

A QTime object contains a clock time, i.e. a 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 (milli)seconds.

QTime operates with 24-hour clock format; it has no concept of AM/PM. It operates with local time; it does not know anything about time zones or daylight savings time.

A QTime object is typically created either by giving the number of hours, minutes, seconds, and milliseconds explicitly, or by using the static function currentTime(), which makes a QTime object which contains the system's clock time. Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.

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

QTime provides a full set of operators to compare two QTime objects. A 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 the addSecs() or addMSecs() functions. Correspondingly, the number of (milli)seconds between two times can be found using the secsTo() or msecsTo() functions.

QTime can be used to measure a span of elapsed time using the start(), restart(), and elapsed() functions.

See also
QDate, QDateTime

Definition at line 108 of file qdatetime.h.

Constructor & Destructor Documentation

QTime::QTime ( )
inline

Constructs the time 0 hours, minutes, seconds and milliseconds, i.e. 00:00:00.000 (midnight). This is a valid time.

See also
isValid()

Definition at line 111 of file qdatetime.h.

111 { ds=0; } // set null time
uint ds
Definition: qdatetime.h:148
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.

h must be in the range 0-23, m and s must be in the range 0-59, and ms must be in the range 0-999.

See also
isValid()

Definition at line 618 of file qdatetime.cpp.

619 {
620  setHMS( h, m, s, ms );
621 }
static constexpr double ms
Definition: Units.h:96
static QCString * s
Definition: config.cpp:1042
bool setHMS(int h, int m, int s, int ms=0)
Definition: qdatetime.cpp:706

Member Function Documentation

QTime QTime::addMSecs ( int  ms) const

Returns a 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() for an example.

See also
addSecs(), msecsTo()

Definition at line 769 of file qdatetime.cpp.

770 {
771  QTime t;
772  if ( ms < 0 ) {
773  // % not well-defined for -ve, but / is.
774  int negdays = (MSECS_PER_DAY-ms) / MSECS_PER_DAY;
775  t.ds = ((int)ds + ms + negdays*MSECS_PER_DAY)
776  % MSECS_PER_DAY;
777  } else {
778  t.ds = ((int)ds + ms) % MSECS_PER_DAY;
779  }
780  return t;
781 }
uint ds
Definition: qdatetime.h:148
static constexpr double ms
Definition: Units.h:96
The QTime class provides clock time functions.
Definition: qdatetime.h:108
static const uint MSECS_PER_DAY
Definition: qdatetime.cpp:65
QTime QTime::addSecs ( int  nsecs) const

Returns a QTime object containing a time nsecs seconds later than the time of this object (or earlier if ms is negative).

Note that the time will wrap if it passes midnight.

Example:

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

Definition at line 739 of file qdatetime.cpp.

740 {
741  return addMSecs(nsecs*1000);
742 }
QTime addMSecs(int ms) const
Definition: qdatetime.cpp:769
QTime QTime::currentTime ( )
static

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.

Definition at line 848 of file qdatetime.cpp.

849 {
850  QTime ct;
851  currentTime( &ct );
852  return ct;
853 }
The QTime class provides clock time functions.
Definition: qdatetime.h:108
static QTime currentTime()
Definition: qdatetime.cpp:848
bool QTime::currentTime ( QTime ct)
staticprivate

Definition at line 863 of file qdatetime.cpp.

864 {
865  if ( !ct ) {
866 #if defined(CHECK_NULL)
867  qWarning( "QTime::currentTime(QTime *): Null pointer not allowed" );
868 #endif
869  return FALSE;
870  }
871 
872 #if defined(_OS_WIN32_)
873 
874  SYSTEMTIME t;
875  GetLocalTime( &t );
876  ct->ds = MSECS_PER_HOUR*t.wHour + MSECS_PER_MIN*t.wMinute +
877  1000*t.wSecond + t.wMilliseconds;
878  return (t.wHour == 0 && t.wMinute == 0);
879 
880 #elif defined(_OS_OS2_)
881 
882  DATETIME t;
883  DosGetDateTime( &t );
884  ct->ds = MSECS_PER_HOUR*t.hours + MSECS_PER_MIN*t.minutes +
885  1000*t.seconds + 10*t.hundredths;
886  return (t.hours == 0 && t.minutes == 0);
887 
888 #elif defined(_OS_MSDOS_)
889 
890  _dostime_t t;
891  _dos_gettime( &t );
892  ct->ds = MSECS_PER_HOUR*t.hour + MSECS_PER_MIN*t.minute +
893  t.second*1000 + t.hsecond*10;
894  return (t.hour== 0 && t.minute == 0);
895 
896 #elif defined(_OS_UNIX_) || defined(_OS_MAC_)
897 
898  struct timeval tv;
899  gettimeofday( &tv, 0 );
900  time_t ltime = tv.tv_sec;
901  tm *t = localtime( &ltime );
902  ct->ds = (uint)( MSECS_PER_HOUR*t->tm_hour + MSECS_PER_MIN*t->tm_min +
903  1000*t->tm_sec + tv.tv_usec/1000 );
904  return (t->tm_hour== 0 && t->tm_min == 0);
905 
906 #else
907 
908  time_t ltime; // no millisecond resolution!!
909  ::time( &ltime );
910  tm *t = localtime( &ltime );
911  ct->ds = MSECS_PER_HOUR*t->tm_hour + MSECS_PER_MIN*t->tm_min +
912  1000*t->tm_sec;
913  return (t->tm_hour== 0 && t->tm_min == 0);
914 #endif
915 }
static const uint MSECS_PER_HOUR
Definition: qdatetime.cpp:67
uint ds
Definition: qdatetime.h:148
#define gettimeofday
Definition: qdatetime.cpp:38
const bool FALSE
Definition: qglobal.h:370
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
tm
Definition: demo.py:21
static const uint MSECS_PER_MIN
Definition: qdatetime.cpp:69
unsigned uint
Definition: qglobal.h:351
int QTime::elapsed ( )

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

Note that the counter wraps to zero 24 hours after the last call to start() 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 time start() or restart() was called, the result is undefined. This can happen e.g. when daylight saving is turned on or off.
See also
start(), restart()

Definition at line 1000 of file qdatetime.cpp.

1001 {
1002  int n = msecsTo( currentTime() );
1003  if ( n < 0 ) // passed midnight
1004  n += 86400*1000;
1005  return n;
1006 }
int msecsTo(const QTime &) const
Definition: qdatetime.cpp:793
std::void_t< T > n
static QTime currentTime()
Definition: qdatetime.cpp:848
int QTime::hour ( ) const

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

Definition at line 648 of file qdatetime.cpp.

649 {
650  return ds / MSECS_PER_HOUR;
651 }
static const uint MSECS_PER_HOUR
Definition: qdatetime.cpp:67
uint ds
Definition: qdatetime.h:148
bool QTime::isNull ( ) const
inline

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

See also
isValid()

Definition at line 114 of file qdatetime.h.

114 { return ds == 0; }
uint ds
Definition: qdatetime.h:148
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()

Definition at line 638 of file qdatetime.cpp.

639 {
640  return ds < MSECS_PER_DAY;
641 }
uint ds
Definition: qdatetime.h:148
static const uint MSECS_PER_DAY
Definition: qdatetime.cpp:65
bool QTime::isValid ( int  h,
int  m,
int  s,
int  ms = 0 
)
static

Returns TRUE if the specified time is valid, otherwise FALSE.

The time is valid if h is in the range 0-23, m and s are in the range 0-59, and ms is in the range 0-999.

Example:

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

Definition at line 930 of file qdatetime.cpp.

931 {
932  return (uint)h < 24 && (uint)m < 60 && (uint)s < 60 && (uint)ms < 1000;
933 }
static constexpr double ms
Definition: Units.h:96
unsigned uint
Definition: qglobal.h:351
static QCString * s
Definition: config.cpp:1042
int QTime::minute ( ) const

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

Definition at line 657 of file qdatetime.cpp.

658 {
659  return (ds % MSECS_PER_HOUR)/MSECS_PER_MIN;
660 }
static const uint MSECS_PER_HOUR
Definition: qdatetime.cpp:67
uint ds
Definition: qdatetime.h:148
static const uint MSECS_PER_MIN
Definition: qdatetime.cpp:69
int QTime::msec ( ) const

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

Definition at line 675 of file qdatetime.cpp.

676 {
677  return ds % 1000;
678 }
uint ds
Definition: qdatetime.h:148
int QTime::msecsTo ( const QTime t) const

Returns the number of milliseconds from this time to t (which is negative if t is earlier than this time).

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

See also
secsTo()

Definition at line 793 of file qdatetime.cpp.

794 {
795  return (int)t.ds - (int)ds;
796 }
uint ds
Definition: qdatetime.h:148
bool QTime::operator!= ( const QTime t) const
inline

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

Definition at line 132 of file qdatetime.h.

132 { return ds != d.ds; }
uint ds
Definition: qdatetime.h:148
bool QTime::operator< ( const QTime t) const
inline

Returns TRUE if this time is earlier than t, otherwise FALSE.

Definition at line 133 of file qdatetime.h.

133 { return ds < d.ds; }
uint ds
Definition: qdatetime.h:148
bool QTime::operator<= ( const QTime t) const
inline

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

Definition at line 134 of file qdatetime.h.

134 { return ds <= d.ds; }
uint ds
Definition: qdatetime.h:148
bool QTime::operator== ( const QTime t) const
inline

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

Definition at line 131 of file qdatetime.h.

131 { return ds == d.ds; }
uint ds
Definition: qdatetime.h:148
bool QTime::operator> ( const QTime t) const
inline

Returns TRUE if this time is later than t, otherwise FALSE.

Definition at line 135 of file qdatetime.h.

135 { return ds > d.ds; }
uint ds
Definition: qdatetime.h:148
bool QTime::operator>= ( const QTime t) const
inline

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

Definition at line 136 of file qdatetime.h.

136 { return ds >= d.ds; }
uint ds
Definition: qdatetime.h:148
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, 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 clock setting has been changed since the last time start() or restart() was called, the result is undefined. This can happen e.g. when daylight saving is turned on or off.
See also
start(), elapsed(), currentTime()

Definition at line 973 of file qdatetime.cpp.

974 {
975  QTime t = currentTime();
976  int n = msecsTo( t );
977  if ( n < 0 ) // passed midnight
978  n += 86400*1000;
979  *this = t;
980  return n;
981 }
The QTime class provides clock time functions.
Definition: qdatetime.h:108
int msecsTo(const QTime &) const
Definition: qdatetime.cpp:793
std::void_t< T > n
static QTime currentTime()
Definition: qdatetime.cpp:848
int QTime::second ( ) const

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

Definition at line 666 of file qdatetime.cpp.

667 {
668  return (ds / 1000)%SECS_PER_MIN;
669 }
uint ds
Definition: qdatetime.h:148
static const uint SECS_PER_MIN
Definition: qdatetime.cpp:68
int QTime::secsTo ( const QTime t) const

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

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() QDateTime::secsTo()

Definition at line 754 of file qdatetime.cpp.

755 {
756  return ((int)t.ds - (int)ds)/1000;
757 }
uint ds
Definition: qdatetime.h:148
bool QTime::setHMS ( int  h,
int  m,
int  s,
int  ms = 0 
)

Sets the time to hour h, minute m, seconds s and milliseconds ms.

h must be in the range 0-23, m and s must be in the range 0-59, and ms must be in the range 0-999. Returns TRUE if the set time is valid, otherwise FALSE.

See also
isValid()

Definition at line 706 of file qdatetime.cpp.

707 {
708  if ( !isValid(h,m,s,ms) ) {
709 #if defined(CHECK_RANGE)
710  qWarning( "QTime::setHMS Invalid time %02d:%02d:%02d.%03d", h, m, s,
711  ms );
712 #endif
713  ds = MSECS_PER_DAY; // make this invalid
714  return FALSE;
715  }
716  ds = (h*SECS_PER_HOUR + m*SECS_PER_MIN + s)*1000 + ms;
717  return TRUE;
718 }
uint ds
Definition: qdatetime.h:148
static const uint SECS_PER_MIN
Definition: qdatetime.cpp:68
const bool FALSE
Definition: qglobal.h:370
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
static constexpr double ms
Definition: Units.h:96
static const uint SECS_PER_HOUR
Definition: qdatetime.cpp:66
bool isValid() const
Definition: qdatetime.cpp:638
static const uint MSECS_PER_DAY
Definition: qdatetime.cpp:65
static QCString * s
Definition: config.cpp:1042
const bool TRUE
Definition: qglobal.h:371
void QTime::start ( )

Sets this time to the current time. This is practical for timing:

t.start(); // start clock
... // some lengthy task
qDebug( "%d\n", t.elapsed() ); // prints # msecs elapsed
See also
restart(), elapsed(), currentTime()

Definition at line 949 of file qdatetime.cpp.

950 {
951  *this = currentTime();
952 }
static QTime currentTime()
Definition: qdatetime.cpp:848
QString QTime::toString ( ) const

Returns the time of this object in a textual format. Milliseconds are not included. The string format is HH:MM:SS, e.g. 1 second before midnight would be "23:59:59".

Definition at line 687 of file qdatetime.cpp.

688 {
689  QString buf;
690  buf.sprintf( "%.2d:%.2d:%.2d", hour(), minute(), second() );
691  return buf;
692 }
QString & sprintf(const char *format,...)
Definition: qstring.cpp:12719
The QString class provides an abstraction of Unicode text and the classic C null-terminated char arra...
Definition: qstring.h:350
int hour() const
Definition: qdatetime.cpp:648
int minute() const
Definition: qdatetime.cpp:657
int second() const
Definition: qdatetime.cpp:666

Friends And Related Function Documentation

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

Writes a time to the stream.

See also
Format of the QDataStream operators

Definition at line 1390 of file qdatetime.cpp.

1391 {
1392  return s << (Q_UINT32)(t.ds);
1393 }
uint ds
Definition: qdatetime.h:148
unsigned int Q_UINT32
Definition: qglobal.h:420
QDataStream & operator>> ( QDataStream s,
QTime t 
)
friend

Reads a time from the stream.

See also
Format of the QDataStream operators

Definition at line 1402 of file qdatetime.cpp.

1403 {
1404  Q_UINT32 ds;
1405  s >> ds;
1406  t.ds = ds;
1407  return s;
1408 }
uint ds
Definition: qdatetime.h:148
unsigned int Q_UINT32
Definition: qglobal.h:420
static QCString * s
Definition: config.cpp:1042
friend class QDateTime
friend

Definition at line 149 of file qdatetime.h.

Member Data Documentation

uint QTime::ds
private

Definition at line 148 of file qdatetime.h.


The documentation for this class was generated from the following files: