cpython: 479787100b91 (original) (raw)
Mercurial > cpython
changeset 97170:479787100b91
Issue #22932: Fix timezones in email.utils.formatdate. Patch from Dmitry Shachnev. [#22932]
Robert Collins rbtcollins@hp.com | |
---|---|
date | Sat, 01 Aug 2015 08:20:04 +1200 |
parents | c13839a3d4fc(current diff)94b43b36e464(diff) |
children | 74fc1af57c72 |
files | Misc/ACKS Misc/NEWS |
diffstat | 4 files changed, 31 insertions(+), 23 deletions(-)[+] [-] Lib/email/utils.py 30 Lib/test/test_email/test_utils.py 20 Misc/ACKS 1 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/email/utils.py +++ b/Lib/email/utils.py @@ -155,30 +155,14 @@ def formatdate(timeval=None, localtime=F # 2822 requires that day and month names be the English abbreviations. if timeval is None: timeval = time.time()
- if localtime or usegmt:
dt = datetime.datetime.fromtimestamp(timeval, datetime.timezone.utc)[](#l1.8)
- else:
if localtime:dt = datetime.datetime.utcfromtimestamp(timeval)[](#l1.10)
now = time.localtime(timeval)[](#l1.12)
# Calculate timezone offset, based on whether the local zone has[](#l1.13)
# daylight savings time, and whether DST is in effect.[](#l1.14)
if time.daylight and now[-1]:[](#l1.15)
offset = time.altzone[](#l1.16)
else:[](#l1.17)
offset = time.timezone[](#l1.18)
hours, minutes = divmod(abs(offset), 3600)[](#l1.19)
# Remember offset is in seconds west of UTC, but the timezone is in[](#l1.20)
# minutes east of UTC, so the signs differ.[](#l1.21)
if offset > 0:[](#l1.22)
sign = '-'[](#l1.23)
else:[](#l1.24)
sign = '+'[](#l1.25)
zone = '%s%02d%02d' % (sign, hours, minutes // 60)[](#l1.26)
- else:
now = time.gmtime(timeval)[](#l1.28)
# Timezone offset is always -0000[](#l1.29)
if usegmt:[](#l1.30)
zone = 'GMT'[](#l1.31)
else:[](#l1.32)
zone = '-0000'[](#l1.33)
- return _format_timetuple_and_zone(now, zone)
def format_datetime(dt, usegmt=False): """Turn a datetime into a date string as specified in RFC 2822.
--- a/Lib/test/test_email/test_utils.py +++ b/Lib/test/test_email/test_utils.py @@ -136,5 +136,25 @@ class LocaltimeTests(unittest.TestCase): t1 = utils.localtime(t0) self.assertEqual(t1.tzname(), 'EET') +class FormatDateTests(unittest.TestCase): +
- @test.support.run_with_tz('Europe/Minsk')
- def test_formatdate(self):
timeval = time.mktime((2011, 12, 1, 18, 0, 0, 4, 335, 0))[](#l2.11)
string = utils.formatdate(timeval, localtime=False, usegmt=False)[](#l2.12)
self.assertEqual(string, 'Thu, 01 Dec 2011 15:00:00 -0000')[](#l2.13)
string = utils.formatdate(timeval, localtime=False, usegmt=True)[](#l2.14)
self.assertEqual(string, 'Thu, 01 Dec 2011 15:00:00 GMT')[](#l2.15)
- @test.support.run_with_tz('Europe/Minsk')
- def test_formatdate_with_localtime(self):
timeval = time.mktime((2011, 1, 1, 18, 0, 0, 6, 1, 0))[](#l2.19)
string = utils.formatdate(timeval, localtime=True)[](#l2.20)
self.assertEqual(string, 'Sat, 01 Jan 2011 18:00:00 +0200')[](#l2.21)
# Minsk moved from +0200 (with DST) to +0300 (without DST) in 2011[](#l2.22)
timeval = time.mktime((2011, 12, 1, 18, 0, 0, 4, 335, 0))[](#l2.23)
string = utils.formatdate(timeval, localtime=True)[](#l2.24)
self.assertEqual(string, 'Thu, 01 Dec 2011 18:00:00 +0300')[](#l2.25)
+ if name == 'main': unittest.main()
--- a/Misc/ACKS +++ b/Misc/ACKS @@ -1282,6 +1282,7 @@ Jerry Seutter Pete Sevander Denis Severson Ian Seyer +Dmitry Shachnev Daniel Shahaf Ha Shao Mark Shannon
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -13,6 +13,9 @@ Core and Builtins Library ------- +- Issue #22932: Fix timezones in email.utils.formatdate.