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:

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

Frequently Used Classes and Functions

Object Type Description
time.time() Function Gets the current time in seconds since the epoch
time.sleep() Function Pauses execution for a specified number of seconds
time.strftime() Function Formats a time tuple as a string
time.strptime() Function Parses a time string into a time tuple
time.perf_counter() Function Measures elapsed time with high precision
time.localtime() Function Converts seconds since epoch to a local time tuple
time.gmtime() Function Converts seconds since epoch to a UTC time tuple
time.ctime() Function Converts seconds since epoch to a readable string
time.monotonic() Function Returns the value of a monotonic clock (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 tuple to seconds since the epoch

Examples

Get the time since epoch:

Python
>>> import time

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

Pause execution for two seconds:

Python
>>> time.sleep(2)

Format the current time as a string:

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

Measure elapsed time with high precision:

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:

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.

Tutorial

How to Get and Use the Current Time in Python

In this tutorial, you'll be getting the current time in Python. You'll get your hands on a datetime object that represents the current time. You'll see how to format it according to international standards, and you'll even check out how computers represent time.

intermediate

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


By Leodanis Pozo Ramos • Updated July 24, 2025