From d39fd49c787ade7691afa8db3c7673476241b29b Mon Sep 17 00:00:00 2001 From: Serge Nikulin Date: Thu, 11 Jan 2018 11:34:41 -0800 Subject: [PATCH 1/2] change rcutils_time_point_value_t type from uint64_t to int64_t --- include/rcutils/time.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rcutils/time.h b/include/rcutils/time.h index 72c257c4..862ea50e 100644 --- a/include/rcutils/time.h +++ b/include/rcutils/time.h @@ -41,7 +41,7 @@ extern "C" #define RCUTILS_NS_TO_US(nanoseconds) (nanoseconds / 1000) /// A single point in time, measured in nanoseconds since the Unix epoch. -typedef uint64_t rcutils_time_point_value_t; +typedef int64_t rcutils_time_point_value_t; /// A duration of time, measured in nanoseconds. typedef int64_t rcutils_duration_value_t; From ff6bf5e1f50d59d1fd245afaa626189bc38dc309 Mon Sep 17 00:00:00 2001 From: Serge Nikulin Date: Thu, 1 Feb 2018 10:57:12 -0800 Subject: [PATCH 2/2] Fix rcutils_steady_time_now overflow and rcutils_system_time_now types --- src/time_win32.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/time_win32.c b/src/time_win32.c index f1dec2d2..72d3a5dd 100644 --- a/src/time_win32.c +++ b/src/time_win32.c @@ -36,14 +36,14 @@ rcutils_system_time_now(rcutils_time_point_value_t * now) now, RCUTILS_RET_INVALID_ARGUMENT, rcutils_get_default_allocator()); FILETIME ft; GetSystemTimePreciseAsFileTime(&ft); - ULARGE_INTEGER uli; - uli.LowPart = ft.dwLowDateTime; - uli.HighPart = ft.dwHighDateTime; + LARGE_INTEGER li; + li.LowPart = ft.dwLowDateTime; + li.HighPart = ft.dwHighDateTime; // Adjust for January 1st, 1970, see: // https://support.microsoft.com/en-us/kb/167296 - uli.QuadPart -= 116444736000000000; + li.QuadPart -= 116444736000000000; // Convert to nanoseconds from 100's of nanoseconds. - *now = uli.QuadPart * 100; + *now = li.QuadPart * 100; return RCUTILS_RET_OK; } @@ -61,8 +61,10 @@ rcutils_steady_time_now(rcutils_time_point_value_t * now) QueryPerformanceFrequency(&cpu_frequency); QueryPerformanceCounter(&performance_count); // Convert to nanoseconds before converting from ticks to avoid precision loss. - rcutils_time_point_value_t intermediate = RCUTILS_S_TO_NS(performance_count.QuadPart); - *now = intermediate / cpu_frequency.QuadPart; + uint64_t intermediate = RCUTILS_S_TO_NS(performance_count.QuadPart); + intermediate /= cpu_frequency.QuadPart; + // This conversion will overflow if the PC runs >292 years non-stop + *now = (rcutils_time_point_value_t)intermediate; return RCUTILS_RET_OK; }