Python Time Module

By | August 18, 2019

In this tutorial we will discuss the python time module, we will also learn how to use different methods present in time module:

Python Time Module

Time is a standard module which is present in the python, and it is used to print the time. It is also used to handle the time-related tasks.

The interesting fact about this time module, it is directly imported from the C library.

to use the time module, we simply have to import it in our source code.

Time module syntax.

import time

Python time.time():

The time.time() method is used to get the epoch time in seconds.

The epoch is the point where the time starts. On January 1st of that year, at 0 hours, the “time since the epoch” is zero. For Unix, the epoch is 1970.

Basically the time.time() is used to give the epoch time of the UNIX system in second.microsecond format.

Example:

import time
epoch_seconds = time.time()
print('the epoch seconds since Unix was introduced=', epoch_seconds)

Output:

the epoch seconds since Unix was introduced= 1565364102.1340282

python time.ctime()

The time.ctime() method is often used get the current time in a string type.

The time.ctime() takes an argument in seconds and convent it into local date and time.

The seconds we pass as an argument in time.ctime() method it takes the epoch time for reference.

Example:

import time
epoch_seconds = 1565364102.1340282
current_time_date = time.ctime(epoch_seconds)
print("It's",current_time_date)

Output:

It's Fri Aug  9 20:51:42 2019

Behind the code:

The time.ctime() use the epoch_seconds we provide it and convert it into a date and time since the Unix epoch time.

Python time.sleep()

This is a very interesting function of time module the time.sleep() delay the output.

It accepts seconds as an argument and delays the next output by passed second.

Example:

import datetime
import time
print("the first statement prints at",datetime.datetime.now().strftime('minute=%M seconds=%S'))
time.sleep(5)
print("the second statement prints at",datetime.datetime.now().strftime('minute=%M seconds=%S'))

Output:

the first statement prints at minute=20 seconds=07
the second statement prints at minute=20 seconds=12

Behind the code:

In the above output, we can see that there is a delay of 5 seconds.

The first print statement executes at 20 minute: 7second and the second statement execute at 20 minute:12 second.

Python time.struct_time

Python time.struct_time is an object. Many time function return value in the form of time.struct_time format.

The syntax of time.struc_time object is:

time.struct_time(tm_year=2019, tm_mon=10, tm_mday=10,
tm_hour=7, tm_min=10, tm_sec=20,
tm_wday=3, tm_yday=314, tm_isdst=0)

struc_time object has some attributes which are used to get the values hold by the struct_time object.

Attribute Values
tm_year 0000, …., 2018, …, 9999
tm_mon 1, 2, …, 12
tm_mday 1, 2, …, 31
tm_hour 0, 1, …, 23
tm_min 0, 1, …, 59
tm_sec 0, 1, …, 61
tm_wday 0, 1, …, 6; Monday is 0
tm_yday 1, 2, …, 366
tm_isdst 0, 1 or -1

Python time.localtime():

Python time.localtime() accept seconds in argument and return a time.struct_time object with a date and time.

Its convent the passed seconds in time relative to the Unix epoch time.

Example:

import time
now = time.localtime(1565364102.1340282)
print("now value is:", now)
print()
print("year:", now.tm_year)
print('month',now.tm_mon)
print("tm_hour:", now.tm_hour)

Output:

now value is: time.struct_time(tm_year=2019, tm_mon=8, tm_mday=9, tm_hour=20, tm_min=51, tm_sec=42, tm_wday=4, tm_yday=221, tm_isdst=0)
year: 2019
month 8
tm_hour: 20

Python time.gmtime()

Python time.gmtime() also accept seconds as an argument and return struct_time object, in UTC.

Example:

import time
now = time.gmtime(1565364102.1340282)
print("now value is:", now)
print()
print("year:", now.tm_year)
print('month',now.tm_mon)
print("tm_hour:", now.tm_hour)

Output:

year: 2019
month 8
tm_hour: 15

Python time.mktime()

With the help of time.mktime() function we can get the seconds since epoch. In time.mktime() function we can pass the struct_time object or a tuple containing 9 elements related to struct_time.

Example:

import time
import time
now = 1565364102.1340282
struct_time_obj = time.localtime(now)
print("struct_time_obj ", struct_time_obj)
epoch_seconds = time.mktime(struct_time_obj)
print("epoch_second: ", epoch_seconds)

output:

struct_time_obj  time.struct_time(tm_year=2019, tm_mon=8, tm_mday=9, tm_hour=20, tm_min=51, tm_sec=42, tm_wday=4, tm_yday=221, tm_isdst=0)
1565364102.0

Python time.asctime()

The time.asctime() method is used to return the time in string format. It accepts struct_time or relevant 9 elements tuple as an argument.

Example:

import time
import time
now = 1565364102.1340282
struct_time_obj = time.localtime(now)
print("struct_time_obj ", struct_time_obj)
Date_Time = time.asctime(struct_time_obj)
print("Date_Time",Date_Time )

Output:

struct_time_obj  time.struct_time(tm_year=2019, tm_mon=8, tm_mday=9, tm_hour=20, tm_min=51, tm_sec=42, tm_wday=4, tm_yday=221, tm_isdst=0)
Date_Time Fri Aug  9 20:51:42 2019

Python Tutorials


Introduction

Flow Control

Functions

Datatypes

File Handling

Object & Class

Additional Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *