[Pkg-voip-commits] [bctoolbox] 49/57: Add a String to second period parser function

daniel at gnoutcheff.name daniel at gnoutcheff.name
Thu Mar 30 04:31:35 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 9d9dd0980a258c70802242b0a90c552c5745aea1
Author: Johan Pascal <johan.pascal at belledonne-communications.com>
Date:   Mon Feb 6 14:31:26 2017 +0700

    Add a String to second period parser function
---
 include/bctoolbox/port.h | 14 +++++++++++
 src/utils/port.c         | 64 ++++++++++++++++++++++++++++++++++++++++++++++++
 tester/port.c            | 16 ++++++++++++
 3 files changed, 94 insertions(+)

diff --git a/include/bctoolbox/port.h b/include/bctoolbox/port.h
index fc7ffc5..700ca73 100644
--- a/include/bctoolbox/port.h
+++ b/include/bctoolbox/port.h
@@ -399,6 +399,20 @@ BCTBX_PUBLIC int bctbx_timespec_compare(const bctoolboxTimeSpec *s1, const bctoo
  */
 BCTBX_PUBLIC void bctbx_timespec_add(bctoolboxTimeSpec *ts, const int64_t lap);
 
+/**
+ * @brief Parse a string into a number of seconds
+ *  Accepted suffixes are Y,M,W,d,h,m,s number is expected to be a base 10 integer, no suffix means seconds
+ *  notes:
+ *     - M suffix(month) is consired a 30 days period without any consideration of the current date, Y is always 365 days.
+ *     - You can combine suffixes in any order: 3Y6M is valid, 15d1M6h is valid too.
+ *     - Any unknown suffix is silently ignored and the value preceding it is discarded
+ *     - NULL or empty string('\0') in timeString are valid and return 0.
+ *
+ * @param[in]	timeString		a string formated like {[0-9]+[Y,M,W,d,h,m,s]?}*'\0' (must be null terminated)
+ * @return	described time period in seconds
+ */
+BCTBX_PUBLIC uint32_t bctbx_time_string_to_sec(const char *timeString);
+
 BCTBX_PUBLIC unsigned int bctbx_random(void);
 
 
diff --git a/src/utils/port.c b/src/utils/port.c
index 0053558..9ea3422 100644
--- a/src/utils/port.c
+++ b/src/utils/port.c
@@ -831,6 +831,70 @@ int bctbx_timespec_compare(const bctoolboxTimeSpec *s1, const bctoolboxTimeSpec
 }
 
 
+uint32_t bctbx_time_string_to_sec(const char *timeString) {
+
+	char *p = NULL;
+	char *o = NULL;
+	int32_t n=0;
+	uint32_t ret=0;
+
+	if (timeString == NULL) {
+		return 0;
+	}
+
+	o = p = bctbx_strdup(timeString);
+
+	while (*p!='\0') {
+		n=strtol(p, &p, 10);
+		switch (*p) {
+			case '\0':
+				ret+=n;
+			break;
+
+			case 'Y':
+				ret +=n*365*24*3600;
+				p++;
+			break;
+
+			case 'M':
+				ret +=n*30*24*3600;
+				p++;
+			break;
+
+			case 'W':
+				ret +=n*7*24*3600;
+				p++;
+			break;
+
+			case 'd':
+				ret +=n*24*3600;
+				p++;
+			break;
+
+			case 'h':
+				ret +=n*3600;
+				p++;
+			break;
+
+			case 'm':
+				ret +=n*60;
+				p++;
+			break;
+
+			case 's':
+				ret+=n;
+				p++;
+			break;
+
+			default: /* just ignore any other suffix */
+				p++;
+			break;
+		}
+	}
+	bctbx_free(o);
+	return ret;
+}
+
 #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 f0cfffd..2904772 100644
--- a/tester/port.c
+++ b/tester/port.c
@@ -114,6 +114,22 @@ static void timeFunctions(void) {
 	bctbx_get_utc_cur_time(&testTs);
 	BC_ASSERT_TRUE(bctbx_timespec_compare(&testTs, &monday6Feb2017)>0);
 
+	BC_ASSERT_EQUAL(bctbx_time_string_to_sec(NULL), 0, uint32_t, "%d");
+	BC_ASSERT_EQUAL(bctbx_time_string_to_sec(""), 0, uint32_t, "%d");
+	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("0"), 0, uint32_t, "%d");
+	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("1500"), 1500, uint32_t, "%d");
+	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("2500s"), 2500, uint32_t, "%d");
+	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("10m"), 600, uint32_t, "%d");
+	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("5h"), 5*3600, uint32_t, "%d");
+	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("2d"), 2*24*3600, uint32_t, "%d");
+	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("3W"), 3*7*24*3600, uint32_t, "%d");
+	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("6M"), 6*30*24*3600, uint32_t, "%d");
+	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("7Y"), 7*365*24*3600, uint32_t, "%d");
+	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("7Y6M2W"), (7*365+6*30+2*7)*24*3600, uint32_t, "%d");
+	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("2m30"), 2*60+30, uint32_t, "%d");
+	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("15d1M"), (15+1*30)*24*3600, uint32_t, "%d");
+	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("15d5z"), 15*24*3600, uint32_t, "%d");
+	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("15dM12h"), (15*24+12)*3600, uint32_t, "%d");
 }
 
 static test_t utils_tests[] = {

-- 
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