Time
Python's `time` module provides time-related functions for working with timestamps, delays, and performance measurement. It operates at the OS level with lower precision than `datetime` for human-readable times. The guide walks through Timestamps & Epoch, Sleep & Timing, struct_time & Formatting, Performance Measurement. `time.time()` returns the current Unix timestamp (seconds since January 1, 1970 UTC) as a float with sub-second precision. `time.ctime([secs])` converts a timestamp to a human-readable string: "Mon Jun 8 14:30:00 2026". `time.gmtime([secs])` converts to a UTC struct_time. `time.localtime([secs])` converts to a local struct_time. `time.mktime(t)` converts a struct_time back to a timestamp. The epoch varies by platform — use UTC for… `time.sleep(seconds)` suspends execution for the given duration. Use fractional seconds: `time.sleep(0.5)` for 500ms. For precise timing: use `time.perf_counter()` which includes time elapsed during sleep and is system-wide. `time.monotonic()` cannot go backwards (unaffected by system clock changes). Example: `start = time.perf_counter(); do_work(); elapsed = time.perf_counter() - start`. This is the recommended way to measure code execution time. `time.strftime(format, t)` formats a struct_time as a string. Common format codes: `%Y` (year), `%m` (month 01-12), `%d` (day 01-31), `%H` (hour 00-23), `%M` (minute 00-59), `%S` (second 00-61). `time.strptime(string, format)` parses a string into a struct_time. Example: `time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())`. The struct_time object has attributes: tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst.