49 #if defined(__APPLE__) 50 #include <mach/clock.h> 51 #include <mach/mach.h> 52 #endif // defined(__APPLE__) 60 #include <boost/thread/mutex.hpp> 61 #include <boost/io/ios_state.hpp> 62 #include <boost/date_time/posix_time/ptime.hpp> 70 #define HAS_CLOCK_GETTIME (_POSIX_C_SOURCE >= 199309L) 83 const Duration
DURATION_MAX(std::numeric_limits<int32_t>::max(), 999999999);
84 const Duration
DURATION_MIN(std::numeric_limits<int32_t>::min(), 0);
86 const Time
TIME_MAX(std::numeric_limits<uint32_t>::max(), 999999999);
107 #if HAS_CLOCK_GETTIME 109 clock_gettime(CLOCK_REALTIME, &start);
110 if (start.tv_sec < 0 || start.tv_sec > std::numeric_limits<uint32_t>::max())
111 throw std::runtime_error(
"Timespec is out of dual 32-bit range");
113 nsec = start.tv_nsec;
115 struct timeval timeofday;
116 gettimeofday(&timeofday,NULL);
117 if (timeofday.tv_sec < 0 || timeofday.tv_sec > std::numeric_limits<uint32_t>::max())
118 throw std::runtime_error(
"Timeofday is out of dual signed 32-bit range");
119 sec = timeofday.tv_sec;
120 nsec = timeofday.tv_usec * 1000;
124 uint64_t now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
128 sec = (uint32_t)now_s;
129 nsec = (uint32_t)now_ns;
137 #if defined(__APPLE__) 141 host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
142 clock_get_time(cclock, &mts);
143 mach_port_deallocate(mach_task_self(), cclock);
144 start.tv_sec = mts.tv_sec;
145 start.tv_nsec = mts.tv_nsec;
146 #else // defined(__APPLE__) 148 clock_gettime(CLOCK_MONOTONIC, &start);
149 #endif // defined(__APPLE__) 151 nsec = start.tv_nsec;
154 uint64_t now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
158 sec = (uint32_t)now_s;
159 nsec = (uint32_t)now_ns;
174 std::this_thread::sleep_for(std::chrono::nanoseconds(static_cast<int64_t>(sec * 1e9 + nsec)));
177 timespec req = { sec, nsec };
178 return nanosleep(&req, NULL);
192 timespec req = { sec, nsec };
193 timespec rem = {0, 0};
194 while (nanosleep(&req, &rem) && !g_stopped)
230 boost::mutex::scoped_lock lock(g_sim_time_mutex);
243 boost::mutex::scoped_lock lock(g_sim_time_mutex);
274 while (!
isValid() && !g_stopped)
294 boost::posix_time::time_duration diff = t - boost::posix_time::from_time_t(0);
301 int64_t sec64 = d.total_seconds();
302 if (sec64 < 0 || sec64 > std::numeric_limits<uint32_t>::max())
303 throw std::runtime_error(
"time_duration is out of dual 32-bit range");
304 t.
sec = (uint32_t)sec64;
305 #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS) 306 t.
nsec = d.fractional_seconds();
308 t.
nsec = d.fractional_seconds()*1000;
315 boost::io::ios_all_saver s(os);
316 os << rhs.
sec <<
"." << std::setw(9) << std::setfill(
'0') << rhs.
nsec;
322 boost::io::ios_all_saver s(os);
323 if (rhs.
sec >= 0 || rhs.
nsec == 0)
325 os << rhs.
sec <<
"." << std::setw(9) << std::setfill(
'0') << rhs.
nsec;
329 os << (rhs.
sec == -1 ?
"-" :
"") << (rhs.
sec + 1) <<
"." << std::setw(9) << std::setfill(
'0') << (1000000000 - rhs.
nsec);
349 while (!g_stopped && (
Time::now() < end))
393 Time end = start + *
this;
400 while (!g_stopped && (
Time::now() < end))
426 boost::io::ios_all_saver s(os);
427 os << rhs.
sec <<
"." << std::setw(9) << std::setfill(
'0') << rhs.
nsec;
433 boost::io::ios_all_saver s(os);
434 os << rhs.
sec <<
"." << std::setw(9) << std::setfill(
'0') << rhs.
nsec;
456 boost::io::ios_all_saver s(os);
457 if (rhs.
sec >= 0 || rhs.
nsec == 0)
459 os << rhs.
sec <<
"." << std::setw(9) << std::setfill(
'0') << rhs.
nsec;
463 os << (rhs.
sec == -1 ?
"-" :
"") << (rhs.
sec + 1) <<
"." << std::setw(9) << std::setfill(
'0') << (1000000000 - rhs.
nsec);
475 uint64_t nsec_part = nsec % 1000000000UL;
476 uint64_t sec_part = nsec / 1000000000UL;
478 if (sec + sec_part > std::numeric_limits<uint32_t>::max())
479 throw std::runtime_error(
"Time is out of dual 32-bit range");
487 uint64_t sec64 =
sec;
488 uint64_t nsec64 =
nsec;
492 sec = (uint32_t)sec64;
493 nsec = (uint32_t)nsec64;
498 int64_t nsec_part = nsec % 1000000000L;
499 int64_t sec_part = sec + nsec / 1000000000L;
502 nsec_part += 1000000000L;
506 if (sec_part < 0 || sec_part > std::numeric_limits<uint32_t>::max())
507 throw std::runtime_error(
"Time is out of dual 32-bit range");
bool ros_wallsleep(uint32_t sec, uint32_t nsec)
Go to the wall!
static Time g_sim_time(0, 0)
ROSTIME_DECL void ros_steadytime(uint32_t &sec, uint32_t &nsec)
Time representation. May either represent wall clock time or ROS clock time.
ROSTIME_DECL void normalizeSecNSecUnsigned(int64_t &sec, int64_t &nsec)
static bool sleepUntil(const Time &end)
Sleep until a specific time has been reached.
bool sleep() const
sleep for the amount of time specified by this Duration. If a signal interrupts the sleep...
Duration representation for use with the WallTime class.
Thrown if the ros subsystem hasn't been initialised before use.
ROSTIME_DECL std::ostream & operator<<(std::ostream &os, const Duration &rhs)
static bool useSystemTime()
static bool sleepUntil(const WallTime &end)
Sleep until a specific time has been reached.
ROSTIME_DECL void ros_walltime(uint32_t &sec, uint32_t &nsec)
static void setNow(const Time &new_now)
static bool g_stopped(false)
static bool isSystemTime()
static bool g_initialized(false)
Time representation. Always wall-clock time.
bool sleep() const
sleep for the amount of time specified by this Duration. If a signal interrupts the sleep...
static bool sleepUntil(const SteadyTime &end)
Sleep until a specific time has been reached.
static SteadyTime now()
Returns the current steady (monotonic) clock time.
static bool isValid()
Returns whether or not the current time source is valid. Simulation time is valid if it is non-zero...
ROSTIME_DECL const Time TIME_MAX
static boost::mutex g_sim_time_mutex
int ros_nanosleep(const uint32_t &sec, const uint32_t &nsec)
Simple representation of the rt library nanosleep function.
Time representation. Always steady-clock time.
ROSTIME_DECL const Time TIME_MIN
ROSTIME_DECL void normalizeSecNSec(uint64_t &sec, uint64_t &nsec)
Duration representation for use with the Time class.
static WallTime now()
Returns the current wall clock time.
static bool g_use_sim_time(true)
static Time now()
Retrieve the current time. If ROS clock time is in use, this returns the time according to the ROS cl...
static Time fromBoost(const boost::posix_time::ptime &t)
ROSTIME_DECL const Duration DURATION_MIN
static bool waitForValid()
Wait for time source to become valid.
ROSTIME_DECL const Duration DURATION_MAX