00001 // Time.hpp 00002 // 00003 // created: June 6, 2011 00004 // 00005 // Mit "-lrt" linken!! 00006 // 00007 00008 #ifndef TIME_HPP 00009 #define TIME_HPP 00010 00011 #include "../BasicDatatypes.hpp" 00012 #include <sys/time.h> 00013 #include <time.h> 00014 00015 // Eine Zeitspanne, in [s] 00016 class TimeDuration 00017 { 00018 public: 00019 TimeDuration(); 00020 TimeDuration(double seconds) { m_duration = seconds; } 00021 00022 void set(double seconds) { m_duration = seconds; } 00023 inline UINT32 total_milliseconds(); 00024 inline TimeDuration& operator=(const double& seconds); 00025 00026 double m_duration; // Zeit, in [s] 00027 }; 00028 00029 // Fuer td = x; 00030 inline TimeDuration& TimeDuration::operator=(const double& seconds) 00031 { 00032 m_duration = seconds; 00033 return *this; 00034 } 00035 00036 // Zeitspanne als [ms] 00037 inline UINT32 TimeDuration::total_milliseconds() 00038 { 00039 UINT32 ms = (UINT32)((m_duration * 1000.0) + 0.5); 00040 return ms; 00041 } 00042 00043 00044 class Time 00045 { 00046 public: 00047 Time(); 00048 Time(timeval time); 00049 ~Time(); 00050 00051 void set(double time); 00052 void set(timeval time); 00053 void set(UINT64 ntpSeconds, UINT32 ntpFractionalSeconds); 00054 void set(UINT64 ntpTime); 00055 double seconds(); 00056 UINT32 total_milliseconds(); 00057 00058 static Time now(); // Returns the current time 00059 00060 Time operator+(const TimeDuration& dur) const; 00061 Time& operator+=(const Time& other); 00062 Time operator+(const Time& other) const; 00063 Time operator-(const Time& other) const; 00064 Time operator-(const double& seconds) const; 00065 bool operator>=(const Time& other) const; 00066 bool operator<(const Time& other) const; 00067 bool operator==(const Time& other) const; 00068 00069 std::string toString() const; 00070 std::string toLongString() const; 00071 00072 static const UINT64 secondsFrom1900to1970; 00073 00074 private: 00075 timeval m_time; // Zeit, in [s] 00076 00077 static const double m_secondFractionNTPtoNanoseconds; // = 2^-32 * 1e9 00078 static const double m_nanosecondsToSecondFractionNTP; // = 2^32 * 1e-9 00079 }; 00080 00081 00082 #endif // TIME_HPP