1*0a6a1f1dSLionel Sambuc.\" $NetBSD: parse_time.3,v 1.1.1.3 2014/04/24 12:45:52 pettai Exp $ 2ebfedea0SLionel Sambuc.\" 3ebfedea0SLionel Sambuc.\" Copyright (c) 2004 Kungliga Tekniska Högskolan 4ebfedea0SLionel Sambuc.\" (Royal Institute of Technology, Stockholm, Sweden). 5ebfedea0SLionel Sambuc.\" All rights reserved. 6ebfedea0SLionel Sambuc.\" 7ebfedea0SLionel Sambuc.\" Redistribution and use in source and binary forms, with or without 8ebfedea0SLionel Sambuc.\" modification, are permitted provided that the following conditions 9ebfedea0SLionel Sambuc.\" are met: 10ebfedea0SLionel Sambuc.\" 11ebfedea0SLionel Sambuc.\" 1. Redistributions of source code must retain the above copyright 12ebfedea0SLionel Sambuc.\" notice, this list of conditions and the following disclaimer. 13ebfedea0SLionel Sambuc.\" 14ebfedea0SLionel Sambuc.\" 2. Redistributions in binary form must reproduce the above copyright 15ebfedea0SLionel Sambuc.\" notice, this list of conditions and the following disclaimer in the 16ebfedea0SLionel Sambuc.\" documentation and/or other materials provided with the distribution. 17ebfedea0SLionel Sambuc.\" 18ebfedea0SLionel Sambuc.\" 3. Neither the name of the Institute nor the names of its contributors 19ebfedea0SLionel Sambuc.\" may be used to endorse or promote products derived from this software 20ebfedea0SLionel Sambuc.\" without specific prior written permission. 21ebfedea0SLionel Sambuc.\" 22ebfedea0SLionel Sambuc.\" THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 23ebfedea0SLionel Sambuc.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24ebfedea0SLionel Sambuc.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25ebfedea0SLionel Sambuc.\" ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 26ebfedea0SLionel Sambuc.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27ebfedea0SLionel Sambuc.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28ebfedea0SLionel Sambuc.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29ebfedea0SLionel Sambuc.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30ebfedea0SLionel Sambuc.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31ebfedea0SLionel Sambuc.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32ebfedea0SLionel Sambuc.\" SUCH DAMAGE. 33ebfedea0SLionel Sambuc.\" Id 34ebfedea0SLionel Sambuc.\" 35ebfedea0SLionel Sambuc.Dd October 31, 2004 36ebfedea0SLionel Sambuc.Dt PARSE_TIME 3 37ebfedea0SLionel Sambuc.Os 38ebfedea0SLionel Sambuc.Sh NAME 39ebfedea0SLionel Sambuc.Nm parse_time , 40ebfedea0SLionel Sambuc.Nm print_time_table , 41ebfedea0SLionel Sambuc.Nm unparse_time , 42ebfedea0SLionel Sambuc.Nm unparse_time_approx , 43ebfedea0SLionel Sambuc.Nd parse and unparse time intervals 44ebfedea0SLionel Sambuc.Sh LIBRARY 45ebfedea0SLionel SambucThe roken library (libroken, -lroken) 46ebfedea0SLionel Sambuc.Sh SYNOPSIS 47ebfedea0SLionel Sambuc.Fd #include <parse_time.h> 48ebfedea0SLionel Sambuc.Ft int 49ebfedea0SLionel Sambuc.Fn parse_time "const char *timespec" "const char *def_unit" 50ebfedea0SLionel Sambuc.Ft void 51ebfedea0SLionel Sambuc.Fn print_time_table "FILE *f" 52ebfedea0SLionel Sambuc.Ft size_t 53ebfedea0SLionel Sambuc.Fn unparse_time "int seconds" "char *buf" "size_t len" 54ebfedea0SLionel Sambuc.Ft size_t 55ebfedea0SLionel Sambuc.Fn unparse_time_approx "int seconds" "char *buf" "size_t len" 56ebfedea0SLionel Sambuc.Sh DESCRIPTION 57ebfedea0SLionel SambucThe 58ebfedea0SLionel Sambuc.Fn parse_time 59ebfedea0SLionel Sambucfunction converts a the period of time specified in 60ebfedea0SLionel Sambucinto a number of seconds. 61ebfedea0SLionel SambucThe 62ebfedea0SLionel Sambuc.Fa timespec 63ebfedea0SLionel Sambuccan be any number of 64ebfedea0SLionel Sambuc.Aq number unit 65ebfedea0SLionel Sambucpairs separated by comma and whitespace. The number can be 66ebfedea0SLionel Sambucnegative. Number without explicit units are taken as being 67ebfedea0SLionel Sambuc.Fa def_unit . 68ebfedea0SLionel Sambuc.Pp 69ebfedea0SLionel SambucThe 70ebfedea0SLionel Sambuc.Fn unparse_time 71ebfedea0SLionel Sambucand 72ebfedea0SLionel Sambuc.Fn unparse_time_approx 73ebfedea0SLionel Sambucdoes the opposite of 74ebfedea0SLionel Sambuc.Fn parse_time , 75ebfedea0SLionel Sambucthat is they take a number of seconds and express that as human 76ebfedea0SLionel Sambucreadable string. 77ebfedea0SLionel Sambuc.Fa unparse_time 78ebfedea0SLionel Sambucproduces an exact time, while 79ebfedea0SLionel Sambuc.Fa unparse_time_approx 80ebfedea0SLionel Sambucrestricts the result to only include one units. 81ebfedea0SLionel Sambuc.Pp 82ebfedea0SLionel Sambuc.Fn print_time_table 83ebfedea0SLionel Sambucprints a descriptive list of available units on the passed file 84ebfedea0SLionel Sambucdescriptor. 85ebfedea0SLionel Sambuc.Pp 86ebfedea0SLionel SambucThe possible units include: 87ebfedea0SLionel Sambuc.Bl -tag -width "month" -compact -offset indent 88ebfedea0SLionel Sambuc.It Li second , s 89ebfedea0SLionel Sambuc.It Li minute , m 90ebfedea0SLionel Sambuc.It Li hour , h 91ebfedea0SLionel Sambuc.It day 92ebfedea0SLionel Sambuc.It week 93ebfedea0SLionel Sambucseven days 94ebfedea0SLionel Sambuc.It month 95ebfedea0SLionel Sambuc30 days 96ebfedea0SLionel Sambuc.It year 97ebfedea0SLionel Sambuc365 days 98ebfedea0SLionel Sambuc.El 99ebfedea0SLionel Sambuc.Pp 100ebfedea0SLionel SambucUnits names can be arbitrarily abbreviated (as long as they are 101ebfedea0SLionel Sambucunique). 102ebfedea0SLionel Sambuc.Sh RETURN VALUES 103ebfedea0SLionel Sambuc.Fn parse_time 104ebfedea0SLionel Sambucreturns the number of seconds that represents the expression in 105ebfedea0SLionel Sambuc.Fa timespec 106ebfedea0SLionel Sambucor -1 on error. 107ebfedea0SLionel Sambuc.Fn unparse_time 108ebfedea0SLionel Sambucand 109ebfedea0SLionel Sambuc.Fn unparse_time_approx 110ebfedea0SLionel Sambucreturn the number of characters written to 111ebfedea0SLionel Sambuc.Fa buf . 112ebfedea0SLionel Sambucif the return value is greater than or equal to the 113ebfedea0SLionel Sambuc.Fa len 114ebfedea0SLionel Sambucargument, the string was too short and some of the printed characters 115ebfedea0SLionel Sambucwere discarded. 116ebfedea0SLionel Sambuc.Sh EXAMPLES 117ebfedea0SLionel Sambuc.Bd -literal 118ebfedea0SLionel Sambuc#include <stdio.h> 119ebfedea0SLionel Sambuc#include <parse_time.h> 120ebfedea0SLionel Sambuc 121ebfedea0SLionel Sambucint 122ebfedea0SLionel Sambucmain(int argc, char **argv) 123ebfedea0SLionel Sambuc{ 124ebfedea0SLionel Sambuc int i; 125ebfedea0SLionel Sambuc int result; 126ebfedea0SLionel Sambuc char buf[128]; 127ebfedea0SLionel Sambuc print_time_table(stdout); 128ebfedea0SLionel Sambuc for (i = 1; i < argc; i++) { 129ebfedea0SLionel Sambuc result = parse_time(argv[i], "second"); 130ebfedea0SLionel Sambuc if(result == -1) { 131ebfedea0SLionel Sambuc fprintf(stderr, "%s: parse error\\n", argv[i]); 132ebfedea0SLionel Sambuc continue; 133ebfedea0SLionel Sambuc } 134ebfedea0SLionel Sambuc printf("--\\n"); 135ebfedea0SLionel Sambuc printf("parse_time = %d\\n", result); 136ebfedea0SLionel Sambuc unparse_time(result, buf, sizeof(buf)); 137ebfedea0SLionel Sambuc printf("unparse_time = %s\\n", buf); 138ebfedea0SLionel Sambuc unparse_time_approx(result, buf, sizeof(buf)); 139ebfedea0SLionel Sambuc printf("unparse_time_approx = %s\\n", buf); 140ebfedea0SLionel Sambuc } 141ebfedea0SLionel Sambuc return 0; 142ebfedea0SLionel Sambuc} 143ebfedea0SLionel Sambuc.Ed 144ebfedea0SLionel Sambuc.Bd -literal 145ebfedea0SLionel Sambuc$ ./a.out "1 minute 30 seconds" "90 s" "1 y -1 s" 146ebfedea0SLionel Sambuc1 year = 365 days 147ebfedea0SLionel Sambuc1 month = 30 days 148ebfedea0SLionel Sambuc1 week = 7 days 149ebfedea0SLionel Sambuc1 day = 24 hours 150ebfedea0SLionel Sambuc1 hour = 60 minutes 151ebfedea0SLionel Sambuc1 minute = 60 seconds 152ebfedea0SLionel Sambuc1 second 153ebfedea0SLionel Sambuc-- 154ebfedea0SLionel Sambucparse_time = 90 155ebfedea0SLionel Sambucunparse_time = 1 minute 30 seconds 156ebfedea0SLionel Sambucunparse_time_approx = 1 minute 157ebfedea0SLionel Sambuc-- 158ebfedea0SLionel Sambucparse_time = 90 159ebfedea0SLionel Sambucunparse_time = 1 minute 30 seconds 160ebfedea0SLionel Sambucunparse_time_approx = 1 minute 161ebfedea0SLionel Sambuc-- 162ebfedea0SLionel Sambucparse_time = 31535999 163ebfedea0SLionel Sambucunparse_time = 12 months 4 days 23 hours 59 minutes 59 seconds 164ebfedea0SLionel Sambucunparse_time_approx = 12 months 165ebfedea0SLionel Sambuc.Ed 166ebfedea0SLionel Sambuc.Sh BUGS 167ebfedea0SLionel SambucSince 168ebfedea0SLionel Sambuc.Fn parse_time 169ebfedea0SLionel Sambucreturns -1 on error there is no way to parse "minus one second". 170ebfedea0SLionel SambucCurrently "s" at the end of units is ignored. This is a hack for 171ebfedea0SLionel SambucEnglish plural forms. If these functions are ever localised, this 172ebfedea0SLionel Sambucscheme will have to change. 173ebfedea0SLionel Sambuc.\".Sh SEE ALSO 174ebfedea0SLionel Sambuc.\".Xr parse_bytes 3 175ebfedea0SLionel Sambuc.\".Xr parse_units 3 176