UTC offsets

Working with Dates and Times in Python

Max Shron

Data Scientist and Author

Working with Dates and Times in Python

Working with Dates and Times in Python

Working with Dates and Times in Python

Working with Dates and Times in Python

Working with Dates and Times in Python

Working with Dates and Times in Python

UTC

# Import relevant classes
from datetime import datetime, timedelta, timezone
Working with Dates and Times in Python

UTC

# Import relevant classes
from datetime import datetime, timedelta, timezone

# US Eastern Standard time zone
ET = timezone(timedelta(hours=-5))

# Timezone-aware datetime dt = datetime(2017, 12, 30, 15, 9, 3, tzinfo = ET)
print(dt)
2017-12-30 15:09:03-05:00
Working with Dates and Times in Python

UTC

# India Standard time zone
IST = timezone(timedelta(hours=5, minutes=30))

# Convert to IST print(dt.astimezone(IST))
2017-12-31 01:39:03+05:30
Working with Dates and Times in Python

Adjusting timezone vs changing tzinfo

print(dt)
2017-12-30 15:09:03-05:00

print(dt.replace(tzinfo=timezone.utc))
2017-12-30 15:09:03+00:00
# Change original to match UTC
print(dt.astimezone(timezone.utc))
2017-12-30 20:09:03+00:00
Working with Dates and Times in Python

UTC Offsets

Working with Dates and Times in Python

Preparing Video For Download...