From 678214a5c1fb818d9d7421479e31309bfeaee254 Mon Sep 17 00:00:00 2001 From: Arpit Jain Date: Tue, 11 Aug 2026 07:26:20 +0900 Subject: [PATCH] Clamp the day of month against 31, not 12, in extract_date extract_date() rejects any day greater than 12 and rewrites it to 01, so every timestamp whose day of month is 13 or later is silently moved to the first of the month. 20121125 comes back as 2012-11-01. The bound looks like a copy of the month check directly above it. Widen it to 31. Days past the end of a short month are already handled downstream by validate_date() in openfda/spl/fix_date.py, which walks them back to a real date instead of discarding them. Signed-off-by: Arpit Jain --- openfda/common.py | 2 +- openfda/tests/common_test.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/openfda/common.py b/openfda/common.py index d2a1cec..d5e09a6 100644 --- a/openfda/common.py +++ b/openfda/common.py @@ -292,7 +292,7 @@ def extract_date(date_str): year = "1900" if 0 >= int(month) or 12 < int(month): month = "01" - if 0 >= int(day) or 12 < int(day): + if 0 >= int(day) or 31 < int(day): day = "01" return year + '-' + month + '-' + day diff --git a/openfda/tests/common_test.py b/openfda/tests/common_test.py index 2829130..ff06c9e 100644 --- a/openfda/tests/common_test.py +++ b/openfda/tests/common_test.py @@ -10,6 +10,8 @@ def test_extract_date(self): assert common.extract_date("201211103422") == '2012-11-10' assert common.extract_date("20121610") == '2012-01-10' assert common.extract_date("20120010") == '2012-01-10' + assert common.extract_date("20121213") == '2012-12-13' + assert common.extract_date("20121231") == '2012-12-31' assert common.extract_date("20121132") == '2012-11-01' assert common.extract_date("2012000001") == '2012-01-01' assert common.extract_date("20561132") == '1900-11-01'