iso8601.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """Originally taken from pyiso8601 (http://code.google.com/p/pyiso8601/)
  2. Modified to match the behavior of dateutil.parser:
  3. - raise ValueError instead of ParseError
  4. - return naive datetimes by default
  5. - uses pytz.FixedOffset
  6. This is the original License:
  7. Copyright (c) 2007 Michael Twomey
  8. Permission is hereby granted, free of charge, to any person obtaining a
  9. copy of this software and associated documentation files (the
  10. "Software"), to deal in the Software without restriction, including
  11. without limitation the rights to use, copy, modify, merge, publish,
  12. distribute, sublicense, and/or sell copies of the Software, and to
  13. permit persons to whom the Software is furnished to do so, subject to
  14. the following conditions:
  15. The above copyright notice and this permission notice shall be included
  16. in all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. """
  25. from __future__ import absolute_import, unicode_literals
  26. import re
  27. from datetime import datetime
  28. from pytz import FixedOffset
  29. __all__ = ['parse_iso8601']
  30. # Adapted from http://delete.me.uk/2005/03/iso8601.html
  31. ISO8601_REGEX = re.compile(
  32. r'(?P<year>[0-9]{4})(-(?P<month>[0-9]{1,2})(-(?P<day>[0-9]{1,2})'
  33. r'((?P<separator>.)(?P<hour>[0-9]{2}):(?P<minute>[0-9]{2})'
  34. '(:(?P<second>[0-9]{2})(\.(?P<fraction>[0-9]+))?)?'
  35. r'(?P<timezone>Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?'
  36. )
  37. TIMEZONE_REGEX = re.compile(
  38. '(?P<prefix>[+-])(?P<hours>[0-9]{2}).(?P<minutes>[0-9]{2})'
  39. )
  40. def parse_iso8601(datestring):
  41. """Parse and convert ISO 8601 string into a datetime object"""
  42. m = ISO8601_REGEX.match(datestring)
  43. if not m:
  44. raise ValueError('unable to parse date string %r' % datestring)
  45. groups = m.groupdict()
  46. tz = groups['timezone']
  47. if tz == 'Z':
  48. tz = FixedOffset(0)
  49. elif tz:
  50. m = TIMEZONE_REGEX.match(tz)
  51. prefix, hours, minutes = m.groups()
  52. hours, minutes = int(hours), int(minutes)
  53. if prefix == '-':
  54. hours = -hours
  55. minutes = -minutes
  56. tz = FixedOffset(minutes + hours * 60)
  57. return datetime(
  58. int(groups['year']), int(groups['month']),
  59. int(groups['day']), int(groups['hour'] or 0),
  60. int(groups['minute'] or 0), int(groups['second'] or 0),
  61. int(groups['fraction'] or 0), tz
  62. )