1
0

parse-duration.h 3.3 KB

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