Pytimeparse is a small Python library designed to parse various kinds of natural language time expressions into a total number of seconds. It supports a wide range of formats including combinations of days, hours, minutes, and seconds, as well as clock-style notations. The current version is 1.1.8, and the library is considered stable with infrequent updates, with its last release in 2018.
pip install pytimeparseVerified import paths — ran on the pinned version, not inferred.
The `parse` function takes a string time expression and returns the total number of seconds as an integer or float.
Be aware of these assumptions when parsing time expressions involving months and years. For more precise date/time handling, consider using `datetime` or `dateutil.relativedelta` in conjunction with pytimeparse if applicable, or a different library altogether for complex date parsing. pytimeparse2, a fork, offers `as_timedelta=True` for `datetime.timedelta` or `dateutil.relativedelta` returns.
Always specify a unit for numerical values (e.g., `parse('60s')` for 60 seconds, `parse('120m')` for 120 minutes). If you expect a bare number to be a specific unit, you must append the unit.Upgrade to pytimeparse version 1.1.4 or newer to ensure Python 3 compatibility. Version 1.1.8 (and newer 1.x versions) is compatible with Python 3.9 and likely newer Python 3 versions.
pip install pytimeparse
Ensure that the argument passed to 'parse' is always a string. Convert other types to strings if appropriate, or validate input type before calling 'parse'.
Ensure the input string represents a duration (e.g., '1 hour 30 minutes', '2d 5h') and handle the 'None' return value explicitly in your code, e.g., 'if seconds is None: print("Could not parse")'.Import the 'parse' function explicitly: 'from pytimeparse.timeparse import parse', then call 'parse("1h")'. Alternatively, use 'import pytimeparse' and call 'pytimeparse.parse("1h")'.No dependency data recorded yet.