TimeBase.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 #include "TimeBase.h"
24 
25 #include <limits>
26 
27 #include "os_lxrt.h"
28 #include "os_time.h"
29 
30 #ifdef _SYSTEM_LXRT_33_
31 # include <rtai_lxrt.h>
32 # include <rtai_posix.h>
33 #endif
34 
35 namespace icl_core {
36 
38 {
39  secs += span.secs;
40  nsecs += span.nsecs;
41  normalizeTime();
42  return *this;
43 }
44 
46 {
47  secs -= span.secs;
48  nsecs -= span.nsecs;
49  normalizeTime();
50  return *this;
51 }
52 
53 bool TimeBase::operator != (const TimeBase& other) const
54 {
55  return (secs != other.secs) || (nsecs != other.nsecs);
56 }
57 
58 bool TimeBase::operator == (const TimeBase& other) const
59 {
60  return (secs == other.secs) && (nsecs == other.nsecs);
61 }
62 
63 bool TimeBase::operator < (const TimeBase& other) const
64 {
65  return (secs == other.secs) ? (nsecs < other.nsecs) : (secs < other.secs);
66 }
67 
68 bool TimeBase::operator > (const TimeBase& other) const
69 {
70  return (secs == other.secs) ? (nsecs > other.nsecs) : (secs > other.secs);
71 }
72 
73 bool TimeBase::operator <= (const TimeBase& other) const
74 {
75  return (secs == other.secs) ? (nsecs <= other.nsecs) : (secs < other.secs);
76 }
77 
78 bool TimeBase::operator >= (const TimeBase& other) const
79 {
80  return (secs == other.secs) ? (nsecs >= other.nsecs) : (secs > other.secs);
81 }
82 
84 {
85  while (((secs >= 0) && (nsecs >= 1000000000)) ||
86  ((secs <= 0) && (nsecs <= -1000000000)) ||
87  ((secs > 0) && (nsecs < 0)) ||
88  ((secs < 0) && (nsecs > 0)))
89  {
90  if ((secs >= 0) && (nsecs >= 1000000000))
91  {
92  nsecs -= 1000000000;
93  ++secs;
94  }
95  else if ((secs <= 0) && (nsecs <= -1000000000))
96  {
97  nsecs += 1000000000;
98  --secs;
99  }
100  else if ((secs > 0) && (nsecs < 0))
101  {
102  nsecs += 1000000000;
103  --secs;
104  }
105  else if ((secs < 0) && (nsecs > 0))
106  {
107  nsecs -= 1000000000;
108  ++secs;
109  }
110  }
111 }
112 
113 struct timespec TimeBase::timespec() const
114 {
115  struct timespec time;
116  time.tv_sec = time_t(secs);
117  time.tv_nsec = long(nsecs);
118  return time;
119 }
120 
122 {
123 #ifdef _SYSTEM_LXRT_33_
124  if (os::isThisLxrtTask())
125  {
126  struct timespec time = timespec();
127  struct timespec global_now;
128  os::gettimeofday(&global_now);
129 
130  RTIME rtime_rtai_now = rt_get_time();
131  RTIME rtime_global_now = timespec2count(&global_now);
132  RTIME rtime_time = timespec2count(&time);
133 
134  count2timespec(rtime_time + rtime_rtai_now - rtime_global_now, &time);
135 
136  return time;
137  }
138  else
139  {
140 #endif
141  return timespec();
142 #ifdef _SYSTEM_LXRT_33_
143  }
144 #endif
145 }
146 
147 #ifdef _SYSTEM_DARWIN_
148 mach_timespec_t TimeBase::machTimespec() const
149 {
150  mach_timespec_t time;
151  time.tv_sec = static_cast<unsigned int>(secs);
152  time.tv_nsec = static_cast<clock_res_t>(nsecs);
153  return time;
154 }
155 #endif
156 
158 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
159 
160  void TimeBase::NormalizeTime()
161  {
162  normalizeTime();
163  }
164 
165  int64_t TimeBase::Days() const
166  {
167  return days();
168  }
169 
170  int64_t TimeBase::Hours() const
171  {
172  return hours();
173  }
174 
175  int64_t TimeBase::Minutes() const
176  {
177  return minutes();
178  }
179 
180  int64_t TimeBase::Seconds() const
181  {
182  return seconds();
183  }
184 
185  int32_t TimeBase::MilliSeconds() const
186  {
187  return milliSeconds();
188  }
189 
190  int32_t TimeBase::MicroSeconds() const
191  {
192  return microSeconds();
193  }
194 
195  int32_t TimeBase::NanoSeconds() const
196  {
197  return nanoSeconds();
198  }
199 
200  int64_t TimeBase::TbSec() const
201  {
202  return tbSec();
203  }
204 
205  int32_t TimeBase::TbNSec() const
206  {
207  return tbNSec();
208  }
209 
210  struct timespec TimeBase::Timespec() const
211  {
212  return timespec();
213  }
214 
215  struct timespec TimeBase::SystemTimespec() const
216  {
217  return systemTimespec();
218  }
219 
220 #ifdef _SYSTEM_DARWIN_
221 
224  mach_timespec_t TimeBase::MachTimespec() const
225  {
226  return machTimespec();
227  }
228 #endif
229 
230 #endif
231 
233 {
234 // TODO: Fix this in a better way!
235 #undef max
236  return TimeBase(std::numeric_limits<int64_t>::max(), 999999999);
237 }
238 
240  : secs(secs),
241  nsecs(nsecs)
242 {
243  normalizeTime();
244 }
245 
246 TimeBase::TimeBase(const struct timespec& time)
247  : secs(time.tv_sec),
248  nsecs(time.tv_nsec)
249 {
250  normalizeTime();
251 }
252 
253 void TimeBase::fromTimespec(const struct timespec& time)
254 {
255  secs = time.tv_sec;
256  nsecs = time.tv_nsec;
257  normalizeTime();
258 }
259 
261 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
262 
263  TimeBase TimeBase::MaxTime()
264  {
265  return maxTime();
266  }
267 
268  void TimeBase::FromTimespec(const struct timespec& time)
269  {
270  fromTimespec(time);
271  }
272 
273 #endif
274 
276 }
bool operator>(const TimeBase &other) const
Definition: TimeBase.cpp:68
signed int int32_t
Definition: msvc_stdint.h:90
int32_t nanoSeconds() const
Definition: TimeBase.h:155
int32_t milliSeconds() const
Definition: TimeBase.h:139
int64_t minutes() const
Definition: TimeBase.h:123
int64_t tbSec() const
Returns the second part of this time.
Definition: TimeBase.h:161
int32_t tbNSec() const
Returns the nanosecond part of this time.
Definition: TimeBase.h:163
int64_t days() const
Use this function if you want to express the time in days.
Definition: TimeBase.h:107
void normalizeTime()
Definition: TimeBase.cpp:83
TimeBase(int64_t secs=0, int32_t nsecs=0)
Definition: TimeBase.cpp:239
static TimeBase maxTime()
Definition: TimeBase.cpp:232
TimeBase & operator-=(const TimeBase &span)
Substracts a TimeSpan.
Definition: TimeBase.cpp:45
Contains TimeBase.
time_t tv_sec
Seconds.
Definition: os_win32_time.h:36
bool operator!=(const TimeBase &other) const
Definition: TimeBase.cpp:53
signed __int64 int64_t
Definition: msvc_stdint.h:102
bool operator<(const TimeBase &other) const
Definition: TimeBase.cpp:63
long tv_nsec
Nanoseconds.
Definition: os_win32_time.h:38
bool operator>=(const TimeBase &other) const
Definition: TimeBase.cpp:78
struct timespec timespec() const
Definition: TimeBase.cpp:113
Repesents time values.
Definition: TimeBase.h:59
TimeBase & operator+=(const TimeBase &span)
Adds a TimeSpan.
Definition: TimeBase.cpp:37
Contains global LXRT functions.
int32_t microSeconds() const
Definition: TimeBase.h:147
bool isThisLxrtTask()
Definition: os_lxrt.cpp:150
Contains global functions for time manipulation, encapsulated into the icl_core::os namespace...
int64_t hours() const
Definition: TimeBase.h:115
bool operator==(const TimeBase &other) const
Definition: TimeBase.cpp:58
void gettimeofday(struct timespec *time)
Definition: os_time.h:41
struct timespec systemTimespec() const
Definition: TimeBase.cpp:121
bool operator<=(const TimeBase &other) const
Definition: TimeBase.cpp:73
void fromTimespec(const struct timespec &time)
Definition: TimeBase.cpp:253
int64_t seconds() const
Definition: TimeBase.h:131


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58