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