Time Expression Parser
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.
Warnings
- gotcha Parsing 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.
- gotcha When 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`.
- breaking Versions prior to 1.1.4 had an `ImportError` on Python 3 due to compatibility issues.
Install
-
pip install pytimeparse
Imports
- parse
from pytimeparse import parse
Quickstart
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'))