Updated On : Nov-25,2019 Tags time, python
time - Time Related Functionalities in Python

Overview

It provides various time-related functions to work with time.

DST - Daylight Saving Time

In [1]:
import time
import datetime

Commonly used functions of module:

  • time.time() - It returns time in seconds since epoch as float. On Windows and most unix systems epoch is 1 Jan - 1970 00:00:00 (UTC) and leap seconds are not counted. Output of this function can be passed to other functions like gmtime(),localtime(), datetime.datetime.fromtimestamp() to convert it to proper structured format.
In [2]:
print(time.gmtime(time.time()))
print(time.localtime(time.time()))
print(datetime.datetime.fromtimestamp(time.time()))
time.struct_time(tm_year=2019, tm_mon=2, tm_mday=19, tm_hour=13, tm_min=56, tm_sec=5, tm_wday=1, tm_yday=50, tm_isdst=0)
time.struct_time(tm_year=2019, tm_mon=2, tm_mday=19, tm_hour=13, tm_min=56, tm_sec=5, tm_wday=1, tm_yday=50, tm_isdst=0)
2019-02-19 13:56:05.123462
  • time.gmtime([secs]) - It turns seconds provided to struct_time object in UTC since epoch. struct_time has same interface as namedtuple. If secs not provided then current time provided by time() will be used. Fraction part of seconds provided as float is ignored.
  • time.localtime([secs]) - It returns struct_time object as local time.
In [3]:
print(time.gmtime(60))
print(time.gmtime(3600))
curr_time = time.gmtime()
print(curr_time.tm_zone)
print(curr_time)
curr_time = time.localtime()
print(curr_time.tm_zone)
print(curr_time)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=1, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=1, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
GMT
time.struct_time(tm_year=2019, tm_mon=2, tm_mday=19, tm_hour=13, tm_min=56, tm_sec=5, tm_wday=1, tm_yday=50, tm_isdst=0)
UTC
time.struct_time(tm_year=2019, tm_mon=2, tm_mday=19, tm_hour=13, tm_min=56, tm_sec=5, tm_wday=1, tm_yday=50, tm_isdst=0)
  • time.ctime([secs]) - Converts time provided in seconds to string representing local time.
  • time.asctime([t]) - It accepts tuple of 9 fields or struct_time object to format Mon Jan 01 00:00:00 1970.If t is not provided then current system time is used.
In [4]:
print(time.ctime(60))
print(time.ctime(3600))
print(time.ctime())
print(time.asctime(time.localtime()))
print(time.asctime(time.gmtime()))
print(time.asctime((1992,1,1,5,30,0,3,1,0)))
Thu Jan  1 00:01:00 1970
Thu Jan  1 01:00:00 1970
Tue Feb 19 13:56:05 2019
Tue Feb 19 13:56:05 2019
Tue Feb 19 13:56:05 2019
Thu Jan  1 05:30:00 1992
  • time.strftime(format[,t]) - Converts time represented as tuple or struct_time object to string provided as format. It t is not provided then current time returned by localtime() is used.
  • time.strptime(string[,format]) - Parse a time represented as string according to format and returns struct_tuple object. Default formating if not provided is %a %b %d %H:%M:%S %Y which is same one printed by ctime().

Refer to this link for formatting options.

In [5]:
print(time.strftime('%Y-%m-%d %H:%M:%S'))
print(time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(60)))
print(time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(3600)))
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(60)))
print(time.strptime('Jan 1 - 2012 10:10:10',"%b %d - %Y %H:%M:%S"))
print(time.strptime('Mon Jan  1 00:01:00 2018'))
2019-02-19 13:56:05
1970-01-01 00:01:00
1970-01-01 01:00:00
1970-01-01 00:01:00
time.struct_time(tm_year=2012, tm_mon=1, tm_mday=1, tm_hour=10, tm_min=10, tm_sec=10, tm_wday=6, tm_yday=1, tm_isdst=-1)
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=1, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)
  • time.sleep(secs) - Suspends execution of the thread in which it executed by a number of seconds. After waking up from sleep after that many second a thread might not get immediate execution as it'll be handled by the scheduler and will go to the ready state. Based on the priority scheduler will execute it.
  • time.struct_time - It's an object with the same interface as that of namedtuple. Method mentioned above like strptime(), gmtime() and localtime() returns time represented as this object.
In [6]:
%%time
## Note below that it takes more time to execute cell due to waiting of seconds. Actual execution time is quite less.
def waiting(title, secs):
    time.sleep(secs)
    print('Waiting : '+str(secs)+ ' before printing title : '+title)

waiting('Hello World', 3)
Waiting : 3 before printing title : Hello World
CPU times: user 0 ns, sys: 4 ms, total: 4 ms
Wall time: 3 s
In [7]:
curr_time = time.gmtime()
print(type(curr_time))
print(curr_time)
#tm_mon[1-12], tm_mday[1-31],tm_hour[0-23],tm_min[0-59],tm_sec[0-61],tm_wday[0-6]0 is Sunday,tm_yday[1-366],
# tm_idst[-1,0,1] - 1 Daylight Saving time,0 Not DST, -1 - Will results in set by system.
curr_time.tm_year, curr_time.tm_mon, curr_time.tm_mday, curr_time.tm_hour,curr_time.tm_min, curr_time.tm_sec, curr_time.tm_wday, curr_time.tm_yday, curr_time.tm_isdst
<class 'time.struct_time'>
time.struct_time(tm_year=2019, tm_mon=2, tm_mday=19, tm_hour=13, tm_min=56, tm_sec=8, tm_wday=1, tm_yday=50, tm_isdst=0)
Out[7]:
(2019, 2, 19, 13, 56, 8, 1, 50, 0)

  Support Us to Make a Difference

Thank You for visiting our website. If you like our work, please support us so that we can keep on creating new tutorials/blogs on interesting topics (like AI, ML, Data Science, Python, Digital Marketing, SEO, etc.) that can help people learn new things faster. You can support us by clicking on the Coffee button at the bottom right corner. We would appreciate even if you can give a thumbs-up to our article in the comments section below.

 Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to let us know in the comments section below (Guest Comments are allowed). We appreciate and value your feedbacks.



Sunny Solanki  Sunny Solanki