Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef __CXUTILS_TIME_H
00041 #define __CXUTILS_TIME_H
00042
00043 #include "cxutils/cxbase.h"
00044 #include <string>
00045 #include <cmath>
00046 #include <vector>
00047
00048 namespace CxUtils
00049 {
00050 class IP4Address;
00051 class Socket;
00052 class Mutex;
00053
00060 class CX_UTILS_DLL Time
00061 {
00062 friend class Synchronizer;
00063 public:
00064 typedef unsigned long long int Stamp;
00065 typedef std::vector<Time> List;
00066 Time(const bool setCurrentTime = false);
00067 Time(const Time& time);
00068 Time(const double seconds)
00069 {
00070 *this = seconds;
00071 }
00072 virtual ~Time();
00073 void SetCurrentTime();
00074 void SetLocalTime();
00075 inline void Clear()
00076 {
00077 mDay = mHour = mMinute = mSecond = mMilliseconds = 0;
00078 }
00079 Stamp ToMs() const;
00080 double ToSeconds() const;
00081 static double DifferenceInSeconds(const Time& a, const Time& b) { return a - b; }
00082 static double DifferenceInMs(const Time& a, const Time& b) { return DifferenceInSeconds(a, b)*1000.0; }
00083 static Time GetUtcTime()
00084 {
00085 Time t;
00086 t.SetCurrentTime();
00087 return t;
00088 }
00089 static Time GetLocalTime()
00090 {
00091 Time t;
00092 t.SetLocalTime();
00093 return t;
00094 }
00095 static Stamp GetUtcTimeMs();
00096 static Stamp GetLocalTimeMs();
00097 std::string ToString() const;
00098 Time& FromString(const std::string& str);
00099 Time& operator = (const Time& time);
00100
00101 Time& operator=(const double& seconds)
00102 {
00103 mDay = mHour = mMinute = mSecond = mMilliseconds = 0;
00104 mMilliseconds = (unsigned int)((seconds - ((unsigned int)seconds))*1000);
00105 mSecond = (unsigned int)(fmod(seconds, 60.0));
00106
00107 mMinute = (unsigned int)(seconds/60.0);
00108 mHour = mMinute/60;
00109 mMinute -= mHour*60;
00110
00111 mDay = mHour/24;
00112 mHour -= mDay*24;
00113
00114 return *this;
00115 }
00116 static Time Add(const Time& t1, const Time& t2);
00117 static Time Subtract(const Time& t1, const Time& t2);
00118 inline Time Add(const Time& time) const { return Add(*this, time); }
00119 inline Time Subtract(const Time& time) const { return Subtract(*this, time); }
00120 inline bool operator==(const Time& time) const
00121 {
00122 if(mDay == time.mDay &&
00123 mHour == time.mHour &&
00124 mMinute == time.mMinute &&
00125 mSecond == time.mSecond &&
00126 mMilliseconds == time.mMilliseconds)
00127 {
00128 return true;
00129 }
00130 return false;
00131 }
00132 inline bool operator==(const double seconds) const
00133 {
00134 return fabs(ToSeconds() - seconds) < 0.001;
00135 }
00136 inline bool operator==(const Stamp milliseconds) const
00137 {
00138 return ToMs() == milliseconds;
00139 }
00140 inline bool operator!=(const Time& time) const
00141 {
00142 if(mDay != time.mDay ||
00143 mHour != time.mHour ||
00144 mMinute != time.mMinute ||
00145 mSecond != time.mSecond ||
00146 mMilliseconds != time.mMilliseconds)
00147 {
00148 return true;
00149 }
00150 return false;
00151 }
00152 inline bool operator!=(const double seconds) const
00153 {
00154 return fabs(ToSeconds() - seconds) > 0.001;
00155 }
00156 inline bool operator!=(const Stamp milliseconds) const
00157 {
00158 return ToMs() != milliseconds;
00159 }
00160 inline bool operator <(const Time& time) const { return ToMs() < time.ToMs(); }
00161 inline bool operator <=(const Time& time) const { return ToMs() <= time.ToMs(); }
00162 inline bool operator >(const Time& time) const { return ToMs() > time.ToMs(); }
00163 inline bool operator >=(const Time& time) const { return ToMs() >= time.ToMs(); }
00167 inline double operator-(const Time& time) const { return ToSeconds() - time.ToSeconds(); }
00168 static bool HaveExternalTime() { return mExternalTimeFlag; }
00169 static Time GetExternalTime();
00170 volatile unsigned int mDay;
00171 volatile unsigned int mHour;
00172 volatile unsigned int mMinute;
00173 volatile unsigned int mSecond;
00174 volatile unsigned int mMilliseconds;
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 class CX_UTILS_DLL Synchronizer
00192 {
00193 public:
00194 Synchronizer();
00195 virtual ~Synchronizer();
00196 bool Start(const bool server,
00197 const unsigned short port,
00198 const IP4Address& multicastGroup,
00199 const unsigned int udelay = 1);
00200 void Stop();
00201 protected:
00202 static void ServerThread(void* args);
00203 void* mpThreadObject;
00204 Socket* mpSocket;
00205 unsigned int mDelayUs;
00206 };
00207 private:
00208 static volatile bool mExternalTimeFlag;
00209 static Time mExternalTime;
00210 static Mutex mExternalTimeMutex;
00211 };
00212
00223 CX_UTILS_DLL void SleepMs(const unsigned int ms);
00224
00230 CX_UTILS_DLL unsigned long long int GetTimeMs();
00231
00237 CX_UTILS_DLL double GetTimeSeconds();
00238
00250 CX_UTILS_DLL void GetSystemTime(unsigned int& day,
00251 unsigned int& hour,
00252 unsigned int& minute,
00253 unsigned int& second,
00254 unsigned int& msecond);
00255
00267 CX_UTILS_DLL void GetSystemTime(unsigned short& day,
00268 unsigned short& hour,
00269 unsigned short& minute,
00270 unsigned short& second,
00271 unsigned short& msecond);
00272
00287 CX_UTILS_DLL bool SetSystemTime(unsigned int year,
00288 unsigned int month,
00289 unsigned int day,
00290 unsigned int hour,
00291 unsigned int minute,
00292 unsigned int second,
00293 unsigned int msecond);
00294
00295 }
00296
00297
00298 #endif
00299