TimeValue.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00019 #include <coil/TimeValue.h>
00020 
00021 namespace coil
00022 {
00030   TimeValue::TimeValue(long sec, long usec)
00031   {
00032     m_sec = sec;
00033     m_usec = usec;
00034     normalize();
00035   }
00036   
00044   TimeValue::TimeValue(double timeval)
00045   {
00046     double dbHalfAdj;
00047     if ( timeval >= 0 ) 
00048     {
00049         dbHalfAdj = +0.5;
00050     }
00051     else
00052     {
00053         dbHalfAdj = -0.5;
00054     }
00055     m_sec = (long int)timeval;
00056     m_usec = (long)((timeval - (double)m_sec)
00057                     * TIMEVALUE_ONE_SECOND_IN_USECS + dbHalfAdj );
00058     normalize();
00059   }
00060   
00068   TimeValue TimeValue::operator-(TimeValue& tm)
00069   {
00070     TimeValue res;
00071     if (m_sec >= tm.m_sec) // +
00072       {
00073         if (m_usec >= tm.m_usec) /* 繰り下がり無し */
00074           {
00075             res.m_sec  = m_sec  - tm.m_sec;  // -
00076             res.m_usec = m_usec - tm.m_usec; // +
00077           }
00078         else /* m_usec < tm.m_usec 繰り下がり有り */
00079           {
00080             res.m_sec  = m_sec  - tm.m_sec - 1;
00081             res.m_usec = (m_usec + 1000000) - tm.m_usec;
00082           }
00083       }
00084     else // m_sec < tm.m_sec // -
00085       {
00086           if (tm.m_usec >= m_usec) /* 繰り下がり無し */
00087             {
00088               res.m_sec  = - (tm.m_sec  - m_sec); // +
00089               res.m_usec = - (tm.m_usec - m_usec);  // +
00090             }
00091           else /* tm.m_usec < m_usec 繰り下がり有り */
00092             {
00093               res.m_sec  = - (tm.m_sec - m_sec  - 1);
00094               res.m_usec = - (tm.m_usec + 1000000) + m_usec;
00095             }
00096       }
00097     res.normalize();
00098     return res;
00099   }
00100   
00108   TimeValue TimeValue::operator+(TimeValue& tm)
00109   {
00110     TimeValue res;
00111     res.m_sec  = m_sec  + tm.m_sec;
00112     res.m_usec = m_usec + tm.m_usec;
00113     if (res.m_usec >= 1000000)
00114       {
00115         ++res.m_sec;
00116         res.m_usec -= 1000000;
00117       }
00118     res.normalize();
00119     return res;
00120   }
00121   
00129   TimeValue TimeValue::operator=(double time)
00130   {
00131    double dbHalfAdj;
00132    if ( time >= 0 ) 
00133    {
00134        dbHalfAdj = +0.5;
00135    }
00136    else
00137    {
00138        dbHalfAdj = -0.5;
00139    }
00140 
00141     m_sec = (long)time;
00142     m_usec = (long)((time - (double)m_sec)*TIMEVALUE_ONE_SECOND_IN_USECS + dbHalfAdj);
00143     normalize();
00144     return *this;
00145   }
00146   
00154   TimeValue::operator double() const
00155   {
00156     return (double)m_sec + ((double)m_usec/TIMEVALUE_ONE_SECOND_IN_USECS);
00157   }
00158   
00166   int TimeValue::sign() const
00167   {
00168     if (m_sec > 0) return 1;
00169     if (m_sec < 0) return -1;
00170     if (m_usec > 0) return 1;
00171     if (m_usec < 0) return -1;
00172     return 0;
00173   }
00174   
00182   void TimeValue::normalize()
00183   {
00184     if (m_usec >= TIMEVALUE_ONE_SECOND_IN_USECS)
00185       {
00186         do
00187           {
00188             ++m_sec;
00189             m_usec -= TIMEVALUE_ONE_SECOND_IN_USECS;
00190           }
00191         while (m_usec >= TIMEVALUE_ONE_SECOND_IN_USECS);
00192       }
00193     else if (m_usec <= -TIMEVALUE_ONE_SECOND_IN_USECS)
00194       {
00195         do
00196           {
00197             --m_sec;
00198             m_usec += TIMEVALUE_ONE_SECOND_IN_USECS;
00199           }
00200         while (m_usec <= -TIMEVALUE_ONE_SECOND_IN_USECS);
00201       }
00202     
00203     if (m_sec >= 1 && m_usec < 0)
00204       {
00205         --m_sec;
00206         m_usec += TIMEVALUE_ONE_SECOND_IN_USECS;
00207       }
00208     else if (m_sec < 0 && m_usec > 0)
00209       {
00210         ++m_sec;
00211         m_usec -= TIMEVALUE_ONE_SECOND_IN_USECS;
00212       }
00213   }
00214   
00215 };


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Aug 27 2015 14:16:39