All contributions

wroberts/pytimeparse

pytimeparse

A small Python module that turns time expressions such as 1h30m or 1:24 into a number of seconds.

Language
Python
GitHub stars
296
Merged changes
1

What we changed

The sign character class no longer accidentally matches a pipe, so input starting with | is rejected instead of being parsed silently.

What this work shows

  • Regular expressions in Python
  • Parser input validation
  • Negative tests with unittest
What was broken

A pipe passed for a sign

The sign pattern was [+|-], and inside a character class | is a plain character. parse('|5m') returned 300 and parse('|1:24') returned 84 instead of rejecting the bad input.

How we fixed it

A character class with only plus and minus

The pattern is now [+-], so the pipe stays in the string, no time format matches it and timeparse returns None. Tests cover |5m, | 5m and |1:24.

#33

Merged change

1 3 October 2026

What it taught us

Inside a regex character class a pipe is a literal, not an alternation. A parser that silently accepts bad input is worse than one that rejects it, so we also test what a parser should refuse.

More about this technology: Python

Other projects we write code for

Describe your project.

A free quote comes back within an hour during working hours.