feat(normalizer): year expansion century rule
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -39,6 +39,25 @@ def resolve_feast_or_season(token: str, year: int):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def expand_year(token: str):
|
||||||
|
"""Expand a 2/3/4-digit year string per the 1873–1957 century rule. None if ambiguous."""
|
||||||
|
token = token.strip()
|
||||||
|
if not token.isdigit():
|
||||||
|
return None
|
||||||
|
n, v = len(token), int(token)
|
||||||
|
if n == 4:
|
||||||
|
return v
|
||||||
|
if n == 3:
|
||||||
|
return 1000 + v
|
||||||
|
if n == 2:
|
||||||
|
if v <= config.TWO_DIGIT_19XX_MAX:
|
||||||
|
return 1900 + v
|
||||||
|
if v >= config.TWO_DIGIT_18XX_MIN:
|
||||||
|
return 1800 + v
|
||||||
|
return None
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def easter(year: int) -> datetime.date:
|
def easter(year: int) -> datetime.date:
|
||||||
"""Easter Sunday (Gregorian) via the Anonymous Gregorian / Butcher algorithm."""
|
"""Easter Sunday (Gregorian) via the Anonymous Gregorian / Butcher algorithm."""
|
||||||
a = year % 19
|
a = year % 19
|
||||||
|
|||||||
@@ -24,3 +24,15 @@ def test_resolve_season():
|
|||||||
|
|
||||||
def test_resolve_unknown_token_returns_none():
|
def test_resolve_unknown_token_returns_none():
|
||||||
assert dates.resolve_feast_or_season("Freitag", 1919) is None
|
assert dates.resolve_feast_or_season("Freitag", 1919) is None
|
||||||
|
|
||||||
|
def test_expand_year():
|
||||||
|
assert dates.expand_year("1888") == 1888
|
||||||
|
assert dates.expand_year("889") == 1889 # 3-digit -> 1DDD
|
||||||
|
assert dates.expand_year("923") == 1923
|
||||||
|
assert dates.expand_year("08") == 1908 # 00..57 -> 19xx
|
||||||
|
assert dates.expand_year("17") == 1917
|
||||||
|
assert dates.expand_year("57") == 1957
|
||||||
|
assert dates.expand_year("73") == 1873 # 73..99 -> 18xx
|
||||||
|
assert dates.expand_year("99") == 1899
|
||||||
|
assert dates.expand_year("65") is None # 58..72 ambiguous
|
||||||
|
assert dates.expand_year("x") is None
|
||||||
|
|||||||
Reference in New Issue
Block a user