Skip to content

Commit 99e5a46

Browse files
committed
Fix speed restrictions date processing
1 parent bab4f8e commit 99e5a46

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

ingestor/chalicelib/speed_restrictions.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313

1414
EntryKey = Tuple[str, date]
1515

16+
DATE_FORMATS = ["%Y-%m-%d", "%m/%d/%y", "%m/%d/%Y"]
17+
18+
19+
def parse_date(date_string: str) -> date:
20+
"""Parse a date string, trying multiple formats."""
21+
for fmt in DATE_FORMATS:
22+
try:
23+
return datetime.strptime(date_string, fmt).date()
24+
except ValueError:
25+
continue
26+
raise ValueError(f"Unable to parse date: {date_string}")
27+
1628

1729
@dataclass
1830
class SpeedRestrictionEntry:
@@ -57,8 +69,8 @@ def parse_restriction_row_to_entry(row: Dict[str, str]) -> Union[None, SpeedRest
5769
line_raw = row["Line"].replace("Line", "").strip()
5870
if "Mattapan" in branch:
5971
line_raw = "Mattapan"
60-
date = datetime.strptime(row["Calendar_Date"], "%Y-%m-%d").date()
61-
reported = datetime.strptime(row["Date_Restriction_Reported"], "%Y-%m-%d").date()
72+
date = parse_date(row["Calendar_Date"])
73+
reported = parse_date(row["Date_Restriction_Reported"])
6274
speed_mph = int(row["Restriction_Speed_MPH"].replace("mph", "").strip())
6375
track_feet = int(float(row["Restriction_Distance_Feet"]))
6476
status = row["Restriction_Status"].lower()

0 commit comments

Comments
 (0)