| 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 | |