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? Timezone Strings

Dates, 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.

timezone.py
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))

Whew! 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.

2 Answers

It 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))

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))

Finally got answer after hours, the videos just do not help you the answer, what a waste of hours

I 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))