diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b3b8322..b1a388f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,2 +1,3 @@ +add_subdirectory(timestamp) add_subdirectory(session) add_subdirectory(stellar) \ No newline at end of file diff --git a/src/timestamp/CMakeLists.txt b/src/timestamp/CMakeLists.txt new file mode 100644 index 0000000..a15c6f8 --- /dev/null +++ b/src/timestamp/CMakeLists.txt @@ -0,0 +1,18 @@ +############################################################################### +# timestamp +############################################################################### + +add_library(timestamp timestamp.cpp) +target_include_directories(timestamp PUBLIC ${CMAKE_SOURCE_DIR}/src/timestamp) +target_link_libraries(timestamp) + +############################################################################### +# gtest +############################################################################### + +add_executable(gtest_timestamp gtest_timestamp.cpp) +target_include_directories(gtest_timestamp PUBLIC ${CMAKE_CURRENT_LIST_DIR}) +target_link_libraries(gtest_timestamp timestamp gtest) + +include(GoogleTest) +gtest_discover_tests(gtest_timestamp) \ No newline at end of file diff --git a/src/timestamp/gtest_timestamp.cpp b/src/timestamp/gtest_timestamp.cpp new file mode 100644 index 0000000..04a1dec --- /dev/null +++ b/src/timestamp/gtest_timestamp.cpp @@ -0,0 +1,28 @@ +#include + +#include "timestamp.h" + +TEST(TIMESTAMP, GET) +{ + uint64_t sec = 0; + uint64_t msec = 0; + + for (int i = 0; i < 4; i++) + { + timestamp_update(); + sec = timestamp_get_sec(); + msec = timestamp_get_msec(); + + printf("current ts in sec : %lu\n", sec); + printf("current ts in msec : %lu\n", msec); + EXPECT_TRUE(sec == msec / 1000); + sleep(i); + printf("sleep %ds\n", i); + } +} + +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/src/timestamp/timestamp.cpp b/src/timestamp/timestamp.cpp new file mode 100644 index 0000000..0748c05 --- /dev/null +++ b/src/timestamp/timestamp.cpp @@ -0,0 +1,38 @@ +#include + +#include "timestamp.h" + +// 1 s = 1000 ms +// 1 ms = 1000 us +// 1 us = 1000 ns + +struct timestamp +{ + struct timespec ts; + uint64_t ts_in_msec; + uint64_t ts_in_sec; +} g_timestamp; + +#define ATOMIC_SET(x, y) __atomic_store_n(x, y, __ATOMIC_RELAXED) +#define ATOMIC_READ(x) __atomic_load_n(x, __ATOMIC_RELAXED) + +void timestamp_update() +{ + clock_gettime(CLOCK_MONOTONIC, &g_timestamp.ts); + + uint64_t current_timestamp_ms = g_timestamp.ts.tv_sec * 1000 + g_timestamp.ts.tv_nsec / 1000000; + uint64_t current_timestamp_sec = g_timestamp.ts.tv_sec + g_timestamp.ts.tv_nsec / 1000000000; + + ATOMIC_SET(&g_timestamp.ts_in_msec, current_timestamp_ms); + ATOMIC_SET(&g_timestamp.ts_in_sec, current_timestamp_sec); +} + +uint64_t timestamp_get_sec() +{ + return ATOMIC_READ(&g_timestamp.ts_in_sec); +} + +uint64_t timestamp_get_msec() +{ + return ATOMIC_READ(&g_timestamp.ts_in_msec); +} \ No newline at end of file diff --git a/src/timestamp/timestamp.h b/src/timestamp/timestamp.h new file mode 100644 index 0000000..d854ac1 --- /dev/null +++ b/src/timestamp/timestamp.h @@ -0,0 +1,19 @@ +#ifndef _TIMESTAMP_H +#define _TIMESTAMP_H + +#ifdef __cpluscplus +extern "C" +{ +#endif + +#include + +void timestamp_update(); +uint64_t timestamp_get_sec(); +uint64_t timestamp_get_msec(); + +#ifdef __cpluscplus +} +#endif + +#endif