Site Tools


import-time

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

  • time.time(): current time as seconds since epoch (Unix timestamp)
  • time.sleep(seconds): pause execution
  • time.ctime(timestamp): format timestamp as readable string
  • time.localtime(timestamp): convert timestamp to local time struct
  • time.gmtime(timestamp): convert timestamp to UTC time struct
  • time.mktime(time_struct): convert time struct to timestamp
  • time.perf_counter(): high-resolution clock for benchmarking
import-time.md · Last modified: by 127.0.0.1