Skip to content

Instantly share code, notes, and snippets.

@JoniKauf
JoniKauf / parse_timedelta.py
Last active March 19, 2025 15:29
Regex + Function for user friendly parsing of a str to timedelta object
from decimal import Decimal
import re
import datetime as dt
# Regex could be optimized:
# - Only capture first letter of time unit
# - Lower string beforehand and remove re.IGNORECASE
# - Only allow day digit count of <= 9 (deltatime limit is 999_999_999 days)
# I decided not to, because one might want to track those
# differences of user input in their own implementation.