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