Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

ISO Datetimes With Timezones In Python

Code

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