diff -r e088fecfe288 Doc/library/email.util.rst --- a/Doc/library/email.util.rst Mon Sep 20 23:28:36 2010 -0400 +++ b/Doc/library/email.util.rst Mon Sep 20 23:39:28 2010 -0400 @@ -70,8 +70,9 @@ a 10-tuple; the first 9 elements make up a tuple that can be passed directly to :func:`time.mktime`, and the tenth is the offset of the date's timezone from UTC (which is the official term for Greenwich Mean Time) [#]_. If the input string - has no timezone, the last element of the tuple returned is ``None``. Note that - indexes 6, 7, and 8 of the result tuple are not usable. + has no timezone, the last element of the tuple returned is ``None``. Since + daylight savings time cannot be known from parsing a date string, the value of + the eigth element of the tuple is always -1. .. function:: mktime_tz(tuple) diff -r e088fecfe288 Lib/email/_parseaddr.py --- a/Lib/email/_parseaddr.py Mon Sep 20 23:28:36 2010 -0400 +++ b/Lib/email/_parseaddr.py Mon Sep 20 23:39:28 2010 -0400 @@ -13,8 +13,13 @@ 'quote', ] +import calendar import time +# Value used in a time struct when daylight savings time is unknown. For more +# information, see the documentation in the time module. +DST_UNKNOWN = -1 + SPACE = ' ' EMPTYSTRING = '' COMMASPACE = ', ' @@ -48,14 +53,16 @@ Accounts for military timezones. """ data = data.split() + weekday = None # The FWS after the comma after the day-of-week is optional, so search and # adjust for this. if data[0].endswith(',') or data[0].lower() in _daynames: - # There's a dayname here. Skip it - del data[0] + # There's a dayname here. Get its corresponding integer representation. + weekday = _daynames.index(data.pop(0).strip(',').lower()) else: i = data[0].rfind(',') if i >= 0: + weekday = _daynames.index(data[0][:i].lower()) data[0] = data[0][i+1:] if len(data) == 3: # RFC 850 date, deprecated stuff = data[0].split('-') @@ -136,8 +143,12 @@ else: tzsign = 1 tzoffset = tzsign * ( (tzoffset//100)*3600 + (tzoffset % 100)*60) - # Daylight Saving Time flag is set to -1, since DST is unknown. - return yy, mm, dd, thh, tmm, tss, 0, 1, -1, tzoffset + # If no weekday was parsed from the string, compute it from the date. + if weekday is None: + weekday = calendar.weekday(yy, mm, dd) + dayofyear = calendar.dayofyear(yy, mm, dd) + # Daylight savings time cannot be known from parsing this date string. + return yy, mm, dd, thh, tmm, tss, weekday, dayofyear, DST_UNKNOWN, tzoffset def parsedate(data): diff -r e088fecfe288 Lib/email/test/test_email.py --- a/Lib/email/test/test_email.py Mon Sep 20 23:28:36 2010 -0400 +++ b/Lib/email/test/test_email.py Mon Sep 20 23:39:28 2010 -0400 @@ -2213,15 +2213,24 @@ self.assertEqual(utils.parsedate('Wed,3 Apr 2002 14:58:26 +0800'), utils.parsedate('Wed, 3 Apr 2002 14:58:26 +0800')) + def test_parsedate_tz_dayofweek(self): + """Test for parsing the day of week from the date string.""" + self.assertEqual(utils.parsedate_tz('Wed,3 Apr 2002 14:58:26 +0800'), + (2002, 4, 3, 14, 58, 26, 2, 93, -1, +28800)) + self.assertEqual(utils.parsedate_tz('Wed, 3 Apr 2002 14:58:26 +0800'), + (2002, 4, 3, 14, 58, 26, 2, 93, -1, +28800)) + self.assertEqual(utils.parsedate_tz('Wed 3 Apr 2002 14:58:26 +0800'), + (2002, 4, 3, 14, 58, 26, 2, 93, -1, +28800)) + def test_parsedate_no_dayofweek(self): eq = self.assertEqual eq(utils.parsedate_tz('25 Feb 2003 13:47:26 -0800'), - (2003, 2, 25, 13, 47, 26, 0, 1, -1, -28800)) + (2003, 2, 25, 13, 47, 26, 1, 56, -1, -28800)) def test_parsedate_compact_no_dayofweek(self): eq = self.assertEqual eq(utils.parsedate_tz('5 Feb 2003 13:47:26 -0800'), - (2003, 2, 5, 13, 47, 26, 0, 1, -1, -28800)) + (2003, 2, 5, 13, 47, 26, 2, 36, -1, -28800)) def test_parsedate_acceptable_to_time_functions(self): eq = self.assertEqual