Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Dates and Times in Python (2014) Where on Earth do Timezones Make Sense? Introduction to Timezones

sidni ahmed
sidni ahmed
3,329 Points

timezone differences

Why is it that 'aware.astimezone(pacific)' give me the same time as my aware variable? pacific time is 8 hours behind so if time is 9am should it not return 1am.

This is same with eastern time. If time is 9am then should not eastern time be 4am

Also with mumbai is showing 14 and a half hours ahead. why is this? if my aware variable is 9am then in mumbai it should be 14:30. but when i do 'aware.astimezone(mumbai)' the time becomes 22:30. Why is it that many hours ahead? What is going on here? did i miss something?

timezones.py
import datetime
>>> pacific = datetime.timezone(datetime.timedelta(hours=-8))
>>> eastern = datetime.timezone(datetime.timedelta(hours=-5))
>>> naive = datetime.datetime(2016, 1, 18, 0)
>>> naive
datetime.datetime(2016, 1, 18, 0, 0)
>>> aware = datetime.datetime(2016, 1, 18, 0, tzinfo=pacific)
>>> aware
datetime.datetime(2016, 1, 18, 0, 0, tzinfo=datetime.timezone(datetime.timedelta(-1, 57600)))
>>> naive.astimezone(eastern)
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    naive.astimezone(eastern)
ValueError: astimezone() cannot be applied to a naive datetime
>>> aware.astimezone(eastern)
datetime.datetime(2016, 1, 18, 3, 0, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400)))
>>> naive = datetime.datetime(2016, 1, 18, 9)
>>> naive
datetime.datetime(2016, 1, 18, 9, 0)
>>> aware = datetime.datetime(2016, 1, 18, 9, tzinfo=pacific)
>>> aware
datetime.datetime(2016, 1, 18, 9, 0, tzinfo=datetime.timezone(datetime.timedelta(-1, 57600)))
>>> aware.astimezone(eastern)
datetime.datetime(2016, 1, 18, 12, 0, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400)))
>>> auckland = datetime.timezone(datetime.timedelta(hours=13))
>>> aware.astimezone(auckland)
datetime.datetime(2016, 1, 19, 6, 0, tzinfo=datetime.timezone(datetime.timedelta(0, 46800)))
>>> mumbai = datetime.timezone(datetime.timedelta(hours=5, minutes=30))
>>> mumbai
datetime.timezone(datetime.timedelta(0, 19800))
>>> aware.astimezone(mumbai)
datetime.datetime(2016, 1, 18, 22, 30, tzinfo=datetime.timezone(datetime.timedelta(0, 19800)))
>>> aware.astimezone(pacific)
datetime.datetime(2016, 1, 18, 9, 0, tzinfo=datetime.timezone(datetime.timedelta(-1, 57600)))
>>>

2 Answers

Seth Kroger
Seth Kroger
56,413 Points

Notice in the video when he creates the variable aware, he includes the parameter tzinfo=pacific when making the datetime. That makes aware relative to Pacific time, not UTC.