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? Datetime Awareness

Question on code challenge

I'm not looking for an answer. I just don't understand what the question is asking of me. Can someone clarify?

Create a new variable named hill_valley where you've replaced the tzinfo of naive with the US/Pacific timezone. US/Pacific is UTC-08:00.

Thanks

Nathan Bentley
Nathan Bentley
12,680 Points

what is the question? that would help us! ;)

LOL Sorry about that! I don't know why I forgot that.

4 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

datetime.datetime objects have a tzinfo attribute that points to a timezone. You change this, like all of the other datetime attributes, using .replace().

import datetime

naive = datetime.datetime(2015, 10, 21, 4, 29)

hill_valley = datetime.datetime(2015, 10, 21, 4, 29, tzinfo = datetime.timezone(datetime.timedelta(hours =- 8)))

paris = hill_valley.replace(tzinfo = datetime.timezone(datetime.timedelta(hours = 1)))

Hey Kenneth Love - I'm getting a message saying "paris has the same date and time as hill_valley. Did you convert timezones?"

Where am I going wrong?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Changing the tzinfo on a datetime doesn't convert it to the new timezone. You have to use astimezone() for that.

sigh! It's always so simple. Thanks Kenneth Love

Here is how I just did it. No need to retype all the info from naive if you use .replace() like Kenneth hinted at above

import datetime

naive = datetime.datetime(2015, 10, 21, 4, 29)

pacific = datetime.timezone(datetime.timedelta(hours = -8))

hill_valley = naive.replace(tzinfo = pacific)

europe_timezone = datetime.timezone(datetime.timedelta(hours =  1))

paris = hill_valley.astimezone(europe_timezone)
akhter ali
akhter ali
15,778 Points

For those who are still confused on the last bit where you have to define paris variable.

As Kenneth mentioned, you have to take hill_valley object and change it so it is "as" timezone utc+1.

Hope this helps without giving away the answer.

Hello there my solution to the second part was this:

import datetime

naive = datetime.datetime(2015, 10, 21, 4, 29)

hill_valley = datetime.datetime(2015, 10, 21, 4, 29, tzinfo = datetime.timezone(datetime.timedelta(hours =- 8)))

europe_timezone = datetime.timezone(datetime.timedelta(hours =  1))

paris = hill_valley.astimezone(europe_timezone)