Time Tracking in Python

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.

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.

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,

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.

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.