We can use the datetime and zoneinfo built-in modules to reliably convert between times and dates in different time zones. The zoneinfo built-in module contains a full database of every time zone definition you might need:
In the code above, we basically parse string to datetime using strptime. Since the string didn’t have any timezone info, print(nyc_dt_naive.tzinfo) will give us None. However, we know it’s a NYC datetime, so we can set the tzinfo explicitly by .replace(tzinfo=eastern), where eastern is a ZoneInfo object.
Tip
To use zoneinfo effectively, you should always convert local times to UTC first. Perform any datetime operations you need on the UTC values (such as offsetting). Then, convert to local times as a final step.