Python time Module
Example
Get current time and sleep for 1 second:
import time
start = time.time()
print(f'Start time: {start}')
time.sleep(1)
end = time.time()
print(f'Elapsed: {end - start:.2f} seconds')
Try it Yourself »
Definition and Usage
The time module provides functions for working with time values and delays.
Use it to measure elapsed time, add delays, format timestamps, or convert between time representations.
Members
Member | Description |
---|---|
asctime() | Convert time tuple to string. |
ctime() | Convert seconds since epoch to string. |
gmtime() | Convert seconds since epoch to UTC time tuple. |
localtime() | Convert seconds since epoch to local time tuple. |
mktime() | Convert time tuple to seconds since epoch. |
monotonic() | Return monotonic clock value (cannot go backwards). |
perf_counter() | Return performance counter for precise timing. |
process_time() | Return CPU time used by current process. |
sleep() | Suspend execution for given number of seconds. |
strftime() | Format time tuple as string. |
strptime() | Parse string to time tuple. |
struct_time | Type for time tuples. |
time() | Return current time in seconds since epoch. |
time_ns() | Return current time in nanoseconds since epoch. |