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

I can pass this code in my local environment, but not the challenge.. Here is my code:

import datetime
import pytz
starter = pytz.utc.localize(datetime.datetime(2015, 10, 21, 23, 29))
def to_timezone(aware_dt, zonename):
    tz = pytz.timezone(zonename)
    return tz.normalize(aware_dt.astimezone(tz))
starter = datetime.datetime(2015, 10, 21, 23, 29, tzinfo=pytz.utc)
print(to_timezone(starter, "America/Los_Angeles"))
2015-10-21 16:29:00-07:00
timezone.py
import datetime

import pytz

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

8 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

to_timezone() only takes one argument, the name of the timezone to convert starter to. starter is already an UTC-aware datetime, so you'll need to use .astimezone() on it. The only problem is figuring out which timezone...good thing you get the name of the TZ as the argument to the function. Use pytz's timezones and return that new datetime.

Since starter is defined outside of the function, you'll be able to get its value inside the function even without getting it as an argument.

Charles Harpke
Charles Harpke
33,986 Points

I'm still trying to figure out the error of my ways on this one... 'to_timezone' has 1 argument....(tz)... I've used .astimezone on my already aware datetime in 'starter' message: 'to_timezone did not return the right, converted datetime...'

import datetime

import pytz
pacific = pytz.timezone('US/Pacific')
fmt = "%Y-%m-%d %H:%M:%S %Z%z"

starter = pytz.utc.localize(datetime.datetime(2015, 10, 21, 23, 29))
def to_timezone(tz):
  tz = starter.astimezone(pacific)
  return tz

Ok so here is what I found out about this Challenge:

def to_timezone(arg):

    timezone = pytz.timezone(arg)

    return starter.astimezone(timezone)
import datetime

import pytz

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

def to_timezone(timezone_str):
    return starter.astimezone(pytz.timezone(timezone))
Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You're still assuming you know the time zone beforehand. The tz argument will be the time zone name. Make a pytz time zone based in that and return starter converted to it.

This is what I needed to read to figure this out. Thanks, from almost 4 years in the future.

Charles Harpke
Charles Harpke
33,986 Points

Argh...sorry about that. I don't know why I was under the impression it needed to be coded....In any event...thank you!

import datetime

import pytz

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

def to_timezone(*arg):

    timezone = pytz.timezone(*arg)

    return starter.astimezone(timezone)

Kenneth Love Thank you sir!!! you gave me a huge clue." You're still assuming you know the time zone beforehand."

Charles Harpke
Charles Harpke
33,986 Points

The time zone name I assign to 'tz' is more or less arbitrary?

import datetime

import pytz

fmt = "%Y-%m-%d %H:%M:%S %Z%z"

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

Stop hard coding the timezone! :)

my_tz = pytz.timezone(tz)

Then return the starter like you're doing already.

What am I doing wrong?

import datetime

import pytz

fmt = '%m-%d %H:%M %Z%z' starter = pytz.utc.localize(datetime.datetime(2015, 10, 21, 23, 29))

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