Python time Module
In this article, we will explore time module in detail. We will learn to use different time-related functions defined in the time module with the help of examples.
Python has a module named time
to handle time-related tasks. To use functions defined in the module, we need to import the module first. Here's how:
Here are commonly used time-related functions.
Python time.time()
The time()
function returns the number of seconds passed since epoch.
For Unix system, January 1, 1970, 00:00:00
at UTC is epoch (the point where time begins).
Python time.ctime()
The time.ctime()
function takes seconds passed since epoch as an argument and returns a string representing local time.
If you run the program, the output will be something like:
Python time.sleep()
The sleep()
function suspends (delays) execution of the current thread for the given number of seconds.
To learn more, visit: Python sleep().
Before we talk about other time-related functions, let's explore time.struct_time
class in brief.
time.struct_time Class
Several functions in the time
module such as gmtime()
, asctime()
etc. either take time.struct_time
object as an argument or return it.
Here's an example of time.struct_time
object.
Index
Attribute
Values
0
tm_year
0000, ...., 2018, ..., 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, ..., 61
6
tm_wday
0, 1, ..., 6; Monday is 0
7
tm_yday
1, 2, ..., 366
8
tm_isdst
0, 1 or -1
The values (elements) of the time.struct_time
object are accessible using both indices and attributes.
Python time.localtime()
The localtime()
function takes the number of seconds passed since epoch as an argument and returns struct_time
in local time.
When you run the program, the output will be something like:
If no argument or None
is passed to localtime()
, the value returned by time()
is used.
Python time.gmtime()
The gmtime()
function takes the number of seconds passed since epoch as an argument and returns struct_time
in UTC.
When you run the program, the output will be:
If no argument or None
is passed to gmtime()
, the value returned by time()
is used.
Python time.mktime()
The mktime()
function takes struct_time
(or a tuple containing 9 elements corresponding to struct_time
) as an argument and returns the seconds passed since epoch in local time. Basically, it's the inverse function of localtime()
.
The example below shows how mktime()
and localtime()
are related.
When you run the program, the output will be something like:
Python time.asctime()
The asctime()
function takes struct_time
(or a tuple containing 9 elements corresponding to struct_time
) as an argument and returns a string representing it. Here's an example:
When you run the program, the output will be:
Python time.strftime()
The strftime()
function takes struct_time
(or tuple corresponding to it) as an argument and returns a string representing it based on the format code used. For example,
When you run the program, the output will be something like:
Here, %Y
, %m
, %d
, %H
etc. are format codes.
%Y
- year [0001,..., 2018, 2019,..., 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, 61]
To learn more, visit: time.strftime().
Python time.strptime()
The strptime()
function parses a string representing time and returns struct_time
.
When you run the program, the output will be:
Last updated