# Time Functionality In Python

> Explore how to work with the time module from the Python Standard Library in detail.

- Canonical: https://dennisokeeffe.com/blog/2021-08-06-time-functionality-in-python
- Published: 2021-08-06
- Updated: 2021-08-06
- Tags: beginner, python, 100daysofpython

> This is Day 18 of the #100DaysOfPython challenge.

This post will use the [`time`](https://docs.python.org/3/library/time.html) module from Python's standard library to explore how we can work with different time capabilities such as getting the local time and sleeping within a program.

All of the code used in this post can be found in my [GitHub repository](https://github.com/okeeffed/hello-python-time-module).

## Prerequisites

1. Familiarity with [Pipenv](https://pipenv.pypa.io/en/latest/). See [here](https://blog.dennisokeeffe.com/blog/2020-07-04-abcs-of-pipenv) for my post on Pipenv.
2. Familiarity with [iPython](https://ipython.org/).
3. ...

## Getting started

Let's create the `hello-python-time-module` directory and install Pillow.

```s
# Make the `hello-python-time-module` directory
$ mkdir hello-python-time-module
$ cd hello-python-time-module

# Init the virtual environment
$ pipenv --three
$ pipenv install --dev ipython
```

At this stage, we are ready to explore using the `time` module using `iPython`.

To do so, run `pipenv run ipython` from the command line to open up the REPL.

## Importing the module

First off, we will want to import the `time` module. We can do so from within the REPL with the following:

```py
import time
```

Once imported, we can check that we have access to the `time` module by checking the `__name__` attribute and playing around with a few methods.

```py
time.__name__ # 'time'
time.time() # 1628200068.664737
```

The method `time` that we called on the module returns the time in seconds since the epoch as a floating point number.

On most systems, the epoch is January 1st, 1970 at midnight. This is currently referred to as Unix time.

## Getting the local time

We can also use the time module to get the local or GM time, as well as format the time to a more readable format.

```py
time.localtime() # time.struct_time(tm_year=2021, tm_mon=8, tm_mday=6, tm_hour=7, tm_min=46, tm_sec=20, tm_wday=4, tm_yday=218, tm_isdst=0)
time.gmtime() # time.struct_time(tm_year=2021, tm_mon=8, tm_mday=5, tm_hour=21, tm_min=46, tm_sec=32, tm_wday=3, tm_yday=217, tm_isdst=0)
time.asctime(time.localtime()) # 'Fri Aug  6 07:51:53 2021'
```

## Formatting and parsing

We have control over how we format and parse time with the [`strftime`](https://docs.python.org/3/library/time.html#time.strftime) and [`strptime`](https://docs.python.org/3/library/time.html#time.strptime) functions.

```py
time.strptime("30 Nov 00", "%d %b %y") # time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)
time.strftime("%m/%d/%Y, %H:%M:%S") # '08/06/2021, 07:56:44'
time.strftime("%d %b %y", time.strptime("30 Nov 00", "%d %b %y")) # '30 Nov 00'
```

## Sleep

We can use the time module to set a timer and sleep for a certain amount of time.

```py
time.sleep(5) # notice that the REPL does not return until 5 seconds have passed
```

This can be useful when looping through intervals based on conditional logic.

## Comparing times

We can use the [`datetime`](https://docs.python.org/3/library/datetime.html) module to compare times by converting `time` objects to `datetime` objects.

We need to do so with the `datetime.datetime.fromtimestamp` method.

```py
import datetime

# Check time now is less than 1 second later
datetime.datetime.fromtimestamp(time.time()) < datetime.datetime.now() + datetime.timedelta(seconds=1) # True
# Check time now is after 1 second before
datetime.datetime.fromtimestamp(time.time()) < datetime.datetime.now() - datetime.timedelta(seconds=1) # False
```

For more examples of `datetime` comparisons, see my post on [Datetime In Python](https://blog.dennisokeeffe.com/blog/2021-08-04-datetime-in-python) and more from my series [Working with dates and times in Python](https://blog.dennisokeeffe.com/blog/series/working-with-dates-and-times-in-python).

## Summary

Today's post demonstrates some usages of the [`time`](https://docs.python.org/3/library/time.html) module from Python's standard library.

We covered a number of the standard methods and finished with an example on how to compare using the [`datetime`](https://docs.python.org/3/library/datetime.html) module.

## Resources and further reading

1. [The ABCs of Pipenv](https://blog.dennisokeeffe.com/blog/2020-07-04-abcs-of-pipenv)
2. [Pipenv](https://pipenv.pypa.io/en/latest/)
3. [`datetime`](https://docs.python.org/3/library/datetime.html)
4. [`time`](https://docs.python.org/3/library/time.html)
5. [Datetime In Python](https://blog.dennisokeeffe.com/blog/2021-08-04-datetime-in-python)
6. [Series: Working with dates and times in Python](https://blog.dennisokeeffe.com/blog/series/working-with-dates-and-times-in-python)
7. [iPython](https://ipython.org/)
8. [GitHub repository with final code](https://github.com/okeeffed/hello-python-time-module)

_Photo credit: [`pawel_czerwinski`](https://unsplash.com/@pawel_czerwinski)_
