Time.cc

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

Generated on Sat Sep 8 08:36:18 2007 for DTN Reference Implementation by  doxygen 1.5.3