mirror of
https://github.com/lifegpc/c-utils.git
synced 2026-06-06 05:08:45 +08:00
UPdate
This commit is contained in:
@@ -95,6 +95,8 @@ if (NOT MSVC)
|
||||
endif()
|
||||
if (NOT WIN32)
|
||||
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()
|
||||
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/utils_config.h.in" "${CMAKE_CURRENT_BINARY_DIR}/utils_config.h")
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ extern "C" {
|
||||
#include <fcntl.h>
|
||||
#if _WIN32
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
/**
|
||||
* @brief Check file exists
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <string.h>
|
||||
#include "cstr_util.h"
|
||||
#include "utils_config.h"
|
||||
|
||||
|
||||
@@ -17,6 +17,10 @@
|
||||
|
||||
#include "err.h"
|
||||
|
||||
#if HAVE_USLEEP
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#if _WIN32
|
||||
void time_util::time_t_to_file_time(time_t t, LPFILETIME pft) {
|
||||
ULARGE_INTEGER time_value;
|
||||
@@ -64,11 +68,11 @@ time_t time_util::timegm(struct tm* tm) {
|
||||
#endif
|
||||
}
|
||||
|
||||
time_t time_util::time_ns() {
|
||||
size_t time_util::time_ns() {
|
||||
#if _WIN32
|
||||
FILETIME 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;
|
||||
#elif HAVE_CLOCK_GETTIME
|
||||
struct timespec ts;
|
||||
@@ -78,3 +82,28 @@ time_t time_util::time_ns() {
|
||||
return time(NULL) * 1000000000LL;
|
||||
#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);
|
||||
}
|
||||
|
||||
16
time_util.h
16
time_util.h
@@ -5,6 +5,8 @@
|
||||
#include <Windows.h>
|
||||
#endif
|
||||
|
||||
#if __cplusplus
|
||||
|
||||
namespace time_util {
|
||||
#if _WIN32
|
||||
/**
|
||||
@@ -17,6 +19,18 @@ namespace time_util {
|
||||
char* strptime(const char* s, const char* format, struct tm* tm);
|
||||
long get_timezone();
|
||||
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
|
||||
|
||||
@@ -27,3 +27,5 @@
|
||||
#cmakedefine HAVE_TZSET @HAVE_TZSET@
|
||||
#cmakedefine HAVE_FPRINTF_S @HAVE_FPRINTF_S@
|
||||
#cmakedefine HAVE_CLOCK_GETTIME @HAVE_CLOCK_GETTIME@
|
||||
#cmakedefine HAVE_USLEEP @HAVE_USLEEP@
|
||||
#cmakedefine HAVE_NANOSLEEP @HAVE_NANOSLEEP@
|
||||
|
||||
Reference in New Issue
Block a user