Convert aware :class:`~datetime.datetime` to another timezone. Using a ZoneInfo timezone will give the most flexibility in terms of ambiguous DST handling.
(dt: datetime, tz: tzinfo)
| 358 | |
| 359 | |
| 360 | def localize(dt: datetime, tz: tzinfo) -> datetime: |
| 361 | """Convert aware :class:`~datetime.datetime` to another timezone. |
| 362 | |
| 363 | Using a ZoneInfo timezone will give the most flexibility in terms of ambiguous DST handling. |
| 364 | """ |
| 365 | if is_naive(dt): # Ensure timezone aware datetime |
| 366 | dt = make_aware(dt, tz) |
| 367 | if dt.tzinfo == ZoneInfo("UTC"): |
| 368 | dt = dt.astimezone(tz) # Always safe to call astimezone on utc zones |
| 369 | return dt |
| 370 | |
| 371 | |
| 372 | def to_utc(dt: datetime) -> datetime: |