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 trialGordon Reeder
2,521 PointsDates, times, timezones in Python. I'm stuck!
I am totally stuck on this code challenge. I am totally confused.
Create a function named to_timezone that takes a timezone name as a string. Convert starter to that timezone using pytz's timezones and return the new datetime.
import datetime
import pytz
def to_timezone(tz_str):
return pytz.timezone(tz_str).localize(starter)
starter = pytz.utc.localize(datetime.datetime(2015, 10, 21, 23, 29))
Chris Freeman
Treehouse Moderator 68,441 PointsCan you post how you solved your error?
2 Answers
manu4000liege
3,055 PointsIt seemed to me that something was missing in the question of this challenge ... That function takes a string and returns the equivalent timezone : ok We then should use this timezone to apply it to starter. But with which text ? Which timezone ? No one was the answer.
import datetime
import pytz
starter = pytz.utc.localize(datetime.datetime(2015, 10, 21, 23, 29))
def to_timezone(stringTZ):
return starter.astimezone(pytz.timezone(stringTZ))
Annie Scott
27,613 Pointsimport datetime
import pytz
starter = pytz.utc.localize(datetime.datetime(2015, 10, 21, 23, 29))
def to_timezone(stringTZ): return starter.astimezone(pytz.timezone(stringTZ))
Finally got answer after hours, the videos just do not help you the answer, what a waste of hours
Annie Scott
27,613 PointsI finally figured it out after 4 hours , videos just not helping with the challenges, I guess just on my own,
import datetime
import pytz
starter = pytz.utc.localize(datetime.datetime(2015, 10, 21, 23, 29))
def to_timezone(stringTZ): return starter.astimezone(pytz.timezone(stringTZ))
Gordon Reeder
2,521 PointsGordon Reeder
2,521 PointsWhew! Got it! I have no idea why that was so hard. PArt of the problem was that I needed to leave starter above the function definition.