59 #define HAS_CLOCK_GETTIME (_POSIX_C_SOURCE >= 199309L) 75 const Time TIME_MAX(std::numeric_limits<uint32_t>::max(), 999999999);
103 #if HAS_CLOCK_GETTIME 105 clock_gettime(CLOCK_REALTIME, &start);
106 sec =
static_cast<uint32_t>(start.tv_sec);
107 nsec =
static_cast<uint32_t>(start.tv_nsec);
109 struct timeval timeofday;
110 gettimeofday(&timeofday,
NULL);
111 sec = timeofday.tv_sec;
112 nsec = timeofday.tv_usec * 1000;
125 static LARGE_INTEGER cpu_freq, init_cpu_time;
128 if ( ( start_sec == 0 ) && ( start_nsec == 0 ) )
130 QueryPerformanceFrequency(&cpu_freq);
131 if (cpu_freq.QuadPart == 0) {
134 QueryPerformanceCounter(&init_cpu_time);
137 GetSystemTimeAsFileTime(&ft);
138 LARGE_INTEGER start_li;
139 start_li.LowPart = ft.dwLowDateTime;
140 start_li.HighPart = ft.dwHighDateTime;
144 start_li.QuadPart -= 116444736000000000Ui64;
146 start_li.QuadPart -= 116444736000000000ULL;
148 start_sec = (
uint32_t)(start_li.QuadPart / 10000000);
149 start_nsec = (start_li.LowPart % 10000000) * 100;
151 LARGE_INTEGER cur_time;
152 QueryPerformanceCounter(&cur_time);
153 LARGE_INTEGER delta_cpu_time;
154 delta_cpu_time.QuadPart = cur_time.QuadPart - init_cpu_time.QuadPart;
157 double d_delta_cpu_time = delta_cpu_time.QuadPart / (double) cpu_freq.QuadPart;
178 LARGE_INTEGER sleepTime;
179 sleepTime.QuadPart = -
180 static_cast<int64_t>(sec)*10000000LL -
181 static_cast<int64_t>(nsec) / 100LL;
189 if (!SetWaitableTimer (timer, &sleepTime, 0,
NULL,
NULL, 0))
194 if (WaitForSingleObject (timer, INFINITE) != WAIT_OBJECT_0)
200 timespec req = {
static_cast<time_t
>(sec), static_cast<long>(nsec) };
201 return nanosleep(&req,
NULL);
215 timespec req = {
static_cast<time_t
>(sec), static_cast<long>(nsec) };
216 timespec rem = {0, 0};
217 while (nanosleep(&req, &rem) && !
g_stopped)
253 std::lock_guard<std::mutex>
lock(g_sim_time_mutex);
266 std::lock_guard<std::mutex>
lock(g_sim_time_mutex);
297 while (!
isValid() && !g_stopped)
336 os << rhs.
sec <<
"." << std::setw(9) << std::setfill(
'0') << rhs.
nsec;
343 if (rhs.
sec >= 0 || rhs.
nsec == 0)
345 os << rhs.
sec <<
"." << std::setw(9) << std::setfill(
'0') << rhs.
nsec;
349 os << (rhs.
sec == -1 ?
"-" :
"") << (rhs.
sec + 1) <<
"." << std::setw(9) << std::setfill(
'0') << (1000000000 - rhs.
nsec);
369 while (!g_stopped && (
Time::now() < end))
409 while (!g_stopped && (
Time::now() < end))
436 os << rhs.
sec <<
"." << std::setw(9) << std::setfill(
'0') << rhs.
nsec;
451 if (rhs.
sec >= 0 || rhs.
nsec == 0)
453 os << rhs.
sec <<
"." << std::setw(9) << std::setfill(
'0') << rhs.
nsec;
457 os << (rhs.
sec == -1 ?
"-" :
"") << (rhs.
sec + 1) <<
"." << std::setw(9) << std::setfill(
'0') << (1000000000 - rhs.
nsec);
469 uint64_t nsec_part = nsec % 1000000000UL;
470 uint64_t sec_part = nsec / 1000000000UL;
472 if (sec + sec_part > UINT_MAX)
473 throw std::runtime_error(
"Time is out of dual 32-bit range");
492 int64_t nsec_part = nsec % 1000000000L;
493 int64_t sec_part = sec + nsec / 1000000000L;
496 nsec_part += 1000000000L;
500 if (sec_part < 0 || sec_part > UINT_MAX)
501 throw std::runtime_error(
"Time is out of dual 32-bit range");
static const textual_icon lock
static bool g_initialized(false)
ROSTIME_DECL void normalizeSecNSec(uint64_t &sec, uint64_t &nsec)
bool sleep() const
sleep for the amount of time specified by this Duration. If a signal interrupts the sleep...
static bool waitForValid()
Wait for time to become valid.
static void setNow(const Time &new_now)
static bool isSystemTime()
ROSTIME_DECL const Time TIME_MAX
Thrown if the ros subsystem hasn't been initialised before use.
static bool g_stopped(false)
void ros_walltime(uint32_t &sec, uint32_t &nsec)
static bool useSystemTime()
static WallTime now()
Returns the current wall clock time.
ROSTIME_DECL const Duration DURATION_MIN
static bool g_use_sim_time(true)
::std_msgs::Duration_< std::allocator< void > > Duration
Duration representation for use with the Time class.
Duration representation for use with the WallTime class.
ROSTIME_DECL void normalizeSecNSecUnsigned(int64_t &sec, int64_t &nsec)
bool sleep() const
sleep for the amount of time specified by this Duration. If a signal interrupts the sleep...
Time representation. Always wall-clock time.
Time representation. May either represent wall clock time or ROS clock time.
unsigned __int64 uint64_t
static Time g_sim_time(0, 0)
static bool sleepUntil(const Time &end)
Sleep until a specific time has been reached.
std::ostream & operator<<(std::ostream &os, const Duration &rhs)
ROSTIME_DECL const Duration DURATION_MAX
static std::mutex g_sim_time_mutex
ROSTIME_DECL const Time TIME_MIN
GLbitfield GLuint64 timeout
bool ros_wallsleep(uint32_t sec, uint32_t nsec)
Go to the wall!
static Time now()
Retrieve the current time. If ROS clock time is in use, this returns the time according to the ROS cl...
static bool sleepUntil(const WallTime &end)
Sleep until a specific time has been reached.
static bool isValid()
Returns whether or not the current time is valid. Time is valid if it is non-zero.
::std_msgs::Time_< std::allocator< void > > Time
int ros_nanosleep(const uint32_t &sec, const uint32_t &nsec)
Simple representation of the rt library nanosleep function.