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

SUDHARSAN CHAKRAVARTHI
PLUS
SUDHARSAN CHAKRAVARTHI
Courses Plus Student 2,434 Points

timezone on starter variable

As my understanding, starter variable is assigned with the time which has a zone info associated [ UTC ] with it and localized.

All we need to do is convert it to UTC timezone using astimezone() method and convert it to any time zone.

Can you please confirm my understanding.

timezone.py
import datetime

import pytz

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

2 Answers

hie Sudharsan. the first step is to create the function as the question says. inside the function you then use your pytz.timezone and astimezone to change starter to that timezone, like so

def to_timezone(tz):

starter_new = starter.astimezone(pytz.timezone(tz))

return starter_new

thank you so much!