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? Datetime Awareness

Will Beasley
Will Beasley
6,020 Points

updating timezone from hill_valley to paris

This makes no sense. Not sure what's going on but here's what I've got:

import datetime

naive = datetime.datetime(2015, 10, 21, 4, 29) pacific = datetime.timezone(datetime.timedelta(hours = -8)) europe = datetime.timezone(datetime.timedelta(hours = 1)) hill_valley = naive.replace(tzinfo = pacific) paris = hill_valley.replace(tzinfo = europe)

It's saying that paris and hill_valley have the same timezone and I can't figure out why. To my knowledge primitive data types are immutable in python so paris = hill_valley.replace(...) would create a new var in memory not overwrite an existing vars values so any idea what's up? Thanks!

aware.py
import datetime

naive = datetime.datetime(2015, 10, 21, 4, 29)
pacific = datetime.timezone(datetime.timedelta(hours = -8))
europe = datetime.timezone(datetime.timedelta(hours = 1))
hill_valley = naive.replace(tzinfo = pacific)
paris = hill_valley.replace(tzinfo = europe)

2 Answers

Will Beasley
Will Beasley
6,020 Points

Figured it out, apparently instead of using the replace() method you call: paris = hill_valley.astimezone(europe)

I would like to know why astimezone and replace aren't interchangeable since the default value is probably none so why can't astimezone be used for hill_valley and why can't replace be used for paris? I'm really asking, I'd be interested to know the nuances.

Cristian Altin
Cristian Altin
12,165 Points

I tested your code using replace and it's giving this output:

print(hill_valley)  =>  2015-10-21 04:29:00-08:00
print(paris)        =>  2015-10-21 04:29:00+01:00

So the two timezones are different but the time is the same. I'd say you are only replacing the timezone but keeping the time.

Using astimezone the output is the following:

print(hill_valley)  =>  2015-10-21 04:29:00-08:00
print(paris)        =>  2015-10-21 13:29:00+01:00

As you can see astimezone also converted the time from hill_valley to paris.

This is why you cannot use them interchangeably.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Because .replace() just changes the timezone's value, it doesn't move the datetime to the new timezone. astimezone() moves the datetime to be the correct value for the new timezone.

Cristian Altin
Cristian Altin
12,165 Points

As also Kenneth said. Consider that the two exist because at times you might need to keep the hours and minutes and adapt them to another timezone while other times you just need to convert them to another timezone.

Suppose for example that you made a script to handle party events that start in the US, Europe and Asia all locally at 8PM. You will use replace() to keep the date and hour but the timezone will change. The rest is DRY.

On the other hand suppose you need to know what was your local time when someone entered your secret facility on the other side of the world while you didn't notice. Then you will have to use astimezone() to find out you were having a siesta early in the afternoon when someone entered the facility during the night. Knowing the time at the facility doesn't help you so replace() would not be the right choice here.

And no, I don't have any secret facility...maybe...hmmm no, definitely. ;-)

Jose Luis Lopez
Jose Luis Lopez
19,179 Points

in this question I used

you have to define europe/paris timezone too UTC =+ 01:00 paris = hill_valley.astimezone(you need a timezone here)