Skip to content

time

The Python time module provides various time-related functions. It allows you to work with time in a variety of ways, including getting the current time, measuring execution durations, and working with time in seconds since the epoch (January 1, 1970, 00:00:00 UTC).

Here’s a quick example:

Language: Python
>>> import time

>>> time.time()
1753361956.4005592

Key Features

  • Retrieves the current time
  • Formats and parses time strings
  • Measures elapsed time with high precision
  • Pauses execution for a given number of seconds
  • Converts between time representations (seconds, tuples, strings)
  • Obtains system time in UTC or local time
  • Works with monotonic clocks for reliable timing
  • Supports sleep with subsecond precision
  • Avoids float precision loss with nanosecond-resolution variants such as time.time_ns(), time.perf_counter_ns(), and time.monotonic_ns()

Frequently Used Classes and Functions

Object Type Description
time.struct_time Class Represents a time value as a named tuple
time.time() Function Gets the current time in seconds since the epoch
time.time_ns() Function Gets the current time in integer nanoseconds since the epoch
time.sleep() Function Pauses execution for a specified number of seconds
time.strftime() Function Formats a time.struct_time as a string
time.strptime() Function Parses a time string into a time.struct_time
time.perf_counter() Function Measures elapsed time with high precision
time.perf_counter_ns() Function Measures elapsed time in integer nanoseconds
time.localtime() Function Converts seconds since the epoch to a local time.struct_time
time.gmtime() Function Converts seconds since the epoch to a UTC time.struct_time
time.ctime() Function Converts seconds since epoch to a readable string
time.monotonic() Function Returns the value of a monotonic clock that can’t go backward
time.process_time() Function Returns the CPU process time
time.asctime() Function Converts a time tuple to a readable string
time.mktime() Function Converts a local time.struct_time to seconds since the epoch

Examples

Get the current local time as a readable string:

Language: Python
>>> import time

>>> time.ctime()
'Thu Jul 24 14:58:22 2025'

Pause execution for two seconds:

Language: Python
>>> time.sleep(2)

Format the current time as a string:

Language: Python
>>> time.strftime(
...     "%Y-%m-%d %H:%M:%S", time.localtime()
... )
'2025-07-24 14:58:53'

Measure elapsed time with high precision:

Language: Python
>>> start = time.perf_counter()
>>> time.sleep(1)
>>> end = time.perf_counter()
>>> end - start
1.000123456

Common Use Cases

  • Measuring the duration of code execution
  • Formatting and displaying the current date and time
  • Scheduling tasks or delays at specific intervals
  • Converting between different time representations
  • Benchmarking and profiling code performance
  • Implementing retries with delays
  • Creating timeouts or intervals in programs
  • Simulating delays in tests, scripts, or teaching examples
  • Generating timestamps for logs or filenames

Real-World Example

Suppose you want to measure the execution time of a block of code to optimize performance. Here’s how you can achieve that using the time module:

Language: Python
>>> import time

>>> def long_running_task():
...     time.sleep(2)
...

>>> start = time.perf_counter()
>>> long_running_task()
>>> end = time.perf_counter()
>>> elapsed_time = end - start

>>> print(
...     f"The task took {elapsed_time:.2f} seconds."
... )
The task took 2.00 seconds.

In this example, the time module helps you measure how long a task takes to execute, which is valuable for performance tuning and optimization.

To see why time.perf_counter() is the right clock for this job, watch three clocks time a similar 3-second run, time.sleep(2) followed by one second of CPU work. Click Run to replay it, then check Set system clock back 4 s at 1 s:

Interactive diagram — enable JavaScript to view.

Each line plots a clock’s reading minus its start value against real time, and the end - start chips show the final result. time.perf_counter() reports the true 3 s. time.process_time() stays flat during time.sleep(2) and reports 1 s because it only counts CPU time. When the system clock is set back, the dashed time.time() line drops at the 1 s mark and ends at −1 s, so don’t use it to time code.

A Beginner’s Guide to the Python time Module

Course

Mastering Python's Built-in time Module

In this course, you'll learn how to use the Python time module to represent dates and times in your application, manage code execution, and measure performance.

intermediate stdlib

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Oct. 21, 2026