Read Python Interview Questions and Answers for Beginners and Experienced

Python Time Functions

In this Post of Python time, we will study time-related activities or functions in Python. we will see various functions of Python time and how we can use it according to the requirement.

Time

As per the Python docs of time , Time module provides various time-related functions or tasks. To use the time related functions , first we need to import the time module in our Python program

 

Import time module

To import time module simple write

import time

 

as the first statement of Python program.

After importing time in program ,we can use various functions of time as below:

 

Time functions

Let’s explore Python time functions.

 

time.time()

As we discuss time is module, so inside time module there are lots of functions, First we will study time() function inside Python time module.

Let’s take example :

 


#Python time function example
import time

t=time.time()

print (t)

 

Output

 

1562570105.2449396

 

Explanation

  • time.time() function return the float number. This float number represents the number of the seconds lapsed since January 1, 1970, 00:00:00 (UTC).
  • January 1, 1970, 00:00:00 (UTC) is called epoch time, This is the point where time starts. It is platform dependent.

 

time.asctime(t)

asctime(t) function return the time in the following format

‘Sun Jun 20 23:21:05 1993′

t in the function argument is tuple of data which will form the time in above said format. if we don’t provide any argument then it will give current date and time. see below example:


#Python time asctime function example
import time

t=time.asctime()
print (t)

tup= (2015,2, 15, 7, 40, 10, 2, 100, 0)
t1=time.asctime(tup)

print (t1)

 

Output

Mon Jul 8 13:12:15 2019
Wed Feb 15 07:40:10 2015

 

  • Here t is simple time, where will did not pass any argument, so it will return the current time and date
  • tup is Python tuplewhich we created to pass as an argument to asctime() function.
  • asctime() function will take tup as argument and will make the date from tuple tup and return  date accordingly.
  • Parameterized asctime() will take tuple having 9 argument tuple, other wise it will throw error.

 

 

time.ctime(sec)

ctime(sec) function return the time from the epoch time ( when time starts Thu Jan 1 00:00:00 1970  UTC).

  • When we don’t pass any value in parameter, ctime will give the current time.
  • When we pass sec as parameter in number format, it will give the time lapse from epoch time + secs added in that timestamp.

Let’s understand by example

 


import time

# return the current time stamp
t1=time.ctime()
print (t1)

t2=time.ctime(1)
print (t2)

 

Output

Mon Jul 8 13:31:26 2019
Thu Jan 1 00:00:01 1970

 

When we print t1, it give us current time stamp.

When we pass sec 1 in t2, it give us  Thu Jan 1 00:00:01 1970 , it added 1 sec to the epoch time.

 

Note: Thu Jan 1 00:00:01 1970 is UTC time, in your time zone this value 00:00:01  may be differ.

 

time.gmtime(secs)

Function gmtime() constructs struct_time from the epoch time by making new time with adding secs (which is parameter).

confused?

Let’s understand by taking example:


#Python gmtime example
import time

t1=time.gmtime(1)
print (t1)

 

Output

time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=1, tm_wday=3, tm_yday=1, tm_isdst=0)

 

  • In above example, when we pass the 1  sec as parameter in gmtime() function, it constructs the date from epoch time by adding 1 sec into tm_sec field.
  • When we dont provide any parameter to gmtime(), it will construct current time stamp value.
  • We can call different attributes of struct_time by simple call like

t1.tm_year, t1.tm_mon etc.

 

time.localtime()

Localtime() is almost similar to gmtime() we discussed above , differnce is that localtime() returns the struct_time in UTC time.

 


#Python gmtime example
import time

t1=time.localtime(1)
print (t1)

 

Output

time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=5, tm_min=30, tm_sec=1, tm_wday=3, tm_yday=1, tm_isdst=0)

 

  • When 1 sec is added as parameter to time.localtime(1), it construct struct_time with UTC time as we can see the value of tm_hour=5, tm_min=30,tm_sec=1 ( here 1 is added by our parameter).

Note: This post is published from UTC +5.30 timezone, thats why we are seeing here tm_hour=5, tm_min=30.

 

time.mktime()

mktime() do the reverse of localtime().  As its clear from its name mktime() make the time from some raw data. raw data can be only Tuple or struct_time. it takes tuple or struct_time as input and returns the time in seconds.

 


#Python mktime example
import time

t1=(2011,3,3,3,3,3,3,3,3)
t2=time.mktime(t1)
print (t2)

 

Output

1299101583.0

 

time.sleep()

 

Sleep method stop or suspend the execution of the program for given number of seconds.

 


#Python sleep example
import time

print("hi")

time.sleep(5)

print("Python")

 

Output

hi
Python

 

Explanation

  • When we execute above example, we will observe that after printing “hi”, program will sleep for 5 seconds, then it will print “Python”.