This commit is contained in:
2024-01-18 17:17:18 +08:00
parent 841bcc3998
commit aedcdbd610
6 changed files with 53 additions and 3 deletions

View File

@@ -95,6 +95,8 @@ if (NOT MSVC)
endif() endif()
if (NOT WIN32) if (NOT WIN32)
check_symbol_exists(clock_gettime "time.h" HAVE_CLOCK_GETTIME) check_symbol_exists(clock_gettime "time.h" HAVE_CLOCK_GETTIME)
check_symbol_exists(usleep "unistd.h" HAVE_USLEEP)
check_symbol_exists(nanosleep "time.h" HAVE_NANOSLEEP)
endif() endif()
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/utils_config.h.in" "${CMAKE_CURRENT_BINARY_DIR}/utils_config.h") configure_file("${CMAKE_CURRENT_SOURCE_DIR}/utils_config.h.in" "${CMAKE_CURRENT_BINARY_DIR}/utils_config.h")

View File

@@ -11,6 +11,8 @@ extern "C" {
#include <fcntl.h> #include <fcntl.h>
#if _WIN32 #if _WIN32
#include <io.h> #include <io.h>
#else
#include <unistd.h>
#endif #endif
/** /**
* @brief Check file exists * @brief Check file exists

View File

@@ -8,6 +8,7 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <string.h>
#include "cstr_util.h" #include "cstr_util.h"
#include "utils_config.h" #include "utils_config.h"

View File

@@ -17,6 +17,10 @@
#include "err.h" #include "err.h"
#if HAVE_USLEEP
#include <unistd.h>
#endif
#if _WIN32 #if _WIN32
void time_util::time_t_to_file_time(time_t t, LPFILETIME pft) { void time_util::time_t_to_file_time(time_t t, LPFILETIME pft) {
ULARGE_INTEGER time_value; ULARGE_INTEGER time_value;
@@ -64,11 +68,11 @@ time_t time_util::timegm(struct tm* tm) {
#endif #endif
} }
time_t time_util::time_ns() { size_t time_util::time_ns() {
#if _WIN32 #if _WIN32
FILETIME ft; FILETIME ft;
GetSystemTimeAsFileTime(&ft); GetSystemTimeAsFileTime(&ft);
time_t t = ((size_t)ft.dwHighDateTime << 32) | (size_t)ft.dwLowDateTime; size_t t = ((size_t)ft.dwHighDateTime << 32) | (size_t)ft.dwLowDateTime;
return t; return t;
#elif HAVE_CLOCK_GETTIME #elif HAVE_CLOCK_GETTIME
struct timespec ts; struct timespec ts;
@@ -78,3 +82,28 @@ time_t time_util::time_ns() {
return time(NULL) * 1000000000LL; return time(NULL) * 1000000000LL;
#endif #endif
} }
size_t time_time_ns() {
return time_util::time_ns();
}
int time_util::mssleep(unsigned int ms) {
#if _WIN32
Sleep(ms);
return 0;
#elif HAVE_USLEEP
return usleep(ms);
#elif HAVE_NANOSLEEP
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000;
return nanosleep(&ts, NULL);
#else
printf("mssleep failed: not implemented\n");
return -1;
#endif
}
int mssleep(unsigned int ms) {
return time_util::mssleep(ms);
}

View File

@@ -5,6 +5,8 @@
#include <Windows.h> #include <Windows.h>
#endif #endif
#if __cplusplus
namespace time_util { namespace time_util {
#if _WIN32 #if _WIN32
/** /**
@@ -17,6 +19,18 @@ namespace time_util {
char* strptime(const char* s, const char* format, struct tm* tm); char* strptime(const char* s, const char* format, struct tm* tm);
long get_timezone(); long get_timezone();
time_t timegm(struct tm* tm); time_t timegm(struct tm* tm);
time_t time_ns(); size_t time_ns();
int mssleep(unsigned int ms);
}
extern "C" {
#endif
size_t time_time_ns();
int mssleep(unsigned int ms);
#if __cplusplus
} }
#endif #endif
#endif

View File

@@ -27,3 +27,5 @@
#cmakedefine HAVE_TZSET @HAVE_TZSET@ #cmakedefine HAVE_TZSET @HAVE_TZSET@
#cmakedefine HAVE_FPRINTF_S @HAVE_FPRINTF_S@ #cmakedefine HAVE_FPRINTF_S @HAVE_FPRINTF_S@
#cmakedefine HAVE_CLOCK_GETTIME @HAVE_CLOCK_GETTIME@ #cmakedefine HAVE_CLOCK_GETTIME @HAVE_CLOCK_GETTIME@
#cmakedefine HAVE_USLEEP @HAVE_USLEEP@
#cmakedefine HAVE_NANOSLEEP @HAVE_NANOSLEEP@