194. Time Zone Handling
1. Getting Current Time in UTC
from datetime import datetime
import pytz
# Get current time in UTC
utc_now = datetime.now(pytz.utc)
print("Current time in UTC:", utc_now)2. Converting Between Time Zones
from datetime import datetime
import pytz
# Define time zones
eastern = pytz.timezone('US/Eastern')
london = pytz.timezone('Europe/London')
# Get current time in UTC and convert to different time zones
utc_now = datetime.now(pytz.utc)
eastern_time = utc_now.astimezone(eastern)
london_time = utc_now.astimezone(london)
print("Time in Eastern US:", eastern_time)
print("Time in London:", london_time)3. Getting the Current Time in a Specific Time Zone
4. Converting Time Between Time Zones
5. Handling Daylight Saving Time (DST)
6. Converting UTC Time to Local Time
7. Creating Time Zone-Aware Datetime Objects
8. Datetime Object Without Time Zone Information (Naive DateTime)
9. Using UTC for Timestamp Conversion
10. Comparing Datetimes Across Time Zones
Summary of Key Concepts:
Last updated