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 trialzfusdjitsz
2,797 PointsQuestion 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
zfusdjitsz
2,797 PointsLOL Sorry about that! I don't know why I forgot that.
4 Answers
Kenneth Love
Treehouse Guest Teacherdatetime.datetime
objects have a tzinfo
attribute that points to a timezone. You change this, like all of the other datetime
attributes, using .replace()
.
zfusdjitsz
2,797 Pointsimport 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
Treehouse Guest TeacherChanging the tzinfo
on a datetime
doesn't convert it to the new timezone. You have to use astimezone()
for that.
zfusdjitsz
2,797 Pointssigh! It's always so simple. Thanks Kenneth Love
calebcordry
9,175 PointsHere 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
15,778 PointsFor 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.
tomasvukasovic
24,022 PointsHello 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)
Nathan Bentley
12,680 PointsNathan Bentley
12,680 Pointswhat is the question? that would help us! ;)