import time
import datetime
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.print(time.gmtime(time.time()))
print(time.localtime(time.time()))
print(datetime.datetime.fromtimestamp(time.time()))
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.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.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.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)))
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.
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'))
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.%%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)
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
If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.
When going through coding examples, it's quite common to have doubts and errors.
If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.
You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.
If you want to