1258945Sroberto/* Parse a time duration and return a seconds count
2285169Scy   Copyright (C) 2008-2015 Free Software Foundation, Inc.
3258945Sroberto   Written by Bruce Korb <bkorb@gnu.org>, 2008.
4258945Sroberto
5258945Sroberto   This program is free software: you can redistribute it and/or modify
6280849Scy   it under the terms of the GNU Lesser General Public License as published by
7280849Scy   the Free Software Foundation; either version 2.1 of the License, or
8258945Sroberto   (at your option) any later version.
9258945Sroberto
10258945Sroberto   This program is distributed in the hope that it will be useful,
11258945Sroberto   but WITHOUT ANY WARRANTY; without even the implied warranty of
12258945Sroberto   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13280849Scy   GNU Lesser General Public License for more details.
14258945Sroberto
15280849Scy   You should have received a copy of the GNU Lesser General Public License
16258945Sroberto   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17258945Sroberto
18258945Sroberto/*
19258945Sroberto
20258945Sroberto  Readers and users of this function are referred to the ISO-8601
21258945Sroberto  specification, with particular attention to "Durations".
22258945Sroberto
23258945Sroberto  At the time of writing, this worked:
24258945Sroberto
25258945Sroberto  http://en.wikipedia.org/wiki/ISO_8601#Durations
26258945Sroberto
27258945Sroberto  The string must start with a 'P', 'T' or a digit.
28258945Sroberto
29258945Sroberto  ==== if it is a digit
30258945Sroberto
31258945Sroberto  the string may contain:  NNN Y NNN M NNN W NNN d NNN h NNN m NNN s
32258945Sroberto  This represents NNN years, NNN months, NNN weeks, NNN days, NNN hours,
33258945Sroberto    NNN minutes and NNN seconds.
34280849Scy  The embedded white space is optional.
35258945Sroberto  These terms must appear in this order.
36258945Sroberto  Case is significant:  'M' is months and 'm' is minutes.
37258945Sroberto  The final "s" is optional.
38258945Sroberto  All of the terms ("NNN" plus designator) are optional.
39258945Sroberto  Minutes and seconds may optionally be represented as NNN:NNN.
40258945Sroberto  Also, hours, minute and seconds may be represented as NNN:NNN:NNN.
41258945Sroberto  There is no limitation on the value of any of the terms, except
42258945Sroberto  that the final result must fit in a time_t value.
43258945Sroberto
44258945Sroberto  ==== if it is a 'P' or 'T', please see ISO-8601 for a rigorous definition.
45258945Sroberto
46258945Sroberto  The 'P' term may be followed by any of three formats:
47258945Sroberto    yyyymmdd
48258945Sroberto    yy-mm-dd
49258945Sroberto    yy Y mm M ww W dd D
50258945Sroberto
51258945Sroberto  or it may be empty and followed by a 'T'.  The "yyyymmdd" must be eight
52258945Sroberto  digits long.
53258945Sroberto
54258945Sroberto  NOTE!  Months are always 30 days and years are always 365 days long.
55258945Sroberto  5 years is always 1825 days, not 1826 or 1827 depending on leap year
56258945Sroberto  considerations.  3 months is always 90 days.  There is no consideration
57258945Sroberto  for how many days are in the current, next or previous months.
58258945Sroberto
59258945Sroberto  For the final format:
60258945Sroberto  *  Embedded white space is allowed, but it is optional.
61258945Sroberto  *  All of the terms are optional.  Any or all-but-one may be omitted.
62258945Sroberto  *  The meanings are yy years, mm months, ww weeks and dd days.
63258945Sroberto  *  The terms must appear in this order.
64258945Sroberto
65258945Sroberto  ==== The 'T' term may be followed by any of these formats:
66258945Sroberto
67258945Sroberto    hhmmss
68258945Sroberto    hh:mm:ss
69258945Sroberto    hh H mm M ss S
70258945Sroberto
71258945Sroberto  For the final format:
72258945Sroberto  *  Embedded white space is allowed, but it is optional.
73258945Sroberto  *  All of the terms are optional.  Any or all-but-one may be omitted.
74258945Sroberto  *  The terms must appear in this order.
75258945Sroberto
76258945Sroberto */
77258945Sroberto#ifndef GNULIB_PARSE_DURATION_H
78258945Sroberto#define GNULIB_PARSE_DURATION_H
79258945Sroberto
80258945Sroberto#include <time.h>
81258945Sroberto
82258945Sroberto/* Return value when a valid duration cannot be parsed.  */
83258945Sroberto#define BAD_TIME        ((time_t)~0)
84258945Sroberto
85258945Sroberto/* Parses the given string.  If it has the syntax of a valid duration,
86258945Sroberto   this duration is returned.  Otherwise, the return value is BAD_TIME,
87258945Sroberto   and errno is set to either EINVAL (bad syntax) or ERANGE (out of range).  */
88258945Srobertoextern time_t parse_duration (char const * in_pz);
89258945Sroberto
90258945Sroberto#endif /* GNULIB_PARSE_DURATION_H */
91