In this article, we are going to learn all about the Python time module along with examples. In the previous tutorial, we have seen all about the Python DateTime module with the help of various examples.
In this guide, you will see almost all the attributes and methods of the python time module which are very helpful to work with time.
To understand this article, you should have basic knowledge of Python programming.
Python time module
time module is a python built-in module that is used to work with time. we will learn different time-related functions which are defined in the time module using an appropriate example.
To use of time module you need to import time module using import keyword.
Here we will see time module’s functions that are used to make easy to work with time.
The time() function return the number of seconds passed since epoch.
import time seconds = time.time() print('Total seconds:- ', seconds)
The ctime() function take seconds passed since epoch and convert into the string local time.
import time seconds = 1545925769.9618232 local_time = time.ctime(seconds) print(local_time)
sleep() method delay the current execution of thread to a given number of seconds.
import time print("Hello, Welcome to ") time.sleep(4) print("Programming Funda")
Python strftime() method used to get the formatted time string.
import time result = time.strftime('%Y-%m-%d %H:%M:%S') print(result)
Python time strptime() method parses the string representing time according to the format. The return value is struct_time.
import time string_date = "30 Jun 21" parsed_date = time.strptime(string_date, "%d %b %y") print(type(parsed_date))
It is the type of the time value sequence returned by the gmtime(), localtime() and strptime().it is the object of a named tuple. Value can be access through index number and by attribute name.
You can find out the index number and attribute name to access the value.
|6||tm_wday||range[0,6], 0 is Monday|
|8||tm_isdst||0,1 or -1|
import time string_date = "30 Jun 21" parsed_date = time.strptime(string_date, "%d %b %y") print(parsed_date) #Output time.struct_time(tm_year=2021, tm_mon=6, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=181, tm_isdst=-1)
This method is used to return the time as the integer number of nanoseconds.
import time print(time.time_ns())
Python time module localtime() method return the structured time tuple.
import time result = time.localtime() print(result)
Converts a timestamp to the structured time of the UTC time zone.
import time seconds = time.time() result = time.gmtime(seconds) print(result)
asctime() method is used to convert structured time to string time format for example ‘Thu Feb 20 16:51:03 2020‘. The default value for parameter t is time.localtime().
import time result = time.localtime() print(time.asctime(result)) print(time.asctime())
The mktime() function takes struct_time( or a tuple containing 9 elements corresponding to the struct_time) as an argument and return the seconds passed in an epoch in local time.
import time struct_time = time.localtime() s = time.mktime(struct_time) print(s)
import time t = (2021, 5, 5, 16, 54, 20, 2, 125, 0) s = time.mktime(t) print(s)
In this article, you have learned all about the Python time module along with examples. When you want to work with time in your Python application, Then the module is going to best for you.
I hope this article very helpful for you, If you like this Python time module tutorial, don’t forget to share.
Reference:- Click Here