# What is Python’s time module?

Python `time` is a module for working with date and time information in the programming language. The module uses Unix time and `struct_time` objects, and can be used in different ways.

## What is the Python `time` module and how can it be used?

The Python `time` module is essential for implementing and handling time data in the programming language. With this module, it’s possible to **display the current time, format dates and times, and specify a timeframe** for running a function. Before you can use Python `time`, you first need to import the module using the `import` keyword:

```python
import time
```

Now, you can use the module together with different functions. We’ll go over the most important ones in the following sections. If you’re looking for an alternative to Python `time`, you can also use [Python datetime](https://www.ionos.ca/digitalguide/websites/web-development/python-datetime/).

## What is `time.time()` and the Unix timestamp?

`time.time()` is a function in Python that returns the current time in seconds using the Unix Epoch as its starting point. The Unix Epoch (**January 1, 1970 at 00:00:00** (Coordinated Universal Time)) is used by Windows and countless Unix systems as a starting point for measuring time. Python also uses the Unix Epoch. This type of calculation is also referred to as “seconds since the Epoch”. Here’s what the code looks like:

```python
import time
elapsed_time = time.time()
print("Elapsed seconds since Epoch: ", elapsed_time)
```

The output for this code constantly changes, reflecting the time when the code is executed. For example, when writing this article, we received the following output:

```python
1716816325
```

## How to make dates readable using `time.ctime()`

Since having the output in seconds is usually not very useful, Python `c.time()` offers programmers a way to convert the Unix timestamp into a **readable date**. This allows you to benefit from the preciseness of `time()`without having to sacrifice readability. Here’s the code:

```python
import time
elapsed_time = 1716816325
current_time = time.ctime(elapsed_time)
print("This is the current time: ", current_time)
```

The local time is used and the output looks like this:

```python
This is the current time: Mon May 27 2024 15:25:25
```

Tip [Deploy Now](https://www.ionos.ca/hosting/deploy-now "Deploy Now from IONOS") from IONOS is the ideal solution for web projects. Deploy changes in your code with GitHub and benefit from real-time adjustments. Find the right plan for your project!

## How to schedule program execution with `time.sleep()`

Python’s `time` module can do more than just return the current time though. The module can also be used to control or delay the execution of a program. You can do this using `time.sleep()`. Here’s an example:

```python
import time
print("This text is displayed immediately… ")
time.sleep(5)
print("…and this one is displayed 5 seconds later.")
```

This is what the output looks like:

```python
This text is displayed immediately…
…and this one is displayed 5 seconds later.
```

The second part is displayed five seconds after the first text is displayed. For useful ways to use [Python time.sleep()](https://www.ionos.ca/digitalguide/websites/web-development/python-sleep/), check out the article on this function in our Digital Guide.

## What is a `struct_time` object?

`struct_time` is an object used by many of the functions in Python’s `time` module. It’s a named tuple containing values that can be accessed by index or attribute, and it’s often used as a parameter or return value. The following attributes can be found in a `struct_time` object:

<table>
  <thead>
    <tr>
      <th>Index</th>
      <th>Attribute</th>
      <th>Description</th>
      <th>Possible Values</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>0</td>
      <td>tm\_year</td>
      <td>Year</td>
      <td>0000, …, 2024, …, 9999</td>
    </tr>
    <tr>
      <td>1</td>
      <td>tm\_mon</td>
      <td>Month</td>
      <td>1, 2, 3, …, 12</td>
    </tr>
    <tr>
      <td>2</td>
      <td>tm\_mday</td>
      <td>Day of the month</td>
      <td>1, 2, 3, 31</td>
    </tr>
    <tr>
      <td>3</td>
      <td>tm\_hour</td>
      <td>Time in hours</td>
      <td>0, 1, 2, …, 23</td>
    </tr>
    <tr>
      <td>4</td>
      <td>tm\_min</td>
      <td>Time in minutes</td>
      <td>0, 1, 2, …, 59</td>
    </tr>
    <tr>
      <td>5</td>
      <td>tm\_sec</td>
      <td>Time in seconds</td>
      <td>0, 1, 2, …, 60, 61</td>
    </tr>
    <tr>
      <td>6</td>
      <td>tm\_wday</td>
      <td>Day of the week</td>
      <td>0 (for Monday), …, 6 (for Sunday)</td>
    </tr>
    <tr>
      <td>7</td>
      <td>tm\_yday</td>
      <td>Day of the year</td>
      <td>1, 2, 3, …, 366</td>
    </tr>
    <tr>
      <td>8</td>
      <td>tm\_isdst</td>
      <td>Daylight Saving Time</td>
      <td>0 (Winter), 1 (Summer) or -1 (Information not available)</td>
    </tr>
  </tbody>
</table>

### `time.localtime()`: Connecting `struct_time` with Epoch time

The function **`time.localtime()`** uses a Unix timestamp to generate a `struct_time` object. Here’s how to write the code:

```python
import time
timestamp = time.time()
current_time = time.localtime(timestamp)
print(current_time)
```

This gives us the following output:

```python
time.struct_time(tm_year=2024, tm_mon=5, tm_mday=27, tm_hour=15, tm_min=25, tm_sec=25, tm_wday=0, tm_yday=148, tm_isdst=1)
```

Alternatively, you can also enter a value for the parameter `localtime()`. This is what the code looks like:

```python
import time
timestamp = time.time()
current_time = time.localtime(1716816325)
print(current_time)
```

The value we chose for the parameter gives us the same output as above:

```python
time.struct_time(tm_year=2024, tm_mon=5, tm_mday=27, tm_hour=15, tm_min=25, tm_sec=25, tm_wday=0, tm_yday=148, tm_isdst=1)
```

### `time.mktime()`: The opposite of `localtime()`

`time.mktime()` is a function from the Python `time` module that is essentially the opposite of `localtime()`.With this function, Python converts a `struct_time` object, which is passed as the argument, to a timestamp, which shows the number of seconds that have passed since the Epoch. Here’s what the code looks like:

```python
import time
timestamp = time.mktime(local_time)
print(timestamp)
```

The output is in seconds and is formatted like this:

```python
1716816325
```

### `time.gmtime()`: Displaying Coordinated Universal Time

`time.gmtime()` is similar to `time.localtime()` in many respects. However, the `struct_time` object returned by `time.gmtime()` is based on Coordinated Universal Time (UTC) rather than local time. Here’s the code:

```python
import time
time_stamp = time.time()
utc_time = time.gmtime(time_stamp)
print(utc_time)
```

Some of the values output below differ from the output in the last example:

```python
time.struct_time(tm_year=2024, tm_mon=5, tm_mday=27, tm_hour=13, tm_min=25, tm_sec=25, tm_wday=0, tm_yday=148, tm_isdst=0)
```

### `time.asctime()`: Converting `struct_time` objects into strings

You can use `time.asctime()` to convert a `struct_time` object into a [Python string](https://www.ionos.ca/digitalguide/websites/web-development/python-string/). Here’s an example:

```python
import time
point_in_time = time.localtime()
readable_format = time.asctime(point_in_time)
print(readable_format)
```

This outputs a date that is easy to understand:

```python
Mon May 27 15:25:25 2024
```

### `time.strftime()`: Creating a string with Python `time`

With `time.strftime()`, you can use different codes to format time data in different ways that are easy to read. Here are some of the most important codes you can use with this function:

- **%Y**: Represents the year (Format: 0001, …, 2024, …, 9999)
- **%m**: Represents the month, using numbers 01 (January) to 12 (December)
- **%d**: Represents the day of the month, using numbers 01 to 31
- **%H**: Represents the hour, using numbers 00 to 23
- **%M**: Represents the minute, using numbers 00 to 59
- **%S**: Represents the second, using numbers 00 bis 61

Here’s an example:

```python
import time
point_in_time = time.localtime()
readable_format = time.strftime("%d-%m-%Y, %H:%M:%S", point_in_time)
print(readable_format)
```

Here’s what the output looks like:

```python
27-05-2024, 15:25:25
```

### `time.strptime()`: Converting strings into `struct_time` objects

Python’s `time` module also lets you convert strings into `struct_time` objects with `time.strptime()`. Here’s how:

```python
import time
point_in_time = time.strptime(readable_format, "%d-%m-%Y, %H: %M:%S")
print(point_in_time)
```

This gives us the following output:

```python
time.struct_time(tm_year=2024, tm_mon=5, tm_mday=27, tm_hour=15, tm_min=25, tm_sec=25, tm_wday=0, tm_yday=148, tm_isdst=1)
```

Tip Explore our Digital Guide to find out more about Python. In addition to instructions on how to [install Python](https://www.ionos.ca/digitalguide/websites/web-development/install-python/), we also have a [Python tutorial](https://www.ionos.ca/digitalguide/websites/web-development/python-tutorial/) that covers the basics of the programming language as well as an article on [Python operators](https://www.ionos.ca/digitalguide/websites/web-development/python-operators/).


This is a markdown version of: [https://www.ionos.ca/digitalguide/websites/web-development/python-time-module/](https://www.ionos.ca/digitalguide/websites/web-development/python-time-module/) for AI/LLM consumption.