F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
TimeInterval.cpp
Go to the documentation of this file.
3 
4 namespace Fw {
6  this->m_val = other.m_val;
7 }
8 
9 TimeInterval::TimeInterval(U32 seconds, U32 useconds) : Serializable() {
10  this->set(seconds, useconds);
11 }
12 
13 void TimeInterval::set(U32 seconds, U32 useconds) {
14  this->m_val.set(seconds, useconds);
15 }
16 
18  if (this != &other) {
19  this->m_val = other.m_val;
20  }
21  return *this;
22 }
23 
24 bool TimeInterval::operator==(const TimeInterval& other) const {
25  return (TimeInterval::compare(*this, other) == EQ);
26 }
27 
28 bool TimeInterval::operator!=(const TimeInterval& other) const {
29  return (TimeInterval::compare(*this, other) != EQ);
30 }
31 
32 bool TimeInterval::operator>(const TimeInterval& other) const {
33  return (TimeInterval::compare(*this, other) == GT);
34 }
35 
36 bool TimeInterval::operator<(const TimeInterval& other) const {
37  return (TimeInterval::compare(*this, other) == LT);
38 }
39 
40 bool TimeInterval::operator>=(const TimeInterval& other) const {
42  return ((GT == c) or (EQ == c));
43 }
44 
45 bool TimeInterval::operator<=(const TimeInterval& other) const {
47  return ((LT == c) or (EQ == c));
48 }
49 
51  // Use TimeIntervalValue's built-in serialization
52  return this->m_val.serializeTo(buffer);
53 }
54 
56  // Use TimeIntervalValue's built-in deserialization
57  return this->m_val.deserializeFrom(buffer);
58 }
59 
61  return this->m_val.get_seconds();
62 }
63 
65  return this->m_val.get_useconds();
66 }
67 
69  const U32 s1 = time1.getSeconds();
70  const U32 s2 = time2.getSeconds();
71  const U32 us1 = time1.getUSeconds();
72  const U32 us2 = time2.getUSeconds();
73 
74  if (s1 < s2) {
75  return LT;
76  } else if (s1 > s2) {
77  return GT;
78  } else if (us1 < us2) {
79  return LT;
80  } else if (us1 > us2) {
81  return GT;
82  } else {
83  return EQ;
84  }
85 }
86 
88  U32 seconds = a.getSeconds() + b.getSeconds();
89  U32 uSeconds = a.getUSeconds() + b.getUSeconds();
90  FW_ASSERT(uSeconds < 1999999);
91  if (uSeconds >= 1000000) {
92  ++seconds;
93  uSeconds -= 1000000;
94  }
95  TimeInterval c(seconds, uSeconds);
96  return c;
97 }
98 
100  const TimeInterval& t2
101 ) {
102  const TimeInterval& minuend = (t1 > t2) ? t1 : t2;
103  const TimeInterval& subtrahend = (t1 > t2) ? t2 : t1;
104 
105  U32 seconds = minuend.getSeconds() - subtrahend.getSeconds();
106  U32 uSeconds;
107  if (subtrahend.getUSeconds() > minuend.getUSeconds()) {
108  seconds--;
109  uSeconds = minuend.getUSeconds() + 1000000 - subtrahend.getUSeconds();
110  } else {
111  uSeconds = minuend.getUSeconds() - subtrahend.getUSeconds();
112  }
113  return TimeInterval(seconds, static_cast<U32>(uSeconds));
114 }
115 
116 void TimeInterval::add(U32 seconds, U32 useconds) {
117  U32 newSeconds = this->m_val.get_seconds() + seconds;
118  U32 newUSeconds = this->m_val.get_useconds() + useconds;
119  FW_ASSERT(newUSeconds < 1999999, static_cast<FwAssertArgType>(newUSeconds));
120  if (newUSeconds >= 1000000) {
121  newSeconds += 1;
122  newUSeconds -= 1000000;
123  }
124  this->m_val.set(newSeconds, newUSeconds);
125 }
126 
127 #ifdef BUILD_UT
128 std::ostream& operator<<(std::ostream& os, const TimeInterval& val) {
129  os << "(" << val.getSeconds() << "s," << val.getUSeconds() << "us)";
130  return os;
131 }
132 #endif
133 
134 } // namespace Fw
SerializeStatus deserializeFrom(SerializeBufferBase &buffer) override
deserialize contents from buffer
Fw::SerializeStatus serializeTo(Fw::SerializeBufferBase &buffer) const
Serialization.
bool operator>=(const TimeInterval &other) const
bool operator<(const TimeInterval &other) const
static Comparison compare(const TimeInterval &time1, const TimeInterval &time2)
bool operator!=(const TimeInterval &other) const
TimeInterval & operator=(const TimeInterval &other)
void set(U32 seconds, U32 useconds)
SerializeStatus
forward declaration for string
U32 get_seconds() const
Get member seconds.
static TimeInterval sub(const TimeInterval &t1, const TimeInterval &t2)
bool operator<=(const TimeInterval &other) const
void add(U32 seconds, U32 mseconds)
bool operator>(const TimeInterval &other) const
SerializeStatus serializeTo(SerializeBufferBase &buffer) const override
serialize contents to buffer
bool operator==(const TimeInterval &other) const
void set(U32 seconds, U32 useconds)
Set all members.
U32 getUSeconds() const
TimeInterval()=default
Fw::SerializeStatus deserializeFrom(Fw::SerializeBufferBase &buffer)
Deserialization.
Comparison
The type of a comparison result.
forward declaration
U32 get_useconds() const
Get member useconds.
#define FW_ASSERT(...)
Definition: Assert.hpp:14
U32 getSeconds() const