F´ Flight Software - C/C++ Documentation devel
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Time.cpp
Go to the documentation of this file.
1#include <Fw/Time/Time.hpp>
2#include <FpConfig.hpp>
3
4namespace Fw {
5 const Time ZERO_TIME = Time();
6
7 Time::Time() : m_seconds(0), m_useconds(0), m_timeBase(TB_NONE), m_timeContext(0) {
8 }
9
11 }
12
13 Time::Time(const Time& other) : Serializable() {
14 this->set(other.m_timeBase,other.m_timeContext,other.m_seconds,other.m_useconds);
15 }
16
17 Time::Time(U32 seconds, U32 useconds) {
18 this->set(TB_NONE,0,seconds,useconds);
19 }
20
21 Time::Time(TimeBase timeBase, U32 seconds, U32 useconds) {
22 this->set(timeBase,0,seconds,useconds);
23 }
24
25 void Time::set(U32 seconds, U32 useconds) {
26 this->set(this->m_timeBase,this->m_timeContext,seconds,useconds);
27 }
28
29 void Time::set(TimeBase timeBase, U32 seconds, U32 useconds) {
30 this->set(timeBase,this->m_timeContext,seconds,useconds);
31 }
32
33 Time::Time(TimeBase timeBase, FwTimeContextStoreType context, U32 seconds, U32 useconds) {
34 this->set(timeBase,context,seconds,useconds);
35 }
36
37 void Time::set(TimeBase timeBase, FwTimeContextStoreType context, U32 seconds, U32 useconds) {
38 this->m_timeBase = timeBase;
39 this->m_timeContext = context;
40 this->m_useconds = useconds;
41 this->m_seconds = seconds;
42 }
43
44 Time& Time::operator=(const Time& other) {
45 this->m_timeBase = other.m_timeBase;
46 this->m_timeContext = other.m_timeContext;
47 this->m_useconds = other.m_useconds;
48 this->m_seconds = other.m_seconds;
49
50 return *this;
51 }
52
53 bool Time::operator==(const Time& other) const {
54 return (Time::compare(*this,other) == EQ);
55 }
56
57 bool Time::operator!=(const Time& other) const {
58 return (Time::compare(*this,other) != EQ);
59 }
60
61 bool Time::operator>(const Time& other) const {
62 return (Time::compare(*this,other) == GT);
63 }
64
65 bool Time::operator<(const Time& other) const {
66 return (Time::compare(*this,other) == LT);
67 }
68
69 bool Time::operator>=(const Time& other) const {
70 Time::Comparison c = Time::compare(*this,other);
71 return ((GT == c) or (EQ == c));
72 }
73
74 bool Time::operator<=(const Time& other) const {
75 Time::Comparison c = Time::compare(*this,other);
76 return ((LT == c) or (EQ == c));
77 }
78
80 // serialize members
82#if FW_USE_TIME_BASE
83 stat = buffer.serialize(static_cast<FwTimeBaseStoreType>(this->m_timeBase));
84 if (stat != FW_SERIALIZE_OK) {
85 return stat;
86 }
87#endif
88
89#if FW_USE_TIME_CONTEXT
90 stat = buffer.serialize(this->m_timeContext);
91 if (stat != FW_SERIALIZE_OK) {
92 return stat;
93 }
94#endif
95
96 stat = buffer.serialize(this->m_seconds);
97 if (stat != FW_SERIALIZE_OK) {
98 return stat;
99 }
100
101 return buffer.serialize(this->m_useconds);
102 }
103
105
107#if FW_USE_TIME_BASE
109
110 stat = buffer.deserialize(deSer);
111 if (stat != FW_SERIALIZE_OK) {
112 return stat;
113 }
114
115 this->m_timeBase = static_cast<TimeBase>(deSer);
116#else
117 this->m_timeBase = TB_NONE;
118#endif
119#if FW_USE_TIME_CONTEXT
120 stat = buffer.deserialize(this->m_timeContext);
121 if (stat != FW_SERIALIZE_OK) {
122 return stat;
123 }
124#else
125 this->m_timeContext = 0;
126#endif
127 stat = buffer.deserialize(this->m_seconds);
128 if (stat != FW_SERIALIZE_OK) {
129 return stat;
130 }
131
132 return buffer.deserialize(this->m_useconds);
133 }
134
135 U32 Time::getSeconds() const {
136 return this->m_seconds;
137 }
138
139 U32 Time::getUSeconds() const {
140 return this->m_useconds;
141 }
142
144 return this->m_timeBase;
145 }
146
148 return this->m_timeContext;
149 }
150
151 Time Time ::
152 zero(TimeBase timeBase)
153 {
154 Time time(timeBase,0, 0, 0);
155 return time;
156 }
157
159 compare(
160 const Time &time1,
161 const Time &time2
162 )
163 {
164#if FW_USE_TIME_BASE
165 if (time1.getTimeBase() != time2.getTimeBase()) {
166 return INCOMPARABLE;
167 }
168#endif
169#if FW_USE_TIME_CONTEXT
170 if (time1.getContext() != time2.getContext()) {
171 return INCOMPARABLE;
172 }
173#endif
174 const U32 s1 = time1.getSeconds();
175 const U32 s2 = time2.getSeconds();
176 const U32 us1 = time1.getUSeconds();
177 const U32 us2 = time2.getUSeconds();
178
179 if (s1 < s2) {
180 return LT;
181 } else if (s1 > s2) {
182 return GT;
183 } else if (us1 < us2) {
184 return LT;
185 } else if (us1 > us2) {
186 return GT;
187 } else {
188 return EQ;
189 }
190 }
191
192 Time Time ::
193 add(
194 const Time& a,
195 const Time& b
196 )
197 {
198#if FW_USE_TIME_BASE
200#endif
201#if FW_USE_TIME_CONTEXT
202 FW_ASSERT(a.getContext() == b.getContext(), a.getContext(), b.getContext() );
203#endif
204 U32 seconds = a.getSeconds() + b.getSeconds();
205 U32 uSeconds = a.getUSeconds() + b.getUSeconds();
206 FW_ASSERT(uSeconds < 1999999);
207 if (uSeconds >= 1000000) {
208 ++seconds;
209 uSeconds -= 1000000;
210 }
211 Time c(a.getTimeBase(),a.getContext(),seconds,uSeconds);
212 return c;
213 }
214
215 Time Time ::
216 sub(
217 const Time& minuend,
218 const Time& subtrahend
219 )
220 {
221#if FW_USE_TIME_BASE
222 FW_ASSERT(minuend.getTimeBase() == subtrahend.getTimeBase(), minuend.getTimeBase(), subtrahend.getTimeBase());
223#endif
224#if FW_USE_TIME_CONTEXT
225 FW_ASSERT(minuend.getContext() == subtrahend.getContext(), minuend.getContext(), subtrahend.getContext());
226#endif
227 // Assert minuend is greater than subtrahend
228 FW_ASSERT(minuend >= subtrahend);
229
230 U32 seconds = minuend.getSeconds() - subtrahend.getSeconds();
231 U32 uSeconds;
232 if (subtrahend.getUSeconds() > minuend.getUSeconds()) {
233 seconds--;
234 uSeconds = minuend.getUSeconds() + 1000000 - subtrahend.getUSeconds();
235 } else {
236 uSeconds = minuend.getUSeconds() - subtrahend.getUSeconds();
237 }
238 return Time(minuend.getTimeBase(), minuend.getContext(), seconds, static_cast<U32>(uSeconds));
239 }
240
241 void Time::add(U32 seconds, U32 useconds) {
242 this->m_seconds += seconds;
243 this->m_useconds += useconds;
244 FW_ASSERT(this->m_useconds < 1999999,this->m_useconds);
245 if (this->m_useconds >= 1000000) {
246 ++this->m_seconds;
247 this->m_useconds -= 1000000;
248 }
249 }
250
252 this->m_timeBase = timeBase;
253 }
254
256 this->m_timeContext = context;
257 }
258
259#ifdef BUILD_UT
260 std::ostream& operator<<(std::ostream& os, const Time& val) {
261
262 os << "(" << val.getTimeBase() << "," << val.getUSeconds() << "," << val.getSeconds() << ")";
263 return os;
264 }
265#endif
266
267}
#define FW_ASSERT(...)
Definition Assert.hpp:14
TimeBase
Definition FpConfig.h:38
@ TB_NONE
No time base has been established.
Definition FpConfig.h:39
U16 FwTimeBaseStoreType
Definition FpConfig.h:47
U8 FwTimeContextStoreType
Definition FpConfig.h:50
C++-compatible configuration header for fprime configuration.
forward declaration
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
bool operator<(const Time &other) const
Definition Time.cpp:65
static Time add(const Time &a, const Time &b)
Definition Time.cpp:193
SerializeStatus deserialize(SerializeBufferBase &buffer)
deserialize to contents
Definition Time.cpp:104
FwTimeContextStoreType getContext() const
Definition Time.cpp:147
U32 getUSeconds() const
Definition Time.cpp:139
void set(U32 seconds, U32 useconds)
Definition Time.cpp:25
Time & operator=(const Time &other)
Definition Time.cpp:44
virtual ~Time()
Definition Time.cpp:10
bool operator>=(const Time &other) const
Definition Time.cpp:69
bool operator!=(const Time &other) const
Definition Time.cpp:57
void setTimeContext(FwTimeContextStoreType context)
Definition Time.cpp:255
TimeBase getTimeBase() const
Definition Time.cpp:143
Comparison
The type of a comparison result.
Definition Time.hpp:45
bool operator<=(const Time &other) const
Definition Time.cpp:74
bool operator>(const Time &other) const
Definition Time.cpp:61
void setTimeBase(TimeBase timeBase)
Definition Time.cpp:251
U32 getSeconds() const
Definition Time.cpp:135
Time()
Definition Time.cpp:7
bool operator==(const Time &other) const
Definition Time.cpp:53
SerializeStatus serialize(SerializeBufferBase &buffer) const
serialize contents
Definition Time.cpp:79
static Comparison compare(const Time &time1, const Time &time2)
Definition Time.cpp:159
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
const Time ZERO_TIME
Definition Time.cpp:5