_parser.py 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  1. # -*- coding: utf-8 -*-
  2. """
  3. This module offers a generic date/time string parser which is able to parse
  4. most known formats to represent a date and/or time.
  5. This module attempts to be forgiving with regards to unlikely input formats,
  6. returning a datetime object even for dates which are ambiguous. If an element
  7. of a date/time stamp is omitted, the following rules are applied:
  8. - If AM or PM is left unspecified, a 24-hour clock is assumed, however, an hour
  9. on a 12-hour clock (``0 <= hour <= 12``) *must* be specified if AM or PM is
  10. specified.
  11. - If a time zone is omitted, a timezone-naive datetime is returned.
  12. If any other elements are missing, they are taken from the
  13. :class:`datetime.datetime` object passed to the parameter ``default``. If this
  14. results in a day number exceeding the valid number of days per month, the
  15. value falls back to the end of the month.
  16. Additional resources about date/time string formats can be found below:
  17. - `A summary of the international standard date and time notation
  18. <http://www.cl.cam.ac.uk/~mgk25/iso-time.html>`_
  19. - `W3C Date and Time Formats <http://www.w3.org/TR/NOTE-datetime>`_
  20. - `Time Formats (Planetary Rings Node) <https://pds-rings.seti.org:443/tools/time_formats.html>`_
  21. - `CPAN ParseDate module
  22. <http://search.cpan.org/~muir/Time-modules-2013.0912/lib/Time/ParseDate.pm>`_
  23. - `Java SimpleDateFormat Class
  24. <https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html>`_
  25. """
  26. from __future__ import unicode_literals
  27. import datetime
  28. import re
  29. import string
  30. import time
  31. import warnings
  32. from calendar import monthrange
  33. from io import StringIO
  34. import six
  35. from six import integer_types, text_type
  36. from decimal import Decimal
  37. from warnings import warn
  38. from .. import relativedelta
  39. from .. import tz
  40. __all__ = ["parse", "parserinfo", "ParserError"]
  41. # TODO: pandas.core.tools.datetimes imports this explicitly. Might be worth
  42. # making public and/or figuring out if there is something we can
  43. # take off their plate.
  44. class _timelex(object):
  45. # Fractional seconds are sometimes split by a comma
  46. _split_decimal = re.compile("([.,])")
  47. def __init__(self, instream):
  48. if six.PY2:
  49. # In Python 2, we can't duck type properly because unicode has
  50. # a 'decode' function, and we'd be double-decoding
  51. if isinstance(instream, (bytes, bytearray)):
  52. instream = instream.decode()
  53. else:
  54. if getattr(instream, 'decode', None) is not None:
  55. instream = instream.decode()
  56. if isinstance(instream, text_type):
  57. instream = StringIO(instream)
  58. elif getattr(instream, 'read', None) is None:
  59. raise TypeError('Parser must be a string or character stream, not '
  60. '{itype}'.format(itype=instream.__class__.__name__))
  61. self.instream = instream
  62. self.charstack = []
  63. self.tokenstack = []
  64. self.eof = False
  65. def get_token(self):
  66. """
  67. This function breaks the time string into lexical units (tokens), which
  68. can be parsed by the parser. Lexical units are demarcated by changes in
  69. the character set, so any continuous string of letters is considered
  70. one unit, any continuous string of numbers is considered one unit.
  71. The main complication arises from the fact that dots ('.') can be used
  72. both as separators (e.g. "Sep.20.2009") or decimal points (e.g.
  73. "4:30:21.447"). As such, it is necessary to read the full context of
  74. any dot-separated strings before breaking it into tokens; as such, this
  75. function maintains a "token stack", for when the ambiguous context
  76. demands that multiple tokens be parsed at once.
  77. """
  78. if self.tokenstack:
  79. return self.tokenstack.pop(0)
  80. seenletters = False
  81. token = None
  82. state = None
  83. while not self.eof:
  84. # We only realize that we've reached the end of a token when we
  85. # find a character that's not part of the current token - since
  86. # that character may be part of the next token, it's stored in the
  87. # charstack.
  88. if self.charstack:
  89. nextchar = self.charstack.pop(0)
  90. else:
  91. nextchar = self.instream.read(1)
  92. while nextchar == '\x00':
  93. nextchar = self.instream.read(1)
  94. if not nextchar:
  95. self.eof = True
  96. break
  97. elif not state:
  98. # First character of the token - determines if we're starting
  99. # to parse a word, a number or something else.
  100. token = nextchar
  101. if self.isword(nextchar):
  102. state = 'a'
  103. elif self.isnum(nextchar):
  104. state = '0'
  105. elif self.isspace(nextchar):
  106. token = ' '
  107. break # emit token
  108. else:
  109. break # emit token
  110. elif state == 'a':
  111. # If we've already started reading a word, we keep reading
  112. # letters until we find something that's not part of a word.
  113. seenletters = True
  114. if self.isword(nextchar):
  115. token += nextchar
  116. elif nextchar == '.':
  117. token += nextchar
  118. state = 'a.'
  119. else:
  120. self.charstack.append(nextchar)
  121. break # emit token
  122. elif state == '0':
  123. # If we've already started reading a number, we keep reading
  124. # numbers until we find something that doesn't fit.
  125. if self.isnum(nextchar):
  126. token += nextchar
  127. elif nextchar == '.' or (nextchar == ',' and len(token) >= 2):
  128. token += nextchar
  129. state = '0.'
  130. else:
  131. self.charstack.append(nextchar)
  132. break # emit token
  133. elif state == 'a.':
  134. # If we've seen some letters and a dot separator, continue
  135. # parsing, and the tokens will be broken up later.
  136. seenletters = True
  137. if nextchar == '.' or self.isword(nextchar):
  138. token += nextchar
  139. elif self.isnum(nextchar) and token[-1] == '.':
  140. token += nextchar
  141. state = '0.'
  142. else:
  143. self.charstack.append(nextchar)
  144. break # emit token
  145. elif state == '0.':
  146. # If we've seen at least one dot separator, keep going, we'll
  147. # break up the tokens later.
  148. if nextchar == '.' or self.isnum(nextchar):
  149. token += nextchar
  150. elif self.isword(nextchar) and token[-1] == '.':
  151. token += nextchar
  152. state = 'a.'
  153. else:
  154. self.charstack.append(nextchar)
  155. break # emit token
  156. if (state in ('a.', '0.') and (seenletters or token.count('.') > 1 or
  157. token[-1] in '.,')):
  158. l = self._split_decimal.split(token)
  159. token = l[0]
  160. for tok in l[1:]:
  161. if tok:
  162. self.tokenstack.append(tok)
  163. if state == '0.' and token.count('.') == 0:
  164. token = token.replace(',', '.')
  165. return token
  166. def __iter__(self):
  167. return self
  168. def __next__(self):
  169. token = self.get_token()
  170. if token is None:
  171. raise StopIteration
  172. return token
  173. def next(self):
  174. return self.__next__() # Python 2.x support
  175. @classmethod
  176. def split(cls, s):
  177. return list(cls(s))
  178. @classmethod
  179. def isword(cls, nextchar):
  180. """ Whether or not the next character is part of a word """
  181. return nextchar.isalpha()
  182. @classmethod
  183. def isnum(cls, nextchar):
  184. """ Whether the next character is part of a number """
  185. return nextchar.isdigit()
  186. @classmethod
  187. def isspace(cls, nextchar):
  188. """ Whether the next character is whitespace """
  189. return nextchar.isspace()
  190. class _resultbase(object):
  191. def __init__(self):
  192. for attr in self.__slots__:
  193. setattr(self, attr, None)
  194. def _repr(self, classname):
  195. l = []
  196. for attr in self.__slots__:
  197. value = getattr(self, attr)
  198. if value is not None:
  199. l.append("%s=%s" % (attr, repr(value)))
  200. return "%s(%s)" % (classname, ", ".join(l))
  201. def __len__(self):
  202. return (sum(getattr(self, attr) is not None
  203. for attr in self.__slots__))
  204. def __repr__(self):
  205. return self._repr(self.__class__.__name__)
  206. class parserinfo(object):
  207. """
  208. Class which handles what inputs are accepted. Subclass this to customize
  209. the language and acceptable values for each parameter.
  210. :param dayfirst:
  211. Whether to interpret the first value in an ambiguous 3-integer date
  212. (e.g. 01/05/09) as the day (``True``) or month (``False``). If
  213. ``yearfirst`` is set to ``True``, this distinguishes between YDM
  214. and YMD. Default is ``False``.
  215. :param yearfirst:
  216. Whether to interpret the first value in an ambiguous 3-integer date
  217. (e.g. 01/05/09) as the year. If ``True``, the first number is taken
  218. to be the year, otherwise the last number is taken to be the year.
  219. Default is ``False``.
  220. """
  221. # m from a.m/p.m, t from ISO T separator
  222. JUMP = [" ", ".", ",", ";", "-", "/", "'",
  223. "at", "on", "and", "ad", "m", "t", "of",
  224. "st", "nd", "rd", "th"]
  225. WEEKDAYS = [("Mon", "Monday"),
  226. ("Tue", "Tuesday"), # TODO: "Tues"
  227. ("Wed", "Wednesday"),
  228. ("Thu", "Thursday"), # TODO: "Thurs"
  229. ("Fri", "Friday"),
  230. ("Sat", "Saturday"),
  231. ("Sun", "Sunday")]
  232. MONTHS = [("Jan", "January"),
  233. ("Feb", "February"), # TODO: "Febr"
  234. ("Mar", "March"),
  235. ("Apr", "April"),
  236. ("May", "May"),
  237. ("Jun", "June"),
  238. ("Jul", "July"),
  239. ("Aug", "August"),
  240. ("Sep", "Sept", "September"),
  241. ("Oct", "October"),
  242. ("Nov", "November"),
  243. ("Dec", "December")]
  244. HMS = [("h", "hour", "hours"),
  245. ("m", "minute", "minutes"),
  246. ("s", "second", "seconds")]
  247. AMPM = [("am", "a"),
  248. ("pm", "p")]
  249. UTCZONE = ["UTC", "GMT", "Z", "z"]
  250. PERTAIN = ["of"]
  251. TZOFFSET = {}
  252. # TODO: ERA = ["AD", "BC", "CE", "BCE", "Stardate",
  253. # "Anno Domini", "Year of Our Lord"]
  254. def __init__(self, dayfirst=False, yearfirst=False):
  255. self._jump = self._convert(self.JUMP)
  256. self._weekdays = self._convert(self.WEEKDAYS)
  257. self._months = self._convert(self.MONTHS)
  258. self._hms = self._convert(self.HMS)
  259. self._ampm = self._convert(self.AMPM)
  260. self._utczone = self._convert(self.UTCZONE)
  261. self._pertain = self._convert(self.PERTAIN)
  262. self.dayfirst = dayfirst
  263. self.yearfirst = yearfirst
  264. self._year = time.localtime().tm_year
  265. self._century = self._year // 100 * 100
  266. def _convert(self, lst):
  267. dct = {}
  268. for i, v in enumerate(lst):
  269. if isinstance(v, tuple):
  270. for v in v:
  271. dct[v.lower()] = i
  272. else:
  273. dct[v.lower()] = i
  274. return dct
  275. def jump(self, name):
  276. return name.lower() in self._jump
  277. def weekday(self, name):
  278. try:
  279. return self._weekdays[name.lower()]
  280. except KeyError:
  281. pass
  282. return None
  283. def month(self, name):
  284. try:
  285. return self._months[name.lower()] + 1
  286. except KeyError:
  287. pass
  288. return None
  289. def hms(self, name):
  290. try:
  291. return self._hms[name.lower()]
  292. except KeyError:
  293. return None
  294. def ampm(self, name):
  295. try:
  296. return self._ampm[name.lower()]
  297. except KeyError:
  298. return None
  299. def pertain(self, name):
  300. return name.lower() in self._pertain
  301. def utczone(self, name):
  302. return name.lower() in self._utczone
  303. def tzoffset(self, name):
  304. if name in self._utczone:
  305. return 0
  306. return self.TZOFFSET.get(name)
  307. def convertyear(self, year, century_specified=False):
  308. """
  309. Converts two-digit years to year within [-50, 49]
  310. range of self._year (current local time)
  311. """
  312. # Function contract is that the year is always positive
  313. assert year >= 0
  314. if year < 100 and not century_specified:
  315. # assume current century to start
  316. year += self._century
  317. if year >= self._year + 50: # if too far in future
  318. year -= 100
  319. elif year < self._year - 50: # if too far in past
  320. year += 100
  321. return year
  322. def validate(self, res):
  323. # move to info
  324. if res.year is not None:
  325. res.year = self.convertyear(res.year, res.century_specified)
  326. if ((res.tzoffset == 0 and not res.tzname) or
  327. (res.tzname == 'Z' or res.tzname == 'z')):
  328. res.tzname = "UTC"
  329. res.tzoffset = 0
  330. elif res.tzoffset != 0 and res.tzname and self.utczone(res.tzname):
  331. res.tzoffset = 0
  332. return True
  333. class _ymd(list):
  334. def __init__(self, *args, **kwargs):
  335. super(self.__class__, self).__init__(*args, **kwargs)
  336. self.century_specified = False
  337. self.dstridx = None
  338. self.mstridx = None
  339. self.ystridx = None
  340. @property
  341. def has_year(self):
  342. return self.ystridx is not None
  343. @property
  344. def has_month(self):
  345. return self.mstridx is not None
  346. @property
  347. def has_day(self):
  348. return self.dstridx is not None
  349. def could_be_day(self, value):
  350. if self.has_day:
  351. return False
  352. elif not self.has_month:
  353. return 1 <= value <= 31
  354. elif not self.has_year:
  355. # Be permissive, assume leap year
  356. month = self[self.mstridx]
  357. return 1 <= value <= monthrange(2000, month)[1]
  358. else:
  359. month = self[self.mstridx]
  360. year = self[self.ystridx]
  361. return 1 <= value <= monthrange(year, month)[1]
  362. def append(self, val, label=None):
  363. if hasattr(val, '__len__'):
  364. if val.isdigit() and len(val) > 2:
  365. self.century_specified = True
  366. if label not in [None, 'Y']: # pragma: no cover
  367. raise ValueError(label)
  368. label = 'Y'
  369. elif val > 100:
  370. self.century_specified = True
  371. if label not in [None, 'Y']: # pragma: no cover
  372. raise ValueError(label)
  373. label = 'Y'
  374. super(self.__class__, self).append(int(val))
  375. if label == 'M':
  376. if self.has_month:
  377. raise ValueError('Month is already set')
  378. self.mstridx = len(self) - 1
  379. elif label == 'D':
  380. if self.has_day:
  381. raise ValueError('Day is already set')
  382. self.dstridx = len(self) - 1
  383. elif label == 'Y':
  384. if self.has_year:
  385. raise ValueError('Year is already set')
  386. self.ystridx = len(self) - 1
  387. def _resolve_from_stridxs(self, strids):
  388. """
  389. Try to resolve the identities of year/month/day elements using
  390. ystridx, mstridx, and dstridx, if enough of these are specified.
  391. """
  392. if len(self) == 3 and len(strids) == 2:
  393. # we can back out the remaining stridx value
  394. missing = [x for x in range(3) if x not in strids.values()]
  395. key = [x for x in ['y', 'm', 'd'] if x not in strids]
  396. assert len(missing) == len(key) == 1
  397. key = key[0]
  398. val = missing[0]
  399. strids[key] = val
  400. assert len(self) == len(strids) # otherwise this should not be called
  401. out = {key: self[strids[key]] for key in strids}
  402. return (out.get('y'), out.get('m'), out.get('d'))
  403. def resolve_ymd(self, yearfirst, dayfirst):
  404. len_ymd = len(self)
  405. year, month, day = (None, None, None)
  406. strids = (('y', self.ystridx),
  407. ('m', self.mstridx),
  408. ('d', self.dstridx))
  409. strids = {key: val for key, val in strids if val is not None}
  410. if (len(self) == len(strids) > 0 or
  411. (len(self) == 3 and len(strids) == 2)):
  412. return self._resolve_from_stridxs(strids)
  413. mstridx = self.mstridx
  414. if len_ymd > 3:
  415. raise ValueError("More than three YMD values")
  416. elif len_ymd == 1 or (mstridx is not None and len_ymd == 2):
  417. # One member, or two members with a month string
  418. if mstridx is not None:
  419. month = self[mstridx]
  420. # since mstridx is 0 or 1, self[mstridx-1] always
  421. # looks up the other element
  422. other = self[mstridx - 1]
  423. else:
  424. other = self[0]
  425. if len_ymd > 1 or mstridx is None:
  426. if other > 31:
  427. year = other
  428. else:
  429. day = other
  430. elif len_ymd == 2:
  431. # Two members with numbers
  432. if self[0] > 31:
  433. # 99-01
  434. year, month = self
  435. elif self[1] > 31:
  436. # 01-99
  437. month, year = self
  438. elif dayfirst and self[1] <= 12:
  439. # 13-01
  440. day, month = self
  441. else:
  442. # 01-13
  443. month, day = self
  444. elif len_ymd == 3:
  445. # Three members
  446. if mstridx == 0:
  447. if self[1] > 31:
  448. # Apr-2003-25
  449. month, year, day = self
  450. else:
  451. month, day, year = self
  452. elif mstridx == 1:
  453. if self[0] > 31 or (yearfirst and self[2] <= 31):
  454. # 99-Jan-01
  455. year, month, day = self
  456. else:
  457. # 01-Jan-01
  458. # Give precedence to day-first, since
  459. # two-digit years is usually hand-written.
  460. day, month, year = self
  461. elif mstridx == 2:
  462. # WTF!?
  463. if self[1] > 31:
  464. # 01-99-Jan
  465. day, year, month = self
  466. else:
  467. # 99-01-Jan
  468. year, day, month = self
  469. else:
  470. if (self[0] > 31 or
  471. self.ystridx == 0 or
  472. (yearfirst and self[1] <= 12 and self[2] <= 31)):
  473. # 99-01-01
  474. if dayfirst and self[2] <= 12:
  475. year, day, month = self
  476. else:
  477. year, month, day = self
  478. elif self[0] > 12 or (dayfirst and self[1] <= 12):
  479. # 13-01-01
  480. day, month, year = self
  481. else:
  482. # 01-13-01
  483. month, day, year = self
  484. return year, month, day
  485. class parser(object):
  486. def __init__(self, info=None):
  487. self.info = info or parserinfo()
  488. def parse(self, timestr, default=None,
  489. ignoretz=False, tzinfos=None, **kwargs):
  490. """
  491. Parse the date/time string into a :class:`datetime.datetime` object.
  492. :param timestr:
  493. Any date/time string using the supported formats.
  494. :param default:
  495. The default datetime object, if this is a datetime object and not
  496. ``None``, elements specified in ``timestr`` replace elements in the
  497. default object.
  498. :param ignoretz:
  499. If set ``True``, time zones in parsed strings are ignored and a
  500. naive :class:`datetime.datetime` object is returned.
  501. :param tzinfos:
  502. Additional time zone names / aliases which may be present in the
  503. string. This argument maps time zone names (and optionally offsets
  504. from those time zones) to time zones. This parameter can be a
  505. dictionary with timezone aliases mapping time zone names to time
  506. zones or a function taking two parameters (``tzname`` and
  507. ``tzoffset``) and returning a time zone.
  508. The timezones to which the names are mapped can be an integer
  509. offset from UTC in seconds or a :class:`tzinfo` object.
  510. .. doctest::
  511. :options: +NORMALIZE_WHITESPACE
  512. >>> from dateutil.parser import parse
  513. >>> from dateutil.tz import gettz
  514. >>> tzinfos = {"BRST": -7200, "CST": gettz("America/Chicago")}
  515. >>> parse("2012-01-19 17:21:00 BRST", tzinfos=tzinfos)
  516. datetime.datetime(2012, 1, 19, 17, 21, tzinfo=tzoffset(u'BRST', -7200))
  517. >>> parse("2012-01-19 17:21:00 CST", tzinfos=tzinfos)
  518. datetime.datetime(2012, 1, 19, 17, 21,
  519. tzinfo=tzfile('/usr/share/zoneinfo/America/Chicago'))
  520. This parameter is ignored if ``ignoretz`` is set.
  521. :param \\*\\*kwargs:
  522. Keyword arguments as passed to ``_parse()``.
  523. :return:
  524. Returns a :class:`datetime.datetime` object or, if the
  525. ``fuzzy_with_tokens`` option is ``True``, returns a tuple, the
  526. first element being a :class:`datetime.datetime` object, the second
  527. a tuple containing the fuzzy tokens.
  528. :raises ParserError:
  529. Raised for invalid or unknown string format, if the provided
  530. :class:`tzinfo` is not in a valid format, or if an invalid date
  531. would be created.
  532. :raises TypeError:
  533. Raised for non-string or character stream input.
  534. :raises OverflowError:
  535. Raised if the parsed date exceeds the largest valid C integer on
  536. your system.
  537. """
  538. if default is None:
  539. default = datetime.datetime.now().replace(hour=0, minute=0,
  540. second=0, microsecond=0)
  541. res, skipped_tokens = self._parse(timestr, **kwargs)
  542. if res is None:
  543. raise ParserError("Unknown string format: %s", timestr)
  544. if len(res) == 0:
  545. raise ParserError("String does not contain a date: %s", timestr)
  546. try:
  547. ret = self._build_naive(res, default)
  548. except ValueError as e:
  549. six.raise_from(ParserError(e.args[0] + ": %s", timestr), e)
  550. if not ignoretz:
  551. ret = self._build_tzaware(ret, res, tzinfos)
  552. if kwargs.get('fuzzy_with_tokens', False):
  553. return ret, skipped_tokens
  554. else:
  555. return ret
  556. class _result(_resultbase):
  557. __slots__ = ["year", "month", "day", "weekday",
  558. "hour", "minute", "second", "microsecond",
  559. "tzname", "tzoffset", "ampm","any_unused_tokens"]
  560. def _parse(self, timestr, dayfirst=None, yearfirst=None, fuzzy=False,
  561. fuzzy_with_tokens=False):
  562. """
  563. Private method which performs the heavy lifting of parsing, called from
  564. ``parse()``, which passes on its ``kwargs`` to this function.
  565. :param timestr:
  566. The string to parse.
  567. :param dayfirst:
  568. Whether to interpret the first value in an ambiguous 3-integer date
  569. (e.g. 01/05/09) as the day (``True``) or month (``False``). If
  570. ``yearfirst`` is set to ``True``, this distinguishes between YDM
  571. and YMD. If set to ``None``, this value is retrieved from the
  572. current :class:`parserinfo` object (which itself defaults to
  573. ``False``).
  574. :param yearfirst:
  575. Whether to interpret the first value in an ambiguous 3-integer date
  576. (e.g. 01/05/09) as the year. If ``True``, the first number is taken
  577. to be the year, otherwise the last number is taken to be the year.
  578. If this is set to ``None``, the value is retrieved from the current
  579. :class:`parserinfo` object (which itself defaults to ``False``).
  580. :param fuzzy:
  581. Whether to allow fuzzy parsing, allowing for string like "Today is
  582. January 1, 2047 at 8:21:00AM".
  583. :param fuzzy_with_tokens:
  584. If ``True``, ``fuzzy`` is automatically set to True, and the parser
  585. will return a tuple where the first element is the parsed
  586. :class:`datetime.datetime` datetimestamp and the second element is
  587. a tuple containing the portions of the string which were ignored:
  588. .. doctest::
  589. >>> from dateutil.parser import parse
  590. >>> parse("Today is January 1, 2047 at 8:21:00AM", fuzzy_with_tokens=True)
  591. (datetime.datetime(2047, 1, 1, 8, 21), (u'Today is ', u' ', u'at '))
  592. """
  593. if fuzzy_with_tokens:
  594. fuzzy = True
  595. info = self.info
  596. if dayfirst is None:
  597. dayfirst = info.dayfirst
  598. if yearfirst is None:
  599. yearfirst = info.yearfirst
  600. res = self._result()
  601. l = _timelex.split(timestr) # Splits the timestr into tokens
  602. skipped_idxs = []
  603. # year/month/day list
  604. ymd = _ymd()
  605. len_l = len(l)
  606. i = 0
  607. try:
  608. while i < len_l:
  609. # Check if it's a number
  610. value_repr = l[i]
  611. try:
  612. value = float(value_repr)
  613. except ValueError:
  614. value = None
  615. if value is not None:
  616. # Numeric token
  617. i = self._parse_numeric_token(l, i, info, ymd, res, fuzzy)
  618. # Check weekday
  619. elif info.weekday(l[i]) is not None:
  620. value = info.weekday(l[i])
  621. res.weekday = value
  622. # Check month name
  623. elif info.month(l[i]) is not None:
  624. value = info.month(l[i])
  625. ymd.append(value, 'M')
  626. if i + 1 < len_l:
  627. if l[i + 1] in ('-', '/'):
  628. # Jan-01[-99]
  629. sep = l[i + 1]
  630. ymd.append(l[i + 2])
  631. if i + 3 < len_l and l[i + 3] == sep:
  632. # Jan-01-99
  633. ymd.append(l[i + 4])
  634. i += 2
  635. i += 2
  636. elif (i + 4 < len_l and l[i + 1] == l[i + 3] == ' ' and
  637. info.pertain(l[i + 2])):
  638. # Jan of 01
  639. # In this case, 01 is clearly year
  640. if l[i + 4].isdigit():
  641. # Convert it here to become unambiguous
  642. value = int(l[i + 4])
  643. year = str(info.convertyear(value))
  644. ymd.append(year, 'Y')
  645. else:
  646. # Wrong guess
  647. pass
  648. # TODO: not hit in tests
  649. i += 4
  650. # Check am/pm
  651. elif info.ampm(l[i]) is not None:
  652. value = info.ampm(l[i])
  653. val_is_ampm = self._ampm_valid(res.hour, res.ampm, fuzzy)
  654. if val_is_ampm:
  655. res.hour = self._adjust_ampm(res.hour, value)
  656. res.ampm = value
  657. elif fuzzy:
  658. skipped_idxs.append(i)
  659. # Check for a timezone name
  660. elif self._could_be_tzname(res.hour, res.tzname, res.tzoffset, l[i]):
  661. res.tzname = l[i]
  662. res.tzoffset = info.tzoffset(res.tzname)
  663. # Check for something like GMT+3, or BRST+3. Notice
  664. # that it doesn't mean "I am 3 hours after GMT", but
  665. # "my time +3 is GMT". If found, we reverse the
  666. # logic so that timezone parsing code will get it
  667. # right.
  668. if i + 1 < len_l and l[i + 1] in ('+', '-'):
  669. l[i + 1] = ('+', '-')[l[i + 1] == '+']
  670. res.tzoffset = None
  671. if info.utczone(res.tzname):
  672. # With something like GMT+3, the timezone
  673. # is *not* GMT.
  674. res.tzname = None
  675. # Check for a numbered timezone
  676. elif res.hour is not None and l[i] in ('+', '-'):
  677. signal = (-1, 1)[l[i] == '+']
  678. len_li = len(l[i + 1])
  679. # TODO: check that l[i + 1] is integer?
  680. if len_li == 4:
  681. # -0300
  682. hour_offset = int(l[i + 1][:2])
  683. min_offset = int(l[i + 1][2:])
  684. elif i + 2 < len_l and l[i + 2] == ':':
  685. # -03:00
  686. hour_offset = int(l[i + 1])
  687. min_offset = int(l[i + 3]) # TODO: Check that l[i+3] is minute-like?
  688. i += 2
  689. elif len_li <= 2:
  690. # -[0]3
  691. hour_offset = int(l[i + 1][:2])
  692. min_offset = 0
  693. else:
  694. raise ValueError(timestr)
  695. res.tzoffset = signal * (hour_offset * 3600 + min_offset * 60)
  696. # Look for a timezone name between parenthesis
  697. if (i + 5 < len_l and
  698. info.jump(l[i + 2]) and l[i + 3] == '(' and
  699. l[i + 5] == ')' and
  700. 3 <= len(l[i + 4]) and
  701. self._could_be_tzname(res.hour, res.tzname,
  702. None, l[i + 4])):
  703. # -0300 (BRST)
  704. res.tzname = l[i + 4]
  705. i += 4
  706. i += 1
  707. # Check jumps
  708. elif not (info.jump(l[i]) or fuzzy):
  709. raise ValueError(timestr)
  710. else:
  711. skipped_idxs.append(i)
  712. i += 1
  713. # Process year/month/day
  714. year, month, day = ymd.resolve_ymd(yearfirst, dayfirst)
  715. res.century_specified = ymd.century_specified
  716. res.year = year
  717. res.month = month
  718. res.day = day
  719. except (IndexError, ValueError):
  720. return None, None
  721. if not info.validate(res):
  722. return None, None
  723. if fuzzy_with_tokens:
  724. skipped_tokens = self._recombine_skipped(l, skipped_idxs)
  725. return res, tuple(skipped_tokens)
  726. else:
  727. return res, None
  728. def _parse_numeric_token(self, tokens, idx, info, ymd, res, fuzzy):
  729. # Token is a number
  730. value_repr = tokens[idx]
  731. try:
  732. value = self._to_decimal(value_repr)
  733. except Exception as e:
  734. six.raise_from(ValueError('Unknown numeric token'), e)
  735. len_li = len(value_repr)
  736. len_l = len(tokens)
  737. if (len(ymd) == 3 and len_li in (2, 4) and
  738. res.hour is None and
  739. (idx + 1 >= len_l or
  740. (tokens[idx + 1] != ':' and
  741. info.hms(tokens[idx + 1]) is None))):
  742. # 19990101T23[59]
  743. s = tokens[idx]
  744. res.hour = int(s[:2])
  745. if len_li == 4:
  746. res.minute = int(s[2:])
  747. elif len_li == 6 or (len_li > 6 and tokens[idx].find('.') == 6):
  748. # YYMMDD or HHMMSS[.ss]
  749. s = tokens[idx]
  750. if not ymd and '.' not in tokens[idx]:
  751. ymd.append(s[:2])
  752. ymd.append(s[2:4])
  753. ymd.append(s[4:])
  754. else:
  755. # 19990101T235959[.59]
  756. # TODO: Check if res attributes already set.
  757. res.hour = int(s[:2])
  758. res.minute = int(s[2:4])
  759. res.second, res.microsecond = self._parsems(s[4:])
  760. elif len_li in (8, 12, 14):
  761. # YYYYMMDD
  762. s = tokens[idx]
  763. ymd.append(s[:4], 'Y')
  764. ymd.append(s[4:6])
  765. ymd.append(s[6:8])
  766. if len_li > 8:
  767. res.hour = int(s[8:10])
  768. res.minute = int(s[10:12])
  769. if len_li > 12:
  770. res.second = int(s[12:])
  771. elif self._find_hms_idx(idx, tokens, info, allow_jump=True) is not None:
  772. # HH[ ]h or MM[ ]m or SS[.ss][ ]s
  773. hms_idx = self._find_hms_idx(idx, tokens, info, allow_jump=True)
  774. (idx, hms) = self._parse_hms(idx, tokens, info, hms_idx)
  775. if hms is not None:
  776. # TODO: checking that hour/minute/second are not
  777. # already set?
  778. self._assign_hms(res, value_repr, hms)
  779. elif idx + 2 < len_l and tokens[idx + 1] == ':':
  780. # HH:MM[:SS[.ss]]
  781. res.hour = int(value)
  782. value = self._to_decimal(tokens[idx + 2]) # TODO: try/except for this?
  783. (res.minute, res.second) = self._parse_min_sec(value)
  784. if idx + 4 < len_l and tokens[idx + 3] == ':':
  785. res.second, res.microsecond = self._parsems(tokens[idx + 4])
  786. idx += 2
  787. idx += 2
  788. elif idx + 1 < len_l and tokens[idx + 1] in ('-', '/', '.'):
  789. sep = tokens[idx + 1]
  790. ymd.append(value_repr)
  791. if idx + 2 < len_l and not info.jump(tokens[idx + 2]):
  792. if tokens[idx + 2].isdigit():
  793. # 01-01[-01]
  794. ymd.append(tokens[idx + 2])
  795. else:
  796. # 01-Jan[-01]
  797. value = info.month(tokens[idx + 2])
  798. if value is not None:
  799. ymd.append(value, 'M')
  800. else:
  801. raise ValueError()
  802. if idx + 3 < len_l and tokens[idx + 3] == sep:
  803. # We have three members
  804. value = info.month(tokens[idx + 4])
  805. if value is not None:
  806. ymd.append(value, 'M')
  807. else:
  808. ymd.append(tokens[idx + 4])
  809. idx += 2
  810. idx += 1
  811. idx += 1
  812. elif idx + 1 >= len_l or info.jump(tokens[idx + 1]):
  813. if idx + 2 < len_l and info.ampm(tokens[idx + 2]) is not None:
  814. # 12 am
  815. hour = int(value)
  816. res.hour = self._adjust_ampm(hour, info.ampm(tokens[idx + 2]))
  817. idx += 1
  818. else:
  819. # Year, month or day
  820. ymd.append(value)
  821. idx += 1
  822. elif info.ampm(tokens[idx + 1]) is not None and (0 <= value < 24):
  823. # 12am
  824. hour = int(value)
  825. res.hour = self._adjust_ampm(hour, info.ampm(tokens[idx + 1]))
  826. idx += 1
  827. elif ymd.could_be_day(value):
  828. ymd.append(value)
  829. elif not fuzzy:
  830. raise ValueError()
  831. return idx
  832. def _find_hms_idx(self, idx, tokens, info, allow_jump):
  833. len_l = len(tokens)
  834. if idx+1 < len_l and info.hms(tokens[idx+1]) is not None:
  835. # There is an "h", "m", or "s" label following this token. We take
  836. # assign the upcoming label to the current token.
  837. # e.g. the "12" in 12h"
  838. hms_idx = idx + 1
  839. elif (allow_jump and idx+2 < len_l and tokens[idx+1] == ' ' and
  840. info.hms(tokens[idx+2]) is not None):
  841. # There is a space and then an "h", "m", or "s" label.
  842. # e.g. the "12" in "12 h"
  843. hms_idx = idx + 2
  844. elif idx > 0 and info.hms(tokens[idx-1]) is not None:
  845. # There is a "h", "m", or "s" preceding this token. Since neither
  846. # of the previous cases was hit, there is no label following this
  847. # token, so we use the previous label.
  848. # e.g. the "04" in "12h04"
  849. hms_idx = idx-1
  850. elif (1 < idx == len_l-1 and tokens[idx-1] == ' ' and
  851. info.hms(tokens[idx-2]) is not None):
  852. # If we are looking at the final token, we allow for a
  853. # backward-looking check to skip over a space.
  854. # TODO: Are we sure this is the right condition here?
  855. hms_idx = idx - 2
  856. else:
  857. hms_idx = None
  858. return hms_idx
  859. def _assign_hms(self, res, value_repr, hms):
  860. # See GH issue #427, fixing float rounding
  861. value = self._to_decimal(value_repr)
  862. if hms == 0:
  863. # Hour
  864. res.hour = int(value)
  865. if value % 1:
  866. res.minute = int(60*(value % 1))
  867. elif hms == 1:
  868. (res.minute, res.second) = self._parse_min_sec(value)
  869. elif hms == 2:
  870. (res.second, res.microsecond) = self._parsems(value_repr)
  871. def _could_be_tzname(self, hour, tzname, tzoffset, token):
  872. return (hour is not None and
  873. tzname is None and
  874. tzoffset is None and
  875. len(token) <= 5 and
  876. (all(x in string.ascii_uppercase for x in token)
  877. or token in self.info.UTCZONE))
  878. def _ampm_valid(self, hour, ampm, fuzzy):
  879. """
  880. For fuzzy parsing, 'a' or 'am' (both valid English words)
  881. may erroneously trigger the AM/PM flag. Deal with that
  882. here.
  883. """
  884. val_is_ampm = True
  885. # If there's already an AM/PM flag, this one isn't one.
  886. if fuzzy and ampm is not None:
  887. val_is_ampm = False
  888. # If AM/PM is found and hour is not, raise a ValueError
  889. if hour is None:
  890. if fuzzy:
  891. val_is_ampm = False
  892. else:
  893. raise ValueError('No hour specified with AM or PM flag.')
  894. elif not 0 <= hour <= 12:
  895. # If AM/PM is found, it's a 12 hour clock, so raise
  896. # an error for invalid range
  897. if fuzzy:
  898. val_is_ampm = False
  899. else:
  900. raise ValueError('Invalid hour specified for 12-hour clock.')
  901. return val_is_ampm
  902. def _adjust_ampm(self, hour, ampm):
  903. if hour < 12 and ampm == 1:
  904. hour += 12
  905. elif hour == 12 and ampm == 0:
  906. hour = 0
  907. return hour
  908. def _parse_min_sec(self, value):
  909. # TODO: Every usage of this function sets res.second to the return
  910. # value. Are there any cases where second will be returned as None and
  911. # we *don't* want to set res.second = None?
  912. minute = int(value)
  913. second = None
  914. sec_remainder = value % 1
  915. if sec_remainder:
  916. second = int(60 * sec_remainder)
  917. return (minute, second)
  918. def _parse_hms(self, idx, tokens, info, hms_idx):
  919. # TODO: Is this going to admit a lot of false-positives for when we
  920. # just happen to have digits and "h", "m" or "s" characters in non-date
  921. # text? I guess hex hashes won't have that problem, but there's plenty
  922. # of random junk out there.
  923. if hms_idx is None:
  924. hms = None
  925. new_idx = idx
  926. elif hms_idx > idx:
  927. hms = info.hms(tokens[hms_idx])
  928. new_idx = hms_idx
  929. else:
  930. # Looking backwards, increment one.
  931. hms = info.hms(tokens[hms_idx]) + 1
  932. new_idx = idx
  933. return (new_idx, hms)
  934. # ------------------------------------------------------------------
  935. # Handling for individual tokens. These are kept as methods instead
  936. # of functions for the sake of customizability via subclassing.
  937. def _parsems(self, value):
  938. """Parse a I[.F] seconds value into (seconds, microseconds)."""
  939. if "." not in value:
  940. return int(value), 0
  941. else:
  942. i, f = value.split(".")
  943. return int(i), int(f.ljust(6, "0")[:6])
  944. def _to_decimal(self, val):
  945. try:
  946. decimal_value = Decimal(val)
  947. # See GH 662, edge case, infinite value should not be converted
  948. # via `_to_decimal`
  949. if not decimal_value.is_finite():
  950. raise ValueError("Converted decimal value is infinite or NaN")
  951. except Exception as e:
  952. msg = "Could not convert %s to decimal" % val
  953. six.raise_from(ValueError(msg), e)
  954. else:
  955. return decimal_value
  956. # ------------------------------------------------------------------
  957. # Post-Parsing construction of datetime output. These are kept as
  958. # methods instead of functions for the sake of customizability via
  959. # subclassing.
  960. def _build_tzinfo(self, tzinfos, tzname, tzoffset):
  961. if callable(tzinfos):
  962. tzdata = tzinfos(tzname, tzoffset)
  963. else:
  964. tzdata = tzinfos.get(tzname)
  965. # handle case where tzinfo is paased an options that returns None
  966. # eg tzinfos = {'BRST' : None}
  967. if isinstance(tzdata, datetime.tzinfo) or tzdata is None:
  968. tzinfo = tzdata
  969. elif isinstance(tzdata, text_type):
  970. tzinfo = tz.tzstr(tzdata)
  971. elif isinstance(tzdata, integer_types):
  972. tzinfo = tz.tzoffset(tzname, tzdata)
  973. else:
  974. raise TypeError("Offset must be tzinfo subclass, tz string, "
  975. "or int offset.")
  976. return tzinfo
  977. def _build_tzaware(self, naive, res, tzinfos):
  978. if (callable(tzinfos) or (tzinfos and res.tzname in tzinfos)):
  979. tzinfo = self._build_tzinfo(tzinfos, res.tzname, res.tzoffset)
  980. aware = naive.replace(tzinfo=tzinfo)
  981. aware = self._assign_tzname(aware, res.tzname)
  982. elif res.tzname and res.tzname in time.tzname:
  983. aware = naive.replace(tzinfo=tz.tzlocal())
  984. # Handle ambiguous local datetime
  985. aware = self._assign_tzname(aware, res.tzname)
  986. # This is mostly relevant for winter GMT zones parsed in the UK
  987. if (aware.tzname() != res.tzname and
  988. res.tzname in self.info.UTCZONE):
  989. aware = aware.replace(tzinfo=tz.UTC)
  990. elif res.tzoffset == 0:
  991. aware = naive.replace(tzinfo=tz.UTC)
  992. elif res.tzoffset:
  993. aware = naive.replace(tzinfo=tz.tzoffset(res.tzname, res.tzoffset))
  994. elif not res.tzname and not res.tzoffset:
  995. # i.e. no timezone information was found.
  996. aware = naive
  997. elif res.tzname:
  998. # tz-like string was parsed but we don't know what to do
  999. # with it
  1000. warnings.warn("tzname {tzname} identified but not understood. "
  1001. "Pass `tzinfos` argument in order to correctly "
  1002. "return a timezone-aware datetime. In a future "
  1003. "version, this will raise an "
  1004. "exception.".format(tzname=res.tzname),
  1005. category=UnknownTimezoneWarning)
  1006. aware = naive
  1007. return aware
  1008. def _build_naive(self, res, default):
  1009. repl = {}
  1010. for attr in ("year", "month", "day", "hour",
  1011. "minute", "second", "microsecond"):
  1012. value = getattr(res, attr)
  1013. if value is not None:
  1014. repl[attr] = value
  1015. if 'day' not in repl:
  1016. # If the default day exceeds the last day of the month, fall back
  1017. # to the end of the month.
  1018. cyear = default.year if res.year is None else res.year
  1019. cmonth = default.month if res.month is None else res.month
  1020. cday = default.day if res.day is None else res.day
  1021. if cday > monthrange(cyear, cmonth)[1]:
  1022. repl['day'] = monthrange(cyear, cmonth)[1]
  1023. naive = default.replace(**repl)
  1024. if res.weekday is not None and not res.day:
  1025. naive = naive + relativedelta.relativedelta(weekday=res.weekday)
  1026. return naive
  1027. def _assign_tzname(self, dt, tzname):
  1028. if dt.tzname() != tzname:
  1029. new_dt = tz.enfold(dt, fold=1)
  1030. if new_dt.tzname() == tzname:
  1031. return new_dt
  1032. return dt
  1033. def _recombine_skipped(self, tokens, skipped_idxs):
  1034. """
  1035. >>> tokens = ["foo", " ", "bar", " ", "19June2000", "baz"]
  1036. >>> skipped_idxs = [0, 1, 2, 5]
  1037. >>> _recombine_skipped(tokens, skipped_idxs)
  1038. ["foo bar", "baz"]
  1039. """
  1040. skipped_tokens = []
  1041. for i, idx in enumerate(sorted(skipped_idxs)):
  1042. if i > 0 and idx - 1 == skipped_idxs[i - 1]:
  1043. skipped_tokens[-1] = skipped_tokens[-1] + tokens[idx]
  1044. else:
  1045. skipped_tokens.append(tokens[idx])
  1046. return skipped_tokens
  1047. DEFAULTPARSER = parser()
  1048. def parse(timestr, parserinfo=None, **kwargs):
  1049. """
  1050. Parse a string in one of the supported formats, using the
  1051. ``parserinfo`` parameters.
  1052. :param timestr:
  1053. A string containing a date/time stamp.
  1054. :param parserinfo:
  1055. A :class:`parserinfo` object containing parameters for the parser.
  1056. If ``None``, the default arguments to the :class:`parserinfo`
  1057. constructor are used.
  1058. The ``**kwargs`` parameter takes the following keyword arguments:
  1059. :param default:
  1060. The default datetime object, if this is a datetime object and not
  1061. ``None``, elements specified in ``timestr`` replace elements in the
  1062. default object.
  1063. :param ignoretz:
  1064. If set ``True``, time zones in parsed strings are ignored and a naive
  1065. :class:`datetime` object is returned.
  1066. :param tzinfos:
  1067. Additional time zone names / aliases which may be present in the
  1068. string. This argument maps time zone names (and optionally offsets
  1069. from those time zones) to time zones. This parameter can be a
  1070. dictionary with timezone aliases mapping time zone names to time
  1071. zones or a function taking two parameters (``tzname`` and
  1072. ``tzoffset``) and returning a time zone.
  1073. The timezones to which the names are mapped can be an integer
  1074. offset from UTC in seconds or a :class:`tzinfo` object.
  1075. .. doctest::
  1076. :options: +NORMALIZE_WHITESPACE
  1077. >>> from dateutil.parser import parse
  1078. >>> from dateutil.tz import gettz
  1079. >>> tzinfos = {"BRST": -7200, "CST": gettz("America/Chicago")}
  1080. >>> parse("2012-01-19 17:21:00 BRST", tzinfos=tzinfos)
  1081. datetime.datetime(2012, 1, 19, 17, 21, tzinfo=tzoffset(u'BRST', -7200))
  1082. >>> parse("2012-01-19 17:21:00 CST", tzinfos=tzinfos)
  1083. datetime.datetime(2012, 1, 19, 17, 21,
  1084. tzinfo=tzfile('/usr/share/zoneinfo/America/Chicago'))
  1085. This parameter is ignored if ``ignoretz`` is set.
  1086. :param dayfirst:
  1087. Whether to interpret the first value in an ambiguous 3-integer date
  1088. (e.g. 01/05/09) as the day (``True``) or month (``False``). If
  1089. ``yearfirst`` is set to ``True``, this distinguishes between YDM and
  1090. YMD. If set to ``None``, this value is retrieved from the current
  1091. :class:`parserinfo` object (which itself defaults to ``False``).
  1092. :param yearfirst:
  1093. Whether to interpret the first value in an ambiguous 3-integer date
  1094. (e.g. 01/05/09) as the year. If ``True``, the first number is taken to
  1095. be the year, otherwise the last number is taken to be the year. If
  1096. this is set to ``None``, the value is retrieved from the current
  1097. :class:`parserinfo` object (which itself defaults to ``False``).
  1098. :param fuzzy:
  1099. Whether to allow fuzzy parsing, allowing for string like "Today is
  1100. January 1, 2047 at 8:21:00AM".
  1101. :param fuzzy_with_tokens:
  1102. If ``True``, ``fuzzy`` is automatically set to True, and the parser
  1103. will return a tuple where the first element is the parsed
  1104. :class:`datetime.datetime` datetimestamp and the second element is
  1105. a tuple containing the portions of the string which were ignored:
  1106. .. doctest::
  1107. >>> from dateutil.parser import parse
  1108. >>> parse("Today is January 1, 2047 at 8:21:00AM", fuzzy_with_tokens=True)
  1109. (datetime.datetime(2047, 1, 1, 8, 21), (u'Today is ', u' ', u'at '))
  1110. :return:
  1111. Returns a :class:`datetime.datetime` object or, if the
  1112. ``fuzzy_with_tokens`` option is ``True``, returns a tuple, the
  1113. first element being a :class:`datetime.datetime` object, the second
  1114. a tuple containing the fuzzy tokens.
  1115. :raises ValueError:
  1116. Raised for invalid or unknown string format, if the provided
  1117. :class:`tzinfo` is not in a valid format, or if an invalid date
  1118. would be created.
  1119. :raises OverflowError:
  1120. Raised if the parsed date exceeds the largest valid C integer on
  1121. your system.
  1122. """
  1123. if parserinfo:
  1124. return parser(parserinfo).parse(timestr, **kwargs)
  1125. else:
  1126. return DEFAULTPARSER.parse(timestr, **kwargs)
  1127. class _tzparser(object):
  1128. class _result(_resultbase):
  1129. __slots__ = ["stdabbr", "stdoffset", "dstabbr", "dstoffset",
  1130. "start", "end"]
  1131. class _attr(_resultbase):
  1132. __slots__ = ["month", "week", "weekday",
  1133. "yday", "jyday", "day", "time"]
  1134. def __repr__(self):
  1135. return self._repr("")
  1136. def __init__(self):
  1137. _resultbase.__init__(self)
  1138. self.start = self._attr()
  1139. self.end = self._attr()
  1140. def parse(self, tzstr):
  1141. res = self._result()
  1142. l = [x for x in re.split(r'([,:.]|[a-zA-Z]+|[0-9]+)',tzstr) if x]
  1143. used_idxs = list()
  1144. try:
  1145. len_l = len(l)
  1146. i = 0
  1147. while i < len_l:
  1148. # BRST+3[BRDT[+2]]
  1149. j = i
  1150. while j < len_l and not [x for x in l[j]
  1151. if x in "0123456789:,-+"]:
  1152. j += 1
  1153. if j != i:
  1154. if not res.stdabbr:
  1155. offattr = "stdoffset"
  1156. res.stdabbr = "".join(l[i:j])
  1157. else:
  1158. offattr = "dstoffset"
  1159. res.dstabbr = "".join(l[i:j])
  1160. for ii in range(j):
  1161. used_idxs.append(ii)
  1162. i = j
  1163. if (i < len_l and (l[i] in ('+', '-') or l[i][0] in
  1164. "0123456789")):
  1165. if l[i] in ('+', '-'):
  1166. # Yes, that's right. See the TZ variable
  1167. # documentation.
  1168. signal = (1, -1)[l[i] == '+']
  1169. used_idxs.append(i)
  1170. i += 1
  1171. else:
  1172. signal = -1
  1173. len_li = len(l[i])
  1174. if len_li == 4:
  1175. # -0300
  1176. setattr(res, offattr, (int(l[i][:2]) * 3600 +
  1177. int(l[i][2:]) * 60) * signal)
  1178. elif i + 1 < len_l and l[i + 1] == ':':
  1179. # -03:00
  1180. setattr(res, offattr,
  1181. (int(l[i]) * 3600 +
  1182. int(l[i + 2]) * 60) * signal)
  1183. used_idxs.append(i)
  1184. i += 2
  1185. elif len_li <= 2:
  1186. # -[0]3
  1187. setattr(res, offattr,
  1188. int(l[i][:2]) * 3600 * signal)
  1189. else:
  1190. return None
  1191. used_idxs.append(i)
  1192. i += 1
  1193. if res.dstabbr:
  1194. break
  1195. else:
  1196. break
  1197. if i < len_l:
  1198. for j in range(i, len_l):
  1199. if l[j] == ';':
  1200. l[j] = ','
  1201. assert l[i] == ','
  1202. i += 1
  1203. if i >= len_l:
  1204. pass
  1205. elif (8 <= l.count(',') <= 9 and
  1206. not [y for x in l[i:] if x != ','
  1207. for y in x if y not in "0123456789+-"]):
  1208. # GMT0BST,3,0,30,3600,10,0,26,7200[,3600]
  1209. for x in (res.start, res.end):
  1210. x.month = int(l[i])
  1211. used_idxs.append(i)
  1212. i += 2
  1213. if l[i] == '-':
  1214. value = int(l[i + 1]) * -1
  1215. used_idxs.append(i)
  1216. i += 1
  1217. else:
  1218. value = int(l[i])
  1219. used_idxs.append(i)
  1220. i += 2
  1221. if value:
  1222. x.week = value
  1223. x.weekday = (int(l[i]) - 1) % 7
  1224. else:
  1225. x.day = int(l[i])
  1226. used_idxs.append(i)
  1227. i += 2
  1228. x.time = int(l[i])
  1229. used_idxs.append(i)
  1230. i += 2
  1231. if i < len_l:
  1232. if l[i] in ('-', '+'):
  1233. signal = (-1, 1)[l[i] == "+"]
  1234. used_idxs.append(i)
  1235. i += 1
  1236. else:
  1237. signal = 1
  1238. used_idxs.append(i)
  1239. res.dstoffset = (res.stdoffset + int(l[i]) * signal)
  1240. # This was a made-up format that is not in normal use
  1241. warn(('Parsed time zone "%s"' % tzstr) +
  1242. 'is in a non-standard dateutil-specific format, which ' +
  1243. 'is now deprecated; support for parsing this format ' +
  1244. 'will be removed in future versions. It is recommended ' +
  1245. 'that you switch to a standard format like the GNU ' +
  1246. 'TZ variable format.', tz.DeprecatedTzFormatWarning)
  1247. elif (l.count(',') == 2 and l[i:].count('/') <= 2 and
  1248. not [y for x in l[i:] if x not in (',', '/', 'J', 'M',
  1249. '.', '-', ':')
  1250. for y in x if y not in "0123456789"]):
  1251. for x in (res.start, res.end):
  1252. if l[i] == 'J':
  1253. # non-leap year day (1 based)
  1254. used_idxs.append(i)
  1255. i += 1
  1256. x.jyday = int(l[i])
  1257. elif l[i] == 'M':
  1258. # month[-.]week[-.]weekday
  1259. used_idxs.append(i)
  1260. i += 1
  1261. x.month = int(l[i])
  1262. used_idxs.append(i)
  1263. i += 1
  1264. assert l[i] in ('-', '.')
  1265. used_idxs.append(i)
  1266. i += 1
  1267. x.week = int(l[i])
  1268. if x.week == 5:
  1269. x.week = -1
  1270. used_idxs.append(i)
  1271. i += 1
  1272. assert l[i] in ('-', '.')
  1273. used_idxs.append(i)
  1274. i += 1
  1275. x.weekday = (int(l[i]) - 1) % 7
  1276. else:
  1277. # year day (zero based)
  1278. x.yday = int(l[i]) + 1
  1279. used_idxs.append(i)
  1280. i += 1
  1281. if i < len_l and l[i] == '/':
  1282. used_idxs.append(i)
  1283. i += 1
  1284. # start time
  1285. len_li = len(l[i])
  1286. if len_li == 4:
  1287. # -0300
  1288. x.time = (int(l[i][:2]) * 3600 +
  1289. int(l[i][2:]) * 60)
  1290. elif i + 1 < len_l and l[i + 1] == ':':
  1291. # -03:00
  1292. x.time = int(l[i]) * 3600 + int(l[i + 2]) * 60
  1293. used_idxs.append(i)
  1294. i += 2
  1295. if i + 1 < len_l and l[i + 1] == ':':
  1296. used_idxs.append(i)
  1297. i += 2
  1298. x.time += int(l[i])
  1299. elif len_li <= 2:
  1300. # -[0]3
  1301. x.time = (int(l[i][:2]) * 3600)
  1302. else:
  1303. return None
  1304. used_idxs.append(i)
  1305. i += 1
  1306. assert i == len_l or l[i] == ','
  1307. i += 1
  1308. assert i >= len_l
  1309. except (IndexError, ValueError, AssertionError):
  1310. return None
  1311. unused_idxs = set(range(len_l)).difference(used_idxs)
  1312. res.any_unused_tokens = not {l[n] for n in unused_idxs}.issubset({",",":"})
  1313. return res
  1314. DEFAULTTZPARSER = _tzparser()
  1315. def _parsetz(tzstr):
  1316. return DEFAULTTZPARSER.parse(tzstr)
  1317. class ParserError(ValueError):
  1318. """Error class for representing failure to parse a datetime string."""
  1319. def __str__(self):
  1320. try:
  1321. return self.args[0] % self.args[1:]
  1322. except (TypeError, IndexError):
  1323. return super(ParserError, self).__str__()
  1324. def __repr__(self):
  1325. return "%s(%s)" % (self.__class__.__name__, str(self))
  1326. class UnknownTimezoneWarning(RuntimeWarning):
  1327. """Raised when the parser finds a timezone it cannot parse into a tzinfo"""
  1328. # vim:ts=4:sw=4:et