From ff7109fcd4beca37d408388aa77427b82d85096c Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Fri, 4 Dec 2020 19:33:21 +0800 Subject: [PATCH] add function: get_current_time_ns --- src/shared_func.c | 21 ++++++++++++++++++--- src/shared_func.h | 4 ++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/shared_func.c b/src/shared_func.c index b748003..17e4db4 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -2536,15 +2536,30 @@ bool is_private_ip(const char* ip) return false; } +int64_t get_current_time_ns() +{ + struct timespec ts; + if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) != 0) + { + logError("file: "__FILE__", line: %d, " + "call clock_gettime fail, " + "errno: %d, error info: %s", + __LINE__, errno, STRERROR(errno)); + return errno != 0 ? errno : EPERM; + } + + return ((int64_t)ts.tv_sec * 1000 * 1000 * 1000LL + (int64_t)ts.tv_nsec); +} + int64_t get_current_time_us() { struct timeval tv; if (gettimeofday(&tv, NULL) != 0) { - logError("file: "__FILE__", line: %d, " \ - "call gettimeofday fail, " \ - "errno: %d, error info: %s", \ + logError("file: "__FILE__", line: %d, " + "call gettimeofday fail, " + "errno: %d, error info: %s", __LINE__, errno, STRERROR(errno)); return errno != 0 ? errno : EPERM; } diff --git a/src/shared_func.h b/src/shared_func.h index 1ab93a1..2a87049 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -711,6 +711,10 @@ double get_line_distance_km(const double lat1, const double lon1, */ bool is_private_ip(const char* ip); +/** get current time in ns + * return: current time + */ +int64_t get_current_time_ns(); /** get current time in us * return: current time