DEV Community

Larkmile road notes
Larkmile road notes

Posted on

08:30-16:30 (days not stated): normalising messy published opening hours in Python

We keep a table of car hire depots in Hobart, Tasmania, with the opening hours each company publishes on its own site. Collecting them is easy. Making them comparable is not. Here are the real strings we had to deal with for nine city depots (checked 25 October 2026), and the small model we ended up with.

The raw strings

Mon-Fri 08:00-17:00; Sat-Sun 08:00-13:00
Every day 08:00-12:00
08:30-16:30 (days not stated)
Mon-Fri 08:00-16:00; Sat-Sun 08:00-12:00   # same page also says 17:00
Enter fullscreen mode Exit fullscreen mode

Three problems show up straight away:

  1. Day ranges come in different shapes ("Mon-Fri", "Every day", nothing).
  2. Some depots publish no days at all.
  3. Some publish two different times on the same site.

Don't guess: make uncertainty a value

The mistake is to fill the gap ("probably weekdays"). We store what was published and flag the rest.

import re
from dataclasses import dataclass, field

DAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

@dataclass
class Hours:
    by_day: dict = field(default_factory=dict)   # "Sat" -> ("08:00", "13:00")
    days_known: bool = True
    conflicts: list = field(default_factory=list)

def expand(span: str):
    if span == "Every day":
        return DAYS
    a, b = span.split("-")
    return DAYS[DAYS.index(a): DAYS.index(b) + 1]

def parse(raw: str) -> Hours:
    h = Hours()
    for part in raw.split(";"):
        part = part.strip()
        m = re.match(r"(Every day|\w{3}-\w{3})\s+(\d\d:\d\d)-(\d\d:\d\d)", part)
        if m:
            for d in expand(m.group(1)):
                h.by_day[d] = (m.group(2), m.group(3))
            continue
        m = re.match(r"(\d\d:\d\d)-(\d\d:\d\d)", part)
        if m:                      # times without days
            h.days_known = False
            h.by_day["?"] = (m.group(1), m.group(2))
    return h
Enter fullscreen mode Exit fullscreen mode

A second published time goes into conflicts, and every query uses the earlier close:

def latest_pickup(h: Hours, day: str):
    if not h.days_known:
        return None                # "ask the depot", never a number
    open_, close = h.by_day.get(day, (None, None))
    closes = [close] + [c for d, c in h.conflicts if d == day]
    return min(c for c in closes if c) if close else None
Enter fullscreen mode Exit fullscreen mode

What it changed

Answering "who can I collect from in town on a Saturday at 1:30pm?" now returns two depots plus one "ask", instead of a confident wrong list. Avis's own FAQ and depot page disagreed by an hour on weekdays; the model surfaces that rather than hiding it.

The rule we kept: a missing day is data, not an empty cell.

The table this feeds, with every source page linked, is Larkmile's Hobart car hire comparison.

Larkmile

Top comments (0)