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  this->m_val.set(timeBase, context, seconds, useconds);
43 }
44 
45 Time& Time::operator=(const Time& other) {
46  if (this != &other) {
47  this->m_val = other.m_val;
48  }
49  return *this;
50 }
51 
52 bool Time::operator==(const Time& other) const {
53  return (Time::compare(*this, other) == EQ);
54 }
55 
56 bool Time::operator!=(const Time& other) const {
57  return (Time::compare(*this, other) != EQ);
58 }
59 
60 bool Time::operator>(const Time& other) const {
61  return (Time::compare(*this, other) == GT);
62 }
63 
64 bool Time::operator<(const Time& other) const {
65  return (Time::compare(*this, other) == LT);
66 }
67 
68 bool Time::operator>=(const Time& other) const {
69  Time::Comparison c = Time::compare(*this, other);
70  return ((GT == c) or (EQ == c));
71 }
72 
73 bool Time::operator<=(const Time& other) const {
74  Time::Comparison c = Time::compare(*this, other);
75  return ((LT == c) or (EQ == c));
76 }
77 
79  return this->m_val.serializeTo(buffer);
80 }
81 
83  return this->m_val.deserializeFrom(buffer);
84 }
85 
86 U32 Time::getSeconds() const {
87  return this->m_val.get_seconds();
88 }
89 
90 U32 Time::getUSeconds() const {
91  return this->m_val.get_useconds();
92 }
93 
95  return this->m_val.get_timeBase();
96 }
97 
99  return this->m_val.get_timeContext();
100 }
101 
103  Time time(timeBase, 0, 0, 0);
104  return time;
105 }
106 
107 Time::Comparison Time ::compare(const Time& time1, const Time& time2) {
108  if (time1.getTimeBase() != time2.getTimeBase()) {
109  return INCOMPARABLE;
110  }
111 
112  // Do not compare time context
113 
114  const U32 s1 = time1.getSeconds();
115  const U32 s2 = time2.getSeconds();
116  const U32 us1 = time1.getUSeconds();
117  const U32 us2 = time2.getUSeconds();
118 
119  if (s1 < s2) {
120  return LT;
121  } else if (s1 > s2) {
122  return GT;
123  } else if (us1 < us2) {
124  return LT;
125  } else if (us1 > us2) {
126  return GT;
127  } else {
128  return EQ;
129  }
130 }
131 
132 Time Time ::add(const Time& a, const Time& b) {
133  FW_ASSERT(a.getTimeBase() == b.getTimeBase(), static_cast<FwAssertArgType>(a.getTimeBase()),
134  static_cast<FwAssertArgType>(b.getTimeBase()));
135  // Do not assert on time context match
136 
137  U32 seconds = a.getSeconds() + b.getSeconds();
138  U32 uSeconds = a.getUSeconds() + b.getUSeconds();
139  FW_ASSERT(uSeconds < 1999999);
140  if (uSeconds >= 1000000) {
141  ++seconds;
142  uSeconds -= 1000000;
143  }
144 
145  // Return a time context of 0 if they do not match
146  FwTimeContextStoreType context = a.getContext();
147  if (a.getContext() != b.getContext()) {
148  context = 0;
149  }
150 
151  Time c(a.getTimeBase(), context, seconds, uSeconds);
152  return c;
153 }
154 
155 Time Time ::sub(const Time& minuend,
156  const Time& subtrahend
157 ) {
158  FW_ASSERT(minuend.getTimeBase() == subtrahend.getTimeBase(), static_cast<FwAssertArgType>(minuend.getTimeBase()),
159  static_cast<FwAssertArgType>(subtrahend.getTimeBase()));
160  // Do not assert on time context match
161  // Assert minuend is greater than subtrahend
162  FW_ASSERT(minuend >= subtrahend);
163 
164  U32 seconds = minuend.getSeconds() - subtrahend.getSeconds();
165  U32 uSeconds;
166  if (subtrahend.getUSeconds() > minuend.getUSeconds()) {
167  seconds--;
168  uSeconds = minuend.getUSeconds() + 1000000 - subtrahend.getUSeconds();
169  } else {
170  uSeconds = minuend.getUSeconds() - subtrahend.getUSeconds();
171  }
172 
173  // Return a time context of 0 if they do not match
174  FwTimeContextStoreType context = minuend.getContext();
175  if (minuend.getContext() != subtrahend.getContext()) {
176  context = 0;
177  }
178 
179  return Time(minuend.getTimeBase(), context, seconds, static_cast<U32>(uSeconds));
180 }
181 
182 void Time::add(U32 seconds, U32 useconds) {
183  U32 newSeconds = this->m_val.get_seconds() + seconds;
184  U32 newUSeconds = this->m_val.get_useconds() + useconds;
185  FW_ASSERT(newUSeconds < 1999999, static_cast<FwAssertArgType>(newUSeconds));
186  if (newUSeconds >= 1000000) {
187  newSeconds += 1;
188  newUSeconds -= 1000000;
189  }
190  this->set(newSeconds, newUSeconds);
191 }
192 
193 void Time::setTimeBase(TimeBase timeBase) {
194  this->m_val.set_timeBase(timeBase);
195 }
196 
198  this->m_val.set_timeContext(context);
199 }
200 
201 #ifdef BUILD_UT
202 std::ostream& operator<<(std::ostream& os, const Time& val) {
203  os << "(" << val.getTimeBase() << "," << val.getUSeconds() << "," << val.getSeconds() << ")";
204  return os;
205 }
206 #endif
207 
208 } // namespace Fw
bool operator<=(const Time &other) const
Definition: Time.cpp:73
U32 get_useconds() const
Get member useconds.
FwTimeContextStoreType get_timeContext() const
Get member timeContext.
Time & operator=(const Time &other)
Definition: Time.cpp:45
const Time ZERO_TIME
Definition: Time.cpp:5
TimeBase getTimeBase() const
Definition: Time.cpp:94
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.
static Time sub(const Time &minuend, const Time &subtrahend)
Definition: Time.cpp:155
Comparison
The type of a comparison result.
Definition: Time.hpp:51
bool operator==(const Time &other) const
Definition: Time.cpp:52
SerializeStatus
forward declaration for string
static Time zero(TimeBase timeBase=TimeBase::TB_NONE)
Definition: Time.cpp:102
bool operator!=(const Time &other) const
Definition: Time.cpp:56
void set_seconds(U32 seconds)
Set member seconds.
void setTimeContext(FwTimeContextStoreType context)
Definition: Time.cpp:197
Fw::SerializeStatus deserializeFrom(Fw::SerializeBufferBase &buffer)
Deserialization.
Fw::SerializeStatus serializeTo(Fw::SerializeBufferBase &buffer) const
Serialization.
void set(U32 seconds, U32 useconds)
Definition: Time.cpp:29
bool operator>(const Time &other) const
Definition: Time.cpp:60
bool operator>=(const Time &other) const
Definition: Time.cpp:68
U32 getSeconds() const
Definition: Time.cpp:86
void set(TimeBase::T timeBase, FwTimeContextStoreType timeContext, U32 seconds, U32 useconds)
Set all members.
void setTimeBase(TimeBase timeBase)
Definition: Time.cpp:193
void set_timeBase(TimeBase::T timeBase)
Set member timeBase.
void set_timeContext(FwTimeContextStoreType timeContext)
Set member timeContext.
static Time add(const Time &a, const Time &b)
Definition: Time.cpp:132
FwTimeContextStoreType getContext() const
Definition: Time.cpp:98
U32 getUSeconds() const
Definition: Time.cpp:90
SerializeStatus deserializeFrom(SerializeBufferBase &buffer) override
deserialize contents from buffer
Definition: Time.cpp:82
SerializeStatus serializeTo(SerializeBufferBase &buffer) const override
serialize contents to buffer
Definition: Time.cpp:78
Time()
Definition: Time.cpp:7
static Comparison compare(const Time &time1, const Time &time2)
Definition: Time.cpp:107
forward declaration
U32 get_seconds() const
Get member seconds.
virtual ~Time()
Definition: Time.cpp:14
bool operator<(const Time &other) const
Definition: Time.cpp:64
Define enumeration for Time base types.
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.
void set_useconds(U32 useconds)
Set member useconds.