There are several Python libraries available for time tracking in Python. We will start by highlighting the commonly used packages for time tracking – datetime and time.
After that, we will discuss two open-source Python tools that use the said libraries to build sophisticated time-tracking systems. These tools can track time spent on computer applications, websites, files, and folders.
The datatime Library
This library is very popular for working with dates and times. You can use the library to track time by creating datetime objects and using them to compute the elapsed time, as shown below.
1 2 3 4 5 6 7 8 9 |
from datetime import datetime import time start = datetime.now() # Just pausing the execution here for 10 seconds time.sleep(10) end = datetime.now() elapsed_time = end - start print(elapsed_time) |
Output:
0:00:10.010209
The execution time took approximately 10 seconds because of time.sleep(10) pause.
The time Library
Another very good tool for tracking elapsed time is the time library (we used it already in the code above to pause the execution). Here is the code example.
1 2 3 4 5 6 7 |
import time start = time.time() time.sleep(7) end = time.time() elapsed_time = end - start print(elapsed_time) |
Output:
7.00372314453125
Custom Context Manager for Time Tracking
You can also create a custom manager for time tracking using either the datetime or time libraries discussed above. Using a custom manager can be particularly useful if you want to reuse the time tracker multiple times in the code. For example,
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import time class MyTimer(object): def __enter__(self): # Start a time at entry self.start = time.time() return self def __exit__(self, *args): self.end = time.time() self.elapsed = self.end - self.start print(f"Elapsed time: {self.elapsed} seconds.") with MyTimer() as timer: time.sleep(3) |
Output:
Elapsed time: 3.003067970275879 seconds.
The __entry__() dunder function is called automatically when MyTime() is called, whereas the __exit__() method is called at the end. In our case above, the __entry__() method starts the time, and __exit() stops it. That means the MyTime() will keep running for all code executed under the with-statement.
Using Decorator Function for Time Tracking
If you want to create a time tracker for a function or a method, this approach is for you. All you need to do is to create a decorator function for timing the execution and wrap it around the functions or methods we want to time, as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import time # Creating a decorator using the time.time() function for timing. def MyTimer(func): def wrapper(*args, **kwargs): start = time.time() result = func(*args, **kwargs) end = time.time() elapsed = end - start print(f"Elapsed time: {elapsed} seconds.") return result return wrapper # Wrapping the decorator on my_func function. @MyTimer def my_func(sleeping_time=3): time.sleep(sleeping_time) my_func(7) |
Output:
Elapsed time: 7.007120370864868 seconds.
Tracking Time Spent on Computer Apps and Websites
There are two open-source Python tools available on GitHub for this. The READMe documents for the two tools provide good information on installing dependencies and using the tool.
Automatic Time Tracking Desktop Client
GitHub link: https://github.com/asperduti/automatic-time-tracking-desktop-client
All you need to do here is to download or clone the repository and execute the run.py script on the root directory. The script then keeps running until you stop it. Once stopped, the details on the sites you visited and the applications you used will be displayed on the console (see below).
The output shows that I spent 2 seconds on the Tilix Terminal, 1 second on Google Docs on Firefox, 16 seconds editing Python Script on Sublime Text, etc.
AutoTimer
GitHub Link: https://github.com/KalleHallden/AutoTimer
The timer tool is started by executing the autotimer.py script on the root folder. The tool tracks the apps used, websites visited, and even files and folders opened. This happens in real-time, and the results are saved in a JSON file.
The following Figure shows the JSON file generated when I used the tool. The file shows that I spent 2 seconds on Evernote on Mozilla Firefox, 2 sessions on VS Code spending a total of 19 minutes, and some sessions on the Desktop.
Conclusion
This post discussed two libraries for performing simple time tracking for code execution. We also learned to create custom time trackers based on context managers and decorators.
Lastly, we highlighted two tools that can take the concept of time tracking with Python to another level. The tools can track time spent on computer apps, website links, and even files and folders.
You can also extend the use of the tools by forking them from GitHub and adding the features you want or analyzing the output data, for example, visualizing data to understand your computer usage behavior.