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