Skip to content

Commit

Permalink
mx_util: Add mxq_strto{seconds,minutes}()
Browse files Browse the repository at this point in the history
  • Loading branch information
mariux committed Aug 26, 2015
1 parent 9aa0024 commit f805931
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
82 changes: 82 additions & 0 deletions mx_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,88 @@ inline int mx_stribeginswithany(char *str, char **starts, char **endptr)
return _mx_strbeginswithany(str, starts, endptr, 1);
}

inline int mx_strtoseconds(char *str, unsigned long long int *seconds)
{
unsigned long long int s = 0;
unsigned long long int t;

char *end;

if (!str || !*str)
return -(errno=EINVAL);

if (strchr(str, '-'))
return -(errno=ERANGE);

do {
errno = 0;
t = strtoull(str, &end, 10);

if (errno)
return -errno;

if (str == end)
return -(errno=EINVAL);

for (;*end && *end == ' '; end++)
/* empty */;

//if (mx_strtounit(end, &end));

switch (*end) {

case 'y': /* years */
t *= 52;

case 'w': /* weeks */
t *= 7;

case 'd': /* days */
t *= 24;

case 'h': /* hours */
t *= 60;

case 'm': /* minutes */
t *= 60;

case 's': /* seconds */
end++;
break;

default:
return -(errno=EINVAL);
}

if (s+t < s)
return -(errno=ERANGE);

s += t;

for (;*end && *end == ' '; end++)
/* empty */;

str = end;

} while (*str);

*seconds = s;

return 0;
}

inline int mx_strtominutes(char *str, unsigned long long int *minutes)
{
int res;

res = mx_strtoseconds(str, minutes);

if (res >= 0)
*minutes /= 60;

return res;
}

inline char *mx_strskipwhitespaces(char *str)
{
char *s;
Expand Down
3 changes: 3 additions & 0 deletions mx_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ int mx_strbeginswithany(char *str, char **starts, char **endptr);

char *mx_strskipwhitespaces(char *str);

int mx_strtoseconds(char *str, unsigned long long int *seconds);
int mx_strtominutes(char *str, unsigned long long int *minutes);

int mx_strtoul(char *str, unsigned long int *to);
int mx_strtoull(char *str, unsigned long long int *to);

Expand Down
64 changes: 64 additions & 0 deletions test_mx_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,68 @@ static void test_mx_strbeginswithany(void)
assert(end == NULL);
}

static void test_mx_strtoseconds(void)
{
unsigned long long int l;

assert(mx_strtoseconds("123s", &l) == 0);
assert(l == 123);

assert(mx_strtoseconds("0123s", &l) == 0);
assert(l == 123);

assert(mx_strtoseconds("123s0s", &l) == 0);
assert(l == 123);

assert(mx_strtoseconds("2m3s", &l) == 0);
assert(l == 123);

assert(mx_strtoseconds(" 2 m 3 s ", &l) == 0);
assert(l == 123);

assert(mx_strtoseconds("1h 2m 3s", &l) == 0);
assert(l == 60*60 + 2*60 + 3);

assert(mx_strtoseconds("2m 3s 1h", &l) == 0);
assert(l == 60*60 + 2*60 + 3);

assert(mx_strtoseconds("2m 3s 1h1y", &l) == 0);
assert(l == 60*60 + 2*60 + 3 + 52*7*24*60*60);

assert(mx_strtoseconds("2m 3s 1h1y", &l) == 0);
assert(l == 60*60 + 2*60 + 3 + 52*7*24*60*60);

assert(mx_strtoseconds("-1", &l) == -ERANGE);
assert(mx_strtoseconds(" -1", &l) == -ERANGE);

assert(mx_strtoseconds("123", &l) == -EINVAL);
assert(mx_strtoseconds("0123", &l) == -EINVAL);
assert(mx_strtoseconds("1.2", &l) == -EINVAL);
assert(mx_strtoseconds("1,2", &l) == -EINVAL);
assert(mx_strtoseconds("test", &l) == -EINVAL);
}

static void test_mx_strtominutes(void)
{
unsigned long long int l;

assert(mx_strtominutes("123s", &l) == 0);
assert(l == 2);

assert(mx_strtominutes("20d", &l) == 0);
assert(l == 20*24*60);


assert(mx_strtominutes("-1", &l) == -ERANGE);
assert(mx_strtominutes(" -1", &l) == -ERANGE);

assert(mx_strtominutes("123", &l) == -EINVAL);
assert(mx_strtominutes("0123", &l) == -EINVAL);
assert(mx_strtominutes("1.2", &l) == -EINVAL);
assert(mx_strtominutes("1,2", &l) == -EINVAL);
assert(mx_strtominutes("test", &l) == -EINVAL);
}

int main(int argc, char *argv[])
{
test_mx_strskipwhitespaces();
Expand All @@ -191,5 +253,7 @@ int main(int argc, char *argv[])
test_mx_strbeginswith();
test_mx_stribeginswith();
test_mx_strbeginswithany();
test_mx_strtoseconds();
test_mx_strtominutes();
return 0;
}

0 comments on commit f805931

Please sign in to comment.