F´ Flight Software - C/C++ Documentation  NASA-v1.6.0
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 <Fw/Types/BasicTypes.hpp>
3 
4 namespace 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
108  FwTimeBaseStoreType deSer;
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 
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 ::
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 ::
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 
251  void Time::setTimeBase(TimeBase timeBase) {
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 }
FwTimeContextStoreType
#define FwTimeContextStoreType
Storage conversion for time context in scripts/ground interface.
Definition: FpConfig.hpp:328
Fw::Time::EQ
@ EQ
Definition: Time.hpp:48
Fw::Time::INCOMPARABLE
@ INCOMPARABLE
Definition: Time.hpp:50
Fw::Time
Definition: Time.hpp:10
Fw::Time::getSeconds
U32 getSeconds() const
Definition: Time.cpp:135
Fw::SerializeBufferBase
Definition: Serializable.hpp:43
Fw::SerializeStatus
SerializeStatus
forward declaration for string
Definition: Serializable.hpp:14
FwTimeBaseStoreType
#define FwTimeBaseStoreType
Storage conversion for time base in scripts/ground interface.
Definition: FpConfig.hpp:324
Fw::SerializeBufferBase::serialize
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
Definition: Serializable.cpp:69
U8
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.hpp:73
Fw::Time::getUSeconds
U32 getUSeconds() const
Definition: Time.cpp:139
Fw::Time::operator!=
bool operator!=(const Time &other) const
Definition: Time.cpp:57
TimeBase
TimeBase
Definition: FpConfig.hpp:315
Fw::Time::deserialize
SerializeStatus deserialize(SerializeBufferBase &buffer)
deserialize to contents
Definition: Time.cpp:104
Fw::Serializable
forward declaration
Definition: Serializable.hpp:26
Fw::Time::zero
static Time zero(TimeBase timeBase=TB_NONE)
Definition: Time.cpp:152
Fw::Time::operator<
bool operator<(const Time &other) const
Definition: Time.cpp:65
Fw::Time::getContext
FwTimeContextStoreType getContext() const
Definition: Time.cpp:147
Fw::Time::set
void set(U32 seconds, U32 useconds)
Definition: Time.cpp:25
Fw::Time::sub
static Time sub(const Time &minuend, const Time &subtrahend)
Definition: Time.cpp:216
Fw::Time::operator>=
bool operator>=(const Time &other) const
Definition: Time.cpp:69
Fw::Time::Comparison
Comparison
The type of a comparison result.
Definition: Time.hpp:46
Fw::Time::~Time
virtual ~Time()
Definition: Time.cpp:10
Fw::Time::LT
@ LT
Definition: Time.hpp:47
Fw::Time::GT
@ GT
Definition: Time.hpp:49
Fw::Time::operator=
Time & operator=(const Time &other)
Definition: Time.cpp:44
Fw::Time::setTimeBase
void setTimeBase(TimeBase timeBase)
Definition: Time.cpp:251
Fw::Time::Time
Time()
Definition: Time.cpp:7
Fw::Time::compare
static Comparison compare(const Time &time1, const Time &time2)
Definition: Time.cpp:159
Fw::Time::operator<=
bool operator<=(const Time &other) const
Definition: Time.cpp:74
FW_ASSERT
#define FW_ASSERT(...)
Definition: Assert.hpp:9
Fw::SerializeBufferBase::deserialize
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
Definition: Serializable.cpp:294
Fw::Time::getTimeBase
TimeBase getTimeBase() const
Definition: Time.cpp:143
Fw::ZERO_TIME
const Time ZERO_TIME
Definition: Time.cpp:5
Fw::Time::serialize
SerializeStatus serialize(SerializeBufferBase &buffer) const
serialize contents
Definition: Time.cpp:79
TB_NONE
@ TB_NONE
No time base has been established.
Definition: FpConfig.hpp:316
Fw::Time::operator==
bool operator==(const Time &other) const
Definition: Time.cpp:53
Fw::Time::add
static Time add(const Time &a, const Time &b)
Definition: Time.cpp:193
Fw::Time::setTimeContext
void setTimeContext(FwTimeContextStoreType context)
Definition: Time.cpp:255
Time.hpp
Fw::Time::operator>
bool operator>(const Time &other) const
Definition: Time.cpp:61
Fw::FW_SERIALIZE_OK
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
Definition: Serializable.hpp:15
Fw
Definition: SerIds.hpp:20