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