Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Dates and Times in Python 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))