[Pkg-voip-commits] [bctoolbox] 39/57: Add timespec add and comparison
daniel at gnoutcheff.name
daniel at gnoutcheff.name
Thu Mar 30 04:31:34 UTC 2017
This is an automated email from the git hooks/post-receive script.
gnoutchd-guest pushed a commit to branch debian/sid
in repository bctoolbox.
commit 09e142f084c0a4b7a21273d6fa324e67493f6694
Author: Johan Pascal <johan.pascal at belledonne-communications.com>
Date: Wed Feb 1 15:51:04 2017 +0700
Add timespec add and comparison
---
include/bctoolbox/port.h | 17 +++++++++++++++++
src/utils/port.c | 39 +++++++++++++++++++++++++++++++++++++++
tester/port.c | 25 +++++++++++++++++++++++++
3 files changed, 81 insertions(+)
diff --git a/include/bctoolbox/port.h b/include/bctoolbox/port.h
index e6e1d31..38824ec 100644
--- a/include/bctoolbox/port.h
+++ b/include/bctoolbox/port.h
@@ -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);
diff --git a/src/utils/port.c b/src/utils/port.c
index e0442e5..4ff3850 100644
--- a/src/utils/port.c
+++ b/src/utils/port.c
@@ -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;
diff --git a/tester/port.c b/tester/port.c
index 0e88b22..e3ece9e 100644
--- a/tester/port.c
+++ b/tester/port.c
@@ -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,
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-voip/bctoolbox.git
More information about the Pkg-voip-commits
mailing list