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 TimeInterval::TimeInterval(const Time& start, const Time& end)
14  : TimeInterval(TimeInterval::sub(TimeInterval(end.getSeconds(), end.getUSeconds()),
15  TimeInterval(start.getSeconds(), start.getUSeconds()))) {}
16 
17 void TimeInterval::set(U32 seconds, U32 useconds) {
18  // Assert microseconds portion is less than 10^6
19  FW_ASSERT(useconds < 1000000, static_cast<FwAssertArgType>(useconds));
20  this->m_val.set(seconds, useconds);
21 }
22 
24  if (this != &other) {
25  this->m_val = other.m_val;
26  }
27  return *this;
28 }
29 
30 bool TimeInterval::operator==(const TimeInterval& other) const {
31  return (TimeInterval::compare(*this, other) == EQ);
32 }
33 
34 bool TimeInterval::operator!=(const TimeInterval& other) const {
35  return (TimeInterval::compare(*this, other) != EQ);
36 }
37 
38 bool TimeInterval::operator>(const TimeInterval& other) const {
39  return (TimeInterval::compare(*this, other) == GT);
40 }
41 
42 bool TimeInterval::operator<(const TimeInterval& other) const {
43  return (TimeInterval::compare(*this, other) == LT);
44 }
45 
46 bool TimeInterval::operator>=(const TimeInterval& other) const {
48  return ((GT == c) or (EQ == c));
49 }
50 
51 bool TimeInterval::operator<=(const TimeInterval& other) const {
53  return ((LT == c) or (EQ == c));
54 }
55 
57  // Use TimeIntervalValue's built-in serialization
58  return this->m_val.serializeTo(buffer);
59 }
60 
62  // Use TimeIntervalValue's built-in deserialization
63  return this->m_val.deserializeFrom(buffer);
64 }
65 
67  return this->m_val.get_seconds();
68 }
69 
71  return this->m_val.get_useconds();
72 }
73 
75  const U32 s1 = time1.getSeconds();
76  const U32 s2 = time2.getSeconds();
77  const U32 us1 = time1.getUSeconds();
78  const U32 us2 = time2.getUSeconds();
79 
80  if (s1 < s2) {
81  return LT;
82  } else if (s1 > s2) {
83  return GT;
84  } else if (us1 < us2) {
85  return LT;
86  } else if (us1 > us2) {
87  return GT;
88  } else {
89  return EQ;
90  }
91 }
92 
94  U32 seconds = a.getSeconds() + b.getSeconds();
95  U32 uSeconds = a.getUSeconds() + b.getUSeconds();
96  FW_ASSERT(uSeconds < 1999999);
97  if (uSeconds >= 1000000) {
98  ++seconds;
99  uSeconds -= 1000000;
100  }
101  TimeInterval c(seconds, uSeconds);
102  return c;
103 }
104 
106  const TimeInterval& t2
107 ) {
108  const TimeInterval& minuend = (t1 > t2) ? t1 : t2;
109  const TimeInterval& subtrahend = (t1 > t2) ? t2 : t1;
110 
111  U32 seconds = minuend.getSeconds() - subtrahend.getSeconds();
112  U32 uSeconds;
113  if (subtrahend.getUSeconds() > minuend.getUSeconds()) {
114  seconds--;
115  uSeconds = minuend.getUSeconds() + 1000000 - subtrahend.getUSeconds();
116  } else {
117  uSeconds = minuend.getUSeconds() - subtrahend.getUSeconds();
118  }
119  return TimeInterval(seconds, static_cast<U32>(uSeconds));
120 }
121 
122 void TimeInterval::add(U32 seconds, U32 useconds) {
123  U32 newSeconds = this->m_val.get_seconds() + seconds;
124  U32 newUSeconds = this->m_val.get_useconds() + useconds;
125  FW_ASSERT(newUSeconds < 1999999, static_cast<FwAssertArgType>(newUSeconds));
126  if (newUSeconds >= 1000000) {
127  newSeconds += 1;
128  newUSeconds -= 1000000;
129  }
130  // Assert microseconds portion is less than 10^6
131  FW_ASSERT(newUSeconds < 1000000, static_cast<FwAssertArgType>(newUSeconds));
132  this->m_val.set(newSeconds, newUSeconds);
133 }
134 
135 #ifdef BUILD_UT
136 std::ostream& operator<<(std::ostream& os, const TimeInterval& val) {
137  os << "(" << val.getSeconds() << "s," << val.getUSeconds() << "us)";
138  return os;
139 }
140 #endif
141 
142 } // 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