Introduction to time module – Learn Basic Python – 2
Python has several in-built modules for working with dates as well as time; In this article, we will be looking at time module, with examples, available in Python to manipulate time.
This article is split into two parts.
- Learn the basics of time module
- Uses of these modules in Test Automation
time Module
The time module provides functions that helps us to manipulate the time in “seconds since the epoch“, the point when time starts. This module also provides other functions like sleep() which is quite useful for pausing script execution for specified number of seconds.
time() method returns elapsed seconds as a simple floating point number that can be converted to an integer. It represents number of seconds since “Jan 1, 1970 00:00:00”.
>>> import time
>>> time.time()
1535408302.471027
>>>
Note: Above script is copied from python shell
Bonus Tip
You can actually find out what the epoch is on your system by running below command.
>>> import time
>>> time.gmtime(0)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
>>>
Arguments passed to gmtime() method is ‘0’ which means 0 seconds. I am actually checking the time on 0th second. You can try passing different values to see what happens.
List of methods available in time module
- time()
- ctime()
- sleep()
- strftime()
- gmtime()
- localtime()
To learn more about all methods available in time module, visit official page.
time()
As we have already seen, time() method returns elapsed seconds as a simple floating point number that can be converted to an integer.
>>> import time
>>> time.time()
1535408302.471027
>>>
1535408302 seconds elapsed since 1970
471027 represents micro seconds
ctime()
This function will convert a time in seconds since the epoch into a human readable string representing current local time. Current time is returned when there is no argument passed to this method.
>>> import time
>>> time.ctime()
'Tue Aug 27 22:27:41 2018'
>>> time.ctime(0)
'Thu Jan 1 01:00:00 1970'
>>> time.ctime(1535408602)
'Tue Aug 27 22:23:22 2018'
>>> time.ctime(1535408302)
'Tue Aug 27 22:18:22 2018'
>>>
Tricky Example – Find time after 5 mins (300 seconds)
>>> import time
>>> time.ctime(int(time.time()+300))
'Tue Aug 27 22:34:11 2018'
>>>
This is very useful to store the time in number of seconds and convert it back to human readable format when required. Simeple but very efficient use case – It is easy to store big integer in a database than storing date objects.
sleep()
Calling this function suspends the execution, like pause, of script for a given given number of seconds.
import time for x in range(0, 5): time.time() time.sleep(2) print("Execution paused for 2 seconds")
Output:
1535408602.930527 Execution paused for 2 seconds 1535408604.93124 Execution paused for 2 seconds 1535408606.931592 Execution paused for 2 seconds 1535408608.936072 Execution paused for 2 seconds 1535408610.937518 Execution paused for 2 seconds
In this example, I am pausing the script for two seconds before proceeding to next statement execution. “Execution paused for 2 seconds” is printed for five times with a two second pause between each print that is evident in the seconds since epoch printed.
strftime()
This method makes things easy while converting to/from any time format including local time.
>>> import time
>>> time.strftime("%Y-%m-%d-%H.%M.%S", time.localtime())
'2018-08-27-22.49.25'
>>> time.strftime("%Y-%m-%d-%H.%M.%S", time.gmtime(0))
'1970-01-01-00.00.00'
>>> time.strftime("%Y-%m-%d-%H.%M.%S", time.gmtime(1535408602))
'2018-08-27-22.23.22'
>>> time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime())
'2018-08-27-22:52:06'
>>> time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
'2018-08-27 22:52:17'
>>> time.strftime("%Y_%m_%d %H:%M:%S", time.localtime())
'2018_08_27 22:52:25'
>>>
localtime()
This could be used to print the current system time.
Use cases in Test Automation
- When you want to store date into database or files or any location
- To pause test execution for specified number of seconds
- To find out future date and time after ‘n’ seconds
- To find out elapsed time or test execution time
Please let us know in comments if you know any other use cases for time module in test automation and execution.
Please visit our previous article to learn more about file operations using python. Happy coding!!!