summaryrefslogtreecommitdiffstats
path: root/Lib/email/utils.py
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2012-08-23 01:34:00 (GMT)
committerR David Murray <rdmurray@bitdance.com>2012-08-23 01:34:00 (GMT)
commitb8687df6532b809f41d77dfc617363d3c093ec88 (patch)
tree48e09484522d82be90843fe16584126f41a63116 /Lib/email/utils.py
parent17183a2972e8981c0d0ca2d599b6e08fc662fc28 (diff)
downloadcpython-b8687df6532b809f41d77dfc617363d3c093ec88.zip
cpython-b8687df6532b809f41d77dfc617363d3c093ec88.tar.gz
cpython-b8687df6532b809f41d77dfc617363d3c093ec88.tar.bz2
#665194: Update email.utils.localtime to use astimezone, and fix bug.
The new code correctly handles historic changes in UTC offsets. A test for this should follow. Original patch by Alexander Belopolsky.
Diffstat (limited to 'Lib/email/utils.py')
-rw-r--r--Lib/email/utils.py51
1 files changed, 22 insertions, 29 deletions
diff --git a/Lib/email/utils.py b/Lib/email/utils.py
index 39f7903..c6204da 100644
--- a/Lib/email/utils.py
+++ b/Lib/email/utils.py
@@ -386,33 +386,26 @@ def localtime(dt=None, isdst=-1):
"""
if dt is None:
- seconds = time.time()
- else:
- if dt.tzinfo is None:
- # A naive datetime is given. Convert to a (localtime)
- # timetuple and pass to system mktime together with
- # the isdst hint. System mktime will return seconds
- # sysce epoch.
- tm = dt.timetuple()[:-1] + (isdst,)
- seconds = time.mktime(tm)
+ dt = datetime.datetime.now(datetime.timezone.utc)
+ if dt.tzinfo is not None:
+ return dt.astimezone()
+ # We have a naive datetime. Convert to a (localtime) timetuple and pass to
+ # system mktime together with the isdst hint. System mktime will return
+ # seconds since epoch.
+ tm = dt.timetuple()[:-1] + (isdst,)
+ seconds = time.mktime(tm)
+ localtm = time.localtime(seconds)
+ try:
+ delta = datetime.timedelta(seconds=localtm.tm_gmtoff)
+ tz = datetime.timezone(delta, localtm.tm_zone)
+ except AttributeError:
+ # Compute UTC offset and compare with the value implied by tm_isdst.
+ # If the values match, use the zone name implied by tm_isdst.
+ delta = dt - datetime.datetime(*time.gmtime(ts)[:6])
+ dst = time.daylight and localtm.tm_isdst > 0
+ gmtoff = -(time.altzone if dst else time.timezone)
+ if delta == datetime.timedelta(seconds=gmtoff):
+ tz = datetime.timezone(delta, time.tzname[dst])
else:
- # An aware datetime is given. Use aware datetime
- # arithmetics to find seconds since epoch.
- delta = dt - datetime.datetime(1970, 1, 1,
- tzinfo=datetime.timezone.utc)
- seconds = delta.total_seconds()
- tm = time.localtime(seconds)
-
- # XXX: The following logic may not work correctly if UTC
- # offset has changed since time provided in dt. This will be
- # corrected in C implementation for platforms that support
- # tm_gmtoff.
- if time.daylight and tm.tm_isdst:
- offset = time.altzone
- tzname = time.tzname[1]
- else:
- offset = time.timezone
- tzname = time.tzname[0]
-
- tz = datetime.timezone(datetime.timedelta(seconds=-offset), tzname)
- return datetime.datetime.fromtimestamp(seconds, tz)
+ tz = datetime.timezone(delta)
+ return dt.replace(tzinfo=tz)