Represents a named timezone. The accepted names are those provided by the IANA time zone database. >>> from pendulum.tz.timezone import Timezone >>> tz = Timezone('Europe/Paris')
| 51 | |
| 52 | |
| 53 | class Timezone(zoneinfo.ZoneInfo, PendulumTimezone): |
| 54 | """ |
| 55 | Represents a named timezone. |
| 56 | |
| 57 | The accepted names are those provided by the IANA time zone database. |
| 58 | |
| 59 | >>> from pendulum.tz.timezone import Timezone |
| 60 | >>> tz = Timezone('Europe/Paris') |
| 61 | """ |
| 62 | |
| 63 | def __new__(cls, key: str) -> Self: |
| 64 | try: |
| 65 | return super().__new__(cls, key) # type: ignore[call-arg] |
| 66 | except zoneinfo.ZoneInfoNotFoundError: |
| 67 | raise InvalidTimezone(key) |
| 68 | |
| 69 | def __eq__(self, other: object) -> bool: |
| 70 | return isinstance(other, Timezone) and self.key == other.key |
| 71 | |
| 72 | @property |
| 73 | def name(self) -> str: |
| 74 | return self.key |
| 75 | |
| 76 | def convert(self, dt: _DT, raise_on_unknown_times: bool = False) -> _DT: |
| 77 | """ |
| 78 | Converts a datetime in the current timezone. |
| 79 | |
| 80 | If the datetime is naive, it will be normalized. |
| 81 | |
| 82 | >>> from datetime import datetime |
| 83 | >>> from pendulum import timezone |
| 84 | >>> paris = timezone('Europe/Paris') |
| 85 | >>> dt = datetime(2013, 3, 31, 2, 30, fold=1) |
| 86 | >>> in_paris = paris.convert(dt) |
| 87 | >>> in_paris.isoformat() |
| 88 | '2013-03-31T03:30:00+02:00' |
| 89 | |
| 90 | If the datetime is aware, it will be properly converted. |
| 91 | |
| 92 | >>> new_york = timezone('America/New_York') |
| 93 | >>> in_new_york = new_york.convert(in_paris) |
| 94 | >>> in_new_york.isoformat() |
| 95 | '2013-03-30T21:30:00-04:00' |
| 96 | """ |
| 97 | |
| 98 | if dt.tzinfo is None: |
| 99 | # Technically, utcoffset() can return None, but none of the zone information |
| 100 | # in tzdata sets _tti_before to None. This can be checked with the following |
| 101 | # code: |
| 102 | # |
| 103 | # >>> import zoneinfo |
| 104 | # >>> from zoneinfo._zoneinfo import ZoneInfo |
| 105 | # |
| 106 | # >>> for tzname in zoneinfo.available_timezones(): |
| 107 | # >>> if ZoneInfo(tzname)._tti_before is None: |
| 108 | # >>> print(tzname) |
| 109 | |
| 110 | offset_before = cast( |
no outgoing calls
no test coverage detected
searching dependent graphs…