Python time Module
In this article, we will go deep into the time module. We will learn how to use different time-related functions defined in the time module.
Python provides a module named time
to handle time-related tasks. To use functions defined in the time
module, we need to import the module first.
import time
Here we will see the most used time-related functions.
Python time.time()
The time.time()
function returns the number of seconds passed since a point of time.
For the Unix system, January 1, 1970, 00:00:00
at UTC is the point where time begins.
import time
seconds = time.time()
print("Seconds since the epoch =", seconds)
Output
Seconds since the epoch = 1635935901.3638976
Python time.ctime()
The time.ctime()
function takes as an argument the seconds passed since the epoch and returns a string representing local time.
import time
seconds = 1494654246
local_time = time.ctime(seconds)
print("Local time =", local_time)
After running the above program, the output will be:
Local time = Sat May 13 05:44:06 2017
Python time.sleep()
The time.sleep()
function delays the current thread's execution for the given number of seconds.
import time
print("This is printed immediately.")
time.sleep(5)
print("This is printed after 5 seconds.")
Output
This is printed immediately.
This is printed after 5 seconds.
Before seeing other time-related function, let us look at time.struct_time
class.
time.struct_time Class
The time
module has several functions as asctime()
, gmtime()
, etc. either take time.struct_time
object as argument or return it.
Let us see an example of the time.struct_time
object.
time.struct_time(tm_year=2017, tm_mon=5, tm_mday=13,
tm_hour=5, tm_min=55, tm_sec=6
tm_wday=5, tm_yday=133, tm_isdst=0)
Index | Attribute | Values |
---|---|---|
0 | tm_year |
0000, ..., 2017, ..., 9999 |
1 | tm_mon |
1, 2, ..., 12 |
2 | tm_mday |
1, 2, ..., 31 |
3 | tm_hour |
0, 1, ..., 23 |
4 | tm_min |
0, 1, ..., 59 |
5 | tm_sec |
0, 1, ..., 60 |
6 | tm_wday |
0, 1, ..., 6; Monday is 0 |
7 | tm_yday |
1, 2, ..., 366 |
8 | tm_isdst |
0, 1 or -1 |
The values of the time.struct_time
object are accessible using both indices and attributes.
Python time.localtime()
The time.localtime()
function takes as an argument the number of seconds passed since the epoch and returns a struct_time
in local time.
import time
After running the above program, the output will be as follows:
import time
result = time.localtime(1494654246)
print("result =", result)
print()
print("year =", result.tm_year)
print("month =", result.tm_mon)
print("day =", result.tm_mday)
print("tm_hour =", result.tm_hour)
The execution of the above program will give the following output:
result = time.struct_time(tm_year=2017, tm_mon=5, tm_mday=13, tm_hour=5, tm_min=44, tm_sec=6, tm_wday=5, tm_yday=133, tm_isdst=0)
year = 2017
month = 5
day = 13
tm_hour = 5
When no argument or None
is passed to localtime()
, the local time is used.
Python time.gmtime()
The time.gmtime()
function takes as an argument the number of seconds passed since the epoch and returns a struct_time
in UTC.
import time
result = time.gmtime(1494654246)
print("result =", result)
print()
print("year =", result.tm_year)
print("month =", result.tm_mon)
print("day =", result.tm_mday)
print("tm_hour =", result.tm_hour)
After running the above program, the output will be:
result = time.struct_time(tm_year=2017, tm_mon=5, tm_mday=13, tm_hour=6, tm_min=44, tm_sec=6, tm_wday=5, tm_yday=133, tm_isdst=0)
year = 2017
month = 5
day = 13
tm_hour = 6
When no argument or None
is passed to gmtime()
, the vlaue returned by time()
is used.
Python time.mktime()
The time.mktime()
function takes as argument a struct_time
object (or a tuple of 9 elements corresponding to struct_time
) and returns the seconds passed since the epoch in local time. We can say it is the inverse function of localtime()
.
import time
t = (2017, 5, 13, 5, 44, 6, 5, 133, 0)
local_time = time.mktime(t)
print("Local time =", local_time)
Output
Local time = 1494654246.0
The following example shows how mktime()
and localtime()
are related.
import time
seconds = 1494654246
# It returns struct_time object
t = time.localtime(seconds)
print("Type of t :", type(t))
print("t =", t)
# It returns seconds from struct_time object
s = time.mktime(t)
print("s =", s)
The output of the above program can be given as follows:
Type of t : <class 'time.struct_time'>
t = time.struct_time(tm_year=2017, tm_mon=5, tm_mday=13, tm_hour=5, tm_min=44, tm_sec=6, tm_wday=5, tm_yday=133, tm_isdst=0)
s = 1494654246.0
Python time.asctime()
The time.asctime()
function takes as argument a struct_time
object (or a tuple of 9 elements corresponding to struct_time
) and returns a string representing it.
import time
t = (2017, 5, 13, 5, 44, 6, 5, 133, 0)
result = time.asctime(t)
print("Result =", result)
After executing the above program, the output will be:
Result = Sat May 13 05:44:06 2017
Python time.strftime()
The time.strftime()
function takes as argument a struct_time
object (or a tuple of 9 elements corresponding to struct_time
) and returns a string representing it based on the format code used.
import time
# get struct_time object
given_tuple = time.localtime()
time_string = time.strftime("%m/%d/%Y, %H:%M:%S", given_tuple)
print("time_string =", time_string)
The output of the above program can be given as follows:
time_string = 11/03/2018, 13:12:17
Above, we used some format codes as %Y
, %m
, %d
, etc.
%Y
- year [0001, ..., 2017, 2018, ..., 9999]%m
- month [01, 02, ..., 11, 12]%d
- day [01, 02, ..., 30, 31]%H
- hour [00, 01, ..., 22, 23]%M
- minutes [00, 01, ..., 58, 59]%S
- second [00, 01, .., 58, 59]
If you want to learn more, you can visit time.strftime().
Python time.strptime()
The time.strptime()
function parses a string representing time and returns a struct_time
object.
import time
time_string = "17 July, 2017"
result = time.strptime(time_string, "%d %B, %Y")
print(result)
After executing the above program, the output will be:
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=17, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=198, tm_isdst=-1)
If you want to learn more, you can visit time.strptime().