Last active
February 13, 2024 19:22
-
-
Save kgriffs/0321be9e627bf42a6dae31028a2523e3 to your computer and use it in GitHub Desktop.
Python TZ Aware datetime and DST Conversion Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import pytz | |
tz_ny = pytz.timezone('America/New_York') | |
dt_aware_dst_active = tz_ny.localize(datetime.datetime(2023, 5, 1, 12, 0)) | |
dt_aware_dst_inactive = tz_ny.localize(datetime.datetime(2023, 12, 1, 12, 0)) | |
print("Original DST Active Offset (America/New_York):", dt_aware_dst_active.dst()) | |
print("Original DST Active (America/New_York):", dt_aware_dst_active) | |
print("To UTC, DST Active (America/New_York):", dt_aware_dst_active.astimezone(pytz.utc)) | |
print("Roundtrip, DST Active (America/New_York):", dt_aware_dst_active.astimezone(pytz.utc).astimezone(tz_ny)) | |
print() | |
print("Original DST Inactive Offset (America/New_York):", dt_aware_dst_inactive.dst()) | |
print("Original DST Inactive (America/New_York):", dt_aware_dst_inactive) | |
print("To UTC, DST Inactive (America/New_York):", dt_aware_dst_inactive.astimezone(pytz.utc)) | |
print("Roundtrip, DST Inactive (America/New_York):", dt_aware_dst_inactive.astimezone(pytz.utc).astimezone(tz_ny)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: