timestamp_base.cpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10 ** Includes
11 *****************************************************************************/
12 
13 #include <ecl/config.hpp>
15 #include "../../include/ecl/time/timestamp_base.hpp"
16 
17 /*****************************************************************************
18 ** Namespaces
19 *****************************************************************************/
20 
21 namespace ecl {
22 
23 /*****************************************************************************
24 ** Implementation
25 *****************************************************************************/
26 
27 TimeStampBase::TimeStampBase (const double& decimal_time_value)
28 {
29  time.tv_sec = static_cast<long>(decimal_time_value);
30  time.tv_nsec = (decimal_time_value - static_cast<long>(decimal_time_value))*1000000000L;
31 }
32 
33 TimeStampBase::TimeStampBase (const time_t& seconds, const long& nanoseconds) {
34  ecl_assert_throw( ( (seconds > 0) && (nanoseconds >= 0) ) ||
35  ( (seconds < 0) && (nanoseconds <= 0) ) ||
36  ( (seconds == 0) ),
37  StandardException(LOC,InvalidInputError,"Positive timestamps require positive nanoseconds, negative timestamps require negative nanoseconds.") );
38  time.tv_sec = seconds;
39  time.tv_nsec = nanoseconds;
40 }
41 
42 /*****************************************************************************
43 ** Implementation [Stamps]
44 *****************************************************************************/
45 
46 const TimeStampBase& TimeStampBase::stamp (const double& decimal_time_value) {
47  time.tv_sec = static_cast<time_t>(decimal_time_value);
48  time.tv_nsec = (decimal_time_value - static_cast<long>(decimal_time_value))*1000000000L;
49  return (*this);
50 }
51 
52 const TimeStampBase& TimeStampBase::stamp (const time_t& seconds, const long& nanoseconds) {
53  ecl_assert_throw( ( (seconds > 0) && (nanoseconds >= 0) ) ||
54  ( (seconds < 0) && (nanoseconds <= 0) ) ||
55  ( (seconds == 0) ),
56  StandardException(LOC,InvalidInputError,"Positive timestamps require positive nanoseconds, negative timestamps require negative nanoseconds.") );
57  time.tv_sec = seconds;
58  time.tv_nsec = nanoseconds;
59  return (*this);
60 }
61 
62 /*****************************************************************************
63 ** Implementation [Comparison Operators]
64 *****************************************************************************/
65 
66 bool TimeStampBase::operator==(const TimeStampBase& time_stamp ) {
67  if( ( time_stamp.time.tv_sec == time.tv_sec ) && ( time_stamp.time.tv_nsec == time.tv_nsec ) ) {
68  return true;
69  } else {
70  return false;
71  }
72 }
73 
74 bool TimeStampBase::operator!=(const TimeStampBase& time_stamp ) {
75  if( ( time_stamp.time.tv_sec == time.tv_sec ) && ( time_stamp.time.tv_nsec == time.tv_nsec ) ) {
76  return false;
77  } else {
78  return true;
79  }
80 }
81 
82 bool TimeStampBase::operator<=(const TimeStampBase& time_stamp ) {
83  if( time.tv_sec > time_stamp.time.tv_sec ) {
84  return false;
85  } else if ( time.tv_sec == time_stamp.time.tv_sec ) {
86  if ( time.tv_nsec > time_stamp.time.tv_nsec ) {
87  return false;
88  }
89  }
90  return true;
91 }
92 
93 bool TimeStampBase::operator>=(const TimeStampBase& time_stamp ) {
94  if( time.tv_sec < time_stamp.time.tv_sec ) {
95  return false;
96  } else if ( time.tv_sec == time_stamp.time.tv_sec ) {
97  if ( time.tv_nsec < time_stamp.time.tv_nsec ) {
98  return false;
99  }
100  }
101  return true;
102 }
103 
104 bool TimeStampBase::operator<(const TimeStampBase& time_stamp ) {
105  if( time.tv_sec > time_stamp.time.tv_sec ) {
106  return false;
107  } else if ( time.tv_sec == time_stamp.time.tv_sec ) {
108  if ( time.tv_nsec >= time_stamp.time.tv_nsec ) {
109  return false;
110  }
111  }
112  return true;
113 }
114 
115 bool TimeStampBase::operator>(const TimeStampBase& time_stamp ) {
116  if( time.tv_sec < time_stamp.time.tv_sec ) {
117  return false;
118  } else if ( time.tv_sec == time_stamp.time.tv_sec ) {
119  if ( time.tv_nsec <= time_stamp.time.tv_nsec ) {
120  return false;
121  }
122  }
123  return true;
124 }
125 /*****************************************************************************
126 ** Implementation [TimeStamp][Mathematical Operators]
127 *****************************************************************************/
128 
129 void TimeStampBase::operator+=(const TimeStampBase& time_stamp ) {
130  time.tv_sec += time_stamp.time.tv_sec;
131  int64 nsec = time.tv_nsec + time_stamp.time.tv_nsec;
132  if ( nsec >= 1000000000L ) {
133  time.tv_sec += 1;
134  nsec -= 1000000000L;
135  } else if ( nsec <= -1000000000L ) {
136  time.tv_sec -= 1;
137  nsec += 1000000000L;
138  }
139  if ( ( time.tv_sec > 0 ) && ( nsec < 0 ) ) {
140  time.tv_sec -= 1;
141  nsec = 1000000000L + nsec;
142  }
143  if ( ( time.tv_sec < 0 ) && ( nsec > 0 ) ) {
144  time.tv_sec += 1;
145  nsec = nsec - 1000000000L;
146  }
147  time.tv_nsec = nsec;
148 }
149 
150 TimeStampBase TimeStampBase::operator+(const TimeStampBase& time_stamp ) {
151  long sec = time.tv_sec + time_stamp.time.tv_sec;
152  int64 nsec = time.tv_nsec + time_stamp.time.tv_nsec;
153  if ( nsec >= 1000000000L ) {
154  sec += 1;
155  nsec -= 1000000000L;
156  } else if ( nsec <= -1000000000L ) {
157  sec -= 1;
158  nsec += 1000000000L;
159  }
160  if ( ( sec > 0 ) && ( nsec < 0 ) ) {
161  sec -= 1;
162  nsec = 1000000000L + nsec;
163  }
164  if ( ( sec < 0 ) && ( nsec > 0 ) ) {
165  sec += 1;
166  nsec = nsec - 1000000000L;
167  }
168  return TimeStampBase(sec,nsec);
169 }
170 
171 void TimeStampBase::operator-=(const TimeStampBase& time_stamp ) {
172  time_t sec = time.tv_sec - time_stamp.time.tv_sec;
173  long nsec = time.tv_nsec - time_stamp.time.tv_nsec;
174 
175  if ( (sec > 0) && ( nsec < 0 )) {
176  sec -= 1;
177  nsec += 1000000000L;
178  } else if ( (sec < 0) && ( nsec > 0 )) {
179  sec += 1;
180  nsec -= 1000000000L;
181  }
182  time.tv_sec = sec;
183  time.tv_nsec = nsec;
184 }
185 
186 TimeStampBase TimeStampBase::operator-(const TimeStampBase& time_stamp ) {
187  time_t sec = time.tv_sec - time_stamp.time.tv_sec;
188  long nsec = time.tv_nsec - time_stamp.time.tv_nsec;
189 
190  if ( (sec > 0) && ( nsec < 0 )) {
191  sec -= 1;
192  nsec += 1000000000L;
193  } else if ( (sec < 0) && ( nsec > 0 )) {
194  sec += 1;
195  nsec -= 1000000000L;
196  }
197  return TimeStampBase(sec,nsec);
198 }
199 
200 
201 
202 } // namespace ecl
203 
ecl::InvalidInputError
InvalidInputError
ecl::TimeStampBase::operator-
TimeStampBase operator-(const TimeStampBase &time_stamp)
Definition: timestamp_base.cpp:190
ecl::TimeStampBase::operator>
bool operator>(const TimeStampBase &time_stamp)
Definition: timestamp_base.cpp:119
ecl::TimeStampBase::TimeStampBase
TimeStampBase()
Definition: timestamp_base.hpp:59
ecl::TimeStampBase::operator<=
bool operator<=(const TimeStampBase &time_stamp)
Definition: timestamp_base.cpp:86
ecl::TimeStampBase::nsec
long nsec() const
Nanoseconds component (after removing seconds).
Definition: timestamp_base.hpp:140
ecl::TimeStampBase::operator==
bool operator==(const TimeStampBase &time_stamp)
Definition: timestamp_base.cpp:70
ecl::TimeStampBase::operator+=
void operator+=(const TimeStampBase &time_stamp)
Definition: timestamp_base.cpp:133
ecl::TimeStampBase::operator!=
bool operator!=(const TimeStampBase &time_stamp)
Definition: timestamp_base.cpp:78
ecl::TimeStampBase::operator+
TimeStampBase operator+(const TimeStampBase &time_stamp)
Definition: timestamp_base.cpp:154
ecl::TimeStampBase::operator>=
bool operator>=(const TimeStampBase &time_stamp)
Definition: timestamp_base.cpp:97
ecl::TimeStampBase::sec
long sec() const
Seconds component.
Definition: timestamp_base.hpp:119
ecl::TimeStampBase::operator<
bool operator<(const TimeStampBase &time_stamp)
Definition: timestamp_base.cpp:108
ecl::StandardException
ecl::TimeStampBase::time
TimeStructure time
Definition: timestamp_base.hpp:231
config.hpp
ecl_assert_throw
#define ecl_assert_throw(expression, exception)
standard_exception.hpp
ecl::TimeStampBase
This class provides common functionality across platforms for the timestamps.
Definition: timestamp_base.hpp:46
ecl::TimeStampBase::stamp
const TimeStampBase & stamp(const double &decimal_time_value)
Manually set the timestamp.
Definition: timestamp_base.cpp:50
ecl
Embedded control libraries.
ecl::TimeStampBase::operator-=
void operator-=(const TimeStampBase &time_stamp)
Definition: timestamp_base.cpp:175


ecl_time
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:19