Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
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)
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 Script Execution
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)
1 2 3 4 5
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.
Working with struct_time Objects
While working with the time module you will notice that you come across the struct_time object a lot.
The struct_time object has the following format ?
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 let's see how to get local time as a struct_time object ?
import time
seconds = time.time()
curr_time = time.localtime(seconds)
print(curr_time)
print("Current year:", curr_time.tm_year)
print("Current month:", curr_time.tm_mon)
print("Current day:", curr_time.tm_mday)
time.struct_time(tm_year=2024, tm_mon=12, tm_mday=19, tm_hour=10, tm_min=30, tm_sec=45, tm_wday=3, tm_yday=354, tm_isdst=0) Current year: 2024 Current month: 12 Current day: 19
In the above example, we obtained the object and accessed its various attributes. You can access all the different attributes to get specific time components.
Converting String to struct_time
Sometimes you might want to convert time in strings into a struct_time object using strptime() ?
import time
date_string = "17 July 2001"
parsed_time = time.strptime(date_string, "%d %B %Y")
print(parsed_time)
print("Year:", parsed_time.tm_year)
print("Month:", parsed_time.tm_mon)
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) Year: 2001 Month: 7
Common Time Format Codes
| Code | Meaning | Example |
|---|---|---|
%Y |
Year (4 digits) | 2024 |
%B |
Full month name | December |
%d |
Day of month | 19 |
%H |
Hour (24-hour) | 14 |
%M |
Minute | 30 |
%S |
Second | 45 |
Conclusion
The time module in Python provides essential functions for working with time data, including getting current time, delaying execution, and converting between different time formats. Use time() for timestamps, sleep() for delays, and strptime() for parsing time strings.
