Python Time Library
- We will consider various functions given by the python module library with examples.
- This python time module helps to work on time in python to get the current time.
- Before going with the example we have to import the library.
Import the time module:
- There is no need to install any module we have just importedthem using the import statement.
import time
Epoch:
- Where the time starts it is called epoch which is platform-dependent.
- The epoch is January 1, 1970, 00:00:00 for windows and most Unix systems.
- We can check the epoch using time.gmtime(0).
Import time
Print( time.gmtime(0) )
Output:
Time.strut_time (tm_year=1970, tm_mon=1, tm_mday=1, tm.hour=0, tm_min=0,tn_sec=0,tm_wday=3, tm_yday=1, tm_isdst=0 )
Time.ctime
- It is a function that returns a string of 24 characters, and time.
- This function takes seconds as an argument and computes until the provided time.
- If the argument is not provided then it will consider the current time and compute till the current time.
Consider an example of getting time in the form of string:
import time
/ / getting current time by passing
/ / seconds passes since epoch
sec = 1545925769.9618232
current = time.ctime ( sec )
Print(“current time:”,current
Output:
Current time: Tue Aug 22 12:33:54 2022
Program Delaying Execution
- The program execution can be delayed using time.sleep ( ) method.
- For a specified amount of time program execution can be delayed by using this function.
Consider an example to implement the above scenario:
import time
for i in range (5):
/ / using the sleep method
time.sleep(2)
print(i)
Output:
0
1
2
3
4
Time.struct_time Class
- This function helps in accessing the local time which is non-epochal timestamps.
- It returns a tuple with a name, where both index and attribute name is used to access the value.
- The objects contain the attribute as follows:
Index | Attribute name | Values |
0 | tm_year | 0000, ….., 9999 |
1 | tm_mon | 1,2,3,4,5,…. 11,12 |
2 | tm_mday | 1,2,3,…,30,31 |
3 | tm_hour | 0,1,2,3,4,….,21,22,23 |
4 | tm_min | 0, 1, 2, 3, …, 57, 58, 59 |
5 | tm_sec | 0, 1, 2, 3, …., 60, 61 |
6 | tm_wday | 0, 1, 2, 3, 4, 5, 6 ;6 is Sunday |
7 | tm_yday | 1, 2, 3, 4, …, 365, 366 |
8 | tm_isdst | 0, 1 or -1 |
Python time.localtime ( )
- This function returns struct time in local time, it takes a number of seconds from epoch as an argument.
- If the argument is not given then time.time( ) is used.
- Let us assume a program for the above scenario:
/ /import the time module
import time
/ / convert the time
/ /from the epoch
/ / time.strurct_time object in local time
obj = time.localtime(1545925769)
print(“Result:”,obj)
print(“/n Year:”,obj.tm_year)
print(“Hour:”obj.tm_hour)
Output:
Result: time.struct_time( tm_year=2022, tm_mo = 8, tm_mday = 3, tm_hour = 16, tm_min = 15,tm_sec = 8,tm_wday = 1,tm_yday = 215, tm_isdst = 0)
Year: 2022
Hour: 15
Python time.gmtime( )
- This function returns the struct_time in UTC.
- It takes a number of seconds from the epoch as an argument.
- This function is used to convert seconds from epoch to struct time.
- If the argument is not given then time.time( ) is used.
- Let us consider an example program:
/ /importing the time module
import time
/ / convert the time in seconds
/ /since the epoch to a
/ / time.struct_time object in local time
obj = time.gmtime(1545925769)
print(“Result:”,obj)
print(“/n Year:”,obj.tm_year)
print(“Hour:”obj.tm_hour)
Output:
Result: time.struct_time( tm_year=2022, tm_mo = 8, tm_mday = 3, tm_hour = 16, tm_min = 15,tm_sec = 8,tm_wday = 1,tm_yday = 215, tm_isdst = 0)
Year: 2022
Hour: 15
Python time.strptime ( )
- This method converts the time which is in form of a string to struct time.
- Let us consider a program for the above case:
/ /importing the time module
import time
/ / declare a variable with current date
time_string = “22 Aug 2022”
obj = time.strptime(time_string, “%d %B,%Y”)
/ / printing the object
print(obj)
Output:
time.struct_time( tm_year=2022, tm_mo = 8, tm_mday = 22, tm_hour = 0, tm_min = 0,tm_sec = 0,tm_wday = 1,tm_yday = 172, tm_isdst = -1)
Python time.mktime ( )
- This function returns the seconds from the beginning of the epoch from local time.
- The struct_time is taken as argument in the mktime( ) function.
- The mktime( ) function is a inverse of localtime( ) function.
- Let us consider a program for the above function:
/ / import the python time module
import time
/ / declaring a variable
time = 1627988053
/ / returning the struct_time
obj1 = time.localtime( time )
print(“obj1:”, obj1 )
/ / returning the time in seconds from struct_time
obj = time.mktime( time )
print(“obj:”, obj )
Output:
Obj1 :time.struct_time( tm_year=2022, tm_mo = 8, tm_mday = 22, tm_hour = 0, tm_min = 0,tm_sec = 0,tm_wday = 1,tm_yday = 172, tm_isdst = -1)
Obj :1627988053
Python time.asctime( )
- This function returns a string representing the time given.
- The function asctime( ), takes struct_time as an argument.
- It converts a struct_time in to string object which represents the time.
- The return string is represented as:
Day Mon Date Hour: Min: Sec Year
- Let us consider an example program for the above function.
/ / import the python time module
import time
/ / declaring a variable
sec = 1627988053
/ / convert the struct time
/ / into string in the form “Day Mon Date Hour: Min: Sec Year”
/ / using the asc.time( ) function
obj = time.gmtime( sec )
time_str = time.asctime( obj )
print( time_str)
/ / convert the struct time
/ / into string in the form “Day Mon Date Hour: Min: Sec Year”
/ / using the asc.time( ) function
Obj1 = time.localtime( sec )
time_str = time.asctime( obj )
print(time_str )
Output:
Mon Aug 22 10:23:09 2022
Mon Aug 22 10:23:09 2022
Python time.strftime( )
- This function returns a string representing the time given.
- The function strftime( ), takes struct_time as an argument.
- The string which the function returns is based on the format codes.
- Let us consider a program for the above function:
/ / import the python time module
import time
/ / declaring a variable
obj = time.local time( )
/ / converting the time in a string using the strf.time ( ) function
stringobj = time.strftime ( “%m %d %Y, %H : %M : %S “, obj )
/ / displaying the time
print( stringobj )
Output:
08/22/2022, 09: 23: 34