🕒 time Module

Last Updated: 17th August 2025


The time module in Python allows us to work with time values, delays, and formatting dates.It’s very useful in Data Science for logging, scheduling tasks, or handling timestamps in datasets.


List of Time Functions:

  • time():Returns current time in seconds since 1 Jan 1970 (epoch time).Useful for measuring execution time of code.
import time
start = time.time()

for i in range(10**6):
    pass

end = time.time()
print("Execution time:", end - start)
  • sleep(seconds): Pauses the execution of the program for the specified number of seconds.
import time
print("Start")
time.sleep(2)
print("After 2 seconds")
  • localtime(): current local time in struct_time format.Struct_time is like a tuple with year, month, day, hour, etc.
import time
t = time.localtime()
print(t)
  • asctime():Converts a struct_time tuple to a readable string.
import time
t = time.localtime()
print(time.asctime(t))
  • strftime():Formats struct_time into a custom string.
import time
t = time.localtime()
print(time.strftime("%Y-%m-%d %H:%M:%S", t))
  • strptime():Converts a string to a struct_time tuple.
import time
t = time.strptime("2023-08-17 12:34:56", "%Y-%m-%d %H:%M:%S")
print(t)
  • ctime(): Returns current time in formatted string.
import time
print(time.ctime())
  • process_time():Returns CPU time used by the program (not affected by sleep).
import time
start = time.process_time()
for i in range(10**6): pass
end = time.process_time()
print("CPU Time:", end - start)
  • gmtime():Returns current UTC time in struct_time format.
import time
t = time.gmtime()
print(t)