TimeValue.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
19 #include <coil/TimeValue.h>
20 
21 namespace coil
22 {
30  TimeValue::TimeValue(long sec, long usec)
31  {
32  m_sec = sec;
33  m_usec = usec;
34  normalize();
35  }
36 
44  TimeValue::TimeValue(double timeval)
45  {
46  double dbHalfAdj;
47  if ( timeval >= 0 )
48  {
49  dbHalfAdj = +0.5;
50  }
51  else
52  {
53  dbHalfAdj = -0.5;
54  }
55  m_sec = (long int)timeval;
56  m_usec = (long)((timeval - (double)m_sec)
57  * TIMEVALUE_ONE_SECOND_IN_USECS + dbHalfAdj );
58  normalize();
59  }
60 
69  {
70  TimeValue res;
71  if (m_sec >= tm.m_sec) // +
72  {
73  if (m_usec >= tm.m_usec) /* 繰り下がり無し */
74  {
75  res.m_sec = m_sec - tm.m_sec; // -
76  res.m_usec = m_usec - tm.m_usec; // +
77  }
78  else /* m_usec < tm.m_usec 繰り下がり有り */
79  {
80  res.m_sec = m_sec - tm.m_sec - 1;
81  res.m_usec = (m_usec + 1000000) - tm.m_usec;
82  }
83  }
84  else // m_sec < tm.m_sec // -
85  {
86  if (tm.m_usec >= m_usec) /* 繰り下がり無し */
87  {
88  res.m_sec = - (tm.m_sec - m_sec); // +
89  res.m_usec = - (tm.m_usec - m_usec); // +
90  }
91  else /* tm.m_usec < m_usec 繰り下がり有り */
92  {
93  res.m_sec = - (tm.m_sec - m_sec - 1);
94  res.m_usec = - (tm.m_usec + 1000000) + m_usec;
95  }
96  }
97  res.normalize();
98  return res;
99  }
100 
109  {
110  TimeValue res;
111  res.m_sec = m_sec + tm.m_sec;
112  res.m_usec = m_usec + tm.m_usec;
113  if (res.m_usec >= 1000000)
114  {
115  ++res.m_sec;
116  res.m_usec -= 1000000;
117  }
118  res.normalize();
119  return res;
120  }
121 
130  {
131  double dbHalfAdj;
132  if ( time >= 0 )
133  {
134  dbHalfAdj = +0.5;
135  }
136  else
137  {
138  dbHalfAdj = -0.5;
139  }
140 
141  m_sec = (long)time;
142  m_usec = (long)((time - (double)m_sec)*TIMEVALUE_ONE_SECOND_IN_USECS + dbHalfAdj);
143  normalize();
144  return *this;
145  }
146 
154  TimeValue::operator double() const
155  {
156  return (double)m_sec + ((double)m_usec/TIMEVALUE_ONE_SECOND_IN_USECS);
157  }
158 
166  int TimeValue::sign() const
167  {
168  if (m_sec > 0) return 1;
169  if (m_sec < 0) return -1;
170  if (m_usec > 0) return 1;
171  if (m_usec < 0) return -1;
172  return 0;
173  }
174 
183  {
185  {
186  do
187  {
188  ++m_sec;
190  }
192  }
194  {
195  do
196  {
197  --m_sec;
199  }
201  }
202 
203  if (m_sec >= 1 && m_usec < 0)
204  {
205  --m_sec;
207  }
208  else if (m_sec < 0 && m_usec > 0)
209  {
210  ++m_sec;
212  }
213  }
214 
215 };
long int m_sec
Definition: TimeValue.h:271
long int sec() const
Get value of second time scale.
Definition: TimeValue.h:110
int sign() const
Sign judgment.
Definition: TimeValue.cpp:166
TimeValue operator+(TimeValue &tm)
Time addition.
Definition: TimeValue.cpp:108
#define TIMEVALUE_ONE_SECOND_IN_USECS
Definition: TimeValue.h:25
long int m_usec
Definition: TimeValue.h:272
void normalize()
Normalize.
Definition: TimeValue.cpp:182
TimeValue(long sec=0, long usec=0)
Constructor.
Definition: TimeValue.cpp:30
TimeValue operator=(double time)
Convert double type into time type.
Definition: TimeValue.cpp:129
long int usec() const
Get value of micro second time scale.
Definition: TimeValue.h:131
TimeValue operator-(TimeValue &tm)
Time subtraction.
Definition: TimeValue.cpp:68
Common Object Interface Layer.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Jun 10 2019 14:07:56