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)
| 445 | |
| 446 | |
| 447 | def localize(dt: datetime, tz: tzinfo) -> datetime: |
| 448 | """Convert aware :class:`~datetime.datetime` to another timezone. |
| 449 | |
| 450 | Using a ZoneInfo timezone will give the most flexibility in terms of ambiguous DST handling. |
| 451 | """ |
| 452 | if is_naive(dt): # Ensure timezone aware datetime |
| 453 | dt = make_aware(dt, tz) |
| 454 | if dt.tzinfo == ZoneInfo("UTC"): |
| 455 | dt = dt.astimezone(tz) # Always safe to call astimezone on utc zones |
| 456 | return dt |
| 457 | |
| 458 | |
| 459 | def to_utc(dt: datetime) -> datetime: |