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 trialMichaël Ménaché
5,261 PointsWhy turn local_date into utc_date? Since local_date is already aware, shouldn't using local_date return the same result?
In the video, local_date (which is already aware) is turned into a UTC date called utc_date. Then utc_date is used display the meeting time in other timezones.
What's the added value of using the utc_time? If local_time is aware, can't we use it as it, without converting it as a utc_time?
1 Answer
Kenneth Love
Treehouse Guest TeacherYou can, yes, but it's usually cleaner and faster to convert from UTC time. Think of it like having a clean, common base for all of your classes or a stable, sane base template to extend all of your other templates from. Every timezone is defined by its offset from UTC, so converting from UTC to a given timezone is just plus or minus some amount of hours (and minutes for a few special places). Converting from a local timezone, though, usually requires moving back to UTC and then moving to the new timezone.
Michaël Ménaché
5,261 PointsMichaël Ménaché
5,261 PointsHi Kenneth, thanks for the super quick reply! I think I get the idea of having a clean reference... But I'm still puzzled by "Converting from a local timezone, though, usually requires moving back to UTC and then moving to the new timezone." Why is that? Isn't not bothering knowing what is the time difference between location X and location Y part of the advantage of using pytz and having an aware datetime? It sounds like local => UTC => new timezone is an extra step and pytz can do local => new timezone directly.
Bah, in any case I'll happily follow your teaching. It's just that... I'm French, so I can't help but insist on details.
Kenneth Love
Treehouse Guest TeacherKenneth Love
Treehouse Guest TeacherIIRC, pytz is doing local -> UTC -> target timezone behind the scenes. If I know that timezone A is +5 and timezone B is -2, even though I'd do -7 to timezone A to get it to timezone B, I'm also, really, doing -5 and then -2. And since we have to deal with the International Date Line, too, and current observation of daylight saving time, it's just easier to jump to UTC, which is a known quantity, and then jump to the new timezone.