timeval.h
上传用户:psq1974
上传日期:2007-01-06
资源大小:1195k
文件大小:5k
- /* Copyright (C) 1998, 1999 State University of New York at Stony Brook
- Author: Andrew V. Shuvalov ( andrew@ecsl.cs.sunysb.edu )
- Software license is located in file "COPYING"
- */
- #ifndef _timeval_h_
- #define _timeval_h_
- //! author="Andrew V. Shuvalov"
- #ifdef _WIN32
- # include <winbase.h>
- #endif
- #ifdef _WIN32
- # define STD std::
- #else
- # define STD /* */
- #endif
- /** hides all operations with struct timeval
- */
- class Timeval {
- struct timeval value;
- public:
- Timeval() {}
- Timeval( const Timeval &t )
- {
- *this = t;
- }
- Timeval( const struct timeval &tv ) : value( tv ) {}
- Timeval( long s, long us ) { value.tv_sec = s; value.tv_usec = us; }
- operator const struct timeval &() const { return value; }
- operator const struct timeval *() const { return &value; }
- struct timeval &val() { return value; }
-
- bool operator <( const Timeval &tv ) const
- {
- if( value.tv_sec == tv.value.tv_sec ) {
- if( value.tv_usec < tv.value.tv_usec )
- return true;
- else
- return false;
- }
- if( value.tv_sec < tv.value.tv_sec )
- return true;
- else
- return false;
- }
- bool operator <( const struct timeval &tv ) const
- {
- if( value.tv_sec == tv.tv_sec ) {
- if( value.tv_usec < tv.tv_usec )
- return true;
- else
- return false;
- }
- if( value.tv_sec < tv.tv_sec )
- return true;
- else
- return false;
- }
- /** compare with now */
- bool reached() {
- Timeval now;
- now.now();
- return *this < now;
- }
- const Timeval &operator =( const struct timeval &tv ) {
- value = tv;
- return *this;
- }
- const Timeval &operator =( const Timeval &tv ) {
- if( this != &tv )
- value = tv;
- return *this;
- }
- void normalize()
- {
- if( value.tv_usec > 0 )
- {
- value.tv_sec = value.tv_sec + value.tv_usec / 1000000;
- value.tv_usec = value.tv_usec % 1000000;
- }
- else
- {
- value.tv_sec = value.tv_sec + value.tv_usec / 1000000;
- value.tv_usec = 1000000 - ( -value.tv_usec % 1000000 );
- }
- }
- // if result is negative, it is not garanteed to be normalized; usec
- // is garanteed to be positive
- // !!! In Linux 2.2 tv_sec is time_t, may not be negative !!!
- Timeval operator -( const Timeval &tv ) const {
- Timeval result;
- result.value.tv_sec = value.tv_sec - tv.value.tv_sec;
- result.value.tv_usec = value.tv_usec - tv.value.tv_usec;
- while( result.value.tv_usec > 1000000 ) {
- result.value.tv_usec -= 1000000;
- result.value.tv_sec ++;
- }
- while( result.value.tv_usec < 0 ) {
- result.value.tv_usec += 1000000;
- result.value.tv_sec --;
- }
- return result;
- }
- Timeval operator +( const Timeval &tv ) const {
- Timeval result;
- result.value.tv_sec = value.tv_sec + tv.value.tv_sec;
- result.value.tv_usec = value.tv_usec + tv.value.tv_usec;
- while( result.value.tv_usec > 1000000 ) {
- result.value.tv_usec -= 1000000;
- result.value.tv_sec ++;
- }
- return result;
- }
- // multiply time offset by float koeff, 0 <= k
- const Timeval &operator *=( float k )
- {
- value.tv_usec = ( long )( value.tv_sec * k * 1000000 +
- value.tv_usec * k );
- value.tv_sec = value.tv_usec / 1000000;
- value.tv_usec = value.tv_usec % 1000000;
- return *this;
- }
- // multiply time offset by int koeff, k > 1
- const Timeval &operator *=( unsigned int k )
- {
- value.tv_usec = ( long )( value.tv_sec * k * 1000000 +
- value.tv_usec * k );
- value.tv_sec = value.tv_usec / 1000000;
- value.tv_usec = value.tv_usec % 1000000;
- return *this;
- }
- long int &tv_sec() { return value.tv_sec; }
- const long int &tv_sec() const { return value.tv_sec; }
- long int &tv_usec() { return value.tv_usec; }
- const long int &tv_usec() const { return value.tv_usec; }
- Timeval &now()
- {
- # ifndef _WIN32 // is how normal people do it:
- gettimeofday( &value, NULL );
- # else
- FILETIME ft;
- GetSystemTimeAsFileTime( &ft );
- // measured in 1/10 millisecond
- // 31536000 sec per year
- unsigned _int64 tim =
- ((unsigned _int64)ft.dwHighDateTime << 32 ) | ft.dwLowDateTime;
- value.tv_sec = tim / 10000000 - 12531803899; // some adjustement
- value.tv_usec = tim % 10000000;
- # endif
- return *this;
- }
- /** describe itself ( for debug ) */
- const STD string get_description() const
- {
- static char buf[100];
- # ifndef _WIN32 // is how normal people do it:
- snprintf
- # else
- _snprintf
- # endif
- ( buf, sizeof( buf ), "%d sec, %d usec ", value.tv_sec,
- value.tv_usec );
- return STD string( buf );
- }
- };
- //: prints itself
- inline ostream &operator <<( ostream &os, const Timeval &t )
- {
- char buf[100];
- # ifndef _WIN32 // is how normal people do it:
- snprintf
- # else
- _snprintf
- # endif
- ( buf, sizeof( buf ), "%d sec, %d usec ", t.tv_sec(), t.tv_usec() );
- os << buf;
- return os;
- }
- #endif // _timeval_h_