00001 #include "ace_pch.h"
00002 #include "ace/Time_Value.h"
00003 #include "ace/Basic_Types.h"
00004
00005
00006 ACE_RCSID (ace,
00007 Time_Value,
00008 "$Id: Time_Value.cpp,v 1.1.1.1.2.1 2003/03/13 19:44:22 chad Exp $")
00009
00010
00011 #if !defined (__ACE_INLINE__)
00012 #include "ace/Time_Value.inl"
00013 #endif
00014
00015
00016
00017
00018 const ACE_Time_Value ACE_Time_Value::zero;
00019
00020
00021
00022
00023
00024
00025
00026 const ACE_Time_Value ACE_Time_Value::max_time (LONG_MAX,
00027 ACE_ONE_SECOND_IN_USECS - 1);
00028
00029 ACE_ALLOC_HOOK_DEFINE (ACE_Time_Value)
00030
00031
00032
00033
00034 ACE_Time_Value
00035 ACE_Time_Value::operator ++ (int)
00036 {
00037
00038 ACE_Time_Value tv (*this);
00039 ++*this;
00040 return tv;
00041 }
00042
00043 ACE_Time_Value &
00044 ACE_Time_Value::operator ++ (void)
00045 {
00046
00047 this->usec (this->usec () + 1);
00048 this->normalize ();
00049 return *this;
00050 }
00051
00052
00053
00054
00055 ACE_Time_Value
00056 ACE_Time_Value::operator -- (int)
00057 {
00058
00059 ACE_Time_Value tv (*this);
00060 --*this;
00061 return tv;
00062 }
00063
00064 ACE_Time_Value &
00065 ACE_Time_Value::operator -- (void)
00066 {
00067
00068 this->usec (this->usec () - 1);
00069 this->normalize ();
00070 return *this;
00071 }
00072
00073 #if defined (ACE_WIN32)
00074
00075
00076
00077
00078
00079
00080
00081 # if defined (ACE_LACKS_LONGLONG_T)
00082 const ACE_U_LongLong ACE_Time_Value::FILETIME_to_timval_skew =
00083 ACE_U_LongLong (0xd53e8000, 0x19db1de);
00084 # else
00085 const DWORDLONG ACE_Time_Value::FILETIME_to_timval_skew =
00086 ACE_INT64_LITERAL (0x19db1ded53e8000);
00087 # endif
00088
00089
00090
00091 ACE_Time_Value::ACE_Time_Value (const FILETIME &file_time)
00092 {
00093
00094 this->set (file_time);
00095 }
00096
00097 void ACE_Time_Value::set (const FILETIME &file_time)
00098 {
00099
00100 #if defined (ACE_LACKS_LONGLONG_T)
00101 ACE_U_LongLong LL_100ns(file_time.dwLowDateTime, file_time.dwHighDateTime);
00102 LL_100ns -= ACE_Time_Value::FILETIME_to_timval_skew;
00103
00104 this->tv_.tv_sec = (long) (LL_100ns / ((double) (10000 * 1000)));
00105
00106 this->tv_.tv_usec = (long)((LL_100ns % ((ACE_UINT32)(10000 * 1000))) / 10);
00107 #else
00108
00109 ULARGE_INTEGER _100ns;
00110 _100ns.LowPart = file_time.dwLowDateTime;
00111 _100ns.HighPart = file_time.dwHighDateTime;
00112
00113 _100ns.QuadPart -= ACE_Time_Value::FILETIME_to_timval_skew;
00114
00115
00116 this->tv_.tv_sec = (long) (_100ns.QuadPart / (10000 * 1000));
00117
00118 this->tv_.tv_usec = (long) ((_100ns.QuadPart % (10000 * 1000)) / 10);
00119 #endif // ACE_LACKS_LONGLONG_T
00120 this->normalize ();
00121 }
00122
00123
00124
00125 ACE_Time_Value::operator FILETIME () const
00126 {
00127 FILETIME file_time;
00128
00129
00130 #if defined (ACE_LACKS_LONGLONG_T)
00131 ACE_U_LongLong LL_sec(this->tv_.tv_sec);
00132 ACE_U_LongLong LL_usec(this->tv_.tv_usec);
00133 ACE_U_LongLong LL_100ns = LL_sec * (ACE_UINT32)(10000 * 1000) +
00134 LL_usec * (ACE_UINT32)10 +
00135 ACE_Time_Value::FILETIME_to_timval_skew;
00136 file_time.dwLowDateTime = LL_100ns.lo();
00137 file_time.dwHighDateTime = LL_100ns.hi();
00138 #else
00139 ULARGE_INTEGER _100ns;
00140 _100ns.QuadPart = (((DWORDLONG) this->tv_.tv_sec * (10000 * 1000) +
00141 this->tv_.tv_usec * 10) +
00142 ACE_Time_Value::FILETIME_to_timval_skew);
00143
00144 file_time.dwLowDateTime = _100ns.LowPart;
00145 file_time.dwHighDateTime = _100ns.HighPart;
00146 #endif //ACE_LACKS_LONGLONG_T
00147
00148 return file_time;
00149 }
00150
00151 #endif
00152
00153 void
00154 ACE_Time_Value::dump (void) const
00155 {
00156
00157 #if 0
00158 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00159 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\ntv_sec_ = %d"), this->tv_.tv_sec));
00160 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\ntv_usec_ = %d\n"), this->tv_.tv_usec));
00161 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00162 #endif
00163 }
00164
00165 void
00166 ACE_Time_Value::normalize (void)
00167 {
00168
00169
00170
00171 if (this->tv_.tv_usec >= ACE_ONE_SECOND_IN_USECS)
00172 {
00173 do
00174 {
00175 this->tv_.tv_sec++;
00176 this->tv_.tv_usec -= ACE_ONE_SECOND_IN_USECS;
00177 }
00178 while (this->tv_.tv_usec >= ACE_ONE_SECOND_IN_USECS);
00179 }
00180 else if (this->tv_.tv_usec <= -ACE_ONE_SECOND_IN_USECS)
00181 {
00182 do
00183 {
00184 this->tv_.tv_sec--;
00185 this->tv_.tv_usec += ACE_ONE_SECOND_IN_USECS;
00186 }
00187 while (this->tv_.tv_usec <= -ACE_ONE_SECOND_IN_USECS);
00188 }
00189
00190 if (this->tv_.tv_sec >= 1 && this->tv_.tv_usec < 0)
00191 {
00192 this->tv_.tv_sec--;
00193 this->tv_.tv_usec += ACE_ONE_SECOND_IN_USECS;
00194 }
00195 else if (this->tv_.tv_sec < 0 && this->tv_.tv_usec > 0)
00196 {
00197 this->tv_.tv_sec++;
00198 this->tv_.tv_usec -= ACE_ONE_SECOND_IN_USECS;
00199 }
00200 }