You may use the Python time package to interact with all time-related methods. The majority of them merely call the same-named platform C library routines. There would be minor changes across platforms. The time package is included in all Python releases.

Example 01: Find Seconds

Let’s start with the first example of the time module. Open your command terminal in Ubuntu 20.04 Linux distribution by a “Ctrl Alt T” shortcut. We are using the below-mentioned command in the console followed by the “Enter” key to create a new file as follows:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image1-51.png" data-lazy- height="23" src="data:image/svg xml,” width=”436″>

Now the file has been created, go to the file explorer first. In your file explorer, navigate towards the home directory. You’ll come across your newly created file in this directory. Open the file “test.py” by double-clicking on it. After the opening of a file, write out the represented below code in it. The code is showing that the python support has been stated in the first line. In the second line of code, we have to import the module “time” to use it in our code further. Then we have created a new variable, “Seconds,” and used a built-in time method “time()” via the “time” module to collect time in seconds that have been passed from midnight today until now. We show the collected seconds in the print statement via our variable “Seconds” in the parameter. Close your file after saving it properly.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image3-52.png" data-lazy- height="101" src="data:image/svg xml,” width=”649″>

Let’s come to the terminal again. To execute the code, we must use the “python3” package in our query. As it’s been successfully installed and supported in our system, we can use it in our instruction. So, try the below query of python along with the name of a file and hit Enter. The resultant output is showing the total number of seconds from midnight until now.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image2-51.png" data-lazy- height="39" src="data:image/svg xml,” width=”637″>

Example 02: Find Epoch

To check the epoch of your system where the time starts as a platform-independent point, we have to use the gmtime() method in our code. Therefore, create a new file “one.py” as:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image5-45.png" data-lazy- height="22" src="data:image/svg xml,” width=”435″>

Write out the below code after opening it from the home directory. This code contains the import of the time module and a print statement that has been used to check the epoch. Now we are defining 0 in a parameter to display the start of the epoch. Save the file and open the shell.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image4-50.png" data-lazy- height="99" src="data:image/svg xml,” width=”353″>

Execution of a file by python repository shows struct time in the Coordinated Universal Time (CTU) format as shown in the image snapshot attached below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image7-33.png" data-lazy- height="54" src="data:image/svg xml,” width=”718″>

Example 03: Get Current Time

To get the current local time, we have to use the “local time()” function in our code. Hence open the “test.py” file from the home directory and update it with the following-below code shown in the picture. Again we have used the time module to use time methods. Then we have initialized a variable “CurrentLocalTime” to get the current local time via the method “local time()” as a value. This variable “CurrentLocalTime” will be printed out in the print statement used in the code with some string value in it. Please save the file and leave it to open the terminal.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image6-41.png" data-lazy- height="110" src="data:image/svg xml,” width=”473″>

Let’s execute the file once again by the same python package support. The output is showing us the structural form of time and date in our output. It’s showing the current year, date, month, time, hours, minutes, seconds, and many more things along with it.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image9-25.png" data-lazy- height="56" src="data:image/svg xml,” width=”721″>

Example 04: Convert Local Time to Seconds

You’ve seen in the example as mentioned above that how to check the local time structurally. We will be using a new method, “time(),” to convert the current local time into seconds. Let’s open the same file again and update the code with the below script. The code is getting the current local time first in the “CurrentLocalTime” variable and then convert this time to seconds via the “mkdir()” method used in the 4th line of code. This converted time is then saved into variable “seconds” and printed out at line 5 of the code.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image8-27.png" data-lazy- height="129" src="data:image/svg xml,” width=”500″>

After saving and then executing the file, we got the below result showing the total seconds in the current local time.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image12-13.png" data-lazy- height="38" src="data:image/svg xml,” width=”457″>

Example 05: Get Formatted Time Using asctime()

If somebody wants to get the formatted time using the local time format, this example is for them. After opening the file, change your code with the below shown one. This code is as simple as the above codes. We have used the time module and got a current local time via the “local time()” method. Then we have passed the value of this function to another method, “asctime(),” to convert it into a standard format and saved it into a variable “CurrentLocalTime.”  This variable has been printed out in the last line of code. After saving the file, we have opened the terminal to execute our code file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image10-19.png" data-lazy- height="114" src="data:image/svg xml,” width=”588″>

Execution of a file is showing the standard format of date and time in the output.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image11-14.png" data-lazy- height="39" src="data:image/svg xml,” width=”457″>

Example 06: Time Sleep Function

To give sleep to your output, we will be using a sleep function. This code contains two print statements with the gap of time module function “sleep” to give a time break of 5 seconds to the second print statement.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image13-12.png" data-lazy- height="127" src="data:image/svg xml,” width=”504″>

Upon execution, after the output of the first print statement, the second statement will appear after 5 seconds.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image14-9.png" data-lazy- height="56" src="data:image/svg xml,” width=”455″>

Example 07: Create a Digital Clock

To create a digital clock on the terminal, update your code with the below-one having while loop in it. This loop is getting current local time and passing this to the strftime() method to create a digital watch. It will print the time-slap after every 2 seconds.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image15-7.png" data-lazy- height="156" src="data:image/svg xml,” width=”510″>

Execution of code is showing the time after every 2 seconds as below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image16-5.png" data-lazy- height="290" src="data:image/svg xml,” width=”469″>

Conclusion:

At last, we have discussed all the possible basic methods of time modules supported in the python implemented in the Ubuntu 20.04 Linux system. All these codes and queries can be used in other distros of Linux as well.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/Author-Image-150×150.jpg60d31bd6c460a.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.