from __future__ import print_function from datetime import datetime, timedelta, tzinfo def fromutc(dt): "fromutc function copied from the Python docs" # raise ValueError error if dt.tzinfo is not self dtoff = dt.utcoffset() dtdst = dt.dst() # raise ValueError if dtoff is None or dtdst is None delta = dtoff - dtdst # this is the timezones's standard offset if delta: dt += delta # convert to standard local time dtdst = dt.dst() # raise ValueError if dtdst is None if dtdst: return dt + dtdst else: return dt DSTDIFF = DSTOFFSET = timedelta(hours = 1) class UKSummerTime(tzinfo): """Simple time zone which pretends to always be in summer time, since that's what shows the failure. """ def utcoffset(self, dt): return DSTOFFSET def dst(self, dt): return DSTDIFF def tzname(self, dt): return 'UKSummerTime' stz = UKSummerTime() aware = datetime(2014, 4, 26, 12, 1, tzinfo=stz) print(aware) # This is the line which I believe is wrong -- it should now have converted # the datetime value to the target timezone, but hasn't. conv = stz.fromutc(aware) print(conv) ref_conv = fromutc(aware) print(ref_conv)