Skip to content

Commit 03cfc2d

Browse files
author
LazyFP Dev
committed
fix: normalize all date formats to YYYY-MM-DD
1 parent 503baec commit 03cfc2d

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

extractors/date_extractor.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
class DateExtractor(BaseExtractor):
88
def clean(self, value: str) -> str:
99
value = value.strip()
10-
# Extract just the date portion if extra text leaked through
11-
m = re.search(r"(\d{4}[-/年.]\d{1,2}[-/月.]\d{1,2})", value)
10+
m = re.search(r"(\d{4})[-/年.](\d{1,2})[-/月.](\d{1,2})", value)
1211
if m:
13-
value = m.group(1)
12+
return f"{m.group(1)}-{int(m.group(2)):02d}-{int(m.group(3)):02d}"
1413
value = value.replace("/", "-").replace(".", "-")
1514
if " " in value:
1615
parts = value.split()
1716
if len(parts) == 3:
18-
return f"{parts[0]}{parts[1]}{parts[2]}日"
17+
try:
18+
dt = datetime.strptime(f"{parts[0]}-{parts[1]}-{parts[2]}", "%Y-%m-%d")
19+
return dt.strftime("%Y-%m-%d")
20+
except ValueError:
21+
pass
1922
return value
2023

2124
def validate(self, value: str) -> bool:
@@ -31,7 +34,7 @@ def fallback(self, text: str, filename: str) -> Optional[str]:
3134
all_digits = "".join(re.findall(r"\d", text))
3235
matches = re.finditer(r"(20[23]\d)(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])", all_digits)
3336
for m in matches:
34-
return f"{m.group(1)}{m.group(2)}{m.group(3)}"
37+
return f"{m.group(1)}-{m.group(2)}-{m.group(3)}"
3538
return None
3639

3740
def parse_quarter(self, date_str: str) -> str:

0 commit comments

Comments
 (0)