F´ Flight Software - C/C++ Documentation  NASA-v2.0.1
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(TB_NONE,0,seconds,useconds);
26  }
27 
28  void Time::set(TimeBase timeBase, U32 seconds, U32 useconds) {
29  this->set(timeBase,0,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  FW_ASSERT(time1.getTimeBase() == time2.getTimeBase(), time1.getTimeBase(), time2.getTimeBase() );
165 #endif
166 #if FW_USE_TIME_CONTEXT
167  FW_ASSERT(time1.getContext() == time2.getContext(), time1.getContext(), time2.getContext() );
168 #endif
169  const U32 s1 = time1.getSeconds();
170  const U32 s2 = time2.getSeconds();
171  const U32 us1 = time1.getUSeconds();
172  const U32 us2 = time2.getUSeconds();
173 
174  if (s1 < s2) {
175  return LT;
176  } else if (s1 > s2) {
177  return GT;
178  } else if (us1 < us2) {
179  return LT;
180  } else if (us1 > us2) {
181  return GT;
182  } else {
183  return EQ;
184  }
185  }
186 
187  Time Time ::
189  Time& a,
190  Time& b
191  )
192  {
193 #if FW_USE_TIME_BASE
195 #endif
196 #if FW_USE_TIME_CONTEXT
197  FW_ASSERT(a.getContext() == b.getContext(), a.getContext(), b.getContext() );
198 #endif
199  U32 seconds = a.getSeconds() + b.getSeconds();
200  U32 uSeconds = a.getUSeconds() + b.getUSeconds();
201  FW_ASSERT(uSeconds < 1999999);
202  if (uSeconds >= 1000000) {
203  ++seconds;
204  uSeconds -= 1000000;
205  }
206  Time c(a.getTimeBase(),a.getContext(),seconds,uSeconds);
207  return c;
208  }
209 
210  Time Time ::
212  Time& minuend,
213  Time& subtrahend
214  )
215  {
216 #if FW_USE_TIME_BASE
217  FW_ASSERT(minuend.getTimeBase() == subtrahend.getTimeBase(), minuend.getTimeBase(), subtrahend.getTimeBase());
218 #endif
219 #if FW_USE_TIME_CONTEXT
220  FW_ASSERT(minuend.getContext() == subtrahend.getContext(), minuend.getContext(), subtrahend.getContext());
221 #endif
222  // Assert minuend is greater than subtrahend
223  FW_ASSERT(minuend >= subtrahend);
224 
225  U32 seconds = minuend.getSeconds() - subtrahend.getSeconds();
226  U32 uSeconds;
227  if (subtrahend.getUSeconds() > minuend.getUSeconds()) {
228  seconds--;
229  uSeconds = minuend.getUSeconds() + 1000000 - subtrahend.getUSeconds();
230  } else {
231  uSeconds = minuend.getUSeconds() - subtrahend.getUSeconds();
232  }
233  return Time(minuend.getTimeBase(), minuend.getContext(), seconds, static_cast<U32>(uSeconds));
234  }
235 
236  void Time::add(U32 seconds, U32 useconds) {
237  this->m_seconds += seconds;
238  this->m_useconds += useconds;
239  FW_ASSERT(this->m_useconds < 1999999,this->m_useconds);
240  if (this->m_useconds >= 1000000) {
241  ++this->m_seconds;
242  this->m_useconds -= 1000000;
243  }
244  }
245 
246  void Time::setTimeBase(TimeBase timeBase) {
247  this->m_timeBase = timeBase;
248  }
249 
251  this->m_timeContext = context;
252  }
253 
254 #ifdef BUILD_UT
255  std::ostream& operator<<(std::ostream& os, const Time& val) {
256 
257  os << "(" << val.getTimeBase() << "," << val.getUSeconds() << "," << val.getSeconds() << ")";
258  return os;
259  }
260 #endif
261 
262 }
Fw::Time::EQ
@ EQ
Definition: Time.hpp:48
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::sub
static Time sub(Time &minuend, Time &subtrahend)
Definition: Time.cpp:211
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:323
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:336
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::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:246
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:332
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
BasicTypes.hpp
Declares ISF basic types.
Fw::Time::setTimeContext
void setTimeContext(FwTimeContextStoreType context)
Definition: Time.cpp:250
Fw::Time::add
static Time add(Time &a, Time &b)
Definition: Time.cpp:188
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:324
Fw
Definition: Buffer.cpp:21