| 2291 | |
| 2292 | |
| 2293 | class timezone(tzinfo): |
| 2294 | __slots__ = '_offset', '_name' |
| 2295 | |
| 2296 | # Sentinel value to disallow None |
| 2297 | _Omitted = object() |
| 2298 | def __new__(cls, offset, name=_Omitted): |
| 2299 | if not isinstance(offset, timedelta): |
| 2300 | raise TypeError("offset must be a timedelta") |
| 2301 | if name is cls._Omitted: |
| 2302 | if not offset: |
| 2303 | return cls.utc |
| 2304 | name = None |
| 2305 | elif not isinstance(name, str): |
| 2306 | raise TypeError("name must be a string") |
| 2307 | if not cls._minoffset <= offset <= cls._maxoffset: |
| 2308 | raise ValueError("offset must be a timedelta " |
| 2309 | "strictly between -timedelta(hours=24) and " |
| 2310 | "timedelta(hours=24).") |
| 2311 | return cls._create(offset, name) |
| 2312 | |
| 2313 | @classmethod |
| 2314 | def _create(cls, offset, name=None): |
| 2315 | self = tzinfo.__new__(cls) |
| 2316 | self._offset = offset |
| 2317 | self._name = name |
| 2318 | return self |
| 2319 | |
| 2320 | def __getinitargs__(self): |
| 2321 | """pickle support""" |
| 2322 | if self._name is None: |
| 2323 | return (self._offset,) |
| 2324 | return (self._offset, self._name) |
| 2325 | |
| 2326 | def __eq__(self, other): |
| 2327 | if isinstance(other, timezone): |
| 2328 | return self._offset == other._offset |
| 2329 | return NotImplemented |
| 2330 | |
| 2331 | def __hash__(self): |
| 2332 | return hash(self._offset) |
| 2333 | |
| 2334 | def __repr__(self): |
| 2335 | """Convert to formal string, for repr(). |
| 2336 | |
| 2337 | >>> tz = timezone.utc |
| 2338 | >>> repr(tz) |
| 2339 | 'datetime.timezone.utc' |
| 2340 | >>> tz = timezone(timedelta(hours=-5), 'EST') |
| 2341 | >>> repr(tz) |
| 2342 | "datetime.timezone(datetime.timedelta(-1, 68400), 'EST')" |
| 2343 | """ |
| 2344 | if self is self.utc: |
| 2345 | return 'datetime.timezone.utc' |
| 2346 | if self._name is None: |
| 2347 | return "%s.%s(%r)" % (self.__class__.__module__, |
| 2348 | self.__class__.__qualname__, |
| 2349 | self._offset) |
| 2350 | return "%s.%s(%r, %r)" % (self.__class__.__module__, |
no test coverage detected