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 <time.h>
29
30#include "CallbackWaitLock.h"
31#include "LocalTimeServer.h"
32
33
34#define USECS_PER_SEC 1000000
35#define NANOSECS_PER_SEC 1000000000
36
37namespace armarx
38{
40
41 IceUtil::Time
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
55 TimeUtil::GetTime(bool forceSystemTime)
56 {
57 if (!forceSystemTime && timeServerPtr)
58 {
60 }
61 else
62 {
64 }
65 }
66
67 IceUtil::Time
68 TimeUtil::GetTimeSince(IceUtil::Time referenceTime, TimeMode timeMode)
69 {
70 return TimeUtil::GetTime(timeMode) - referenceTime;
71 }
72
73 IceUtil::Time
74 TimeUtil::GetTimeSince(IceUtil::Time referenceTime, bool forceSystemTime)
75 {
76 return TimeUtil::GetTime(forceSystemTime) - referenceTime;
77 }
78
79 void
80 TimeUtil::Sleep(IceUtil::Time duration)
81 {
82 if (duration.toMicroSeconds() <= 0)
83 {
84 return;
85 }
86 if (timeServerPtr)
87 {
88 CallbackWaitLock waitLock;
89 timeServerPtr->registerTimer(GetTime() + duration,
90 static_cast<CallbackReceiver*>(&waitLock));
91 waitLock.wait();
92 }
93 else
94 {
95 USleep(duration.toMicroSeconds());
96 }
97 }
98
99 void
100 TimeUtil::MSSleep(int durationMS)
101 {
102 Sleep(IceUtil::Time::milliSeconds(durationMS));
103 }
104
105 void
110
111 void
113 {
114 MSSleep(0); // even duration=0 will block until next tick, if timeserver active
115 }
116
119 {
120 return timeServerPtr;
121 }
122
123 bool
125 {
126 return timeServerPtr._ptr != nullptr;
127 }
128
129 // bool TimeUtil::TimedWait(boost::condition_variable& cond, boost::unique_lock<boost::mutex>& lock, IceUtil::Time duration, IceUtil::Time granularity)
130 // {
131 // if (timeServerPtr)
132 // {
133 // auto endTime = GetTime() + duration;
134 // boost::posix_time::time_duration td;
135 // if (granularity.toMicroSeconds() == 0)
136 // {
137 // td = boost::posix_time::microseconds((duration / 10).toMicroSeconds()); // assumption: granularity of a tenth of the total duration is ok-ish
138 // }
139 // else
140 // {
141 // td = boost::posix_time::microseconds(granularity.toMicroSeconds());
142 // }
143 // while (!cond.timed_wait(lock, td))
144 // {
145 // if (GetTime() > endTime)
146 // {
147 // return false;
148 // }
149 // }
150 // return true;
151 // }
152 // else
153 // {
154 // return cond.timed_wait(lock, boost::posix_time::microseconds(duration.toMicroSeconds()));
155 // }
156 // }
157
158 int
160 {
161 return NanoSleep(usec * 1000);
162 }
163
164 int
166 {
167 struct timespec ts;
168 ts.tv_sec = nanosec / NANOSECS_PER_SEC;
169 ts.tv_nsec = (nanosec % NANOSECS_PER_SEC);
170 return nanosleep(&ts, nullptr);
171 }
172
173 std::string
174 TimeUtil::toStringDate(const IceUtil::Time& time)
175 {
176 return time.toString("%Y-%m-%d");
177 }
178
179 std::string
180 TimeUtil::toStringTime(const IceUtil::Time& time)
181 {
182 return time.toString("%H-%M-%S");
183 }
184
185 std::string
186 TimeUtil::toStringDateTime(const IceUtil::Time& time)
187 {
188 return time.toString("%Y-%m-%d_%H-%M-%S");
189 }
190
191} // namespace armarx
#define NANOSECS_PER_SEC
Definition TimeUtil.cpp:35
Used by CallbackWaitLock.
CallbackWaitLock is used to block a thread and unblock it from another thread via callback.
void wait()
wait blocks the calling thread until CallbackWaitLock::call() is called.
static LocalTimeServerPtr GetTimeServer()
Definition TimeUtil.cpp:118
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition TimeUtil.cpp:42
static void SetTimeServer(LocalTimeServerPtr ts)
Definition TimeUtil.cpp:106
static std::string toStringDate(const IceUtil::Time &time)
Return a date string like "2020-01-31" (Y-M-D).
Definition TimeUtil.cpp:174
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:68
static std::string toStringTime(const IceUtil::Time &time)
Return a time string like "15-30-05" (H-M-S).
Definition TimeUtil.cpp:180
static void WaitForNextTick()
block until the next tick of the timeserver.
Definition TimeUtil.cpp:112
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:186
static void MSSleep(int durationMS)
lock the calling thread for a given duration (like usleep(...) but using Timeserver time)
Definition TimeUtil.cpp:100
static int NanoSleep(long usec)
Nanosleep convenience function.
Definition TimeUtil.cpp:165
static LocalTimeServerPtr timeServerPtr
pointer to the applications LocalTimeServer if NULL, system time is used
Definition TimeUtil.h:276
static int USleep(long usec)
like timed_wait on boost condition_variables, but with timeserver support
Definition TimeUtil.cpp:159
static void Sleep(IceUtil::Time duration)
lock the calling thread for a given duration (like usleep(...) but using Timeserver time)
Definition TimeUtil.cpp:80
static bool HasTimeServer()
check if we have been initialized with a Timeserver
Definition TimeUtil.cpp:124
TimeMode
Time mode to be used.
Definition TimeUtil.h:124
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< LocalTimeServer > LocalTimeServerPtr