Time.cc

Go to the documentation of this file.
00001 #include "Time.h"
00002 
00003 #if defined(_WIN32)
00004 
00005 #include "compat/MSVC.h"
00006 
00007 #else
00008 
00009 #include <sys/time.h>
00010 #include <time.h>
00011 
00012 #endif
00013 
00014 namespace oasys {
00015 
00016 //----------------------------------------------------------------------------
00017 void
00018 Time::get_time() 
00019 {
00020 #if defined(_WIN32)
00021 
00022     SYSTEMTIME systime;
00023     FILETIME   filetime;
00024 
00025     GetSystemTime(&systime);
00026     SystemTimeToFileTime(&systime, &filetime);
00027     
00028     // FileTime is in 100-nanosecond intervals since UTC January 1, 1601
00029     // 116444736000000000 0.1 usec difference
00030     // 11644473600 sec difference
00031     __int64 ft;
00032     ft = filetime.dwLowDateTime;
00033     ft |= ((__int64)filetime.dwHighDateTime)<<32;
00034 
00035     ft -= 116444736000000000;
00036 
00037     // Get microseconds
00038     ft /= 10; 
00039     usec_ = static_cast<u_int32_t>(ft % 1000000);
00040 
00041     // Get seconds
00042     ft /= 1000000;
00043     sec_ = static_cast<u_int32_t>(ft);
00044 
00045 #else
00046     struct timeval tv;
00047 
00048     gettimeofday(&tv, 0);
00049     sec_  = static_cast<u_int32_t>(tv.tv_sec);
00050     usec_ = static_cast<u_int32_t>(tv.tv_usec);
00051 
00052 #endif
00053 
00054     cleanup();
00055 }
00056 
00057 //----------------------------------------------------------------------------
00058 double 
00059 Time::in_seconds()
00060 {
00061     return static_cast<double>(sec_) + 
00062         static_cast<double>(usec_)/1000000;
00063 }
00064 
00065 //----------------------------------------------------------------------------
00066 u_int32_t
00067 Time::in_microseconds()
00068 {
00069     return sec_ * 1000000 + usec_;
00070 }
00071     
00072 //----------------------------------------------------------------------------
00073 u_int32_t
00074 Time::in_milliseconds()
00075 {
00076     return sec_ * 1000 + usec_/1000;
00077 }
00078 
00079 //----------------------------------------------------------------------
00080 u_int32_t
00081 Time::elapsed_ms()
00082 {
00083     Time t;
00084     t.get_time();
00085     t -= *this;
00086     return t.in_milliseconds();
00087 }
00088 
00089 //----------------------------------------------------------------------------
00090 Time
00091 Time::operator+(const Time& t) const
00092 {
00093     return Time(t.sec_ + sec_, t.usec_ + usec_);
00094 }
00095 
00096 //----------------------------------------------------------------------------
00097 Time&
00098 Time::operator+=(const Time& t)
00099 {
00100     sec_  += t.sec_;
00101     usec_ += t.usec_;
00102     cleanup();
00103     return *this;
00104 }
00105 
00106 //----------------------------------------------------------------------------
00107 Time&
00108 Time::operator-=(const Time& t)
00109 {
00110     // a precondition for this fn to be correct is (*this >= t)
00111     if (usec_ < t.usec_) {
00112         usec_ += 1000000;
00113         sec_  -= 1;
00114     }
00115 
00116     sec_  -= t.sec_;
00117     usec_ -= t.usec_;
00118     return *this;
00119 }
00120 
00121 //----------------------------------------------------------------------------
00122 Time
00123 Time::operator-(const Time& t) const
00124 {
00125     // a precondition for this fn to be correct is (*this >= t)
00126     Time t2(*this);
00127     t2 -= t;
00128     return t2;
00129 }
00130 
00131 //----------------------------------------------------------------------------
00132 bool
00133 Time::operator==(const Time& t) const
00134 {
00135     return (sec_ == t.sec_) && (usec_ == t.usec_);
00136 }
00137 
00138 //----------------------------------------------------------------------------
00139 bool
00140 Time::operator<(const Time& t) const
00141 {
00142     return (sec_ < t.sec_) 
00143         || ( (sec_ == t.sec_) && (usec_ < t.usec_));
00144 }
00145 
00146 //----------------------------------------------------------------------------
00147 bool
00148 Time::operator>(const Time& t) const
00149 {
00150     return (sec_ > t.sec_) 
00151         || ( (sec_ == t.sec_) && (usec_ > t.usec_));    
00152 }
00153 
00154 //----------------------------------------------------------------------------
00155 bool
00156 Time::operator<=(const Time& t) const
00157 {
00158     return (*this == t) || (*this < t);
00159 }
00160 
00161 //----------------------------------------------------------------------------
00162 bool
00163 Time::operator>=(const Time& t) const
00164 {
00165     return (*this == t) || (*this > t);
00166 }
00167  
00168 //----------------------------------------------------------------------------
00169 void
00170 Time::cleanup()
00171 {
00172     if (usec_ > 1000000) 
00173     {
00174         sec_ += usec_ / 1000000;
00175         usec_ /= 1000000;
00176     }
00177 }
00178 
00179 } // namespace oasys
00180 
00181 #if 0
00182 
00183 #include <cstdio>
00184 
00185 int 
00186 main()
00187 {
00188     oasys::Time t;
00189 
00190     t.get_time();
00191 
00192     printf("%d %d\n", t.sec_, t.usec_);
00193 
00194     return 0;
00195 } 
00196 
00197 #endif

Generated on Fri Dec 22 14:48:01 2006 for DTN Reference Implementation by  doxygen 1.5.1