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? Introduction to Timezones

Not getting ValueError on "naive". What's different on newer versions of python?

>>> import datetime
>>> pacific = datetime.timezone(datetime.timedelta(hours=-8))
>>> eastern = datetime.timezone(datetime.timedelta(hours=-5))
>>> naive = datetime.datetime(2019, 1, 22, 9)
>>> naive
datetime.datetime(2019, 1, 22, 9, 0)
>>> aware = datetime.datetime(2019, 1, 22, 9, tzinfo=pacific)
>>> aware
datetime.datetime(2019, 1, 22, 9, 0, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=57600)))
>>> naive.astimezone(eastern)
datetime.datetime(2019, 1, 22, 7, 0, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=68400)))

Can someone explain me what's naive tzinfo argument set when I don't pass any argument to it?

I supposed that if I don't pass any tzinfo when creating a date time object, it will be set to the timezone to the location I'm at. But I would appreciate someone more knowledgeable if I'm correct and what are the differences between newer versions of python and the one Kenneth's using.

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 67,995 Points

You are correct! The behavior of .astimezone() changed in Python 3.6:

"Changed in version 3.6: The astimezone() method can now be called on naive instances that are presumed to represent system local time."

good I thought my computer was broken

Em Noble
Em Noble
5,052 Points

thanks!! i was confused on this also