iso8601.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """
  2. Originally taken from pyiso8601 (http://code.google.com/p/pyiso8601/)
  3. Modified to match the behavior of dateutil.parser:
  4. - raise ValueError instead of ParseError
  5. - returns naive datetimes by default
  6. - uses pytz.FixedOffset
  7. This is the original License:
  8. Copyright (c) 2007 Michael Twomey
  9. Permission is hereby granted, free of charge, to any person obtaining a
  10. copy of this software and associated documentation files (the
  11. "Software"), to deal in the Software without restriction, including
  12. without limitation the rights to use, copy, modify, merge, publish,
  13. distribute, sublicense, and/or sell copies of the Software, and to
  14. permit persons to whom the Software is furnished to do so, subject to
  15. the following conditions:
  16. The above copyright notice and this permission notice shall be included
  17. in all copies or substantial portions of the Software.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  22. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. """
  26. from __future__ import absolute_import
  27. import re
  28. from datetime import datetime
  29. from pytz import FixedOffset
  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. """Parses ISO 8601 dates into datetime objects"""
  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 and tz != 'Z':
  48. m = TIMEZONE_REGEX.match(tz)
  49. prefix, hours, minutes = m.groups()
  50. hours, minutes = int(hours), int(minutes)
  51. if prefix == '-':
  52. hours = -hours
  53. minutes = -minutes
  54. tz = FixedOffset(minutes + hours * 60)
  55. frac = groups['fraction']
  56. groups['fraction'] = int(float('0.%s' % frac) * 1e6) if frac else 0
  57. return datetime(
  58. int(groups['year']), int(groups['month']), int(groups['day']),
  59. int(groups['hour']), int(groups['minute']), int(groups['second']),
  60. int(groups['fraction']), tz
  61. )