1 .\" Copyright (c) 2004 Kungliga Tekniska Högskolan
2 .\" (Royal Institute of Technology, Stockholm, Sweden).
3 .\" All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\" notice, this list of conditions and the following disclaimer in the
14 .\" documentation and/or other materials provided with the distribution.
16 .\" 3. Neither the name of the Institute nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 .Nm print_time_table ,
40 .Nm unparse_time_approx ,
41 .Nd parse and unparse time intervals
43 The roken library (libroken, -lroken)
45 .Fd #include <parse_time.h>
47 .Fn parse_time "const char *timespec" "const char *def_unit"
49 .Fn print_time_table "FILE *f"
51 .Fn unparse_time "int seconds" "char *buf" "size_t len"
53 .Fn unparse_time_approx "int seconds" "char *buf" "size_t len"
57 function converts a the period of time specified in
58 into a number of seconds.
63 pairs separated by comma and whitespace. The number can be
64 negative. Number without explicit units are taken as being
70 .Fn unparse_time_approx
73 that is they take a number of seconds and express that as human
76 produces an exact time, while
77 .Fa unparse_time_approx
78 restricts the result to only include one units.
81 prints a descriptive list of available units on the passed file
84 The possible units include:
85 .Bl -tag -width "month" -compact -offset indent
98 Units names can be arbitrarily abbreviated (as long as they are
102 returns the number of seconds that represents the expression in
107 .Fn unparse_time_approx
108 return the number of characters written to
110 if the return value is greater than or equal to the
112 argument, the string was too short and some of the printed characters
117 #include <parse_time.h>
120 main(int argc, char **argv)
125 print_time_table(stdout);
126 for (i = 1; i < argc; i++) {
127 result = parse_time(argv[i], "second");
129 fprintf(stderr, "%s: parse error\\n", argv[i]);
133 printf("parse_time = %d\\n", result);
134 unparse_time(result, buf, sizeof(buf));
135 printf("unparse_time = %s\\n", buf);
136 unparse_time_approx(result, buf, sizeof(buf));
137 printf("unparse_time_approx = %s\\n", buf);
143 $ ./a.out "1 minute 30 seconds" "90 s" "1 y -1 s"
149 1 minute = 60 seconds
153 unparse_time = 1 minute 30 seconds
154 unparse_time_approx = 1 minute
157 unparse_time = 1 minute 30 seconds
158 unparse_time_approx = 1 minute
160 parse_time = 31535999
161 unparse_time = 12 months 4 days 23 hours 59 minutes 59 seconds
162 unparse_time_approx = 12 months
167 returns -1 on error there is no way to parse "minus one second".
168 Currently "s" at the end of units is ignored. This is a hack for
169 English plural forms. If these functions are ever localised, this
170 scheme will have to change.