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

Andy Hughes
Andy Hughes
8,478 Points

Confused by this datetime timezone challenge task?

Ok so this challenge was made somewhat more complicated by the way the challenge is actually written. So here's how I figured it out:

Challenge Task 1 (info).

Naive is a datetime with no timezone. Create a new timezone for US/Pacific, which is 8 hours behind UTC (UTC-08:00). Then make a new variable named hill_valley. Copy the datetime info from naive and add the tzinfo attribute with the US/Pacific timezone you made.

Task 1

1) Create a variable for US/Pacific time. Remember you can only use numbers and letters for variable names, so "us/pacific" as a variable name won't work. Creating the timezone is exactly the same as in Kenneth's first video in this section, so use this. You should be able to do in one line of code. (or scroll to the bottom of my post for a cheat)

2) This next step is asking you to take the content attributes from inside the 'naive' variable (everything on the right side of the '=' sign) and use it inside 'hill_valley' variable. You then get asked to add the 'tzinfo' with it. Again this is one line of code and if you watch Kenneth's video back, you'll see him demonstrate exactly how to do it.

That should complete task 1.

Challenge Task 2 (info).

Great, but the tzinfo attribute just sets the timezone, it doesn't move the datetime to the new timezone. Let's move one. Make a new timezone that is UTC+01:00 and set it to a variable called tz. Create a new variable named paris that uses hill_valley and the astimezone() method to change hill_valley to the new timezone.

Task 2

1) You're being asked to create a new variable called tz. Then just like step 1 in the first challenge, you need to create a timezone that is +1 hours. This is exactly the same as above.

2) Now this is where I got really confused the first couple of times, because it's not written in the most straightforward way. First step, create the variable named 'paris'.

3) Now you're being asked to change the hill_valley timezone to the new tz timezone, using 'astimezone' and then store the result in the paris variable. In effect this is saying, please take hill_valley and set it (astimezone) tz. One line of code again and also Kenneth's video shows him using the 'astimezone', so just follow that and you should pass the challenge.

Just remember the format for creating timedelta and using the astimezone. The () is easy to forget as they don't include it in the challenge description.

Example format

variable = datetime.timezone(datetime.timedelta(hours=x))

variable = datetime.datetime(2015, 10, 21, 4, 29, tzinfo = variable)

.astimezone("a parameter goes here such as a variable name")

1 Answer