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

Charles Harpke
Charles Harpke
33,986 Points

Create a function named to_timezone that takes a timezone name as a string. Convert starter to that timezone using pytz'

Is this challenge taking one random timezone zone name of our choosing and converting it to a string?

timezone.py
import datetime

import pytz

starter = pytz.utc.localize(datetime.datetime(2015, 10, 21, 23, 29))
Charles Harpke
Charles Harpke
33,986 Points
import datetime

import pytz

starter = pytz.utc.localize(datetime.datetime(2015, 10, 21, 23, 29))
to_timezone = pytz.timezone('US/Pacific')
fmt = '%Y-%m-%d %H:%M:%S %Z%z'
utc = pytz.utc
starter.strftime(fmt)

2 Answers

Mikael Enarsson
Mikael Enarsson
7,056 Points

No. The challenge is to make a function that takes a timezone name string as an argument, and return starter converted to that timezone.

def to_timezone(a_timezone):
  return #code to convert starter to the passed in timezone
Jose Luis Lopez
Jose Luis Lopez
19,179 Points

import datetime

import pytz

starter = pytz.utc.localize(datetime.datetime(2015, 10, 21, 23, 29)) fmt = '%Y-%m-%d %H:%M:%S %Z%z' timezone as a string = pytz.timezone('*********') # timezone declared as a string

def to_timezone(it takes the a string timezone): # function named to_timezone new_time = pytz.timezone(********) # conver starter to the timezone using pytz return starter.astimezone(*******) # return the new datetime

I'm not giving the answer because it took me a while to figure this out. I had red the teachers notes and review the videos like 5 times. but I'm pretty sure this will help a lot. posting this because I did really have a hard time in this challenge