Python time in Seconds

00:00 In this lesson, I’m going to take you through the foundational way that Python handles time, which is as a floating-point number of seconds. And to understand how that works and how that’s even possible, you’ll need to understand a concept called the epoch.

00:16 What is the epoch? Well, an epoch is just a fixed point in time from which other times can then be represented as distances from that time. So, a very simple example is if you set the epoch as today and your unit of measurement as days, then tomorrow is simply one day from the epoch, and the day after tomorrow is just two days from the epoch, right?

00:40 And then, of course, you could represent earlier times as negative, which is, in fact, how Python does this with floating-point seconds. So, you could say that yesterday was -1 days from the epoch, and the day before yesterday was -2 days from the epoch.

00:54 Now, in most Windows and UNIX computing systems, the epoch is January 1st, 1970, at midnight. And that’s really the case for historical reasons, but as long as it’s standardized, it tends to work pretty well. Now, why exactly do you want to even use the epoch, and use floating-point seconds in Python from the epoch as your measurement of time? Well, there are two reasons for that. First, it makes calculation really easy.

01:18 So going back to this simple example, if you want to figure out timespans, time deltas, as they’re often called, you can just use integer subtraction, right?

01:27 So, if I want to say, “How far away is the day after tomorrow from yesterday?” I can just do 2 minus -1 is 3, and you know that there are 3 days in between those.

01:38 I’ll demonstrate that in the REPL later. And then the second reason is that floats and integers are very easy to serialize, or to store in data. And so that makes it an easy thing, as well.

01:51 The time module has several basic functions for dealing with time and I’ll go through all these in the REPL, so I won’t take too much time to go over them here. But in general, what it is, is that you have a function called time(), which returns the current time in seconds from the epoch, you have the nanoseconds version of that same function, and then you have a very similar function that returns a time in timestamp format, which I’ll go over.

02:16 And then these next two functions are going to be much more useful when I deal with time zones in the next lesson, but for the moment, the gmtime() just returns the GMT, Greenwich Mean Time, time as a struct_time object, so this is really useful for showing the epoch. And the localtime() does the same thing, just for the local time instead of the particular time zone standard.

02:38 Let’s take a look at how these functions work in the Python REPL.

02:43 Of course, the first thing to do is to import time,

02:48 and then I can get the epoch with the time.gmtime function.

02:54 And as you can see, it takes in a parameter of seconds, which is the number of seconds since the epoch. So I’ll just pass in 0, which will return me the epoch as a struct_time. And as you can see, it has 1970, the 1st month of that year, January, the 1st day, all those zeros with hours, minutes, seconds.

03:10 So it’s midnight on the first day of 1970, January 1st. Now the most basic time function is just time.time(). And that returns, as you can see, a large number of seconds. Now, if you wanted to check and just make sure that this was accurate, that this is the number of seconds from the epoch, then you could do something like this: you could say year, in seconds, is equal to 60 seconds in a minute, times 60 minutes in an hour, times 24 hours in a day, times 365 days a year. So, that’s the number of seconds in a year.

03:44 And then you could just say time.time() divided by year_secs. And as you can see, it’s been roughly 50 years since 1970, which checks out because I am speaking to you live from March, or sorry, from April of 2020. So this all works out just fine.

04:02 I saw this 50.3 and I immediately thought of March as the 3rd month of the year. But in fact, it’s April, goodness! So that’s time.time(), and if I use time.time_ns(), I get, as you can see, essentially the same figure, it’s just multiplied by a factor of a billion, because there are a billion nanoseconds in a second. So this works out perfectly.

04:23 And then you could also use time.ctime(), and if you pass a parameter of seconds, it will give you that number of seconds since the epoch, but if you pass it no parameter at all, it will just pass in the result of the time.time function.

04:38 So, as you can see, if I say ctime(), and then I pass it time.time(), it’s essentially the exact same thing. And it is the exact same thing, it’s just that I waited a few seconds between these calls, but if I wanted to say something more like time.ctime, and maybe just do, I don’t know, 10,0000 seconds after the epoch? As you can see, I’ll get not a very long time away at all from the epoch.

05:03 It’s not even a full day from the epoch, but if I pass in, let’s see, how many is that? A million seconds? Then it will get me about 12 days past the epoch.

05:14 If I pass in 10 million, it gets me even farther. And so on, I go up by orders of magnitude and start to really quickly get into the multiple years. And so you can see just how that kind of seconds progression happens.

05:27 So those are the three really basic functions that I wanted to show you from this module. Just to give you a quick little taste again of why it’s a good thing to represent time in this manner, well, one thing you can do is you can say t1 = time.time(), and then t2 = time.time().

05:46 And these are both time variables, and they represent times perfectly well, but you can still perform just floating-point operations on them and get time deltas, right? So it was about four and a third seconds between the time that I wrote these two variables.

06:01 And this is really interesting and important for things like timing your code, or just dealing with time in general; we often have to deal with things like time deltas.

06:09 You could also say t2 plus the year_secs variable I defined earlier, and get the exact time, the Python time, of a year from t2.

06:21 And then, of course, if you pass that into ctime,

06:26 then you will be able to get the year a year away from t2.

06:32 So it makes it really simple to do all of this and to represent time in machine terms so that you can actually perform operations on it. And as I said, it’s also really easy to store, and then to reconstruct from stored data, and so that’s a really important thing about how Python works with time. In the next lesson, I’ll get into time zones and I’ll tell you a little bit about how you have to deal with local time, and UTC time, and things like that in your Python applications.

Become a Member to join the conversation.