$ show wroberts/pytimeparse
This repository provides a Python library for parsing human-readable time expressions into seconds. The core functionality is implemented in the `timeparse` function, which accepts strings in various formats (e.g., '32m', '2h32m', '1w3d2h32m', '4:13:02', '2 days, 4:13:02') and returns the equivalent duration in seconds as an integer or float. The library handles optional signs, fractional values, and ambiguous colon-separated values via a granularity parameter. It has no external dependencies and is designed for simplicity and reliability. The library is structured as a single module with a clear public interface. The implementation uses a series of regular expressions to match different time formats and converts the matched components to seconds using predefined multipliers. The code is well-tested with a comprehensive test suite that covers the various input formats and edge cases. Despite its simplicity, the library demonstrates careful attention to detail in handling the complexities of time expression parsing, such as supporting both written and abbreviated units, handling spaces and commas as separators, and providing control over the interpretation of colon-separated values.
The LLM identified 1 files worth highlighting for reuse:
The `timeparse.py` module contains the core `timeparse` function that converts time strings to seconds. It uses a list of regex patterns to match various time formats (including written units, colon-separated times, and combinations) and computes the total seconds using predefined multipliers for weeks, days, hours, minutes, and seconds. The function handles optional signs, fractional values, and an optional granularity parameter to resolve ambiguities in colon-separated values. This component is highly reusable due to its self-contained nature, lack of external dependencies, and clear, focused functionality.