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