TimeUtil.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
10 *
11 * ArmarX is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @package ArmarXCore::core
20 * @author Clemens Wallrath (uagzs at student dot kit dot edu)
21 * @date 2015
22 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
23 * GNU General Public License
24 */
25 
26 #include "TimeUtil.h"
27 
28 #include "CallbackWaitLock.h"
29 #include "LocalTimeServer.h"
30 #include <time.h>
31 
32 
33 #define USECS_PER_SEC 1000000
34 #define NANOSECS_PER_SEC 1000000000
35 
36 
37 
38 namespace armarx
39 {
41 
43  {
44  if (timeMode != TimeMode::SystemTime and timeServerPtr)
45  {
46  return IceUtil::Time::milliSeconds(timeServerPtr->getTime());
47  }
48  else
49  {
50  return IceUtil::Time::now();
51  }
52  }
53 
54  IceUtil::Time TimeUtil::GetTime(bool forceSystemTime)
55  {
56  if (!forceSystemTime && timeServerPtr)
57  {
59  }
60  else
61  {
63  }
64  }
65 
67  {
68  return TimeUtil::GetTime(timeMode) - referenceTime;
69  }
70 
71  IceUtil::Time TimeUtil::GetTimeSince(IceUtil::Time referenceTime, bool forceSystemTime)
72  {
73  return TimeUtil::GetTime(forceSystemTime) - referenceTime;
74  }
75 
77  {
78  if (duration.toMicroSeconds() <= 0)
79  {
80  return;
81  }
82  if (timeServerPtr)
83  {
84  CallbackWaitLock waitLock;
85  timeServerPtr->registerTimer(GetTime() + duration, static_cast<CallbackReceiver*>(&waitLock));
86  waitLock.wait();
87  }
88  else
89  {
90  USleep(duration.toMicroSeconds());
91  }
92  }
93 
94  void TimeUtil::MSSleep(int durationMS)
95  {
96  Sleep(IceUtil::Time::milliSeconds(durationMS));
97  }
98 
100  {
101  timeServerPtr = ts;
102  }
103 
105  {
106  MSSleep(0); // even duration=0 will block until next tick, if timeserver active
107  }
108 
110  {
111  return timeServerPtr;
112  }
113 
115  {
116  return timeServerPtr._ptr != nullptr;
117  }
118 
119  // bool TimeUtil::TimedWait(boost::condition_variable& cond, boost::unique_lock<boost::mutex>& lock, IceUtil::Time duration, IceUtil::Time granularity)
120  // {
121  // if (timeServerPtr)
122  // {
123  // auto endTime = GetTime() + duration;
124  // boost::posix_time::time_duration td;
125  // if (granularity.toMicroSeconds() == 0)
126  // {
127  // td = boost::posix_time::microseconds((duration / 10).toMicroSeconds()); // assumption: granularity of a tenth of the total duration is ok-ish
128  // }
129  // else
130  // {
131  // td = boost::posix_time::microseconds(granularity.toMicroSeconds());
132  // }
133  // while (!cond.timed_wait(lock, td))
134  // {
135  // if (GetTime() > endTime)
136  // {
137  // return false;
138  // }
139  // }
140  // return true;
141  // }
142  // else
143  // {
144  // return cond.timed_wait(lock, boost::posix_time::microseconds(duration.toMicroSeconds()));
145  // }
146  // }
147 
148  int TimeUtil::USleep(long usec)
149  {
150  return NanoSleep(usec * 1000);
151  }
152 
153  int TimeUtil::NanoSleep(long nanosec)
154  {
155  struct timespec ts;
156  ts.tv_sec = nanosec / NANOSECS_PER_SEC;
157  ts.tv_nsec = (nanosec % NANOSECS_PER_SEC);
158  return nanosleep(&ts, nullptr);
159  }
160 
161 
162  std::string TimeUtil::toStringDate(const IceUtil::Time& time)
163  {
164  return time.toString("%Y-%m-%d");
165  }
166 
167  std::string TimeUtil::toStringTime(const IceUtil::Time& time)
168  {
169  return time.toString("%H-%M-%S");
170  }
171 
172  std::string TimeUtil::toStringDateTime(const IceUtil::Time& time)
173  {
174  return time.toString("%Y-%m-%d_%H-%M-%S");
175  }
176 
177 }
armarx::TimeUtil::toStringTime
static std::string toStringTime(const IceUtil::Time &time)
Return a time string like "15-30-05" (H-M-S).
Definition: TimeUtil.cpp:167
armarx::TimeUtil::toStringDate
static std::string toStringDate(const IceUtil::Time &time)
Return a date string like "2020-01-31" (Y-M-D).
Definition: TimeUtil.cpp:162
NANOSECS_PER_SEC
#define NANOSECS_PER_SEC
Definition: TimeUtil.cpp:34
armarx::TimeUtil::toStringDateTime
static std::string toStringDateTime(const IceUtil::Time &time)
Return a date & time string like "2020-01-31_15-30-05" (Y-M-D_H-M-S).
Definition: TimeUtil.cpp:172
armarx::TimeUtil::MSSleep
static void MSSleep(int durationMS)
lock the calling thread for a given duration (like usleep(...) but using Timeserver time)
Definition: TimeUtil.cpp:94
CallbackWaitLock.h
armarx::TimeUtil::NanoSleep
static int NanoSleep(long usec)
Nanosleep convenience function.
Definition: TimeUtil.cpp:153
armarx::CallbackWaitLock
CallbackWaitLock is used to block a thread and unblock it from another thread via callback.
Definition: CallbackWaitLock.h:42
armarx::TimeMode::SystemTime
@ SystemTime
armarx::TimeUtil::GetTimeServer
static LocalTimeServerPtr GetTimeServer()
Definition: TimeUtil.cpp:109
armarx::LocalTimeServerPtr
IceInternal::Handle< LocalTimeServer > LocalTimeServerPtr
Definition: LocalTimeServer.h:58
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::CallbackWaitLock::wait
void wait()
wait blocks the calling thread until CallbackWaitLock::call() is called.
Definition: CallbackWaitLock.cpp:31
armarx::TimeUtil::timeServerPtr
static LocalTimeServerPtr timeServerPtr
pointer to the applications LocalTimeServer if NULL, system time is used
Definition: TimeUtil.h:267
LocalTimeServer.h
armarx::TimeUtil::USleep
static int USleep(long usec)
like timed_wait on boost condition_variables, but with timeserver support
Definition: TimeUtil.cpp:148
armarx::TimeUtil::HasTimeServer
static bool HasTimeServer()
check if we have been initialized with a Timeserver
Definition: TimeUtil.cpp:114
armarx::CallbackReceiver
Used by CallbackWaitLock.
Definition: LocalTimeServer.h:42
armarx::TimeUtil::Sleep
static void Sleep(IceUtil::Time duration)
lock the calling thread for a given duration (like usleep(...) but using Timeserver time)
Definition: TimeUtil.cpp:76
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::TimeUtil::WaitForNextTick
static void WaitForNextTick()
block until the next tick of the timeserver.
Definition: TimeUtil.cpp:104
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
armarx::TimeMode
TimeMode
Time mode to be used.
Definition: TimeUtil.h:119
armarx::TimeUtil::GetTimeSince
static IceUtil::Time GetTimeSince(IceUtil::Time referenceTime, TimeMode timeMode=TimeMode::VirtualTime)
Get the difference between the current time and a reference time.
Definition: TimeUtil.cpp:66
armarx::TimeUtil::SetTimeServer
static void SetTimeServer(LocalTimeServerPtr ts)
Definition: TimeUtil.cpp:99
TimeUtil.h
armarx::TimeMode::VirtualTime
@ VirtualTime
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28