isoparser.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. # -*- coding: utf-8 -*-
  2. """
  3. This module offers a parser for ISO-8601 strings
  4. It is intended to support all valid date, time and datetime formats per the
  5. ISO-8601 specification.
  6. ..versionadded:: 2.7.0
  7. """
  8. from datetime import datetime, timedelta, time, date
  9. import calendar
  10. from dateutil import tz
  11. from functools import wraps
  12. import re
  13. import six
  14. __all__ = ["isoparse", "isoparser"]
  15. def _takes_ascii(f):
  16. @wraps(f)
  17. def func(self, str_in, *args, **kwargs):
  18. # If it's a stream, read the whole thing
  19. str_in = getattr(str_in, 'read', lambda: str_in)()
  20. # If it's unicode, turn it into bytes, since ISO-8601 only covers ASCII
  21. if isinstance(str_in, six.text_type):
  22. # ASCII is the same in UTF-8
  23. try:
  24. str_in = str_in.encode('ascii')
  25. except UnicodeEncodeError as e:
  26. msg = 'ISO-8601 strings should contain only ASCII characters'
  27. six.raise_from(ValueError(msg), e)
  28. return f(self, str_in, *args, **kwargs)
  29. return func
  30. class isoparser(object):
  31. def __init__(self, sep=None):
  32. """
  33. :param sep:
  34. A single character that separates date and time portions. If
  35. ``None``, the parser will accept any single character.
  36. For strict ISO-8601 adherence, pass ``'T'``.
  37. """
  38. if sep is not None:
  39. if (len(sep) != 1 or ord(sep) >= 128 or sep in '0123456789'):
  40. raise ValueError('Separator must be a single, non-numeric ' +
  41. 'ASCII character')
  42. sep = sep.encode('ascii')
  43. self._sep = sep
  44. @_takes_ascii
  45. def isoparse(self, dt_str):
  46. """
  47. Parse an ISO-8601 datetime string into a :class:`datetime.datetime`.
  48. An ISO-8601 datetime string consists of a date portion, followed
  49. optionally by a time portion - the date and time portions are separated
  50. by a single character separator, which is ``T`` in the official
  51. standard. Incomplete date formats (such as ``YYYY-MM``) may *not* be
  52. combined with a time portion.
  53. Supported date formats are:
  54. Common:
  55. - ``YYYY``
  56. - ``YYYY-MM``
  57. - ``YYYY-MM-DD`` or ``YYYYMMDD``
  58. Uncommon:
  59. - ``YYYY-Www`` or ``YYYYWww`` - ISO week (day defaults to 0)
  60. - ``YYYY-Www-D`` or ``YYYYWwwD`` - ISO week and day
  61. The ISO week and day numbering follows the same logic as
  62. :func:`datetime.date.isocalendar`.
  63. Supported time formats are:
  64. - ``hh``
  65. - ``hh:mm`` or ``hhmm``
  66. - ``hh:mm:ss`` or ``hhmmss``
  67. - ``hh:mm:ss.ssssss`` (Up to 6 sub-second digits)
  68. Midnight is a special case for `hh`, as the standard supports both
  69. 00:00 and 24:00 as a representation. The decimal separator can be
  70. either a dot or a comma.
  71. .. caution::
  72. Support for fractional components other than seconds is part of the
  73. ISO-8601 standard, but is not currently implemented in this parser.
  74. Supported time zone offset formats are:
  75. - `Z` (UTC)
  76. - `±HH:MM`
  77. - `±HHMM`
  78. - `±HH`
  79. Offsets will be represented as :class:`dateutil.tz.tzoffset` objects,
  80. with the exception of UTC, which will be represented as
  81. :class:`dateutil.tz.tzutc`. Time zone offsets equivalent to UTC (such
  82. as `+00:00`) will also be represented as :class:`dateutil.tz.tzutc`.
  83. :param dt_str:
  84. A string or stream containing only an ISO-8601 datetime string
  85. :return:
  86. Returns a :class:`datetime.datetime` representing the string.
  87. Unspecified components default to their lowest value.
  88. .. warning::
  89. As of version 2.7.0, the strictness of the parser should not be
  90. considered a stable part of the contract. Any valid ISO-8601 string
  91. that parses correctly with the default settings will continue to
  92. parse correctly in future versions, but invalid strings that
  93. currently fail (e.g. ``2017-01-01T00:00+00:00:00``) are not
  94. guaranteed to continue failing in future versions if they encode
  95. a valid date.
  96. .. versionadded:: 2.7.0
  97. """
  98. components, pos = self._parse_isodate(dt_str)
  99. if len(dt_str) > pos:
  100. if self._sep is None or dt_str[pos:pos + 1] == self._sep:
  101. components += self._parse_isotime(dt_str[pos + 1:])
  102. else:
  103. raise ValueError('String contains unknown ISO components')
  104. if len(components) > 3 and components[3] == 24:
  105. components[3] = 0
  106. return datetime(*components) + timedelta(days=1)
  107. return datetime(*components)
  108. @_takes_ascii
  109. def parse_isodate(self, datestr):
  110. """
  111. Parse the date portion of an ISO string.
  112. :param datestr:
  113. The string portion of an ISO string, without a separator
  114. :return:
  115. Returns a :class:`datetime.date` object
  116. """
  117. components, pos = self._parse_isodate(datestr)
  118. if pos < len(datestr):
  119. raise ValueError('String contains unknown ISO ' +
  120. 'components: {!r}'.format(datestr.decode('ascii')))
  121. return date(*components)
  122. @_takes_ascii
  123. def parse_isotime(self, timestr):
  124. """
  125. Parse the time portion of an ISO string.
  126. :param timestr:
  127. The time portion of an ISO string, without a separator
  128. :return:
  129. Returns a :class:`datetime.time` object
  130. """
  131. components = self._parse_isotime(timestr)
  132. if components[0] == 24:
  133. components[0] = 0
  134. return time(*components)
  135. @_takes_ascii
  136. def parse_tzstr(self, tzstr, zero_as_utc=True):
  137. """
  138. Parse a valid ISO time zone string.
  139. See :func:`isoparser.isoparse` for details on supported formats.
  140. :param tzstr:
  141. A string representing an ISO time zone offset
  142. :param zero_as_utc:
  143. Whether to return :class:`dateutil.tz.tzutc` for zero-offset zones
  144. :return:
  145. Returns :class:`dateutil.tz.tzoffset` for offsets and
  146. :class:`dateutil.tz.tzutc` for ``Z`` and (if ``zero_as_utc`` is
  147. specified) offsets equivalent to UTC.
  148. """
  149. return self._parse_tzstr(tzstr, zero_as_utc=zero_as_utc)
  150. # Constants
  151. _DATE_SEP = b'-'
  152. _TIME_SEP = b':'
  153. _FRACTION_REGEX = re.compile(b'[\\.,]([0-9]+)')
  154. def _parse_isodate(self, dt_str):
  155. try:
  156. return self._parse_isodate_common(dt_str)
  157. except ValueError:
  158. return self._parse_isodate_uncommon(dt_str)
  159. def _parse_isodate_common(self, dt_str):
  160. len_str = len(dt_str)
  161. components = [1, 1, 1]
  162. if len_str < 4:
  163. raise ValueError('ISO string too short')
  164. # Year
  165. components[0] = int(dt_str[0:4])
  166. pos = 4
  167. if pos >= len_str:
  168. return components, pos
  169. has_sep = dt_str[pos:pos + 1] == self._DATE_SEP
  170. if has_sep:
  171. pos += 1
  172. # Month
  173. if len_str - pos < 2:
  174. raise ValueError('Invalid common month')
  175. components[1] = int(dt_str[pos:pos + 2])
  176. pos += 2
  177. if pos >= len_str:
  178. if has_sep:
  179. return components, pos
  180. else:
  181. raise ValueError('Invalid ISO format')
  182. if has_sep:
  183. if dt_str[pos:pos + 1] != self._DATE_SEP:
  184. raise ValueError('Invalid separator in ISO string')
  185. pos += 1
  186. # Day
  187. if len_str - pos < 2:
  188. raise ValueError('Invalid common day')
  189. components[2] = int(dt_str[pos:pos + 2])
  190. return components, pos + 2
  191. def _parse_isodate_uncommon(self, dt_str):
  192. if len(dt_str) < 4:
  193. raise ValueError('ISO string too short')
  194. # All ISO formats start with the year
  195. year = int(dt_str[0:4])
  196. has_sep = dt_str[4:5] == self._DATE_SEP
  197. pos = 4 + has_sep # Skip '-' if it's there
  198. if dt_str[pos:pos + 1] == b'W':
  199. # YYYY-?Www-?D?
  200. pos += 1
  201. weekno = int(dt_str[pos:pos + 2])
  202. pos += 2
  203. dayno = 1
  204. if len(dt_str) > pos:
  205. if (dt_str[pos:pos + 1] == self._DATE_SEP) != has_sep:
  206. raise ValueError('Inconsistent use of dash separator')
  207. pos += has_sep
  208. dayno = int(dt_str[pos:pos + 1])
  209. pos += 1
  210. base_date = self._calculate_weekdate(year, weekno, dayno)
  211. else:
  212. # YYYYDDD or YYYY-DDD
  213. if len(dt_str) - pos < 3:
  214. raise ValueError('Invalid ordinal day')
  215. ordinal_day = int(dt_str[pos:pos + 3])
  216. pos += 3
  217. if ordinal_day < 1 or ordinal_day > (365 + calendar.isleap(year)):
  218. raise ValueError('Invalid ordinal day' +
  219. ' {} for year {}'.format(ordinal_day, year))
  220. base_date = date(year, 1, 1) + timedelta(days=ordinal_day - 1)
  221. components = [base_date.year, base_date.month, base_date.day]
  222. return components, pos
  223. def _calculate_weekdate(self, year, week, day):
  224. """
  225. Calculate the day of corresponding to the ISO year-week-day calendar.
  226. This function is effectively the inverse of
  227. :func:`datetime.date.isocalendar`.
  228. :param year:
  229. The year in the ISO calendar
  230. :param week:
  231. The week in the ISO calendar - range is [1, 53]
  232. :param day:
  233. The day in the ISO calendar - range is [1 (MON), 7 (SUN)]
  234. :return:
  235. Returns a :class:`datetime.date`
  236. """
  237. if not 0 < week < 54:
  238. raise ValueError('Invalid week: {}'.format(week))
  239. if not 0 < day < 8: # Range is 1-7
  240. raise ValueError('Invalid weekday: {}'.format(day))
  241. # Get week 1 for the specific year:
  242. jan_4 = date(year, 1, 4) # Week 1 always has January 4th in it
  243. week_1 = jan_4 - timedelta(days=jan_4.isocalendar()[2] - 1)
  244. # Now add the specific number of weeks and days to get what we want
  245. week_offset = (week - 1) * 7 + (day - 1)
  246. return week_1 + timedelta(days=week_offset)
  247. def _parse_isotime(self, timestr):
  248. len_str = len(timestr)
  249. components = [0, 0, 0, 0, None]
  250. pos = 0
  251. comp = -1
  252. if len_str < 2:
  253. raise ValueError('ISO time too short')
  254. has_sep = False
  255. while pos < len_str and comp < 5:
  256. comp += 1
  257. if timestr[pos:pos + 1] in b'-+Zz':
  258. # Detect time zone boundary
  259. components[-1] = self._parse_tzstr(timestr[pos:])
  260. pos = len_str
  261. break
  262. if comp == 1 and timestr[pos:pos+1] == self._TIME_SEP:
  263. has_sep = True
  264. pos += 1
  265. elif comp == 2 and has_sep:
  266. if timestr[pos:pos+1] != self._TIME_SEP:
  267. raise ValueError('Inconsistent use of colon separator')
  268. pos += 1
  269. if comp < 3:
  270. # Hour, minute, second
  271. components[comp] = int(timestr[pos:pos + 2])
  272. pos += 2
  273. if comp == 3:
  274. # Fraction of a second
  275. frac = self._FRACTION_REGEX.match(timestr[pos:])
  276. if not frac:
  277. continue
  278. us_str = frac.group(1)[:6] # Truncate to microseconds
  279. components[comp] = int(us_str) * 10**(6 - len(us_str))
  280. pos += len(frac.group())
  281. if pos < len_str:
  282. raise ValueError('Unused components in ISO string')
  283. if components[0] == 24:
  284. # Standard supports 00:00 and 24:00 as representations of midnight
  285. if any(component != 0 for component in components[1:4]):
  286. raise ValueError('Hour may only be 24 at 24:00:00.000')
  287. return components
  288. def _parse_tzstr(self, tzstr, zero_as_utc=True):
  289. if tzstr == b'Z' or tzstr == b'z':
  290. return tz.UTC
  291. if len(tzstr) not in {3, 5, 6}:
  292. raise ValueError('Time zone offset must be 1, 3, 5 or 6 characters')
  293. if tzstr[0:1] == b'-':
  294. mult = -1
  295. elif tzstr[0:1] == b'+':
  296. mult = 1
  297. else:
  298. raise ValueError('Time zone offset requires sign')
  299. hours = int(tzstr[1:3])
  300. if len(tzstr) == 3:
  301. minutes = 0
  302. else:
  303. minutes = int(tzstr[(4 if tzstr[3:4] == self._TIME_SEP else 3):])
  304. if zero_as_utc and hours == 0 and minutes == 0:
  305. return tz.UTC
  306. else:
  307. if minutes > 59:
  308. raise ValueError('Invalid minutes in time zone offset')
  309. if hours > 23:
  310. raise ValueError('Invalid hours in time zone offset')
  311. return tz.tzoffset(None, mult * (hours * 60 + minutes) * 60)
  312. DEFAULT_ISOPARSER = isoparser()
  313. isoparse = DEFAULT_ISOPARSER.isoparse