time module in python

Previous
Next

time module:

  • “time” module allows to handle various operations regarding time, its conversions, its representations.
  • The beginning of time is started measuring from 1 january, 1970 , 12am
  • It is termed as “epoch” in python.

time() : Function is used to count the number of seconds elapsed since the “epoch”

gmtime():

  • It returns a structure of 9 values.
  • It converts the time(seconds) into attributes(days, months, years….)
import time
tym = time.time()
print(tym)

We convert the returned values by time() function using local time format:

import time
tym = time.time()
info = time.gmtime(tym)
print(info)

We can display the Year, Month and Day using the dynamic variables of time object:

import time
tym = time.time()
info = time.gmtime(tym)

print("Year :",info.tm_year)
print("Month :",info.tm_mon)
print("day :",info.tm_mday)

We can display the above things using format specifiers:

import time
tym = time.time()
info = time.gmtime(tym)

print("Date is : %d-%d-%d" %(info.tm_year,info.tm_mon,info.tm_mday))
print("Time is : %d-%d-%d" %(info.tm_hour,info.tm_min,info.tm_sec))

sleep():

  • Stop the program execution for specified number of seconds.
  • The prototype is
    • sleep(seconds)
import time
for i in range(10):
    print("i value : %d" %i)
    time.sleep(1)
Previous
Next

Add Comment

Courses Enquiry Form