sampleqa.in tutorial, Python Programming  tutorial,Python time module

Python Time Module Example


     Python time related functions exists in time module

      		import time
      
Time FunctionsDescription
asctime
string asctime([timetuple])
time tuple is not found , localtime() method is called, which returns struct_time struct_time structure has year month day hours(24-hours format) minutes seconds weekday yeartoday DST(day light sayings time) fields
ctime
				string ctime(seconds)
			
Converts time in seconds Since Epoch time to a string in localtime.
Epoch time is 1970 Jan 1st at mid-night
This method is equivalent to asctime(localtime(seconds))
when seconds not found ctime() returns current date and time for ex: 'Sat Feb 11 20:55:37 2023'
gmtime
			struct_time gmtime( [seconds] )
		
1.if EPOCH seconds found ,returns struct_time with all fields date and time with DST 2.if EPOCH seconds not found, returns current date & time in struct_time. Date & time represented in UTC/GMT struct_time has filed to access each Unit of date and time
tm_year tm_mon tm_mday tm_hour tm_min tm_sec
localtime
			struct_time localtime([seconds])
		
1.if EPOCH seconds found ,returns struct_time with all fields date and time with DST 2.if EPOCH seconds not found, returns current date & time in struct_time,Date & time represented in local time struct_time has filed to access each Unit of date and time
tm_year tm_mon tm_mday tm_hour tm_min tm_sec
mktime
				float  mktime(timetuple)
			
Converts the localtime in tuple to EPOCH seconds. mktime(localtime()) returns EPOCH seconds
sleep
				sleep(seconds)
			
stops execution for a given number of seconds.
strftime
				string strftime(format[, tuple])
			
strptime
			struct_time strptime(string, format) 
			
struct_time class struct_time has fields to store date & time and DST values, which can be accessed later.
struct_time returned by gmtime(), localtime(), strptime()
gmtime returns date & time in UTC/GMT localtime returns local date time strptime returns string into struct_time
time
				float time()
			
time method returns current time in seconds since 1970-1-1 00:00



Commonly used format codes:

    
    %Y  Year with century as a decimal number.
    %m  Month as a decimal number [01,12].
    %d  Day of the month as a decimal number [01,31].
    %H  Hour (24-hour clock) as a decimal number [00,23].
    %M  Minute as a decimal number [00,59].
    %S  Second as a decimal number [00,61].
    %z  Time zone offset from UTC.
    %a  Locale's abbreviated weekday name.
    %A  Locale's full weekday name.
    %b  Locale's abbreviated month name.
    %B  Locale's full month name.
    %c  Locale's appropriate date and time representation.
    %I  Hour (12-hour clock) as a decimal number [01,12].
    %p  Locale's equivalent of either AM or PM.
	
	

ADS