Registry / data / pytimeparse

pytimeparse

JSON →
library1.1.8pypypi✓ verified 27d ago

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 pytimeparse
INSTALL
IMPORT
SIG · PYTIMEPARSE
P
pytimeparse
datapythonv1.1.8
Install
1.5s avg
Import
10ms
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.1.8 · 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.010s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.006s · 18MB
16MB installed
● package 16MB
Code
Verified usage

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

parse
from pytimeparse import parse
from pytimeparse.timeparse import timeparse
The `parse` function is the preferred top-level alias available since version 1.1.3. While `pytimeparse.timeparse.timeparse` still works, it's considered an older pattern.

The `parse` function takes a string time expression and returns the total number of seconds as an integer or float.

from pytimeparse import parse # Parse various time expressions print(parse('32m')) print(parse('2h33m')) print(parse('4:17')) print(parse('5hr34m56s')) print(parse('1.2 minutes')) print(parse('2 days, 4:13:02.266')) print(parse('1w 3d 2h 32m'))
Debug
Known issues
gotchaParsing of months and years assumes fixed durations (30 days for a month, 365 days for a year) and does not account for leap years or leap seconds. This can lead to inaccuracies for precise date calculations.
fix
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.
affects: All versions
gotchaWhen a number is provided without an explicit unit, `parse()` will return `None`. It does not assume 'seconds' as a default unit. For example, `parse('60')` will return `None`.
fix
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.
affects: All versions
breakingVersions prior to 1.1.4 had an `ImportError` on Python 3 due to compatibility issues.
fix
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.
affects: < 1.1.4
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pytimeparse'
The 'pytimeparse' library has not been installed in the current Python environment.
fix
pip install pytimeparse
AttributeError: 'int' object has no attribute 'lower'
The 'pytimeparse.timeparse.parse' function expects a string as input, but a non-string type (like an integer, list, or None) was provided.
fix
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'.
pytimeparse returns None
The input string provided to the 'parse' function cannot be understood by the library, often because it contains unsupported natural language (e.g., 'tomorrow', 'next week') or an invalid format.
fix
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")'.
TypeError: 'module' object is not callable
The user attempted to call the 'timeparse' submodule directly (e.g., 'timeparse("1h")') after importing it as 'from pytimeparse import timeparse', instead of calling the 'parse' function within it.
fix
Import the 'parse' function explicitly: 'from pytimeparse.timeparse import parse', then call 'parse("1h")'. Alternatively, use 'import pytimeparse' and call 'pytimeparse.parse("1h")'.
Upgrade
Version history
1.1.8latest on PyPI · released May 18, 2018
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources
pytimeparse — pip install pytimeparse · libregistry