Time expression parser that converts various time formats into numeric seconds
npx @tessl/cli install tessl/pypi-pytimeparse@1.1.0A Python library for parsing various kinds of time expressions into numeric values representing seconds. It handles diverse time formats including abbreviated units, colon-separated time formats, natural language expressions, and mixed formats with decimal values. The library exposes a single main function that converts time strings into integer or floating-point seconds, making it highly reusable for applications requiring time duration parsing, scheduling systems, configuration parsers, and any tool that needs to interpret human-readable time expressions in a consistent programmatic format.
pip install pytimeparseimport pytimeparseCommon usage pattern:
from pytimeparse import parseAlternative direct import:
from pytimeparse.timeparse import timeparsefrom pytimeparse import parse
# Parse abbreviated time formats
seconds = parse('32m') # 1920 (32 minutes)
seconds = parse('2h32m') # 9120 (2 hours 32 minutes)
seconds = parse('1w3d2h32m') # 873120 (1 week 3 days 2 hours 32 minutes)
# Parse clock formats
seconds = parse('4:13') # 253 (4 minutes 13 seconds)
seconds = parse('4:13:02') # 15182 (4 hours 13 minutes 2 seconds)
seconds = parse(':22') # 22 (22 seconds)
# Parse natural language
seconds = parse('1 minute, 24 secs') # 84
seconds = parse('5 hours, 34 minutes, 56 seconds') # 20096
seconds = parse('1.2 minutes') # 72.0
# Parse signed expressions
seconds = parse('+32m') # 1920
seconds = parse('-32m') # -1920
# Handle unparseable strings
result = parse('invalid') # NoneParses time expressions and returns the equivalent number of seconds as an integer or float. Returns None if the expression cannot be parsed.
def parse(sval, granularity='seconds'):
"""
Parse a time expression, returning it as a number of seconds.
Parameters:
- sval (str): The string value to parse
- granularity (str, optional): Either 'seconds' (default) or 'minutes'.
Affects interpretation of ambiguous colon-separated times.
When 'minutes', formats like '1:30' are interpreted as
1 hour 30 minutes instead of 1 minute 30 seconds.
Returns:
- int or float: Number of seconds, or None if parsing fails
Raises:
- No exceptions raised for invalid input
"""The granularity parameter controls how ambiguous colon-separated time formats are interpreted:
from pytimeparse import parse
# Default behavior: interpret as minutes:seconds
parse('1:30') # 90 (1 minute 30 seconds)
# Minutes granularity: interpret as hours:minutes
parse('1:30', granularity='minutes') # 5400 (1 hour 30 minutes)
# Granularity only affects ambiguous formats (single colon, no decimals)
parse('1:30:45') # 5445 (same regardless of granularity)
parse('1:30:45', granularity='minutes') # 5445 (same result)32m - 32 minutes2h32m - 2 hours 32 minutes3d2h32m - 3 days 2 hours 32 minutes1w3d2h32m - 1 week 3 days 2 hours 32 minutes1w 3d 2h 32m - With spaces between units1 w 3 d 2 h 32 m - With spaces around units4:13 - 4 minutes 13 seconds (or 4 hours 13 minutes with granularity='minutes')4:13:02 - 4 hours 13 minutes 2 seconds4:13:02.266 - With fractional seconds2:04:13:02.266 - 2 days 4 hours 13 minutes 2.266 seconds:22 - 22 seconds (bare seconds)2 days, 4:13:02 - Combined day count with clock format2 days, 4:13:02.266 - With fractional seconds5hr34m56s - Abbreviated mixed format5 hours, 34 minutes, 56 seconds - Full words5 hrs, 34 mins, 56 secs - Abbreviated words2 days, 5 hours, 34 minutes, 56 seconds - Multi-unit natural language1.2 m / 1.2 min / 1.2 mins / 1.2 minute / 1.2 minutes - Decimal minutes172 hours / 172 hr / 172 h / 172 hrs / 172 hour - Various hour formats1.24 days / 5 d / 5 day / 5 days - Day formats5.6 wk / 5.6 week / 5.6 weeks - Week formats1.75 s / 1.75 sec / 1.75 secs / 1.75 second / 1.75 seconds - Second formats+32m - Positive 32 minutes (explicit positive sign)-32m - Negative 32 minutes+ 32m / - 32m - With spaces after signThe library uses these conversion factors:
None for unparseable time expressionsNone'32m - 1s') return None# Direct access to main function
from pytimeparse.timeparse import timeparse
result = timeparse('1h30m')
# Access to version information
import pytimeparse
version = pytimeparse.__version__ # '1.1.8'
# Both parse and timeparse refer to the same function
from pytimeparse import parse
from pytimeparse.timeparse import timeparse
assert parse is timeparse # True