00:00 In this lesson, I’m going to cover a very important point in Python time, and in fact, in just how the world represents time as a whole, which is time zones. You might’ve been thinking during the last lesson, you know, all of this is great, but how do you deal with the fact that the whole world has many different time zones, all of which might have to have different representations of time, in Python as well? Luckily that’s what this lesson is here for.
00:25 So to understand time zones in Python, the first thing to understand is the concept of the UTC. And the UTC is a little strange because UTC actually stands for Coordinated Universal Time, but this is really a compromise between the English abbreviation, which would have been CUT, and the French abbreviation, which would have been TUC for Temps Universel Coordonné.
00:50 And I apologize to anyone who speaks French watching this video because I’m sure that was horrifically mispronounced. But regardless, so the English and the French would have had two conflicting abbreviations and they were the two main parties developing this concept, and so they decided to compromise by having an abbreviation that didn’t make sense with either time. So the UTC is a reference time zone, and it’s what allows pretty much all of the world’s time to really mesh smoothly with all of the different time zones that are around the world, because it’s like the epoch, but for time zones.
01:35 And then other times zones, like the actual time zones of the world, are all defined via offset from UTC. So my time zone that I’m currently speaking to you from is EDT, Eastern Daylight Time, in the United States.
As I mentioned in the last lesson, there are several
time module functions that deal explicitly with time zones, and the two that I’m going to examine in-depth in this lesson, over in the Python REPL are
And both of these take an optional seconds parameter, and if you don’t pass that parameter to either of these functions, it’ll just pass in the
time.time(), so they’ll default to the current time.
gmtime() returns the Greenwich Mean Time, or essentially in line with the UTC time, as a
struct_time object, whereas
localtime() returns the local time. And the object that’s returned, the
struct_time, which I’m going to talk more about in the next lesson, also includes access to the time zone field and time GMT offset fields.
and then the first example that I gave in the last lesson was
time.gmtime with a parameter of 0 seconds to give you the epoch, and as you can see that still works just as it did a second ago, but I think it’s instructive to take a look at the
time.localtime, 0 seconds from the epoch.
03:43 So the local epoch, you might call it, for the Eastern Standard Time, which is during that time of the year, what the local time is, it’s Eastern Standard rather than Eastern Daylight, as it is now in April, as you can see, it’s five hours behind.
So it’s December 31st, 1969, and 19 hours into the day, as opposed to 24, which is what this is. And so, as you can see, the
time.localtime, you can also use the parameters that I mentioned in the slide just a second ago, you can get the time zone, which is Eastern Standard Time because it’s in late December.
So of course, if I did, though, if I said just
time.localtime() right now, which is the same as passing it with
time.time() as the parameter, then I get: the year is 2020, the month is April, the day is the 14th, and it’s about 9 minutes after noon.
EDT, Eastern Daylight Time. So as you can see, all of this really starts to get complicated. And that’s what the main lesson that I want to provide you in this lesson about time zones is just that time zones are complex. As you can see, this has a
1, whereas it wasn’t daylight savings time, simply because of the time of the year when I did
time.localtime(0).tm_zone, that’s Eastern Standard Time.
05:47 And so there’s a lot of complexity here, and what I really want to encourage you to do is try your best to understand this quite well, UTC and how the different time zones relate to that, but then, once you feel like you have a really good understanding, rely on a really well-documented and well-supported library to do all of that work for you.
datetime library is a fantastic library for working with dates and times, frameworks like Django will do a lot of the work of dealing with different user times for you already, so you don’t need to worry about that stuff. So that’s, I think, the main lesson that I want to provide in this short intro to time zones in Python is not necessarily that you need to be accounting for all of this in your own code, but that you need to understand why it’s necessary to use well-supported, Python or otherwise, libraries that already do a lot of this work for you. In the next lesson, I’ll take you through these data structures that I’ve been talking about and showing you, but I haven’t really explained in full detail yet.
Become a Member to join the conversation.