| 2085 | return timezone(timedelta(seconds=gmtoff), zone) |
| 2086 | |
| 2087 | def astimezone(self, tz=None): |
| 2088 | if tz is None: |
| 2089 | tz = self._local_timezone() |
| 2090 | elif not isinstance(tz, tzinfo): |
| 2091 | raise TypeError("tz argument must be an instance of tzinfo") |
| 2092 | |
| 2093 | mytz = self.tzinfo |
| 2094 | if mytz is None: |
| 2095 | mytz = self._local_timezone() |
| 2096 | myoffset = mytz.utcoffset(self) |
| 2097 | else: |
| 2098 | myoffset = mytz.utcoffset(self) |
| 2099 | if myoffset is None: |
| 2100 | mytz = self.replace(tzinfo=None)._local_timezone() |
| 2101 | myoffset = mytz.utcoffset(self) |
| 2102 | |
| 2103 | if tz is mytz: |
| 2104 | return self |
| 2105 | |
| 2106 | # Convert self to UTC, and attach the new time zone object. |
| 2107 | utc = (self - myoffset).replace(tzinfo=tz) |
| 2108 | |
| 2109 | # Convert from UTC to tz's local time. |
| 2110 | return tz.fromutc(utc) |
| 2111 | |
| 2112 | # Ways to produce a string. |
| 2113 | |