Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python Time Module

  • Difficulty Level : Basic
  • Last Updated : 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. 

Importing time module

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

What is epoch?

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)


="code-output">Output

Current time in seconds since epoch = 1627908387.764925

Getting time string from seconds

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 epoch
curr = time.ctime(1627908313.717886)
print("Current time:", curr)


Output

Current time: Mon Aug  2 12:45:13 2021

Delaying Execution of programs

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)


Output

0
1
2
3

time.struct_time Class

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 – 

Index  Attribute Name  Values
0 tm_year 0000, …, 9999
1 tm_mon 1, 2, …, 11, 12
2 tm_mday 1, 2, …, 30, 31
3 tm_hour 0, 1, …, 22, 23
4 tm_min 0, 1, …, 58, 59
5 tm_sec 0, 1, …, 60, 61
6 tm_wday 0, 1, …, 6; Sunday is 6
7 tm_yday 1, 2, …, 365, 366
8 tm_isdst 0, 1 or -1

This class contains various functions. Let’s discuss each function in detail.

time.localtime() method

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.

Examp

Getting local time from epoch

Python3




# importing time module
import time
 
# Convert the current time in seconds
# since the epoch to a
# time.struct_time object in Local time
obj = 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() method

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 module
import time
 
obj1 = time.gmtime(1627987508.6496193)
 
# Convert the time.struct_time
# object to local time expressed in
# seconds since the epoch
# using time.mktime() method
time_sec = time.mktime(obj1)
 
# Print the local time in seconds
print("Local time (in seconds):", time_sec)


Output

Local time (in seconds): 1627987508.0

time.gmtime() method

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 module
import time
 
# Convert the current time in seconds
# since the epoch to a
# time.struct_time object in UTC
obj = time.gmtime(1627987508.6496193)
 
# Print the time.struct.time object
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=0)

time.strftime() method

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 f

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 time
s = strftime("%a, %d %b %Y %H:%M:%S",
             gmtime(1627987508.6496193))
print(s)


Output

Tue, 03 Aug 2021 10:45:08

time.asctime() method

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 module
import 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() method
time_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() method
time_str = time.asctime(obj)
print(time_str)


Output

Tue Aug  3 10:45:08 2021
Tue Aug  3 10:45:08 2021

time.strptime() method

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)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!