Open In App

Python Time Module

Improve
Improve
Like Article
Like
Save
Share
Report

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

The code uses the time module to print the result of time.gmtime(0), which represents the time in the GMT (Greenwich Mean Time) timezone at the Unix epoch (January 1, 1970, 00:00:00 UTC).

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)

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

The code uses the time module to retrieve the current time in seconds since the Unix epoch (January 1, 1970).

Python3




import time
curr = time.time()
print("Current time in seconds since epoch =", curr)


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

The code uses the time module to convert a specified timestamp (1627908313.717886) into a human-readable date and time format.

Python3




import time
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.

This code uses the time module to introduce a one-second delay using time.sleep(1) inside a loop that iterates four times.

Python3




import time
for i in range(4):
    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.

Example: Getting local time from epoch

The code uses the time module to convert a specified timestamp (1627987508.6496193) into a time.struct_time object representing the corresponding date and time.

Python3




import 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

This code first uses the time module to convert a specified timestamp (1627987508.6496193) into a time.struct_time object in the GMT (Greenwich Mean Time) timezone using time.gmtime(). Then, it uses time.mktime() to convert this time.struct_time object back into a timestamp.

Python3




import time
obj1 = time.gmtime(1627987508.6496193)
time_sec = time.mktime(obj1)
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

The code uses the time module to convert a specified timestamp (1627987508.6496193) into a time.struct_time object representing the corresponding date and time in the GMT (Greenwich Mean Time) timezone.

Python3




import time
obj = time.gmtime(1627987508.6496193)
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 field in t is outside of the allowed range.

Example: Converting struct_time object to a string using strftime() method

The code uses the gmtime function from the time module to convert a specified timestamp (1627987508.6496193) into a formatted string using strftime. The output represents the date and time in the GMT (Greenwich Mean Time) timezone in the format specified by the format string.

Python3




from time import gmtime, strftime
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

This code uses the time module to convert a specified timestamp (1627987508.6496193) into a human-readable date and time format using time.asctime(). It does so both for the GMT (Greenwich Mean Time) timezone and the local timezone.

Python3




import time
obj = time.gmtime(1627987508.6496193)
time_str = time.asctime(obj)
print(time_str)
obj = time.localtime(1627987508.6496193)
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.

This code uses the time module to parse a formatted string representing a date and time and convert it into a time.struct_time object. The time.strptime() function is used for this purpose.

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)



Last Updated : 21 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads