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.

I get the following:

Bummer! to_timezone didn't return the right, converted datetime.

``

import datetime

import pytz fmt = '%Y-%m-%d %H:%M:%S %Z%z' utc= pytz.utc eastern = pytz.timezone('US/Eastern')

starter = pytz.utc.localize(datetime.datetime(2015, 10, 21, 23, 29)) starter_eastern = starter.astimezone(eastern) starter.strftime(fmt) def to_timezone(a_timezone): return starter_eastern

``

timezone.py
import datetime

import pytz
fmt = '%Y-%m-%d %H:%M:%S %Z%z'
utc= pytz.utc
eastern = pytz.timezone('US/Eastern')

starter = pytz.utc.localize(datetime.datetime(2015, 10, 21, 23, 29))
starter_eastern = starter.astimezone(eastern)
starter.strftime(fmt)
def to_timezone(a_timezone):
  return starter_eastern

4 Answers

Mikael Enarsson
Mikael Enarsson
7,056 Points

What they want you to do is take the argument a_timezone and us that to convert starter.

def to_timezone(a_timezone):
  return starter.astimezone([do something with a_timezone so that it can be used to convert starter.])

I hope you find this helpfull ^^

import datetime

import pytz

starter = pytz.utc.localize(datetime.datetime(2015, 10, 21, 23, 29))

def to_timezone(tz): timezone = pytz.timezone(tz) return starter.astimezone(timezone)

Tobias Edwards
Tobias Edwards
14,458 Points

For this assignment, I would firstly recommend reading the link posted in the teacher's notes a couple of tasks back from here: http://www.saltycrane.com/blog/2009/05/converting-time-zones-datetime-objects-python/

Read the Convert time zones code and notice how she converts time zones.

HINT: Instead of starter.astimezone(eastern) , you may want to change this to something else.

Also, shouldn't you be making the adjustments to starter inside the function?

EDIT: In the link, she imports datetime and timezone straight from datetime and pytz, so make some adjustments in your code because of this. (I posted this as a comments instead first time oops)

Charles Harpke
Charles Harpke
33,986 Points

Bummer! to_timezone didn't return the right, converted datetime.

import datetime
import pytz
utc = pytz.utc

starter = pytz.utc.localize(datetime.datetime(2015, 10, 21, 23, 29))
def to_timezone(tz):
  stsrter = pytz.timezone('UTC')
  new_datetime = starter.astimezone(utc)
  return new_datetime
Tobias Edwards
Tobias Edwards
14,458 Points

Ok. Firstly, you need to Create a function named to_timezone that takes a timezone name as a string.. That means putting all the code to convert starter into a function called to_timzone.

Once that is done, you have made one tiny error: to_timezone = starter.astimezone(timezone('US/Pacific'))

When you set the timezone of starter to US/Pacific, timezone is not defined. This is because timezone belongs to the pytz module.

Summary: - create a function called to_timezone - alter you code so timezone is 'pulled out of' the pytz module.

I think that should solve it without giving too much away!

Youssef Moustahib
Youssef Moustahib
7,779 Points

excellent hints Tobias Edwards, stopped me from reading all the other comments and finding the answer

Challenge Task 1 of 1

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. Challenge solving This one. Any Help Please.

import datetime

import pytz

starter = pytz.utc.localize(datetime.datetime(2015, 10, 21, 23, 29)) def to_timezone(tz): my_tz = pytz.timezone('US/Pacific') return starter.astimezone(tz)