>>> import time; >>> localtime = time.localtime(time.time()) >>> print ("Current Time is :", localtime) Current Time is : time.struct_time(tm_year=2017, tm_mon=8, tm_mday=14, tm_hour=17, tm_min=43, tm_sec=54, tm_wday=0, tm_yday=226, tm_isdst=0) >>>
The time returned is a time structure which includes 9 attributes. These are summoned in the table given below.
|tm_year||Returns the current year (|
|tm_mon||Returns the current month (|
|tm_mday||Returns the current month day (|
|tm_hour||Returns the current hour (|
|tm_min||Returns the current minute (|
|tm_sec||Returns current seconds (|
|tm_wday||Returns the week day (|
|tm_yday||Returns the year day (|
|tm_isdst||It returns -1,0 or 1.|
Python also support formatted time. Proceed as follows :
1. Pass the time structure in a predefined function
asctime(). It is a function defined in time module.
2. It returns a formatted time which includes Day ,month, date, time and year.
3. Print the formatted time.
>>> import time; >>> localtime = time.asctime( time.localtime(time.time()) ) >>> print ("Time Format :", localtime) Time Format : Mon Aug 14 18:02:00 2017 >>>
There is a popular time module available in Python which provides functions for working with times and for converting between representations. Here is the list of all available methods
|time()||Returns floating point value in seconds since epoch i.e.,12:00am, January 1, 1970|
|asctime(time)||It takes the tuple returned by localtime() as parameter. It returns a 24 character string.|
|sleep(time)||The execution will be stopped for the given interval of time.|
|strptime(String,format)||It returns an tuple with 9 time attributes. It receives an String of date and a format.|
|gtime()/gtime(sec)||It returns struct_time which contains 9 time attributes. In case seconds are not specified it takes current second from epoch.|
|mktime()||Returns second in floating point since epoch.|
|strftime(format)/strftime(format,time)||Returns time in particular format. If time is not given, current time in seconds is fetched.|
|tzset()||Resets the time conversion rules used by the library routines. The environment variable TZ specifies how this is done.|
|localtime([secs])||Accepts an instant expressed in seconds since the epoch and returns a time-tuple t with the local time|
|clock( )||Returns the current CPU time as a floating-point number of seconds.|