add function: get_current_time_ns

pull/37/head
YuQing 2020-12-04 19:33:21 +08:00
parent 85354b6ef6
commit ff7109fcd4
2 changed files with 22 additions and 3 deletions

View File

@ -2536,15 +2536,30 @@ bool is_private_ip(const char* ip)
return false; 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() int64_t get_current_time_us()
{ {
struct timeval tv; struct timeval tv;
if (gettimeofday(&tv, NULL) != 0) if (gettimeofday(&tv, NULL) != 0)
{ {
logError("file: "__FILE__", line: %d, " \ logError("file: "__FILE__", line: %d, "
"call gettimeofday fail, " \ "call gettimeofday fail, "
"errno: %d, error info: %s", \ "errno: %d, error info: %s",
__LINE__, errno, STRERROR(errno)); __LINE__, errno, STRERROR(errno));
return errno != 0 ? errno : EPERM; return errno != 0 ? errno : EPERM;
} }

View File

@ -711,6 +711,10 @@ double get_line_distance_km(const double lat1, const double lon1,
*/ */
bool is_private_ip(const char* ip); 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 /** get current time in us
* return: current time * return: current time