| by Arround The Web | No comments

Python Time Module Examples Usage

In Python, the “Datetime” and “time” modules are standard modules that are used to deal with datetime objects and time objects. To handle time-specific tasks, the “time” module is used in Python. This module has several functions that perform several different tasks on time objects. Some of the tasks include creating a time object, representing time in various formats, getting specific times, and others.

This write-up will deliver you a comprehensive guide on the Python “time” module by explaining various functions.

What is a Python “time” Module?

The “time” module in Python is utilized to manipulate time in a program. This module provides several features such as getting current time, pausing the execution, and more. To utilize the time module, you need to import it first, as it is found in Python’s standard library, so we don’t need to install anything. Type this to import in Python:

import time

 

Functions of Python “time” Module

This module provides several time-related functions that are used to represent time in different ways. Let’s discuss different functions with the following examples:

Example 1: Using “time.time()” Function

In Python, the “time.time()” function is used to provide the elapsed seconds since the epoch. The epoch is the start of time for Unix systems “UTC midnight on Jan 1, 1970”. Here in this example code, the “time.time()” function gets the epoch seconds of the current time.

import time
print("Current Time in Seconds Since Epoch: ", time.time())

 

The epoch passed seconds of the current time is shown below:

Example 2: Using “time.ctime()” Function

In Python, the “time.ctime()” function is utilized to convert epoch passed seconds to a local time string. Here, in this code, the “time.time()” method retrieves the seconds since the epoch, which is then passed to the “time.ctime()” function. This function then retrieves the readable format of the given time.

import time
seconds = time.time()
print("Current Time in Seconds Since Epoch: ", seconds)
print("Local time:", time.ctime(seconds))

 

The above code shows the following output:

Example 3: Using “time.localtime()” Function

In Python, the “localtime()” method is used to retrieve the seconds since the epoch to a tuple of “9” local time elements (refers to struct_time). In the below code, the time.time() retrieves seconds since epoch. Next, the time.localtime() takes seconds and retrieves the tuple called “struct_time”. Lastly, we displayed the tuple and the attributes of year and hour from the tuple.

import time
seconds = time.time()
output = time.localtime(seconds)
print(output)
print("\nyear:", output.tm_year)
print("hour:", output.tm_hour)

 

Here is the output:

If we break the “struct_time” object, here is what each element means:

Attribute Index Description      Values
tm_year 0 Year 0000, …, 9999
tm_mon 1 Month 1, 2, …, 11, 12
tm_mday 2 Day of the month 1, 2, 3, 4 …, 29, 30, 31
tm_hour 3 Hour 0, 1, …, 23
tm_min 4 Minute 0, 1, …, 59
tm_sec 5 Second 0, 1, …, 60, 61
tm_wday 6 Weekday 0, 1, …, 6; Sunday is 6 and Monday is 0.
tm_yday 7 Day of the year 1, 2, …, 366
tm_isdst 8 Daylight Savings Time -1, 0 or 1

 

Example 4: Using “time.strftime()” Function

In Python, the “time.strftime()” function accepts the “struct_time” tuple and retrieves the string representing it according to the specified format code. Here, the below code shows the string of the specified format using the “time.strftime()” function.

import time
res = time.strftime("%m/%d/%Y, %H:%M:%S", time.localtime())
print(res)

 

Here is the output:

Example 5: Using “time.strptime()” Function

In Python, the “time.strptime()” function parses the time string and retrieves the struct_time object. For example, the “time.strptime()” takes the time and format of the time as an argument and retrieves the “struct_time” object.

import time
res = time.strptime("14 July, 2023", "%d %B, %Y")
print(res)

 

The below snippet shows the “struct_time” object:

Example 6: Using “time.gmtime()” Function

In Python, the “time.gmtime()” function retrieves the “struct_time” tuple object in UTC by taking the passed epoch seconds. Here in the below code, we passed the current epoch time in seconds to the “time.gmtime()” function. The “tm_year”, “tm_wday” and “tm_hour” are used to get the UTC time element named “hour”, “day” and “year”.

import time
res = time.gmtime(time.time())
print(res)
print("\nyear:", res.tm_year)
print("day:", res.tm_wday)
print("hour:", res.tm_hour)

 

Here is the output:

Example 7: Using “time.sleep()” Function

In Python, the “time.sleep()” function is used to delay or suspend the execution of the present thread for the input number of seconds. For example, we use the “print()” function to display the string immediately and after the delay of the “3” seconds. The delay is applied in the program using the “time.sleep()” function.

import time
print("Welcome")
time.sleep(3)
print("Python Guide")

 

The above code generates this output:

Example 8: Using “time.asctime()” Function

In Python, the “time.asctime()” function is used to convert a tuple or “struct_time” object into a 24-character string:

(year value, month value, day of month, hour of time, minute of time, second of time, weekday, day of the year, daylight saving)

 

The following code converts the specified struct_time object value into the time string:

import time
res = time.asctime((2023, 11, 15, 3, 22, 6, 5, 262, 0))
print(res)

 

Here is the output:

Example 9: Using “time.mktime()” Function

In Python, the “time.mktime()” function retrieves the seconds since the epoch by taking the tuple or struct_time object. The below code imports the time module and passes a specified tuple value to the “time.mktime()” function to retrieve seconds since the epoch.

import time
res = time.mktime((2023, 10, 12, 10, 44, 4, 4, 362, 0))
print(res)

 

The above code displays this:

Note: You can also use other time functions such as “time.timezone()” and “time.tzname()” to check the information related to time zones. For further details, you can check this time module official guide.

Conclusion

The inbuilt “time” module in Python provides multiple functions to deal with time objects and perform several operations on them. The “time.time()” function of the “time” module converts the current time to epoch seconds. Similarly the “time.ctime()” and “time.localtime()” represent the seconds since the epoch to local strings and tuple object “struct_time”. This detailed guide explained numerous examples of Python’s “time” module.

Share Button

Source: linuxhint.com

Leave a Reply