Python time Module

1. What is the time Module

The time module provides low-level time-related functions for:

  • Timestamps

  • Delays

  • Performance measurement

  • System clock interactions

import time

print(time.time())

Returns current time in seconds since the Unix Epoch.


2. Get Current Time as Readable String

import time

current_time = time.ctime()
print(current_time)

Outputs human-readable system time, e.g.: Mon Nov 24 09:45:32 2025


3. Get Current Time Components

Breaks current time into structured components.


4. Sleep / Delay Execution

Pauses program execution for specified seconds.


5. Measure Execution Time (Performance Timing)

Used for benchmarking and performance profiling.


6. High-Precision Timer with perf_counter()

Recommended for performance-sensitive measurements.


7. Formatting Time with strftime()

Formats time using system clock.


8. Parsing Time with strptime()

Converts strings into time-struct objects.


9. Convert Timestamp to Readable Time

Common pattern for API responses and logs.


10. Enterprise Use Case: Rate Limiting Logic

Controls execution frequency in APIs and services.


Key Functions Overview

Function
Purpose

time.time()

Current timestamp

time.ctime()

Readable time string

time.sleep()

Delay execution

time.localtime()

Structured local time

time.gmtime()

UTC structured time

time.strftime()

Format time

time.strptime()

Parse time

time.perf_counter()

High precision timer


time vs datetime

time Module
datetime Module

Low-level functions

High-level abstraction

System clock focus

Date/time manipulation

Ideal for delays & timing

Ideal for business logic


Best Practices

  • Use time.sleep() for scheduling delays

  • Use perf_counter() for benchmarking

  • Use datetime for business date logic

  • Prefer UTC timestamps for server operations


Last updated