INTRODUCTION Overview Download and Install Quick Start Documentation Publications NONFRAMEWORK CODE Driver Interfaces Drivers Libraries Utilities FRAMEWORK CODE Interfaces Components Libraries Utilities Full Software Listings DEVELOPER Tutorials Examples Dev Guide Dashboard PEOPLE Contributors Users Project Download Mailing lists
|
hydrotime/time.h00001 #ifndef HYDROTIME_TIME_H 00002 #define HYDROTIME_TIME_H 00003 00004 #include <string> 00005 #include <cmath> 00006 00007 namespace hydrotime { 00008 00014 class Time 00015 { 00016 00017 public: 00018 00019 Time() {} 00020 Time( int seconds, int useconds ) 00021 : seconds_(seconds), useconds_(useconds) {} 00022 00023 int seconds() const { return seconds_; } 00024 int useconds() const { return useconds_; } 00025 00026 int &seconds() { return seconds_; } 00027 int &useconds() { return useconds_; } 00028 00029 private: 00030 00031 int seconds_; 00032 int useconds_; 00033 }; 00034 00036 Time getNow(); 00037 00040 inline double timeDiffAsDouble( const Time &t1, const Time &t2 ) 00041 { return ( double(t1.seconds() - t2.seconds()) + 1.0e-6 * double(t1.useconds() - t2.useconds()) ); } 00042 00048 Time toTime( double seconds ); 00049 00052 inline Time timeDiff( const Time &t1, const Time &t2 ) 00053 { return toTime( timeDiffAsDouble( t1, t2 ) ); } 00054 00059 inline double timeAsDouble( const Time &t ) 00060 { return ((double)(t.seconds()) + 1.0e-6 * (double)(t.useconds())); } 00061 00063 void add( Time &t, double secondsAsDouble ); 00065 void subtract( Time &t, double secondsAsDouble ); 00067 void add( Time &t1, const Time &t2 ); 00069 void subtract( Time &t1, const Time &t2 ); 00070 00071 std::string toString( const Time &t ); 00072 inline std::ostream &operator<<( std::ostream &s, const Time &t ) 00073 { return s << toString(t); } 00074 00075 std::string toStringAsSecUSec( const Time &t ); 00076 00078 // operators 00080 00081 // add 00082 inline Time operator+( const Time &t, double secondsToAdd ) 00083 { Time result=t; add(result,secondsToAdd); return result; } 00084 inline Time operator+( const Time &t1, const Time &t2 ) 00085 { Time result=t1; add(result,t2); return result; } 00086 00087 inline void operator+=( Time &t1, const double secondsToAdd ) 00088 { t1 = t1+secondsToAdd; } 00089 inline void operator+=( Time &t1, const Time &t2 ) 00090 { t1 = t1+t2; } 00091 00092 // subtract 00093 inline Time operator-( const Time &t, double secondsToSubtract ) 00094 { Time result=t; subtract(result,secondsToSubtract); return result; } 00095 inline Time operator-( const Time &t1, const Time &t2 ) 00096 { Time result=t1; subtract(result,t2); return result; } 00097 00098 // compare 00099 inline bool operator>( const Time &t1, const Time &t2 ) 00100 { return timeDiffAsDouble( t1, t2 ) > 0; } 00101 inline bool operator<( const Time &t1, const Time &t2 ) 00102 { return timeDiffAsDouble( t1, t2 ) < 0; } 00103 inline bool operator==( const Time &t1, const Time &t2 ) 00104 { return t1.seconds()==t2.seconds() && t1.useconds()==t2.useconds(); } 00105 inline bool operator!=( const Time &t1, const Time &t2 ) 00106 { return !(t1==t2); } 00107 inline bool operator<=( const Time &t1, const Time &t2 ) 00108 { return !(t1>t2); } 00109 inline bool operator>=( const Time &t1, const Time &t2 ) 00110 { return !(t1<t2); } 00111 00112 inline bool near( const Time &t1, const Time &t2, double secondsTolerance ) 00113 { return fabs(timeDiffAsDouble(t1,t2)) <= secondsTolerance; } 00114 00115 inline Time min( const Time &t1, const Time &t2 ) 00116 { if (t1<t2) return t1; else return t2; } 00117 inline Time max( const Time &t1, const Time &t2 ) 00118 { if (t1>t2) return t1; else return t2; } 00119 00120 00121 } 00122 00123 #endif |
Webmaster: Tobias Kaupp (tobasco at users.sourceforge.net)