Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Time_Value.cpp

Go to the documentation of this file.
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 /* __ACE_INLINE__ */
00014 
00015 
00016 // Static constant representing `zero-time'.
00017 // Note: this object requires static construction.
00018 const ACE_Time_Value ACE_Time_Value::zero;
00019 
00020 // Constant for maximum time representable.  Note that this time
00021 // is not intended for use with select () or other calls that may
00022 // have *their own* implementation-specific maximum time representations.
00023 // Its primary use is in time computations such as those used by the
00024 // dynamic subpriority strategies in the ACE_Dynamic_Message_Queue class.
00025 // Note: this object requires static construction.
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 // Increment microseconds (the only reason this is here is to allow
00032 // the use of ACE_Atomic_Op with ACE_Time_Value).
00033 
00034 ACE_Time_Value
00035 ACE_Time_Value::operator ++ (int)
00036 {
00037   // ACE_OS_TRACE ("ACE_Time_Value::operator ++ (int)");
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   // ACE_OS_TRACE ("ACE_Time_Value::operator ++ (void)");
00047   this->usec (this->usec () + 1);
00048   this->normalize ();
00049   return *this;
00050 }
00051 
00052 // Decrement microseconds (the only reason this is here is / to allow
00053 // the use of ACE_Atomic_Op with ACE_Time_Value).
00054 
00055 ACE_Time_Value
00056 ACE_Time_Value::operator -- (int)
00057 {
00058   // ACE_OS_TRACE ("ACE_Time_Value::operator -- (int)");
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   // ACE_OS_TRACE ("ACE_Time_Value::operator -- (void)");
00068   this->usec (this->usec () - 1);
00069   this->normalize ();
00070   return *this;
00071 }
00072 
00073 #if defined (ACE_WIN32)
00074 // Static constant to remove time skew between FILETIME and POSIX
00075 // time.  POSIX and Win32 use different epochs (Jan. 1, 1970 v.s.
00076 // Jan. 1, 1601).  The following constant defines the difference
00077 // in 100ns ticks.
00078 //
00079 // In the beginning (Jan. 1, 1601), there was no time and no computer.
00080 // And Bill said: "Let there be time," and there was time....
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 //  Initializes the ACE_Time_Value object from a Win32 FILETIME
00090 
00091 ACE_Time_Value::ACE_Time_Value (const FILETIME &file_time)
00092 {
00093   // // ACE_OS_TRACE ("ACE_Time_Value::ACE_Time_Value");
00094   this->set (file_time);
00095 }
00096 
00097 void ACE_Time_Value::set (const FILETIME &file_time)
00098 {
00099   //  Initializes the ACE_Time_Value object from a Win32 FILETIME
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   // Convert 100ns units to seconds;
00104   this->tv_.tv_sec = (long) (LL_100ns / ((double) (10000 * 1000)));
00105   // Convert remainder to microseconds;
00106   this->tv_.tv_usec = (long)((LL_100ns % ((ACE_UINT32)(10000 * 1000))) / 10);
00107 #else
00108   // Don't use a struct initializer, gcc don't like it.
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   // Convert 100ns units to seconds;
00116   this->tv_.tv_sec = (long) (_100ns.QuadPart / (10000 * 1000));
00117   // Convert remainder to microseconds;
00118   this->tv_.tv_usec = (long) ((_100ns.QuadPart % (10000 * 1000)) / 10);
00119 #endif // ACE_LACKS_LONGLONG_T
00120   this->normalize ();
00121 }
00122 
00123 // Returns the value of the object as a Win32 FILETIME.
00124 
00125 ACE_Time_Value::operator FILETIME () const
00126 {
00127   FILETIME file_time;
00128   // ACE_OS_TRACE ("ACE_Time_Value::operator FILETIME");
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 /* ACE_WIN32 */
00152 
00153 void
00154 ACE_Time_Value::dump (void) const
00155 {
00156   // ACE_OS_TRACE ("ACE_Time_Value::dump");
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 /* 0 */
00163 }
00164 
00165 void
00166 ACE_Time_Value::normalize (void)
00167 {
00168   // // ACE_OS_TRACE ("ACE_Time_Value::normalize");
00169   // From Hans Rohnert...
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 }

Generated on Mon Jun 16 11:21:47 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002