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

Timezone Maker

variables for moscow, pacific, and india

using datetime.timezone?

Link to 3 part challenge: http://teamtreehouse.com/library/dates-and-times-in-python/where-on-earth-do-timezones-make-sense/timezone-maker

This was really confusing... It seemed obvious, I tried

 moscow = datetime.timezone + 4

...but it said something about type and int not being compatible! AAAARRRGGG!

Why aren't they compatible?

I almost broke down and started using pytz,

but I saw there were a bunch of forum posts that used timedelta for time zones,

so in the end I came up with (after much trial and error):

import datetime

moscow = datetime.timezone(datetime.timedelta(hours=4))
pacific = datetime.timezone(datetime.timedelta(hours=-8))
india = datetime.timezone(datetime.timedelta(hours=+5.5))

..and for some reason (??) it passed.

Note: I also spent an hour fiddling around with astimezone() to no avail.

Hopefully others doing this Python DateTime find this post and aren't as frustrated as I was..

3 Answers

Brittany Kozura
Brittany Kozura
17,143 Points

This is all correct usage using only datetime and without pyzt, and exactly how you would do it.

With one exception: india's time zone should be written as (hours=+5, minutes=+30) instead of (hours=+5.5), as this is more correct and will always work without exception.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Also, they're not compatible because datetime.timezone isn't a number.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

That's exactly how we did them in the videos...