Blame src/libopts/parse-duration.h

Packit Service 4684c1
/* Parse a time duration and return a seconds count
Packit Service 4684c1
   Copyright (C) 2008-2018 Free Software Foundation, Inc.
Packit Service 4684c1
   Written by Bruce Korb <bkorb@gnu.org>, 2008.
Packit Service 4684c1
Packit Service 4684c1
   This program is free software: you can redistribute it and/or modify
Packit Service 4684c1
   it under the terms of the GNU Lesser General Public License as published by
Packit Service 4684c1
   the Free Software Foundation; either version 2.1 of the License, or
Packit Service 4684c1
   (at your option) any later version.
Packit Service 4684c1
Packit Service 4684c1
   This program is distributed in the hope that it will be useful,
Packit Service 4684c1
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 4684c1
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 4684c1
   GNU Lesser General Public License for more details.
Packit Service 4684c1
Packit Service 4684c1
   You should have received a copy of the GNU Lesser General Public License
Packit Service 4684c1
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit Service 4684c1
Packit Service 4684c1
/*
Packit Service 4684c1
Packit Service 4684c1
  Readers and users of this function are referred to the ISO-8601
Packit Service 4684c1
  specification, with particular attention to "Durations".
Packit Service 4684c1
Packit Service 4684c1
  At the time of writing, this worked:
Packit Service 4684c1
Packit Service 4684c1
  https://en.wikipedia.org/wiki/ISO_8601#Durations
Packit Service 4684c1
Packit Service 4684c1
  The string must start with a 'P', 'T' or a digit.
Packit Service 4684c1
Packit Service 4684c1
  ==== if it is a digit
Packit Service 4684c1
Packit Service 4684c1
  the string may contain:  NNN Y NNN M NNN W NNN d NNN h NNN m NNN s
Packit Service 4684c1
  This represents NNN years, NNN months, NNN weeks, NNN days, NNN hours,
Packit Service 4684c1
    NNN minutes and NNN seconds.
Packit Service 4684c1
  The embedded white space is optional.
Packit Service 4684c1
  These terms must appear in this order.
Packit Service 4684c1
  Case is significant:  'M' is months and 'm' is minutes.
Packit Service 4684c1
  The final "s" is optional.
Packit Service 4684c1
  All of the terms ("NNN" plus designator) are optional.
Packit Service 4684c1
  Minutes and seconds may optionally be represented as NNN:NNN.
Packit Service 4684c1
  Also, hours, minute and seconds may be represented as NNN:NNN:NNN.
Packit Service 4684c1
  There is no limitation on the value of any of the terms, except
Packit Service 4684c1
  that the final result must fit in a time_t value.
Packit Service 4684c1
Packit Service 4684c1
  ==== if it is a 'P' or 'T', please see ISO-8601 for a rigorous definition.
Packit Service 4684c1
Packit Service 4684c1
  The 'P' term may be followed by any of three formats:
Packit Service 4684c1
    yyyymmdd
Packit Service 4684c1
    yy-mm-dd
Packit Service 4684c1
    yy Y mm M ww W dd D
Packit Service 4684c1
Packit Service 4684c1
  or it may be empty and followed by a 'T'.  The "yyyymmdd" must be eight
Packit Service 4684c1
  digits long.
Packit Service 4684c1
Packit Service 4684c1
  NOTE!  Months are always 30 days and years are always 365 days long.
Packit Service 4684c1
  5 years is always 1825 days, not 1826 or 1827 depending on leap year
Packit Service 4684c1
  considerations.  3 months is always 90 days.  There is no consideration
Packit Service 4684c1
  for how many days are in the current, next or previous months.
Packit Service 4684c1
Packit Service 4684c1
  For the final format:
Packit Service 4684c1
  *  Embedded white space is allowed, but it is optional.
Packit Service 4684c1
  *  All of the terms are optional.  Any or all-but-one may be omitted.
Packit Service 4684c1
  *  The meanings are yy years, mm months, ww weeks and dd days.
Packit Service 4684c1
  *  The terms must appear in this order.
Packit Service 4684c1
Packit Service 4684c1
  ==== The 'T' term may be followed by any of these formats:
Packit Service 4684c1
Packit Service 4684c1
    hhmmss
Packit Service 4684c1
    hh:mm:ss
Packit Service 4684c1
    hh H mm M ss S
Packit Service 4684c1
Packit Service 4684c1
  For the final format:
Packit Service 4684c1
  *  Embedded white space is allowed, but it is optional.
Packit Service 4684c1
  *  All of the terms are optional.  Any or all-but-one may be omitted.
Packit Service 4684c1
  *  The terms must appear in this order.
Packit Service 4684c1
Packit Service 4684c1
 */
Packit Service 4684c1
#ifndef GNULIB_PARSE_DURATION_H
Packit Service 4684c1
#define GNULIB_PARSE_DURATION_H
Packit Service 4684c1
Packit Service 4684c1
#include <time.h>
Packit Service 4684c1
Packit Service 4684c1
/* Return value when a valid duration cannot be parsed.  */
Packit Service 4684c1
#define BAD_TIME        ((time_t)~0)
Packit Service 4684c1
Packit Service 4684c1
/* Parses the given string.  If it has the syntax of a valid duration,
Packit Service 4684c1
   this duration is returned.  Otherwise, the return value is BAD_TIME,
Packit Service 4684c1
   and errno is set to either EINVAL (bad syntax) or ERANGE (out of range).  */
Packit Service 4684c1
extern time_t parse_duration (char const * in_pz);
Packit Service 4684c1
Packit Service 4684c1
#endif /* GNULIB_PARSE_DURATION_H */