parse-duration.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 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 General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://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. http://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 d NNN h NNN m NNN s
  22. This represents NNN days, NNN hours, NNN minutes and NNN seconds.
  23. The embeded white space is optional.
  24. These terms must appear in this order.
  25. The final "s" is optional.
  26. All of the terms ("NNN" plus designator) are optional.
  27. Minutes and seconds may optionally be represented as NNN:NNN.
  28. Also, hours, minute and seconds may be represented as NNN:NNN:NNN.
  29. There is no limitation on the value of any of the terms, except
  30. that the final result must fit in a time_t value.
  31. ==== if it is a 'P' or 'T', please see ISO-8601 for a rigorous definition.
  32. The 'P' term may be followed by any of three formats:
  33. yyyymmdd
  34. yy-mm-dd
  35. yy Y mm M ww W dd D
  36. or it may be empty and followed by a 'T'. The "yyyymmdd" must be eight
  37. digits long. Note: months are always 30 days and years are always 365
  38. days long. 5 years is always 1825, not 1826 or 1827 depending on leap
  39. year considerations. 3 months is always 90 days. There is no consideration
  40. for how many days are in the current, next or previous months.
  41. For the final format:
  42. * Embedded white space is allowed, but it is optional.
  43. * All of the terms are optional. Any or all-but-one may be omitted.
  44. * The meanings are yy years, mm months, ww weeks and dd days.
  45. * The terms must appear in this order.
  46. ==== The 'T' term may be followed by any of these formats:
  47. hhmmss
  48. hh:mm:ss
  49. hh H mm M ss S
  50. For the final format:
  51. * Embedded white space is allowed, but it is optional.
  52. * All of the terms are optional. Any or all-but-one may be omitted.
  53. * The terms must appear in this order.
  54. */
  55. #ifndef GNULIB_PARSE_DURATION_H
  56. #define GNULIB_PARSE_DURATION_H
  57. #include <time.h>
  58. #define BAD_TIME ((time_t)~0)
  59. extern time_t parse_duration(char const * in_pz);
  60. #endif /* GNULIB_PARSE_DURATION_H */