F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Time.cpp
Go to the documentation of this file.
2 #include <Fw/Time/Time.hpp>
3 
4 namespace Fw {
5 const Time ZERO_TIME = Time();
6 
7 Time::Time() : m_val() {
9  m_val.set_timeContext(0);
10  m_val.set_seconds(0);
11  m_val.set_useconds(0);
12 }
13 
15 
16 Time::Time(const Time& other) : Serializable() {
17  this->set(other.m_val.get_timeBase(), other.m_val.get_timeContext(), other.m_val.get_seconds(),
18  other.m_val.get_useconds());
19 }
20 
21 Time::Time(U32 seconds, U32 useconds) {
22  this->set(TimeBase::TB_NONE, 0, seconds, useconds);
23 }
24 
25 Time::Time(TimeBase timeBase, U32 seconds, U32 useconds) {
26  this->set(timeBase, 0, seconds, useconds);
27 }
28 
29 void Time::set(U32 seconds, U32 useconds) {
30  this->set(this->m_val.get_timeBase(), this->m_val.get_timeContext(), seconds, useconds);
31 }
32 
33 void Time::set(TimeBase timeBase, U32 seconds, U32 useconds) {
34  this->set(timeBase, this->m_val.get_timeContext(), seconds, useconds);
35 }
36 
37 Time::Time(TimeBase timeBase, FwTimeContextStoreType context, U32 seconds, U32 useconds) {
38  this->set(timeBase, context, seconds, useconds);
39 }
40 
41 void Time::set(TimeBase timeBase, FwTimeContextStoreType context, U32 seconds, U32 useconds) {
42  // Assert microseconds portion is less than 10^6
43  FW_ASSERT(useconds < 1000000, static_cast<FwAssertArgType>(useconds));
44  this->m_val.set(timeBase, context, seconds, useconds);
45 }
46 
47 Fw::Time::Time(F64 seconds) {
48  this->set(seconds);
49 }
50 
51 void Fw::Time::set(F64 seconds) {
52  U32 parsedSeconds = this->parseSeconds(seconds);
53  U32 parsedUseconds = this->parseUSeconds(seconds);
54  this->set(parsedSeconds, parsedUseconds);
55 }
56 
57 Time& Time::operator=(const Time& other) {
58  if (this != &other) {
59  this->m_val = other.m_val;
60  }
61  return *this;
62 }
63 
65  this->set(seconds);
66  return *this;
67 }
68 
70  this->add(seconds);
71  return *this;
72 }
73 
74 bool Time::operator==(const Time& other) const {
75  return (Time::compare(*this, other) == TimeComparison::EQ);
76 }
77 
78 bool Time::operator!=(const Time& other) const {
79  return (Time::compare(*this, other) != TimeComparison::EQ);
80 }
81 
82 bool Time::operator>(const Time& other) const {
83  return (Time::compare(*this, other) == TimeComparison::GT);
84 }
85 
86 bool Time::operator<(const Time& other) const {
87  return (Time::compare(*this, other) == TimeComparison::LT);
88 }
89 
90 bool Time::operator>=(const Time& other) const {
91  TimeComparison c = Time::compare(*this, other);
92  return ((TimeComparison::GT == c) or (TimeComparison::EQ == c));
93 }
94 
95 bool Time::operator<=(const Time& other) const {
96  TimeComparison c = Time::compare(*this, other);
97  return ((TimeComparison::LT == c) or (TimeComparison::EQ == c));
98 }
99 
100 Fw::Time::operator F64() const {
101  const U32 seconds = this->m_val.get_seconds();
102  const U32 useconds = this->m_val.get_useconds();
103  return static_cast<F64>(seconds) + (static_cast<F64>(useconds) / 1000000.0);
104 }
105 
107  return this->m_val;
108 }
109 
111  return this->m_val.serializeTo(buffer, mode);
112 }
113 
115  return this->m_val.deserializeFrom(buffer, mode);
116 }
117 
118 U32 Time::getSeconds() const {
119  return this->m_val.get_seconds();
120 }
121 
122 U32 Time::getUSeconds() const {
123  return this->m_val.get_useconds();
124 }
125 
127  return this->m_val.get_timeBase();
128 }
129 
131  return this->m_val.get_timeContext();
132 }
133 
135  Time time(timeBase, 0, 0, 0);
136  return time;
137 }
138 
139 TimeComparison Time ::compare(const Time& time1, const Time& time2) {
140  if (time1.getTimeBase() != time2.getTimeBase()) {
142  }
143 
144  // Do not compare time context
145 
146  const U32 s1 = time1.getSeconds();
147  const U32 s2 = time2.getSeconds();
148  const U32 us1 = time1.getUSeconds();
149  const U32 us2 = time2.getUSeconds();
150 
151  if (s1 < s2) {
152  return TimeComparison::LT;
153  } else if (s1 > s2) {
154  return TimeComparison::GT;
155  } else if (us1 < us2) {
156  return TimeComparison::LT;
157  } else if (us1 > us2) {
158  return TimeComparison::GT;
159  } else {
160  return TimeComparison::EQ;
161  }
162 }
163 
164 Time Time ::add(const Time& a, const Time& b) {
165  FW_ASSERT(a.getTimeBase() == b.getTimeBase(), static_cast<FwAssertArgType>(a.getTimeBase()),
166  static_cast<FwAssertArgType>(b.getTimeBase()));
167  // Do not assert on time context match
168 
169  U32 seconds = a.getSeconds() + b.getSeconds();
170  U32 uSeconds = a.getUSeconds() + b.getUSeconds();
171  FW_ASSERT(uSeconds < 1999999);
172  if (uSeconds >= 1000000) {
173  ++seconds;
174  uSeconds -= 1000000;
175  }
176 
177  // Return a time context of 0 if they do not match
178  FwTimeContextStoreType context = a.getContext();
179  if (a.getContext() != b.getContext()) {
180  context = 0;
181  }
182 
183  Time c(a.getTimeBase(), context, seconds, uSeconds);
184  return c;
185 }
186 
187 Time Time ::sub(const Time& minuend,
188  const Time& subtrahend
189 ) {
190  FW_ASSERT(minuend.getTimeBase() == subtrahend.getTimeBase(), static_cast<FwAssertArgType>(minuend.getTimeBase()),
191  static_cast<FwAssertArgType>(subtrahend.getTimeBase()));
192  // Do not assert on time context match
193  // Assert minuend is greater than subtrahend
194  FW_ASSERT(minuend >= subtrahend);
195 
196  U32 seconds = minuend.getSeconds() - subtrahend.getSeconds();
197  U32 uSeconds;
198  if (subtrahend.getUSeconds() > minuend.getUSeconds()) {
199  seconds--;
200  uSeconds = minuend.getUSeconds() + 1000000 - subtrahend.getUSeconds();
201  } else {
202  uSeconds = minuend.getUSeconds() - subtrahend.getUSeconds();
203  }
204 
205  // Return a time context of 0 if they do not match
206  FwTimeContextStoreType context = minuend.getContext();
207  if (minuend.getContext() != subtrahend.getContext()) {
208  context = 0;
209  }
210 
211  return Time(minuend.getTimeBase(), context, seconds, static_cast<U32>(uSeconds));
212 }
213 
215  // Assert negative value
216  FW_ASSERT(seconds >= static_cast<F64>(0.0));
217  return static_cast<U32>(seconds);
218 }
219 
221  // Assert negative value
222  FW_ASSERT(seconds >= static_cast<F64>(0.0));
223  U32 parsedSeconds = static_cast<U32>(seconds);
224  const F64 fractionalPart = seconds - static_cast<F64>(parsedSeconds);
225  // Add 0.5 to round to nearest microsecond (e.g, 999999.9999 = 1000000)
226  U32 parsedUSeconds = static_cast<U32>(fractionalPart * static_cast<F64>(1000000.0) + static_cast<F64>(0.5));
227  return parsedUSeconds;
228 }
229 
230 void Time::add(U32 seconds, U32 useconds) {
231  U32 newSeconds = this->m_val.get_seconds() + seconds;
232  U32 newUSeconds = this->m_val.get_useconds() + useconds;
233  FW_ASSERT(newUSeconds < 1999999, static_cast<FwAssertArgType>(newUSeconds));
234  if (newUSeconds >= 1000000) {
235  newSeconds += 1;
236  newUSeconds -= 1000000;
237  }
238  this->set(newSeconds, newUSeconds);
239 }
240 
241 void Time::add(F64 seconds) {
242  U32 parsedSeconds = this->parseSeconds(seconds);
243  U32 parsedUseconds = this->parseUSeconds(seconds);
244  // Add parsed seconds and useconds
245  this->add(parsedSeconds, parsedUseconds);
246 }
247 
248 void Time::setTimeBase(TimeBase timeBase) {
249  this->m_val.set_timeBase(timeBase);
250 }
251 
253  this->m_val.set_timeContext(context);
254 }
255 
256 #ifdef BUILD_UT
257 std::ostream& operator<<(std::ostream& os, const Time& val) {
258  os << "(" << val.getTimeBase() << "," << val.getUSeconds() << "," << val.getSeconds() << ")";
259  return os;
260 }
261 #endif
262 
263 } // namespace Fw
bool operator<=(const Time &other) const
Definition: Time.cpp:95
U32 get_useconds() const
Get member useconds.
FwTimeContextStoreType get_timeContext() const
Get member timeContext.
Time & operator=(const Time &other)
Definition: Time.cpp:57
TimeValue asTimeValue() const
get the underlying TimeValue
Definition: Time.cpp:106
Fw::SerializeStatus deserializeFrom(Fw::SerialBufferBase &buffer, Fw::Endianness mode=Fw::Endianness::BIG)
Deserialization.
const Time ZERO_TIME
Definition: Time.cpp:5
TimeBase getTimeBase() const
Definition: Time.cpp:126
No time base has been established (Required)
TimeBase::T get_timeBase() const
Get member timeBase.
U8 FwTimeContextStoreType
The type used to serialize a time context value.
Fw::SerializeStatus serializeTo(Fw::SerialBufferBase &buffer, Fw::Endianness mode=Fw::Endianness::BIG) const
Serialization.
static Time sub(const Time &minuend, const Time &subtrahend)
Definition: Time.cpp:187
bool operator==(const Time &other) const
Definition: Time.cpp:74
SerializeStatus
forward declaration for string
static Time zero(TimeBase timeBase=TimeBase::TB_NONE)
The type of a comparison result.
Definition: Time.cpp:134
bool operator!=(const Time &other) const
Definition: Time.cpp:78
void set_seconds(U32 seconds)
Set member seconds.
void setTimeContext(FwTimeContextStoreType context)
Definition: Time.cpp:252
static U32 parseUSeconds(F64 seconds)
Extract microseconds from a floating-point (F64)
Definition: Time.cpp:220
void set(U32 seconds, U32 useconds)
Definition: Time.cpp:29
Time & operator+=(F64 seconds)
Add floating-point (F64) value to this Time.
Definition: Time.cpp:69
bool operator>(const Time &other) const
Definition: Time.cpp:82
bool operator>=(const Time &other) const
Definition: Time.cpp:90
static U32 parseSeconds(F64 seconds)
Extract seconds from a floating-point (F64)
Definition: Time.cpp:214
U32 getSeconds() const
Definition: Time.cpp:118
void set(TimeBase::T timeBase, FwTimeContextStoreType timeContext, U32 seconds, U32 useconds)
Set all members.
void setTimeBase(TimeBase timeBase)
Definition: Time.cpp:248
Data structure for Time.
void set_timeBase(TimeBase::T timeBase)
Set member timeBase.
void set_timeContext(FwTimeContextStoreType timeContext)
Set member timeContext.
static TimeComparison compare(const Time &time1, const Time &time2)
Definition: Time.cpp:139
static Time add(const Time &a, const Time &b)
Definition: Time.cpp:164
FwTimeContextStoreType getContext() const
Definition: Time.cpp:130
U32 getUSeconds() const
Definition: Time.cpp:122
Time()
Definition: Time.cpp:7
double F64
64-bit floating point (double). Required for compiler-supplied double promotion.
Definition: BasicTypes.h:85
U32 get_seconds() const
Get member seconds.
SerializeStatus serializeTo(SerialBufferBase &buffer, Fw::Endianness mode=Fw::Endianness::BIG) const override
Serialize the contents of this object to a buffer.
Definition: Time.cpp:110
Implementation of malloc based allocator.
Endianness
virtual ~Time()
Definition: Time.cpp:14
bool operator<(const Time &other) const
Definition: Time.cpp:86
Define enumeration for Time base types.
#define FW_ASSERT(...)
Definition: Assert.hpp:14
SerializeStatus deserializeFrom(SerialBufferBase &buffer, Fw::Endianness mode=Fw::Endianness::BIG) override
Deserialize the contents of this object from a buffer.
Definition: Time.cpp:114
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.
void set_useconds(U32 useconds)
Set member useconds.