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

Python timezone issue

I can't see what is happening with my code. It says 'paris' isn't a datetime, but when I run my code on the Terminal, it's working fine.

Please find the code below:

import datetime

naive = datetime.datetime(2015, 10, 21, 4, 29)
pacific = datetime.timezone(datetime.timedelta(hours=-8))
hill_valley = naive.replace(tzinfo = pacific)
paris = datetime.timezone(datetime.timedelta(hours=1))
hill_valley.astimezone(paris)

I would appreciate some help.

Thank you in advance.

[MOD: edit formatting -cf]

1 Answer

Hello there!

I was just stuck on this problem as well, took me multiple looks at it to figure it out. I think your issues is the same as my issue in that you are reading the problem incorrectly. Here's the breakdown of the problem correctly:

  1. Create a new timezone (I called mine paris_timezone) and you want to set it equal to UTC+01:00
  2. Then make paris a new variable and set it equal to hill_valley's datetime, but as the new timezone you created using the new method .astimezone()

You're on the right path same as I was, but here is the code example, I left off the answer this time to see if you can get it yourself:

import datetime

#  Challenge 1/2

naive = datetime.datetime(2015, 10, 21, 4, 29)
#  whatever you call your variable from the first challenge
pacific = datetime.timezone(datetime.timedelta(hours=-8))
hill_valley = naive.replace(tzinfo=pacific)

#  Challenge 2/2

#  new variable, I used paris_timezone
paris_timezone = datetime.timezone(datetime.timedelta(hours=1))
#  set new variable 'paris' equal to hill_valley amended to the new timezone with .astimezone()
paris = ???

If you're still struggling, let me know. I can provide the answer for you and try and walk you through it further. Hope this helps!

Thanks, Andrew G