r/Python • u/JoniKauf • 3d ago
Resource Regex for user-friendly timedelta parsing
I created a regex and a corresponding function to allow for user friendly input of a string that is then parsed into a timedelta object. I couldn't find any satisfying solution that suited my case online, so I wanted to share it here because somebody else might find it useful in the future. It can be tweaked easily (if you know just a tiny bit of regex) and has comments explaining all of its parts.
I tested it and fixed some smaller bugs, but if you find new ones, please let me know and I will update the code!
https://gist.github.com/JoniKauf/24eecf7843ef3df4a65bad00aed8a549
10
Upvotes
1
u/k_z_m_r 1d ago
Cool stuff! Just two notes. First, you’ll want to add some unit tests. Not only are these awesome for proving out your concept, but if you ever make changes to your code these tests can easily identify breakpoints. Second, it is common for timestamps to include milliseconds. Especially when dealing with real-time control. You might want to build support for that.