refactor: move times API to utils.h

This commit is contained in:
luwenpeng
2024-08-16 10:19:36 +08:00
parent 5c0a40638c
commit bfa5564558
9 changed files with 34 additions and 143 deletions

View File

@@ -7,6 +7,31 @@ extern "C"
#include <stdint.h>
#include <stdio.h>
#include <time.h>
/*
* The maximum number of seconds that can be stored in the time_t value is 2147483647 - a little over 68 years.
*
* struct timespec
* {
* time_t tv_sec; // seconds
* long tv_nsec; // nanoseconds
* };
*
* 1 s = 1000 ms
* 1 ms = 1000 us
* 1 us = 1000 ns
*/
#define TIMESPEC_TO_MSEC(ts) ((ts).tv_sec * 1000 + (ts).tv_nsec / 1000000)
static inline uint64_t clock_get_real_time_ms()
{
struct timespec now;
clock_gettime(CLOCK_REALTIME_COARSE, &now);
return TIMESPEC_TO_MSEC(now);
}
static inline void hexdump_to_fd(int fd, uint32_t idx, const char *data, uint16_t len)
{