00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #if !defined(AFX_TIMEOFDAY_H__DBA78EB9_4A80_4718_AE19_64A999BDE4A6__INCLUDED_)
00012 #define AFX_TIMEOFDAY_H__DBA78EB9_4A80_4718_AE19_64A999BDE4A6__INCLUDED_
00013
00014 #if _MSC_VER > 1000
00015 #pragma once
00016 #endif // _MSC_VER > 1000
00017
00018 #include <assert.h>
00019 class TimeOfDay;
00020 #ifdef _MSC_VER
00021 #define _TOD_USE_WINDOWS_TIME
00022 #endif
00023 #ifdef __BORLANDC__
00024 #define _TOD_USE_WINDOWS_TIME
00025 #endif
00026
00027 #ifdef _TOD_USE_WINDOWS_TIME
00028
00029
00030
00031
00032 typedef unsigned __int64 UInt64Type;
00033 struct TimeOfDayImpl__Win32
00034 {
00035 unsigned __int64 fileTime_;
00036 TimeOfDayImpl__Win32(__int64 ft = 0)
00037 :fileTime_(ft)
00038 {
00039 }
00040 TimeOfDayImpl__Win32 minus(const TimeOfDayImpl__Win32& rhs) const
00041 {
00042 if(fileTime_ < rhs.fileTime_)
00043 return TimeOfDayImpl__Win32();
00044 return fileTime_ - rhs.fileTime_;
00045 }
00046 UInt64Type totalSeconds() const
00047 {
00048 return (unsigned long)(fileTime_ / 10000000);
00049 }
00050 UInt64Type totalMilliSeconds() const
00051 {
00052 return (unsigned long)(fileTime_ / 10000);
00053 }
00054 UInt64Type totalMicroSeconds() const
00055 {
00056 return (unsigned long)(fileTime_ / 10);
00057 }
00058 void setToCurrentTime();
00059 };
00060 typedef TimeOfDayImpl__Win32 TimeOfDayImpl;
00061 #else
00062 #ifdef __GNUC__
00063 #include <sys/time.h>
00064 typedef unsigned long long UInt64Type;
00065 struct TimeOfDayImpl__GCC
00066 {
00067 struct timeval tv;
00068 UInt64Type getAbsoluteMicroSecs() const
00069 {
00070 UInt64Type ret = tv.tv_sec;
00071 ret *= 1000000;
00072 ret += tv.tv_usec;
00073 return ret;
00074 }
00075 TimeOfDayImpl__GCC(UInt64Type usecs=0)
00076 {
00077 tv.tv_sec = usecs / 1000000;
00078 tv.tv_usec = usecs % 1000000;
00079 }
00080 TimeOfDayImpl__GCC minus(const TimeOfDayImpl__GCC& rhs) const
00081 {
00082 if(getAbsoluteMicroSecs() < rhs.getAbsoluteMicroSecs())
00083 return TimeOfDayImpl__GCC();
00084 return getAbsoluteMicroSecs() - rhs.getAbsoluteMicroSecs();
00085 }
00086 UInt64Type totalSeconds() const
00087 {
00088 return (unsigned long)(getAbsoluteMicroSecs() / 1000000);
00089 }
00090 UInt64Type totalMilliSeconds() const
00091 {
00092 return (unsigned long)(getAbsoluteMicroSecs() / 1000);
00093 }
00094 UInt64Type totalMicroSeconds() const
00095 {
00096 return (unsigned long)(getAbsoluteMicroSecs());
00097 }
00098 void setToCurrentTime()
00099 {
00100 gettimeofday(&tv,0);
00101 }
00102 };
00103 typedef TimeOfDayImpl__GCC TimeOfDayImpl;
00104 #endif //#ifdef __GNUC__
00105 #endif //#ifdef _TOD_USE_WINDOWS_TIME ... #else
00106
00107 class TimeOfDay
00108 {
00109 TimeOfDayImpl impl_;
00110 TimeOfDay(const TimeOfDayImpl& impl)
00111 :impl_(impl)
00112 {
00113 }
00114 public:
00115 static void DoTests();
00116 TimeOfDay()
00117 {
00118 }
00119 TimeOfDay operator-(const TimeOfDay& rhs) const
00120 {
00121 return TimeOfDay(impl_.minus(rhs.impl_));
00122 }
00123 UInt64Type totalSeconds() const
00124 {
00125 return impl_.totalSeconds();
00126 }
00127 UInt64Type totalMilliSeconds() const
00128 {
00129 return impl_.totalMilliSeconds();
00130 }
00131 UInt64Type totalMicroSeconds() const
00132 {
00133 return impl_.totalMicroSeconds();
00134 }
00135 void setToCurrentTime()
00136 {
00137 impl_.setToCurrentTime();
00138 }
00139 };
00140
00141 #endif // !defined(AFX_TIMEOFDAY_H__DBA78EB9_4A80_4718_AE19_64A999BDE4A6__INCLUDED_)
00142 {
00143 impl_.setToCurrentTime();
00144 }
00145 };
00146
00147 #endif // !defined(AFX_TIMEOFDAY_H__DBA78EB9_4A80_4718_AE19_64A999BDE4A6__INCLUDED_)