ISO Datetimes With Timezones In Python

November - 2020
import pytz

from dateutil.parser import isoparse

def make_eastern_time(*, input):
    naive_datetime = isoparse(input)
    nyc_timezone = pytz.timezone("America/New_York")
    nyc_aware_datetime = nyc_timezone.localize(naive_datetime)
    iso_formatted_string = nyc_aware_datetime.isoformat('T', 'seconds')
    return iso_formatted_string
    
# Example: Eastern Daylight Time
print(make_eastern_time(input='2020-07-15T12:30:00'))
# >>>>>>>>>>>>>>>>>>>>> OUTPUT 2020-07-15T12:30:00-04:00

# Example: Eastern Standard Time
print(make_eastern_time(input='2020-11-15T12:30:00'))
# >>>>>>>>>>>>>>>>>>>>> OUTPUT 2020-11-15T12:30:00-05:00