Commit 881c264
fix: Default to day-first format for invalid/unknown locales (#1168)
## Summary
Fixes a bug where invalid/unknown locales would default to month-first
(American) format instead of day-first format. Since most of the world
uses day-first, this is the better default.
## Problem
When an invalid or unknown locale was provided (e.g., typo, unsupported
locale), the exception handler would return `False` (month-first):
```python
except Exception:
return False # ❌ Defaults to month-first (MM/DD/YYYY)
```
This caused incorrect parsing:
- User provides `locale="xyz_ABC"` (typo)
- Date `"01/02/2023"` gets parsed as **January 2nd** (month-first)
- Should be parsed as **February 1st** (day-first, more common globally)
### Why this is wrong
Only a handful of locales use month-first format:
- `en_US` - American English
- A few Pacific island locales
**Most of the world uses day-first:**
- All of Europe
- Most of Asia
- Middle East
- Africa
- South America
- Australia
## Solution
Changed exception handler to return `True` (day-first) for invalid
locales:
```python
except Exception:
# Invalid/unknown locale: default to day-first since most of the world uses it
# Only en_US and a few other locales use month-first (MM/DD/YYYY)
return True # ✅ Defaults to day-first (DD/MM/YYYY)
```
### The logic now:
1. **ISO 8601** (`YYYY-MM-DD`) → `False` (month before day)
2. **No locale** (`None`) → `False` (backward compat, American default)
3. **Valid locale** → Check Babel CLDR data
4. **Invalid locale** → `True` (day-first is global default)
## Code Changes
**Before:**
```python
except Exception:
return False # Defaulted to month-first
```
**After:**
```python
except Exception:
# Invalid/unknown locale: default to day-first since most of the world uses it
# Only en_US and a few other locales use month-first (MM/DD/YYYY)
return True # Default to day-first (DD/MM/YYYY)
```
## Testing
**Updated test:**
```python
def test_invalid_locale_returns_true(self) -> None:
# Invalid/unknown locales default to day-first since most of world uses it
assert _should_use_day_first("01/15/2023", "invalid_LOCALE") is True
assert _should_use_day_first("15/01/2023", "xyz_ABC") is True
```
**New integration test:**
```python
def test_invalid_locale_defaults_to_day_first(self) -> None:
parser = TimestampParser()
# With an invalid locale, ambiguous dates should parse as day-first
with set_locale_context("invalid_LOCALE"):
result = parser.parse("01/02/2023")
assert "2023-02-01" in result # February 1st (day-first)
```
- ✅ All 19 timestamp parser tests pass
- ✅ All 547 tests pass
- ✅ Linting clean
## Impact
- **Fixes bug** - Invalid locales now use the globally more common
format
- **Better user experience** - Fewer parsing errors for international
users
- **No breaking changes** - Only affects invalid locale case (which was
already broken)
- **Backward compatible** - `None` locale still defaults to American
format
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Opus 4.1 <noreply@anthropic.com>1 parent a8c5ae1 commit 881c264
2 files changed
Lines changed: 24 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
| 45 | + | |
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
64 | 66 | | |
65 | 67 | | |
66 | 68 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
46 | | - | |
47 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
48 | 50 | | |
49 | 51 | | |
50 | 52 | | |
| |||
142 | 144 | | |
143 | 145 | | |
144 | 146 | | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
145 | 162 | | |
146 | 163 | | |
147 | 164 | | |
| |||
0 commit comments