time.strptime()
parses strings into time tuples again; the default parse format matches the output of time.ctime()
and time.asctime()
:
>>>
import time
>>>
time.ctime(time.time())
'Fri Apr 25 12:12:06 2014' >>>
time.strptime(time.ctime(time.time()))
time.struct_time(tm_year = 2014, tm_mon = 4, tm_mday = 25, tm_hour = 12, tm_min = 12, tm_sec = 10, tm_wday = 4, tm_yday = 115, tm_isdst = -1) >>>
time.strptime(time.asctime())
time.struct_time(tm_year = 2014, tm_mon = 4, tm_mday = 25, tm_hour = 12, tm_min = 13, tm_sec = 6, tm_wday = 4, tm_yday = 115, tm_isdst = -1)
The time value as returned by gmtime(), localtime(), and strptime(), and accepted by asctime(), mktime() and strftime(), is a sequence of 9 integers. The return values of gmtime(), localtime(), and strptime() also offer attribute names for individual fields.,The type of the time value sequence returned by gmtime(), localtime(), and strptime(). It is an object with a named tuple interface: values can be accessed by index and by attribute name. The following values are present:,Parse a string representing a time according to a format. The return value is a struct_time as returned by gmtime() or localtime().,localtime() may raise OverflowError, if the timestamp is outside the range of values supported by the platform C localtime() or gmtime() functions, and OSError on localtime() or gmtime() failure. It’s common for this to be restricted to years between 1970 and 2038.
>>> from time
import gmtime, strftime
>>>
strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
'Thu, 28 Jun 2001 14:17:15 +0000'
>>>
import time
>>>
time.strptime("30 Nov 00", "%d %b %y")
time.struct_time(tm_year = 2000, tm_mon = 11, tm_mday = 30, tm_hour = 0, tm_min = 0,
tm_sec = 0, tm_wday = 3, tm_yday = 335, tm_isdst = -1)
std offset[dst[offset[, start[/time], end[/time]]]]
>>> os.environ['TZ'] = 'EST+05EDT,M4.1.0,M10.5.0' >>>
time.tzset() >>>
time.strftime('%X %x %Z')
'02:07:36 05/08/03 EDT' >>>
os.environ['TZ'] = 'AEST-10AEDT-11,M10.5.0,M3.5.0' >>>
time.tzset() >>>
time.strftime('%X %x %Z')
'16:08:12 05/08/03 AEST'
>>> os.environ['TZ'] = 'US/Eastern' >>>
time.tzset() >>>
time.tzname('EST', 'EDT') >>>
os.environ['TZ'] = 'Egypt' >>>
time.tzset() >>>
time.tzname('EET', 'EEST')
Pythom time method asctime() converts a tuple or struct_time representing a time as returned by gmtime() or localtime() to a 24-character string of the following form: 'Tue Feb 17 23:21:05 2009'.,t − This is a tuple of 9 elements or struct_time representing a time as returned by gmtime() or localtime() function.,This method returns 24-character string of the following form: 'Tue Feb 17 23:21:05 2009'.,The following example shows the usage of asctime() method.
Following is the syntax for asctime() method −
time.asctime([t]))
#!/usr/bin/python import time t = time.localtime() print "time.asctime(t): %s " % time.asctime(t)
When we run above program, it produces following result −
time.asctime(t): Tue Feb 17 09: 42: 58 2009
Last Updated : 30 Nov, 2021,GATE CS 2021 Syllabus
The time module comes with Python’s standard utility module, so there is no need to install it externally. We can simply import it using the import statement.
import time
Current time in seconds since epoch = 1627908387.764925
time.asctime() method is used to convert a tuple or a time.struct_time object representing a time as returned by time.gmtime() or time.localtime() method to a string of the following form:
Day Mon Date Hour: Min: Sec Year
Current time in seconds since epoch = 1627908387.764925
Current time: Mon Aug 2 12: 45: 13 2021
0 1 2 3
Local time(in seconds): 1627987508.0