Improve date parsing in OOP handling to support additional formats and enhance error reporting.

This commit is contained in:
Ross
2026-01-05 20:03:41 +00:00
parent b98c28401f
commit 62e48ef205
+27 -32
View File
@@ -357,41 +357,36 @@ def main(
if oop:
formatted_oops = []
for dates in oop.split(","):
print(dates)
if "-" in dates:
s, e = dates.split("-")
elif "to" in dates:
s, e = dates.split(" to ")
raw = dates.strip()
# strip surrounding brackets/parentheses/quotes
raw = raw.strip("()[]\"' ")
if "-" in raw:
s, e = raw.split("-", 1)
elif " to " in raw:
s, e = raw.split(" to ", 1)
elif "to" in raw:
s, e = raw.split("to", 1)
else:
raise ValueError(f"Cannot parse OOP dates: '{oop}' for {worker}")
try:
formatted_oops.append(
{
"start_date": datetime.datetime.strptime(
s.strip(), "%d/%m/%Y"
).date(),
"end_date": datetime.datetime.strptime(
e.strip(), "%d/%m/%Y"
).date(),
}
)
except ValueError:
s = s.strip().strip("()[]\"' ")
e = e.strip().strip("()[]\"' ")
parsed = False
for fmt in ("%d/%m/%Y", "%d/%m/%y"):
try:
formatted_oops.append(
{
"start_date": datetime.datetime.strptime(
s.strip(), "%d/%m/%Y"
).date(),
"end_date": datetime.datetime.strptime(
e.strip(), "%d/%m/%Y"
).date(),
}
)
except ValueError as e:
print(e)
print("WORKER", worker)
print("DATES", s, e)
raise
start_dt = datetime.datetime.strptime(s, fmt).date()
end_dt = datetime.datetime.strptime(e, fmt).date()
formatted_oops.append({"start_date": start_dt, "end_date": end_dt})
parsed = True
break
except ValueError:
continue
if not parsed:
print("WORKER", worker)
print("DATES", s, e)
raise ValueError(f"Cannot parse OOP dates: '{oop}' for {worker}")
oop = formatted_oops
else: