Commit 09e142f0 authored by johan's avatar johan

Add timespec add and comparison

parent fcebd9f0
......@@ -368,7 +368,24 @@ void _bctbx_get_cur_time(bctoolboxTimeSpec *ret, bool_t realtime);
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);
/**
* @brief Compares two TimeSpec s1 and s2.
*
* @param[in] s1 First time spec
* @param[in] s2 Second time spec
*
* @return a negative value if s1 is earlier than s2, 0 if they are equal, a positive value if s1 is later than s2
*/
BCTBX_PUBLIC int bctbx_timespec_compare(const bctoolboxTimeSpec *s1, const bctoolboxTimeSpec *s2);
/**
* @brief Add given amount of seconds to a timeSpec structure
*
* @param[in/out] ts The timeSpec structure used as input, modified in output by increnting it according to second argument
* @param[in] lap In seconds, number of seconds to modify the given timeSpec, can be negative(which may set the original timeSpec to 0)
*/
BCTBX_PUBLIC void bctbx_timespec_add(bctoolboxTimeSpec *ts, const int64_t lap);
BCTBX_PUBLIC unsigned int bctbx_random(void);
......
......@@ -788,6 +788,45 @@ void bctbx_sleep_until(const bctoolboxTimeSpec *ts){
#endif
}
/**
* @brief Add given amount of seconds to a timeSpec structure
*
* @param[in/out] ts The timeSpec structure used as input, modified in output by increnting it according to second argument
* @param[in] lap In seconds, number of seconds to modify the given timeSpec, can be negative(which may set the original timeSpec to 0)
*/
void bctbx_timespec_add(bctoolboxTimeSpec *ts, const int64_t lap) {
if (lap<0 && -lap > ts->tv_sec) {
ts->tv_sec = 0;
ts->tv_nsec = 0;
} else {
ts->tv_sec += lap;
}
}
/**
* @brief Compares two TimeSpec s1 and s2.
*
* @param[in] s1 First time spec
* @param[in] s2 Second time spec
*
* @return a negative value if s1 is earlier than s2, 0 if they are equal, a positive value if s1 is later than s2
*/
int bctbx_timespec_compare(const bctoolboxTimeSpec *s1, const bctoolboxTimeSpec *s2){
int64_t secdiff = s1->tv_sec - s2->tv_sec;
if (secdiff == 0){
int64_t nsec_diff = s1->tv_nsec - s2->tv_nsec;
if (nsec_diff < 0){
return -1;
}else if (nsec_diff > 0){
return 1;
}else return 0;
}else if (secdiff < 0){
return -1;
}else
return 1;
}
#if defined(_WIN32) && !defined(_MSC_VER)
char* strtok_r(char *str, const char *delim, char **nextp){
char *ret;
......
......@@ -82,9 +82,34 @@ static void bytesToFromHexaStrings(void) {
BC_ASSERT_EQUAL(bctbx_strToUint64("fedcba9876543210"), 0xfedcba9876543210, uint64_t, "0x%016lx");
}
static void timeFunctions(void) {
bctoolboxTimeSpec testTs;
bctoolboxTimeSpec y2k;
y2k.tv_sec = 946684800;
y2k.tv_nsec = 123456789;
memcpy(&testTs, &y2k, sizeof(bctoolboxTimeSpec));
BC_ASSERT_EQUAL(bctbx_timespec_compare(&y2k, &testTs), 0, int, "%d");
bctbx_timespec_add(&testTs, 604800);
BC_ASSERT_EQUAL(testTs.tv_sec, y2k.tv_sec+604800, int64_t, "%ld");
BC_ASSERT_EQUAL(testTs.tv_nsec, y2k.tv_nsec, int64_t, "%ld");
BC_ASSERT_TRUE(bctbx_timespec_compare(&y2k, &testTs)<0);
memcpy(&testTs, &y2k, sizeof(bctoolboxTimeSpec));
bctbx_timespec_add(&testTs, -604800);
BC_ASSERT_EQUAL(testTs.tv_sec, y2k.tv_sec-604800, int64_t, "%ld");
BC_ASSERT_EQUAL(testTs.tv_nsec, y2k.tv_nsec, int64_t, "%ld");
BC_ASSERT_TRUE(bctbx_timespec_compare(&y2k, &testTs)>0);
memcpy(&testTs, &y2k, sizeof(bctoolboxTimeSpec));
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");
}
static test_t utils_tests[] = {
TEST_NO_TAG("Bytes to/from Hexa strings", bytesToFromHexaStrings),
TEST_NO_TAG("Time", timeFunctions)
};
test_suite_t utils_test_suite = {"Utils", NULL, NULL, NULL, NULL,
......
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