Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db01ee0

Browse files
committed
Patch by Mike Meyer:
Extended the rfc822 parsedate routines to handle the cases they failed on in an archive of ~37,000 messages. I believe the changes are compatible, in that all previously correct parsing are still correct. [I still see problems with some messages, but no showstoppers.]
1 parent 9a4d637 commit db01ee0

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

Lib/rfc822.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -748,9 +748,11 @@ def dump_address_pair(pair):
748748

749749
# Parse a date field
750750

751-
_monthnames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
752-
'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
753-
_daynames = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
751+
_monthnames = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul',
752+
'aug', 'sep', 'oct', 'nov', 'dec',
753+
'january', 'february', 'march', 'april', 'may', 'june', 'july',
754+
'august', 'september', 'october', 'november', 'december']
755+
_daynames = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
754756

755757
# The timezone table does not include the military time zones defined
756758
# in RFC822, other than Z. According to RFC1123, the description in
@@ -773,7 +775,7 @@ def parsedate_tz(data):
773775
Accounts for military timezones.
774776
"""
775777
data = string.split(data)
776-
if data[0][-1] == ',' or data[0] in _daynames:
778+
if data[0][-1] in (',', '.') or string.lower(data[0]) in _daynames:
777779
# There's a dayname here. Skip it
778780
del data[0]
779781
if len(data) == 3: # RFC 850 date, deprecated
@@ -791,11 +793,23 @@ def parsedate_tz(data):
791793
return None
792794
data = data[:5]
793795
[dd, mm, yy, tm, tz] = data
796+
mm = string.lower(mm)
794797
if not mm in _monthnames:
795-
dd, mm, yy, tm, tz = mm, dd, tm, yy, tz
798+
dd, mm = mm, string.lower(dd)
796799
if not mm in _monthnames:
797800
return None
798801
mm = _monthnames.index(mm)+1
802+
if dd[-1] == ',':
803+
dd = dd[:-1]
804+
i = string.find(yy, ':')
805+
if i > 0:
806+
yy, tm = tm, yy
807+
if yy[-1] == ',':
808+
yy = yy[:-1]
809+
if yy[0] not in string.digits:
810+
yy, tz = tz, yy
811+
if tm[-1] == ',':
812+
tm = tm[:-1]
799813
tm = string.splitfields(tm, ':')
800814
if len(tm) == 2:
801815
[thh, tmm] = tm

0 commit comments

Comments
 (0)