1PARSE_TIME(3)            BSD Library Functions Manual            PARSE_TIME(3)
2

NAME

4     parse_time, print_time_table, unparse_time, unparse_time_approx, — parse
5     and unparse time intervals
6

LIBRARY

8     The roken library (libroken, -lroken)
9

SYNOPSIS

11     #include <parse_time.h>
12
13     int
14     parse_time(const char *timespec, const char *def_unit);
15
16     void
17     print_time_table(FILE *f);
18
19     size_t
20     unparse_time(int seconds, char *buf, size_t len);
21
22     size_t
23     unparse_time_approx(int seconds, char *buf, size_t len);
24

DESCRIPTION

26     The parse_time() function converts the period of time specified into a
27     number of seconds.  The timespec can be any number of ⟨number unit⟩ pairs
28     separated by comma and whitespace. The number can be negative. Numbers
29     without explicit units are taken as being def_unit.
30
31     The unparse_time() and unparse_time_approx() do the opposite of
32     parse_time(), that is they take a number of seconds and express that as
33     human readable strings.  unparse_time produces an exact time, while
34     unparse_time_approx restricts the result to include only one unit.
35
36     print_time_table() prints a descriptive list of available units on the
37     passed file descriptor.
38
39     The possible units include:
40           second, s
41           minute, m
42           hour, h
43           day
44           week   seven days
45           month  30 days
46           year   365 days
47
48     Units names can be arbitrarily abbreviated (as long as they are unique).
49

RETURN VALUES

51     parse_time() returns the number of seconds that represents the expression
52     in timespec or -1 on error.  unparse_time() and unparse_time_approx()
53     return the number of characters written to buf.  if the return value is
54     greater than or equal to the len argument, the string was too short and
55     some of the printed characters were discarded.
56

EXAMPLES

58     #include <stdio.h>
59     #include <parse_time.h>
60
61     int
62     main(int argc, char **argv)
63     {
64         int i;
65         int result;
66         char buf[128];
67         print_time_table(stdout);
68         for (i = 1; i < argc; i++) {
69             result = parse_time(argv[i], "second");
70             if(result == -1) {
71                 fprintf(stderr, "%s: parse error\n", argv[i]);
72                 continue;
73             }
74             printf("--\n");
75             printf("parse_time = %d\n", result);
76             unparse_time(result, buf, sizeof(buf));
77             printf("unparse_time = %s\n", buf);
78             unparse_time_approx(result, buf, sizeof(buf));
79             printf("unparse_time_approx = %s\n", buf);
80         }
81         return 0;
82     }
83
84     $ ./a.out "1 minute 30 seconds" "90 s" "1 y -1 s"
85     1   year = 365 days
86     1  month = 30 days
87     1   week = 7 days
88     1    day = 24 hours
89     1   hour = 60 minutes
90     1 minute = 60 seconds
91     1 second
92     --
93     parse_time = 90
94     unparse_time = 1 minute 30 seconds
95     unparse_time_approx = 1 minute
96     --
97     parse_time = 90
98     unparse_time = 1 minute 30 seconds
99     unparse_time_approx = 1 minute
100     --
101     parse_time = 31535999
102     unparse_time = 12 months 4 days 23 hours 59 minutes 59 seconds
103     unparse_time_approx = 12 months
104

BUGS

106     Since parse_time() returns -1 on error there is no way to parse "minus
107     one second".  Currently "s" at the end of units is ignored. This is a
108     hack for English plural forms. If these functions are ever localised,
109     this scheme will have to change.
110
111HEIMDAL                        November 17, 2013                       HEIMDAL
Impressum