xref: /freebsd/contrib/ntp/sntp/libopts/parse-duration.h (revision 2b15cb3d0922bd70ea592f0da9b4a5b167f4d53f)
1*2b15cb3dSCy Schubert /* Parse a time duration and return a seconds count
2*2b15cb3dSCy Schubert    Copyright (C) 2008-2014 Free Software Foundation, Inc.
3*2b15cb3dSCy Schubert    Written by Bruce Korb <bkorb@gnu.org>, 2008.
4*2b15cb3dSCy Schubert 
5*2b15cb3dSCy Schubert    This program is free software: you can redistribute it and/or modify
6*2b15cb3dSCy Schubert    it under the terms of the GNU Lesser General Public License as published by
7*2b15cb3dSCy Schubert    the Free Software Foundation; either version 2.1 of the License, or
8*2b15cb3dSCy Schubert    (at your option) any later version.
9*2b15cb3dSCy Schubert 
10*2b15cb3dSCy Schubert    This program is distributed in the hope that it will be useful,
11*2b15cb3dSCy Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*2b15cb3dSCy Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*2b15cb3dSCy Schubert    GNU Lesser General Public License for more details.
14*2b15cb3dSCy Schubert 
15*2b15cb3dSCy Schubert    You should have received a copy of the GNU Lesser General Public License
16*2b15cb3dSCy Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17*2b15cb3dSCy Schubert 
18*2b15cb3dSCy Schubert /*
19*2b15cb3dSCy Schubert 
20*2b15cb3dSCy Schubert   Readers and users of this function are referred to the ISO-8601
21*2b15cb3dSCy Schubert   specification, with particular attention to "Durations".
22*2b15cb3dSCy Schubert 
23*2b15cb3dSCy Schubert   At the time of writing, this worked:
24*2b15cb3dSCy Schubert 
25*2b15cb3dSCy Schubert   http://en.wikipedia.org/wiki/ISO_8601#Durations
26*2b15cb3dSCy Schubert 
27*2b15cb3dSCy Schubert   The string must start with a 'P', 'T' or a digit.
28*2b15cb3dSCy Schubert 
29*2b15cb3dSCy Schubert   ==== if it is a digit
30*2b15cb3dSCy Schubert 
31*2b15cb3dSCy Schubert   the string may contain:  NNN Y NNN M NNN W NNN d NNN h NNN m NNN s
32*2b15cb3dSCy Schubert   This represents NNN years, NNN months, NNN weeks, NNN days, NNN hours,
33*2b15cb3dSCy Schubert     NNN minutes and NNN seconds.
34*2b15cb3dSCy Schubert   The embedded white space is optional.
35*2b15cb3dSCy Schubert   These terms must appear in this order.
36*2b15cb3dSCy Schubert   Case is significant:  'M' is months and 'm' is minutes.
37*2b15cb3dSCy Schubert   The final "s" is optional.
38*2b15cb3dSCy Schubert   All of the terms ("NNN" plus designator) are optional.
39*2b15cb3dSCy Schubert   Minutes and seconds may optionally be represented as NNN:NNN.
40*2b15cb3dSCy Schubert   Also, hours, minute and seconds may be represented as NNN:NNN:NNN.
41*2b15cb3dSCy Schubert   There is no limitation on the value of any of the terms, except
42*2b15cb3dSCy Schubert   that the final result must fit in a time_t value.
43*2b15cb3dSCy Schubert 
44*2b15cb3dSCy Schubert   ==== if it is a 'P' or 'T', please see ISO-8601 for a rigorous definition.
45*2b15cb3dSCy Schubert 
46*2b15cb3dSCy Schubert   The 'P' term may be followed by any of three formats:
47*2b15cb3dSCy Schubert     yyyymmdd
48*2b15cb3dSCy Schubert     yy-mm-dd
49*2b15cb3dSCy Schubert     yy Y mm M ww W dd D
50*2b15cb3dSCy Schubert 
51*2b15cb3dSCy Schubert   or it may be empty and followed by a 'T'.  The "yyyymmdd" must be eight
52*2b15cb3dSCy Schubert   digits long.
53*2b15cb3dSCy Schubert 
54*2b15cb3dSCy Schubert   NOTE!  Months are always 30 days and years are always 365 days long.
55*2b15cb3dSCy Schubert   5 years is always 1825 days, not 1826 or 1827 depending on leap year
56*2b15cb3dSCy Schubert   considerations.  3 months is always 90 days.  There is no consideration
57*2b15cb3dSCy Schubert   for how many days are in the current, next or previous months.
58*2b15cb3dSCy Schubert 
59*2b15cb3dSCy Schubert   For the final format:
60*2b15cb3dSCy Schubert   *  Embedded white space is allowed, but it is optional.
61*2b15cb3dSCy Schubert   *  All of the terms are optional.  Any or all-but-one may be omitted.
62*2b15cb3dSCy Schubert   *  The meanings are yy years, mm months, ww weeks and dd days.
63*2b15cb3dSCy Schubert   *  The terms must appear in this order.
64*2b15cb3dSCy Schubert 
65*2b15cb3dSCy Schubert   ==== The 'T' term may be followed by any of these formats:
66*2b15cb3dSCy Schubert 
67*2b15cb3dSCy Schubert     hhmmss
68*2b15cb3dSCy Schubert     hh:mm:ss
69*2b15cb3dSCy Schubert     hh H mm M ss S
70*2b15cb3dSCy Schubert 
71*2b15cb3dSCy Schubert   For the final format:
72*2b15cb3dSCy Schubert   *  Embedded white space is allowed, but it is optional.
73*2b15cb3dSCy Schubert   *  All of the terms are optional.  Any or all-but-one may be omitted.
74*2b15cb3dSCy Schubert   *  The terms must appear in this order.
75*2b15cb3dSCy Schubert 
76*2b15cb3dSCy Schubert  */
77*2b15cb3dSCy Schubert #ifndef GNULIB_PARSE_DURATION_H
78*2b15cb3dSCy Schubert #define GNULIB_PARSE_DURATION_H
79*2b15cb3dSCy Schubert 
80*2b15cb3dSCy Schubert #include <time.h>
81*2b15cb3dSCy Schubert 
82*2b15cb3dSCy Schubert /* Return value when a valid duration cannot be parsed.  */
83*2b15cb3dSCy Schubert #define BAD_TIME        ((time_t)~0)
84*2b15cb3dSCy Schubert 
85*2b15cb3dSCy Schubert /* Parses the given string.  If it has the syntax of a valid duration,
86*2b15cb3dSCy Schubert    this duration is returned.  Otherwise, the return value is BAD_TIME,
87*2b15cb3dSCy Schubert    and errno is set to either EINVAL (bad syntax) or ERANGE (out of range).  */
88*2b15cb3dSCy Schubert extern time_t parse_duration (char const * in_pz);
89*2b15cb3dSCy Schubert 
90*2b15cb3dSCy Schubert #endif /* GNULIB_PARSE_DURATION_H */
91