Fixed offset from UTC implementation of tzinfo.
| 2442 | |
| 2443 | |
| 2444 | class timezone(tzinfo): |
| 2445 | """Fixed offset from UTC implementation of tzinfo.""" |
| 2446 | |
| 2447 | __slots__ = '_offset', '_name' |
| 2448 | |
| 2449 | # Sentinel value to disallow None |
| 2450 | _Omitted = object() |
| 2451 | def __new__(cls, offset, name=_Omitted): |
| 2452 | if not isinstance(offset, timedelta): |
| 2453 | raise TypeError("offset must be a timedelta") |
| 2454 | if name is cls._Omitted: |
| 2455 | if not offset: |
| 2456 | return cls.utc |
| 2457 | name = None |
| 2458 | elif not isinstance(name, str): |
| 2459 | raise TypeError("name must be a string") |
| 2460 | if not cls._minoffset <= offset <= cls._maxoffset: |
| 2461 | raise ValueError("offset must be a timedelta " |
| 2462 | "strictly between -timedelta(hours=24) and " |
| 2463 | f"timedelta(hours=24), not {offset!r}") |
| 2464 | return cls._create(offset, name) |
| 2465 | |
| 2466 | def __init_subclass__(cls): |
| 2467 | raise TypeError("type 'datetime.timezone' is not an acceptable base type") |
| 2468 | |
| 2469 | @classmethod |
| 2470 | def _create(cls, offset, name=None): |
| 2471 | self = tzinfo.__new__(cls) |
| 2472 | self._offset = offset |
| 2473 | self._name = name |
| 2474 | return self |
| 2475 | |
| 2476 | def __getinitargs__(self): |
| 2477 | """pickle support""" |
| 2478 | if self._name is None: |
| 2479 | return (self._offset,) |
| 2480 | return (self._offset, self._name) |
| 2481 | |
| 2482 | def __eq__(self, other): |
| 2483 | if isinstance(other, timezone): |
| 2484 | return self._offset == other._offset |
| 2485 | return NotImplemented |
| 2486 | |
| 2487 | def __hash__(self): |
| 2488 | return hash(self._offset) |
| 2489 | |
| 2490 | def __repr__(self): |
| 2491 | """Convert to formal string, for repr(). |
| 2492 | |
| 2493 | >>> tz = timezone.utc |
| 2494 | >>> repr(tz) |
| 2495 | 'datetime.timezone.utc' |
| 2496 | >>> tz = timezone(timedelta(hours=-5), 'EST') |
| 2497 | >>> repr(tz) |
| 2498 | "datetime.timezone(datetime.timedelta(-1, 68400), 'EST')" |
| 2499 | """ |
| 2500 | if self is self.utc: |
| 2501 | return 'datetime.timezone.utc' |
searching dependent graphs…