| 2399 | |
| 2400 | |
| 2401 | class timezone(tzinfo): |
| 2402 | __slots__ = '_offset', '_name' |
| 2403 | |
| 2404 | # Sentinel value to disallow None |
| 2405 | _Omitted = object() |
| 2406 | def __new__(cls, offset, name=_Omitted): |
| 2407 | if not isinstance(offset, timedelta): |
| 2408 | raise TypeError("offset must be a timedelta") |
| 2409 | if name is cls._Omitted: |
| 2410 | if not offset: |
| 2411 | return cls.utc |
| 2412 | name = None |
| 2413 | elif not isinstance(name, str): |
| 2414 | raise TypeError("name must be a string") |
| 2415 | if not cls._minoffset <= offset <= cls._maxoffset: |
| 2416 | raise ValueError("offset must be a timedelta " |
| 2417 | "strictly between -timedelta(hours=24) and " |
| 2418 | f"timedelta(hours=24), not {offset!r}") |
| 2419 | return cls._create(offset, name) |
| 2420 | |
| 2421 | def __init_subclass__(cls): |
| 2422 | raise TypeError("type 'datetime.timezone' is not an acceptable base type") |
| 2423 | |
| 2424 | @classmethod |
| 2425 | def _create(cls, offset, name=None): |
| 2426 | self = tzinfo.__new__(cls) |
| 2427 | self._offset = offset |
| 2428 | self._name = name |
| 2429 | return self |
| 2430 | |
| 2431 | def __getinitargs__(self): |
| 2432 | """pickle support""" |
| 2433 | if self._name is None: |
| 2434 | return (self._offset,) |
| 2435 | return (self._offset, self._name) |
| 2436 | |
| 2437 | def __eq__(self, other): |
| 2438 | if isinstance(other, timezone): |
| 2439 | return self._offset == other._offset |
| 2440 | return NotImplemented |
| 2441 | |
| 2442 | def __hash__(self): |
| 2443 | return hash(self._offset) |
| 2444 | |
| 2445 | def __repr__(self): |
| 2446 | """Convert to formal string, for repr(). |
| 2447 | |
| 2448 | >>> tz = timezone.utc |
| 2449 | >>> repr(tz) |
| 2450 | 'datetime.timezone.utc' |
| 2451 | >>> tz = timezone(timedelta(hours=-5), 'EST') |
| 2452 | >>> repr(tz) |
| 2453 | "datetime.timezone(datetime.timedelta(-1, 68400), 'EST')" |
| 2454 | """ |
| 2455 | if self is self.utc: |
| 2456 | return 'datetime.timezone.utc' |
| 2457 | if self._name is None: |
| 2458 | return "%s%s(%r)" % (_get_class_module(self), |