AppDividend
Latest Code Tutorials

# Python Time Module Example | Python Time Tutorial

Python has the module named time to handle time-related tasks. In this article, we will explore the time module in detail. We will learn to use the different time-related functions defined in the time module. The Python time module provides many ways of representing time in the code, such as objects, numbers, and strings.

It also provides functionality other than representing time, like waiting during the code execution and measuring the efficiency of the code.

## Python Time Module

Content Overview

If we want to use functions defined in the module, we need to import the module first.

`import time`

You can manage the concept of Python time in your application is by using the floating-point number that represents a number of seconds that have passed since the beginning of an era that is, since the particular starting point. Let’s go to that Epoch point.

### The Epoch

It is a Python time with a floating-point number representing elapsed time since the beginning of an era.

An important concept to grasp here is that, when dealing with Python time, you’re considering the period identified by the starting point.

In computing, you call that starting point the epoch.

## Python time.time()

Let’s calculate the total seconds since the epoch.

```# app.py

from time import time

seconds = time()
print("Seconds since epoch =", seconds)
```

#### Output

```➜  pyt python3 app.py
Seconds since epoch = 1575025885.825993
➜  pyt```

For the Unix system, January 1, 1970, 00:00:00 at UTC is epoch (the point where time begins).

## Python time.gmtime()

We can use the time.gmtime() to determine your system’s epoch.

```# app.py

import time

print(time.gmtime(0))
```

#### Output

```➜  pyt python3 app.py
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
➜  pyt```

## Python time.ctime()

Python time.ctime() function takes seconds passed since epoch as the argument and returns a string representing local time.

```# app.py

from time import ctime

import time

# seconds passed since epoch
seconds = 1575025885.825993

local_time = ctime(seconds)
print("Local time:", local_time)
```

#### Output

```➜  pyt python3 app.py
Local time: Fri Nov 29 16:41:25 2019
➜  pyt```

A string representation of time, also known as the timestamp, returned by Python ctime() is formatted with the following structure:

1. Day of the week: Fri (Friday)
2. The month of the year: Nov (November)
3. Day of the month: 29
4. Hours, minutes, and seconds using a 24-hour clock notation: 16:41:25
5. Year: 2019

Python 3.7 introduced the time_ns() function, which returns the integer value representing the same elapsed time since an epoch, but in nanoseconds rather than seconds.

Measuring time in seconds is useful for several reasons:

1. You can use the float to calculate the difference between two points in time.
2. The float is easily serializable, meaning that it can be stored for data transfer and come out intact on the other side.

## Python time.sleep()

Python sleep() function suspends (delays) execution of the current thread for the given number of seconds. To learn more, visit Python sleep().

```# app.py

from time import sleep

sleep(1.1)
print("After 1.1 seconds")
```

#### Output

```➜  pyt python3 app.py
After 1.1 seconds
➜  pyt```

It will print the statement after 1.1 seconds.

## Python time.localtime()

Python localtime() function takes the number of seconds passed since epoch as an argument and returns struct_time in local time. See the following code.

```# app.py

from time import localtime

ltime = localtime(1575125769)
print(ltime)
```

#### Output

```➜  pyt python3 app.py
time.struct_time(tm_year=2019, tm_mon=11, tm_mday=30, tm_hour=20, tm_min=26, tm_sec=9, tm_wday=5, tm_yday=334, tm_isdst=0)
➜  pyt```

You can extract the year, month, day, hour, min, sec, and more data.

```# app.py

from time import localtime

ltime = localtime(1575125769)
print(ltime)
print("Year", ltime.tm_year)
print("Month", ltime.tm_mon)
print("Day", ltime.tm_mday)
print("Second", ltime.tm_sec)
```

#### Output

```➜  pyt python3 app.py
time.struct_time(tm_year=2019, tm_mon=11, tm_mday=30, tm_hour=20, tm_min=26, tm_sec=9, tm_wday=5, tm_yday=334, tm_isdst=0)
Year 2019
Month 11
Day 30
Second 9
➜  pyt```

If there is no argument or None is passed to localtime() function, then the value returned by time() is used.

## Python time.mktime()

The mktime() function takes the struct_time (or the tuple containing 9 elements corresponding to struct_time) as the argument and returns the seconds passed since epoch in local time.  It’s the inverse function of localtime().

See the following code.

```# app.py

from time import mktime

tm = (2019, 11, 29, 8, 44, 4, 4, 362, 0)
local_time = mktime(tm)
print("Local time:", local_time)```

#### Output

```➜  pyt python3 app.py
Local time: 1574997244.0
➜  pyt```

## Python time.asctime()

Python asctime() function takes struct_time (or a tuple containing 9 elements corresponding to struct_time) as the argument and returns the string representing it.

```# app.py

from time import asctime

tm = (2019, 11, 29, 8, 44, 4, 4, 362, 0)
local_time = asctime(tm)
print("Local time:", local_time)```

#### Output

```➜  pyt python3 app.py
Local time: Fri Nov 29 08:44:04 2019
➜  pyt```

## Python time.struct_time Class

Several functions in the time module such as gmtime(), asctime() etc. either take time.struct_time object as the argument or return it.

Here’s an example of time.struct_time object.

```time.struct_time(tm_year=2019, tm_mon=11, tm_mday=29,
tm_hour=6, tm_min=35, tm_sec=17,
tm_wday=3, tm_yday=361, tm_isdst=0)```
Index Attribute Values
0 tm_year 0000, …., 2018, …, 9999
1 tm_mon 1, 2, …, 12
2 tm_mday 1, 2, …, 31
3 tm_hour 0, 1, …, 23
4 tm_min 0, 1, …, 59
5 tm_sec 0, 1, …, 61
6 tm_wday 0, 1, …, 6; Monday is 0
7 tm_yday 1, 2, …, 366
8 tm_isdst 0, 1 or -1

The values (elements) of the time.struct_time object is accessible using both indices and attributes.

Finally, Python Time Module Example is over.

## Related Posts

Python time format

Python Datetime example

Python format() example

This site uses Akismet to reduce spam. Learn how your comment data is processed.