How to access and convert time using the time library in Python


Introduction

The time library in Python is used to obtain time in the real world and perform various tasks related to it. You can even manipulate execution time using this module.

Getting Started

The time module comes packaged with Python. This means you do not have to install it separately using the PIP package manager.

In order to use its various functions and methods, you must first import it.

import time

Printing current local time

In order to print the current local time, we will be using the ctime() function.

But firstly, we must obtain the number of seconds since epoch. That is, the number of seconds since January 1st of 1970, 00:00:00.

import time
seconds = time.time()
local_time = time.ctime(seconds)
print("Local time:", local_time)

Output

Local time: Sun Jan 31 23:50:16 2021

In the above program, we first obtain the time since epoch and then provide that as an argument to the ctime function which returns the current local time.

Suspending the execution of the Python script

Sometimes you might want to slow down or delay the execution of the Python script. For example, you might want to print the numbers slowly while iterating through a for loop.

You can do this using the sleep function in the time module.

Example

import time
for i in range (1,6):
   print(i)
   time.sleep(1)

The above program prints from 1 to 5 and waits 1 second before printing the next number.

This way, you can avoid printing the entire content at once on the output screen.

Printing local time as a time.struct_time object

While working on the time module you will notice that you come across the sturct_time object a lot.

In order to create your own object, follow the syntax below −

time.struct_time(tm_year=2021, tm_mon=1, tm_mday=31,
   tm_hour=9, tm_min=28, tm_sec=56,
   tm_wday=6, tm_yday=31, tm_isdst=0)

Now that you know what the struct_time object is, let us start working on printing the local time.

Example

import time
seconds = time.time()
curr_time = time.localtime(seconds)
print(curr_time)
print(“Current year −> “, curr_time.tm_year)

In the above example, we obtained the object and accessed its various arguments. You can access all the different arguments following the syntax of struct_time mentioned above to get a better idea of how things work.

Converting string data into struct_time

Sometimes you might want to convert time in strings into a struct_time object.

Example

import time
example = “17 July 2001”
ans = time.strptime(example, “%d %B %Y”)
print(ans)

Output

time.struct_time(tm_year=2001, tm_mon=7, tm_mday=17, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=198, tm_isdst=−1)

Conclusion

You now understand the different uses and functionalities of the time module present in Python.

You’ve learnt about the struct_time object and how to use and manipulate it. And also to convert string data into struct_time object.

For more information on the time module and about its various other features, read through its official documentation at − https://docs.python.org/3/library/time.html.

Updated on: 11-Feb-2021

315 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements