Time module

Python offers a module called ‘time’ to create varying functions that are related to time as well as to achieve time-related tasks. Consider a function from the ‘time’ module which returns the number of seconds passed since ‘epoch’. Epoch is the time passed since January the first of 1970. 

import time

# This prints time passed since epoch

We get:

We get the seconds returned as a floating-point number.

NOTE: 1970 is not the birth of epoch, but was conveniently chosen when it was being upgraded. To learn more check out this article -> https://kb.narrative.io/what-is-unix-time

Another useful function from this module is the ctime() function which takes the current seconds passed since epoch and returns that value in a readable format.

import time

# This prints time passed since epoch
seconds_since_epoch = time.time()

# returns epoch passed in a string format
time_in_string = time.ctime(seconds_since_epoch)

We get:

Alternatively, we can also achieve the same as above by giving the seconds passed directly as an argument. NOTE: Seconds passed since epoch is constantly incremented thus each time you run this code you will get a different output.

# returns epoch passed in a string format
time_in_string = time.ctime(time.time())

The ‘time’ module also offers a function called gmtime(), which returns a structure of time that has 9 attributes, and it has the current year, month, day, hour, minute, second, weekday, year day, and isdst (to check daylight saving time DST). 

NOTE: To learn more about DST check out this article -> https://towardsdev.com/giant-mess-dealing-with-timezones-and-daylight-saving-time-in-python-7222d37658cf


Consider an example:

import time

# takes time passed since epoch as argument
struct = time.gmtime(time.time())

We get the output as:

We have frequently used a function from this module in our earlier blogs, and that is the sleep() function. This function takes the number of seconds the program should wait before execution. Consider an example:

import time

print("First print statement and it prints without any delay")

# Make the program wait 3 seconds

print("Second print statement and it prints after 3 seconds")

We get the output of the above as:

We can use the time passed since epoch as a timestamp and consequently that time can be saved by programs to a database. However, when using that timestamp it makes no sense to the user, and thus, we need to convert it into a human-readable format. Using the following code we get:

from datetime import datetime
import time

# current timestamp
time_stamp = time.time()

# timestamp to datetime
date_time_object = datetime.fromtimestamp(time_stamp)


We get the output as:

Conversely, we can also convert a datetime object to a timestamp in the following manner.

from datetime import datetime

# getting the current date and time
current_time = datetime.now()

# converting to epoch
time_stamp = datetime.timestamp(current_time)
print("Date in time stamp format is:", time_stamp)

We get the output:

What have we learned?

  • What is the use of the time module?
  • What is epoch?
  • What does the gtime() function return?
  • What is DST?
  • How can we convert a timestamp to a datetime object?
Notify of
Inline Feedbacks
View all comments