Python Time Module

30 Nov, 2021
In this article, we will discuss the time module and various functions provided by this module with the help of good examples. 
As the name suggests Python time module allows to work with time in Python. It allows functionality like getting the current time, pausing the Program from executing, etc. So before starting with this module we need to import it. 
The time module comes with Python’s standard utility module, so there is no need to install it externally. We can simply import it using the import statement.
import time
The epoch is the point where the time starts and is platform-dependent. On Windows and most Unix systems, the epoch is January 1, 1970, 00:00:00 (UTC), and leap seconds are not counted towards the time in seconds since the epoch. To check what the epoch is on a given platform we can use time.gmtime(0).
Example: Getting epoch
Python3
import time print(time.gmtime(0))
Output:
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)
From the above example, you can see that epoch is 1 January 1970. This means that 2 January 1970 can be expressed as 86400 seconds since epoch as there are 86400 seconds in a day.
Note: The time before the epoch can still be represented in seconds but it will be negative. For example, 31 December 1969 will be represented as -86400 seconds.
Getting current time in seconds since epoch
time.time() methods return the current time in seconds since epoch. It returns a floating-point number.
Example: Current time in seconds since epoch
Python3
import time  curr = time.time()print("Current time in seconds since epoch =", curr)
Current time in seconds since epoch = 1627908387.764925
time.ctime() function returns a 24 character time string but takes seconds as argument and computes time till mentioned seconds. If no argument is passed, time is calculated till the present.
Example: Getting time string from seconds
Python3
import time # getting current time by passing# the number of seconds since epochcurr = time.ctime(1627908313.717886)print("Current time:", curr)
Current time: Mon Aug  2 12:45:13 2021
Execution can be delayed using time.sleep() method. This method is used to halt the program execution for the time specified in the arguments.
Example: Delaying execution time of programs in Python.
Python3
import time for i in range(4):         # using sleep() to hault execution    time.sleep(1)    print(i)
0
1
2
3
Struct_time class helps to access local time i.e. non-epochal timestamps. It returns a named tuple whose value can be accessed by both index and attribute name. Its object contains the following attributes – 
This class contains various functions. Let’s discuss each function in detail.
localtime() method returns the struct_time object in local time. It takes the number of seconds passed since epoch as an argument. If the seconds parameter is not given then the current time returned by time.time() method is used.
Example: Getting local time from epoch
Python3
# importing time moduleimport time # Convert the current time in seconds# since the epoch to a# time.struct_time object in Local timeobj = time.localtime(1627987508.6496193) print(obj)
Output
time.struct_time(tm_year=2021, tm_mon=8, tm_mday=3, tm_hour=16, tm_min=15, tm_sec=8, tm_wday=1, tm_yday=215, tm_isdst=0)
time.mktime() is the inverse function of time.localtime() which converts the time expressed in seconds since the epoch to a time.struct_time object in local time.
Example: Converting the struct_time object to seconds since epoch
Python3
# importing time moduleimport time obj1 = time.gmtime(1627987508.6496193) # Convert the time.struct_time# object to local time expressed in# seconds since the epoch# using time.mktime() methodtime_sec = time.mktime(obj1) # Print the local time in secondsprint("Local time (in seconds):", time_sec)
Local time (in seconds): 1627987508.0
time.gmtime() is used to convert a time expressed in seconds since the epoch to a time.struct_time object in UTC in which tm_isdst attribute is always 0. If the seconds parameter is not given then the current time returned by time.time() method is used.
Example: Use of time.gmtime() method
Python3
# importing time moduleimport time # Convert the current time in seconds# since the epoch to a# time.struct_time object in UTCobj = time.gmtime(1627987508.6496193) # Print the time.struct.time objectprint(obj)
Output
time.struct_time(tm_year=2021, tm_mon=8, tm_mday=3, tm_hour=10, tm_min=45, tm_sec=8, tm_wday=1, tm_yday=215, tm_isdst=0)
time.strftime() function converts a tuple or struct_time representing a time as returned by gmtime() or localtime() to a string as specified by the format argument. If t is not provided, the current time as returned by localtime() is used. The format must be a string. ValueError is raised if any field in t is outside of the allowed range.
Example: Converting struct_time object to a string using strftime() method
Python3
from time import gmtime, strftime # using simple format of showing times = strftime("%a, %d %b %Y %H:%M:%S",             gmtime(1627987508.6496193))print(s)
Tue, 03 Aug 2021 10:45:08
time.asctime() method is used to convert a tuple or a time.struct_time object representing a time as returned by time.gmtime() or time.localtime() method to a string of the following form:
Day Mon Date Hour:Min:Sec Year
Example: Converting tuple to time.struct_time object to string
Python3
# importing time moduleimport time  obj = time.gmtime(1627987508.6496193) # Convert the time.struct_time# object to a string of the# form 'Day Mon Date Hour:Min:Sec Year'# using time.asctime() methodtime_str = time.asctime(obj)print(time_str)  obj = time.localtime(1627987508.6496193) # Convert the time.struct_time# object to a string of the# form 'Day Mon Date Hour:Min:Sec Year'# using time.asctime() methodtime_str = time.asctime(obj)print(time_str)
Tue Aug  3 10:45:08 2021
Tue Aug  3 10:45:08 2021
time.strptime() method converts the string representing time to the struct_time object.
Example: Converting string to struct_time object.
Python3
import time string = "Tue, 03 Aug 2021 10:45:08"obj = time.strptime(string, "%a, %d %b %Y %H:%M:%S") print(obj)
Output
time.struct_time(tm_year=2021, tm_mon=8, tm_mday=3, tm_hour=10, tm_min=45, tm_sec=8, tm_wday=1, tm_yday=215, tm_isdst=-1)
rkbhola5
python-modules
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Python OOPs Concepts