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.
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.
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.
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.
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.
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.
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.
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
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: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.
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: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.