Table of Contents

import time

import time is a Python import that provides time-related functions: sleeping, measuring elapsed time, and getting the current timestamp.

Example

# Python
# description: sleep, measure elapsed time, get timestamp
 
import time
 
# Current time (seconds since epoch)
timestamp = time.time()
print(timestamp)  # 1724439045.123456
 
# Sleep for seconds
print("Starting...")
time.sleep(2)
print("Done (2 seconds later)")
 
# Measure elapsed time
start = time.time()
sum(range(1000000))
elapsed = time.time() - start
print(f"Computation took {elapsed:.3f} seconds")
 
# Convert timestamp to readable format
readable = time.ctime(timestamp)
print(readable)  # Fri Aug 24 15:30:45 2025
 
# Get time struct
ts = time.localtime(timestamp)
print(f"Year: {ts.tm_year}, Month: {ts.tm_mon}")

Common functions