Registry / serialization / pytimeparse2

pytimeparse2

JSON →
library1.7.1pypypi✓ verified 25d ago

pytimeparse2 is a small Python library for parsing various human-readable time expressions (e.g., '2h32m', '1.5 days', '5hr 34mins 56secs') into a total number of seconds or `datetime.timedelta` objects. It is a fork of the original `pytimeparse` project, aiming for optimized functionality and stable support. The current version is 1.7.1, and releases are made on an as-needed basis for bug fixes and feature enhancements.

pip install pytimeparse2
INSTALL
IMPORT
SIG · PYTIMEPARSE2
P
pytimeparse2
serializationpythonv1.7.1
Install
1.5s avg
Import
25ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.7.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.028s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.022s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse
from pytimeparse2 import parse
from pytimeparse import parse
pytimeparse2 is a distinct fork from the original 'pytimeparse' library. Importing 'parse' from 'pytimeparse' will use the older, unmaintained version.

This quickstart demonstrates how to parse a time expression into total seconds or a `datetime.timedelta` object using the `parse` function. For advanced `timedelta` results (e.g., handling months/years), `python-dateutil` is recommended.

from pytimeparse2 import parse # Parse to total seconds seconds = parse('1 day, 2 hours, 30 minutes, 15 seconds') print(f"Parsed to seconds: {seconds}") # Parse to timedelta (requires python-dateutil for relativedelta if complex units are used) timedelta_obj = parse('1y 2mo 3w 4d 5h 6m 7s 8ms', as_timedelta=True) print(f"Parsed to timedelta: {timedelta_obj}")
Debug
Known issues
gotchaWhen parsing months ('mo') and years ('y'), `pytimeparse2` assumes fixed durations (30 days for a month, 365 days for a year). It does not account for leap years or leap seconds, which can lead to inaccuracies for precise date calculations.
fix
Be aware of this limitation when parsing expressions involving 'mo' or 'y'. For highly accurate date/time arithmetic across varying year lengths, consider using libraries like `python-dateutil` directly, especially its `relativedelta` functionality.
affects: All versions
gotchaThe `as_timedelta=True` option will return a `datetime.timedelta` object by default. If `python-dateutil` is installed, it will instead return a `dateutil.relativedelta.relativedelta` object, which offers more sophisticated handling for units like months and years. Ensure `python-dateutil` is installed if `relativedelta` behavior is expected.
fix
Install `python-dateutil` via `pip install python-dateutil` if `relativedelta` objects are desired for more precise and flexible date calculations with `as_timedelta=True`.
affects: All versions
gotcha`pytimeparse2` is a fork of the original `pytimeparse` library. While functionally similar, using `from pytimeparse import parse` will import the older, less maintained version. Always use `from pytimeparse2 import parse` to ensure you are using this library.
fix
Explicitly import from `pytimeparse2` (i.e., `from pytimeparse2 import parse`) to leverage the updates and support provided by this specific fork.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pytimeparse'
The user has installed the `pytimeparse2` library but is attempting to import the original `pytimeparse` library, which is not installed or available in the current environment.
fix
Change the import statement to use `pytimeparse2` instead: `from pytimeparse2 import parse`.
pytimeparse2.timeparse.ParseError: Unable to parse time expression
The `parse` function was called with an invalid or unparseable time string, and the `error=True` argument was set, causing the library to raise a `ParseError` instead of returning `None`.
fix
Provide a valid time string that `pytimeparse2` can parse, or catch the `ParseError` exception to handle gracefully if invalid input is expected: `try: parse('invalid', error=True) except ParseError: print('Invalid time string')`.
AttributeError: module 'pytimeparse2' has no attribute 'TimeParse'
The user is attempting to instantiate a non-existent `TimeParse` class, possibly based on an incorrect assumption about the library's API structure or confusion with other libraries.
fix
The main parsing function `parse` is directly available from the `pytimeparse2` module; use `from pytimeparse2 import parse` and then call `parse('time string')` directly.
TypeError: expected string or bytes-like object
The `parse` function received a non-string argument (e.g., an integer or float) when it expects a string representing the time expression to be parsed.
fix
Ensure the argument passed to `parse` is always a string: `parse('123')` instead of `parse(123)`.
Upgrade
Version history
1.7.1latest on PyPI · released May 11, 2023
Audit
Dependencies
python-dateutiloptionalEnables returning `dateutil.relativedelta.relativedelta` objects when `as_timedelta=True` is used. Otherwise, it defaults to `datetime.timedelta`.
Agent activity
7 hits · last 30 days
node
6
Resources
pytimeparse2 — pip install pytimeparse2 · libregistry