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  return this->m_val;
58 }
59 
61  // Use TimeIntervalValue's built-in serialization
62  return this->m_val.serializeTo(buffer, mode);
63 }
64 
66  // Use TimeIntervalValue's built-in deserialization
67  return this->m_val.deserializeFrom(buffer, mode);
68 }
69 
71  return this->m_val.get_seconds();
72 }
73 
75  return this->m_val.get_useconds();
76 }
77 
79  const U32 s1 = time1.getSeconds();
80  const U32 s2 = time2.getSeconds();
81  const U32 us1 = time1.getUSeconds();
82  const U32 us2 = time2.getUSeconds();
83 
84  if (s1 < s2) {
85  return LT;
86  } else if (s1 > s2) {
87  return GT;
88  } else if (us1 < us2) {
89  return LT;
90  } else if (us1 > us2) {
91  return GT;
92  } else {
93  return EQ;
94  }
95 }
96 
98  U32 seconds = a.getSeconds() + b.getSeconds();
99  U32 uSeconds = a.getUSeconds() + b.getUSeconds();
100  FW_ASSERT(uSeconds < 1999999);
101  if (uSeconds >= 1000000) {
102  ++seconds;
103  uSeconds -= 1000000;
104  }
105  TimeInterval c(seconds, uSeconds);
106  return c;
107 }
108 
110  const TimeInterval& t2
111 ) {
112  const TimeInterval& minuend = (t1 > t2) ? t1 : t2;
113  const TimeInterval& subtrahend = (t1 > t2) ? t2 : t1;
114 
115  U32 seconds = minuend.getSeconds() - subtrahend.getSeconds();
116  U32 uSeconds;
117  if (subtrahend.getUSeconds() > minuend.getUSeconds()) {
118  seconds--;
119  uSeconds = minuend.getUSeconds() + 1000000 - subtrahend.getUSeconds();
120  } else {
121  uSeconds = minuend.getUSeconds() - subtrahend.getUSeconds();
122  }
123  return TimeInterval(seconds, static_cast<U32>(uSeconds));
124 }
125 
126 void TimeInterval::add(U32 seconds, U32 useconds) {
127  U32 newSeconds = this->m_val.get_seconds() + seconds;
128  U32 newUSeconds = this->m_val.get_useconds() + useconds;
129  FW_ASSERT(newUSeconds < 1999999, static_cast<FwAssertArgType>(newUSeconds));
130  if (newUSeconds >= 1000000) {
131  newSeconds += 1;
132  newUSeconds -= 1000000;
133  }
134  // Assert microseconds portion is less than 10^6
135  FW_ASSERT(newUSeconds < 1000000, static_cast<FwAssertArgType>(newUSeconds));
136  this->m_val.set(newSeconds, newUSeconds);
137 }
138 
139 #ifdef BUILD_UT
140 std::ostream& operator<<(std::ostream& os, const TimeInterval& val) {
141  os << "(" << val.getSeconds() << "s," << val.getUSeconds() << "us)";
142  return os;
143 }
144 #endif
145 
146 } // namespace Fw
SerializeStatus serializeTo(SerialBufferBase &buffer, Fw::Endianness mode=Fw::Endianness::BIG) const override
Serialize the contents of this object to a buffer.
Data structure for Time Interval.
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.
Fw::SerializeStatus deserializeFrom(Fw::SerialBufferBase &buffer, Fw::Endianness mode=Fw::Endianness::BIG)
Deserialization.
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 deserializeFrom(SerialBufferBase &buffer, Fw::Endianness mode=Fw::Endianness::BIG) override
Deserialize the contents of this object from a buffer.
bool operator==(const TimeInterval &other) const
void set(U32 seconds, U32 useconds)
Set all members.
U32 getUSeconds() const
TimeInterval()=default
Comparison
The type of a comparison result.
TimeIntervalValue asTimeIntervalValue() const
get the underlying TimeIntervalValue
Fw::SerializeStatus serializeTo(Fw::SerialBufferBase &buffer, Fw::Endianness mode=Fw::Endianness::BIG) const
Serialization.
U32 get_useconds() const
Get member useconds.
Implementation of malloc based allocator.
Endianness
#define FW_ASSERT(...)
Definition: Assert.hpp:14
U32 getSeconds() const