Commit 7494e721 authored by johan's avatar johan

Rework the get_current_time API

- have a function which enforce we get the UTC current time
- return UTC current time for the get_cur_time_ms
- do not expose _bctbx_get_cur_time in the API.
parent 448d0f0b
......@@ -363,8 +363,21 @@ BCTBX_PUBLIC char *bctbx_concat (const char *str, ...) ;
BCTBX_PUBLIC int bctbx_file_exist(const char *pathname);
/**
* @brief return a timeSpec structure(sec and nsec) containing current time(WARNING: there is no guarantees it is UTC ).
* The time returned may refers to UTC or last boot.
* Use this function only to compute a time span between two calls
* @param[in/out] ret The current time (seconds and nano seconds).
*/
BCTBX_PUBLIC void bctbx_get_cur_time(bctoolboxTimeSpec *ret);
void _bctbx_get_cur_time(bctoolboxTimeSpec *ret, bool_t realtime);
/**
* @brief return a timeSpec structure(sec and nsec) containing current UTC time.
*
* @param[in/out] ret The current UTC time, (seconds and nano seconds)
*/
BCTBX_PUBLIC void bctbx_get_utc_cur_time(bctoolboxTimeSpec *ret);
BCTBX_PUBLIC uint64_t bctbx_get_cur_time_ms(void);
BCTBX_PUBLIC void bctbx_sleep_ms(int ms);
BCTBX_PUBLIC void bctbx_sleep_until(const bctoolboxTimeSpec *ts);
......
......@@ -727,6 +727,10 @@ void _bctbx_get_cur_time(bctoolboxTimeSpec *ret, bool_t realtime){
#endif
}
void bctbx_get_utc_cur_time(bctoolboxTimeSpec *ret){
_bctbx_get_cur_time(ret, TRUE);
}
void bctbx_get_cur_time(bctoolboxTimeSpec *ret){
_bctbx_get_cur_time(ret, FALSE);
}
......@@ -734,7 +738,7 @@ void bctbx_get_cur_time(bctoolboxTimeSpec *ret){
uint64_t bctbx_get_cur_time_ms(void) {
bctoolboxTimeSpec ts;
bctbx_get_cur_time(&ts);
_bctbx_get_cur_time(&ts, TRUE);
return (ts.tv_sec * 1000LL) + ((ts.tv_nsec + 500000LL) / 1000000LL);
}
......
......@@ -84,9 +84,11 @@ static void bytesToFromHexaStrings(void) {
static void timeFunctions(void) {
bctoolboxTimeSpec testTs;
bctoolboxTimeSpec y2k;
bctoolboxTimeSpec y2k,monday6Feb2017;
y2k.tv_sec = 946684800;
y2k.tv_nsec = 123456789;
monday6Feb2017.tv_sec = 1486347823;
monday6Feb2017.tv_nsec = 0;
memcpy(&testTs, &y2k, sizeof(bctoolboxTimeSpec));
BC_ASSERT_EQUAL(bctbx_timespec_compare(&y2k, &testTs), 0, int, "%d");
......@@ -105,6 +107,13 @@ static void timeFunctions(void) {
bctbx_timespec_add(&testTs, -946684801);
BC_ASSERT_EQUAL(testTs.tv_sec, 0, int64_t, "%ld");
BC_ASSERT_EQUAL(testTs.tv_nsec, 0, int64_t, "%ld");
/* test the get utc time function
* there is no easy way to ensure we get the correct time, just check it is at least not the time from last boot,
* check it is greater than the current time as this test was written(6feb2017) */
bctbx_get_utc_cur_time(&testTs);
BC_ASSERT_TRUE(bctbx_timespec_compare(&testTs, &monday6Feb2017)>0);
}
static test_t utils_tests[] = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment