diff options
author | Robert Collins <rbtcollins@hp.com> | 2015-08-01 08:20:04 +1200 |
---|---|---|
committer | Robert Collins <rbtcollins@hp.com> | 2015-08-01 08:20:04 +1200 |
commit | 354b6e50bb23b8801b6d5636231f0662b5bfdecc (patch) | |
tree | 4fa1cd06b67e32c93c92fa704aafaf94e8d2e613 /Lib/email | |
parent | 12f896c52128178b8b220ba63084cd68a4c15574 (diff) | |
parent | 19fae1a932e48d2fd6434ba39685ab733d463097 (diff) | |
download | cpython-git-354b6e50bb23b8801b6d5636231f0662b5bfdecc.tar.gz |
Issue #22932: Fix timezones in email.utils.formatdate.
Patch from Dmitry Shachnev.
Diffstat (limited to 'Lib/email')
-rw-r--r-- | Lib/email/utils.py | 30 |
1 files changed, 7 insertions, 23 deletions
diff --git a/Lib/email/utils.py b/Lib/email/utils.py index 317fdfa3b6..5080d81909 100644 --- a/Lib/email/utils.py +++ b/Lib/email/utils.py @@ -155,30 +155,14 @@ def formatdate(timeval=None, localtime=False, usegmt=False): # 2822 requires that day and month names be the English abbreviations. if timeval is None: timeval = time.time() - if localtime: - now = time.localtime(timeval) - # Calculate timezone offset, based on whether the local zone has - # daylight savings time, and whether DST is in effect. - if time.daylight and now[-1]: - offset = time.altzone - else: - offset = time.timezone - hours, minutes = divmod(abs(offset), 3600) - # Remember offset is in seconds west of UTC, but the timezone is in - # minutes east of UTC, so the signs differ. - if offset > 0: - sign = '-' - else: - sign = '+' - zone = '%s%02d%02d' % (sign, hours, minutes // 60) + if localtime or usegmt: + dt = datetime.datetime.fromtimestamp(timeval, datetime.timezone.utc) else: - now = time.gmtime(timeval) - # Timezone offset is always -0000 - if usegmt: - zone = 'GMT' - else: - zone = '-0000' - return _format_timetuple_and_zone(now, zone) + dt = datetime.datetime.utcfromtimestamp(timeval) + if localtime: + dt = dt.astimezone() + usegmt = False + return format_datetime(dt, usegmt) def format_datetime(dt, usegmt=False): """Turn a datetime into a date string as specified in RFC 2822. |