A parser for the opening_hours fields from OpenStreetMap.
Actively maintained. Last release 2 weeks ago, commits in 9 of the last 26 weeks. A safe choice.
pip install opening-hours-py
Python bindings for OSM Opening Hours
Usage
The pre-compiled package is published for Python 3.9 and above and new releases will adapt to officially supported Python versions.
If you want to install this library with older version of Python, you will
need the Rust toolchain (rustc and cargo).
Install opening-hours-py from PyPI, for example using pip:
pip install --user opening-hours-py
Then, the main object that you will interact with will be OpeningHours:
from opening_hours import OpeningHours
oh = OpeningHours("Mo-Fr 10:00-18:00; Sa-Su 10:00-12:00")
print("Current status is", oh.state())
print("This will change at", oh.next_change())
# You can also attach a timezone to your expression. If you use timezone-aware
# dates, they will be converted to local time before any computation is done.
from zoneinfo import ZoneInfo
oh = OpeningHours("Mo-Fr 10:00-18:00; Sa-Su 10:00-12:00", timezone=ZoneInfo("Europe/Paris"))
# The timezone can also be infered with coordinates
oh = OpeningHours("Mo-Fr 10:00-18:00; Sa-Su 10:00-12:00", coords=(48.8535, 2.34839))
# You can normalize the expression
assert str(OpeningHours("24/7 ; Su closed").normalize()) == "Mo-Sa"