MCPcopy Index your code
hub / github.com/python/cpython / astimezone

Method astimezone

Lib/_pydatetime.py:2126–2149  ·  view source on GitHub ↗
(self, tz=None)

Source from the content-addressed store, hash-verified

2124 return timezone(timedelta(seconds=gmtoff), zone)
2125
2126 def astimezone(self, tz=None):
2127 if tz is None:
2128 tz = self._local_timezone()
2129 elif not isinstance(tz, tzinfo):
2130 raise TypeError("tz argument must be an instance of tzinfo")
2131
2132 mytz = self.tzinfo
2133 if mytz is None:
2134 mytz = self._local_timezone()
2135 myoffset = mytz.utcoffset(self)
2136 else:
2137 myoffset = mytz.utcoffset(self)
2138 if myoffset is None:
2139 mytz = self.replace(tzinfo=None)._local_timezone()
2140 myoffset = mytz.utcoffset(self)
2141
2142 if tz is mytz:
2143 return self
2144
2145 # Convert self to UTC, and attach the new time zone object.
2146 utc = (self - myoffset).replace(tzinfo=tz)
2147
2148 # Convert from UTC to tz's local time.
2149 return tz.fromutc(utc)
2150
2151 # Ways to produce a string.
2152

Callers 15

file_mtimeFunction · 0.80
Time2InternaldateFunction · 0.80
_date_to_stringFunction · 0.80
_write_objectMethod · 0.80
formatdateFunction · 0.80
localtimeFunction · 0.80
test_astimezoneMethod · 0.80
test_tzinfo_nowMethod · 0.80
test_utctimetupleMethod · 0.80
test_more_astimezoneMethod · 0.80

Calls 5

_local_timezoneMethod · 0.95
replaceMethod · 0.95
utcoffsetMethod · 0.45
replaceMethod · 0.45
fromutcMethod · 0.45