From 32f096fa3ff87c834980816a9e197ce254752f26 Mon Sep 17 00:00:00 2001 From: Takeshi Nakatani Date: Sun, 7 Feb 2021 15:02:03 +0000 Subject: [PATCH] Use clock_gettime instead of gettimeofday --- src/s3fs_logger.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/s3fs_logger.h b/src/s3fs_logger.h index a78e571..1690361 100644 --- a/src/s3fs_logger.h +++ b/src/s3fs_logger.h @@ -25,6 +25,13 @@ #include #include +#ifdef CLOCK_MONOTONIC_COARSE +#define S3FS_CLOCK_MONOTONIC CLOCK_MONOTONIC_COARSE +#else +// case of OSX +#define S3FS_CLOCK_MONOTONIC CLOCK_MONOTONIC +#endif + //------------------------------------------------------------------- // S3fsLog class //------------------------------------------------------------------- @@ -73,11 +80,16 @@ class S3fsLog static const char* GetCurrentTime() { - struct timeval now; + struct timeval now; + struct timespec tsnow; struct tm res; - char tmp[32]; - - gettimeofday(&now, NULL); + char tmp[32]; + if(-1 == clock_gettime(S3FS_CLOCK_MONOTONIC, &tsnow)){ + now.tv_sec = tsnow.tv_sec; + now.tv_usec = (tsnow.tv_nsec / 1000); + }else{ + gettimeofday(&now, NULL); + } strftime(tmp, sizeof(tmp), "%Y-%m-%dT%H:%M:%S", gmtime_r(&now.tv_sec, &res)); snprintf(current_time, sizeof(current_time), "%s.%03ldZ", tmp, (now.tv_usec / 1000)); return current_time;