The Fn unparse_time and Fn unparse_time_approx does the opposite of Fn parse_time , that is they take a number of seconds and express that as human readable string. Fa unparse_time produces an exact time, while Fa unparse_time_approx restricts the result to only include one units.
Fn print_time_table prints a descriptive list of available units on the passed file descriptor.
The possible units include:
Units names can be arbitrarily abbreviated (as long as they are unique).
#include <stdio.h>
#include <parse_time.h>
int
main(int argc, char **argv)
{
int i;
int result;
char buf[128];
print_time_table(stdout);
for (i = 1; i < argc; i++) {
result = parse_time(argv[i], "second");
if(result == -1) {
fprintf(stderr, "%s: parse error\n", argv[i]);
continue;
}
printf("--\n");
printf("parse_time = %d\n", result);
unparse_time(result, buf, sizeof(buf));
printf("unparse_time = %s\n", buf);
unparse_time_approx(result, buf, sizeof(buf));
printf("unparse_time_approx = %s\n", buf);
}
return 0;
}
$ ./a.out "1 minute 30 seconds" "90 s" "1 y -1 s" 1 year = 365 days 1 month = 30 days 1 week = 7 days 1 day = 24 hours 1 hour = 60 minutes 1 minute = 60 seconds 1 second -- parse_time = 90 unparse_time = 1 minute 30 seconds unparse_time_approx = 1 minute -- parse_time = 90 unparse_time = 1 minute 30 seconds unparse_time_approx = 1 minute -- parse_time = 31535999 unparse_time = 12 months 4 days 23 hours 59 minutes 59 seconds unparse_time_approx = 12 months