Introduction
The time module provided by Python is an essential module through which we can perform various tasks like getting the current time in various formats or even pausing the execution of the program. There are various functions in the time module, along with many attributes. All these functions are used to do time-related tasks.
In this article, we will learn about some of the most popular and most used functions in the time module. But before any of these functions can be used, we need to import the time module. This can be done using the following code.
import time
Also See, Intersection in Python and Convert String to List Python.
Important Functions In The Time Module
There are various functions in the time module in Python. Let's learn about them.
Epoch
For a computer system, epoch is the point when time starts. For Linux and Windows, the epoch is January 1, 1970, 00:00:00 (UTC). The leap seconds are not considered in calculating the epoch.
time.time()
This function is used to get the number of seconds passed since the epoch. Whenever the time.time() is called, it calculates the time in seconds since the epoch. The function returns a floating-point number.
Example:
import time
seconds = time.time()
print("Seconds since epoch :", seconds)
Output:
Seconds since epoch : 1644894198.1863573
If you wish to check what is the epoch on your system, you can do it using:
import time
print(time.gmtime(0))
The output of above code will be:
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
time.ctime()
The time.ctime() takes seconds as the input parameter and returns the string representation of the local time.
Example:
import time
secondsPassed = 1445925769.9618232
local_time = time.ctime(secondsPassed)
print("The Local time is :", local_time)
Output:
The Local time is : Tue Oct 27 06:02:49 2015
If no argument is passed in the parameter, it returns the current time.
Example:
import time
local_time = time.ctime()
print("The current time is :", local_time)
Output:
The current time is : Tue Feb 15 03:44:50 2022
The time.ctime() function can thus be utilised to get the current time in the above format. The output is in the form of: day month date hh:mm:ss year.
time.sleep()
The time.sleep() function is used to delay the execution of the current program. It receives the number of seconds as an argument and then halts the program for the number of seconds it has received as the argument. It is mainly used to suspend the current thread for some specific time. The argument passed can also be a floating value.
Example:
import time
print("Printing for the first time")
time.sleep(10)
print("Printing again after delay")
Output:
Printing for the first time
Printing again after delay
The second line will be printed 10 seconds after the first line has been printed.
time.struct_time class
Many functions in the time module either return or take the object of time.struct_time.It returns a named tuple whose value can be accessed by both index and attribute name.
Example:
time.struct_time(tm_year=2012, tm_mon=8, tm_mday=22, tm_hour=23, tm_min=6, tm_sec=29, tm_wday=2, tm_yday=235, tm_isdst=0)
The below chart represents the various attributes and values of time.struct_time object:
time.localtime()
The time.local time() function takes the number of seconds passed since epoch as the argument and returns a struct_time object in local time. If any argument is not passed, it will automatically fetch the argument by calling time.time() which returns the current time.
Example 1: with arguments
import time
t=123456789
out_time= time.localtime(t)
print(out_time)
Output:
time.struct_time(tm_year=1973, tm_mon=11, tm_mday=29, tm_hour=21, tm_min=33, tm_sec=9, tm_wday=3, tm_yday=333, tm_isdst=0)
Example 2: without arguments
import time
out_time= time.localtime()
print(out_time)
Output:
time.struct_time(tm_year=2022, tm_mon=2, tm_mday=15, tm_hour=5, tm_min=7, tm_sec=48, tm_wday=1, tm_yday=46, tm_isdst=0)
Example 3: fetching the value of a particular attribute
import time
out_time= time.localtime()
print("current year :",out_time.tm_year)
print("current month :",out_time.tm_mon)
print("day of the month :",out_time.tm_mday)
Output:
current year : 2022
current month : 2
day of the month : 15
time.gmtime()
The time.gmtime() function returns a struct_time object in UTC. It takes the number of seconds since epoch as the argument. As in the case of localtime(), if the argument is not passed, it takes the value provided by the time.time() as the default value.
Example 1: with arguments
import time
t= 123456789
gm_time= time.gmtime(t)
print(gm_time)
Output:
time.struct_time(tm_year=1973, tm_mon=11, tm_mday=29, tm_hour=21, tm_min=33, tm_sec=9, tm_wday=3, tm_yday=333, tm_isdst=0)
Example 2: without arguments
import time
gm_time= time.gmtime()
print(gm_time)
Output:
time.struct_time(tm_year=2022, tm_mon=2, tm_mday=15, tm_hour=5, tm_min=25, tm_sec=3, tm_wday=1, tm_yday=46, tm_isdst=0)
The value of tm_isdst is always zero. We can also fetch the value of a particular attribute similarly as we did in the time.localtime() method.
time.mktime()
The time.mktime() can be thought of as the reverse of time.localtime(). In this method, we either pass the time.struct_time object as the argument or the set of nine values of the struct_time object as the argument. The function then returns the seconds that have passed since the epoch.
Example 1: passing the struct_time object
import time
# get a struct_time object using time.gmtime() or time.localtime()
gm_time_struct_object= time.gmtime()
#pass the object as argument to mktime() function
mk_time= time.mktime(gm_time_struct_object)
print(mk_time)
Output:
1644903322.0
Example 2: passing 9 values corresponding to struct_time
import time
t = (2022, 2, 14, 8, 44, 4, 4, 362, 0)
print(time.mktime(t))
Output:
1644828244.0
time.asctime()
The time.asctime() function is used to get the string representation of time in the format: Day Mon Date Hour:Min:Sec Year . It takes either the struct_time object as the argument or the set of 9 values corresponding to the attributes of struct_time as the argument.
Example 1: passing the struct_time object
import time
#get a struct_time object using gmtime()
get_struct_time_object= time.gmtime()
#pass struct_time object to asctime()
get_string_representation= time.asctime(get_struct_time_object)
#finally display the string representation
print(get_string_representation)
Output:
Tue Feb 15 05:44:55 2022
Example 2:
import time
struct_time_values=(2022, 2, 15, 8, 44, 4, 1, 46, 0)
string_representation= time.asctime(struct_time_values)
print(string_representation)
Output:
Tue Feb 15 08:44:04 2022
time.strftime()
In the time.asctime() we saw that the string representation could be done in only one format. The time.strftime() gives us the flexibility to display the time in various formats. The string representation is displayed depending on the way we describe the format. The time.strftime() takes the struct_time object or the corresponding 9 values as the argument. If not provided, then the time returned by the time.local time() is used.
Example 1:
import time
struct_object = time.localtime() # get struct_time
time_string_representation = time.strftime("%m/%d/%Y, %H:%M:%S", struct_object)
print(time_string_representation)
Output:
02/15/2022, 06:12:51
Example 2:
import time
time_string_representation = time.strftime("%Y/%d/%m, %H:%M")
print(time_string_representation)
Output:
2022/15/02, 06:16
time.strptime()
The time.strptime() function returns a time.struct_time object by taking the string representation of time.
Example:
import time
time_string = "Tue, 14 Feb 2022 10:45:08"
struct_time_object = time.strptime(time_string, "%a, %d %b %Y %H:%M:%S")
print(struct_time_object)
Output:
time.struct_time(tm_year=2022, tm_mon=2, tm_mday=14, tm_hour=10, tm_min=45, tm_sec=8, tm_wday=1, tm_yday=45, tm_isdst=-1)
You can practice by yourself with the help of online python compiler.
Check out Escape Sequence in Python