Commit 9d9dd098 authored by johan's avatar johan

Add a String to second period parser function

parent 7494e721
......@@ -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);
......
......@@ -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;
......
......@@ -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[] = {
......
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