Add timestamp
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
add_subdirectory(timestamp)
|
||||
add_subdirectory(session)
|
||||
add_subdirectory(stellar)
|
||||
18
src/timestamp/CMakeLists.txt
Normal file
18
src/timestamp/CMakeLists.txt
Normal file
@@ -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)
|
||||
28
src/timestamp/gtest_timestamp.cpp
Normal file
28
src/timestamp/gtest_timestamp.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#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();
|
||||
}
|
||||
38
src/timestamp/timestamp.cpp
Normal file
38
src/timestamp/timestamp.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <time.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
19
src/timestamp/timestamp.h
Normal file
19
src/timestamp/timestamp.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef _TIMESTAMP_H
|
||||
#define _TIMESTAMP_H
|
||||
|
||||
#ifdef __cpluscplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void timestamp_update();
|
||||
uint64_t timestamp_get_sec();
|
||||
uint64_t timestamp_get_msec();
|
||||
|
||||
#ifdef __cpluscplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user