- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
How to access and convert time using the time library in Python
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.
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.
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)
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.
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.
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.
import time example = “17 July 2001” ans = time.strptime(example, “%d %B %Y”) print(ans)
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)
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.
- Related Articles
- How to check file last access time using Python?
- How to Convert Time String to Time in Excel?
- How to convert date and time with different timezones in Python?
- How to convert varchar “time” to real time in MySQL?
- How to Convert Military Time to Standard Time in Excel?
- How to convert time seconds to h:m:s format in Python?
- How to print current date and time using Python?
- Arduino Time Library Introduction
- How to check the Date and time of Access (last modified) of a File using Java?
- How to find time difference using Python?
- Convert string of time to time object in Java
- How to convert 12-hour time scale to 24-hour time in R?
- How to track access time of a webpage using session in a JSP page?
- How to convert string to time in MySQL?