diff --git a/HISTORY b/HISTORY index 112eb08..1962e4f 100644 --- a/HISTORY +++ b/HISTORY @@ -1,7 +1,8 @@ -Version 1.23 2015-10-16 +Version 1.23 2015-10-26 * sched_thread.c: task can execute in a new thread * sched_thread.c: support delay tasks + * add function get_current_time_us and get_current_time_ms Version 1.22 2015-10-10 * export php function: fastcommon_get_first_local_ip diff --git a/src/shared_func.c b/src/shared_func.c index c7caebb..73407c8 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -2251,3 +2251,19 @@ bool is_private_ip(const char* ip) return false; } +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", \ + __LINE__, errno, STRERROR(errno)); + return errno != 0 ? errno : EPERM; + } + + return ((int64_t)tv.tv_sec * 1000 * 1000 + (int64_t)tv.tv_usec); +} + diff --git a/src/shared_func.h b/src/shared_func.h index b9ee212..1256063 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -520,6 +520,14 @@ double get_line_distance_km(const double lat1, const double lon1, */ bool is_private_ip(const char* ip); + +/** get current time in us + * return: current time + */ +int64_t get_current_time_us(); + +#define get_current_time_ms() (get_current_time_us() / 1000) + #ifdef __cplusplus } #endif