From 02b3f7d4327a872b0cd2ddff8abf59820207a3b5 Mon Sep 17 00:00:00 2001
From: Tulir Asokan <tulir@maunium.net>
Date: Wed, 25 Mar 2020 23:28:24 +0200
Subject: [PATCH] Add support for 2-digit years

---
 reminder/locale_util.py | 12 ++++++++++++
 reminder/locales.py     | 11 ++++++-----
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/reminder/locale_util.py b/reminder/locale_util.py
index dd5462f..c1e6551 100644
--- a/reminder/locale_util.py
+++ b/reminder/locale_util.py
@@ -99,6 +99,18 @@ class TimeMatcher(RegexMatcher):
         return MatcherReturn(params=params, end=match.end())
 
 
+class ShortYearMatcher(RegexMatcher):
+    def _convert_match(self, match: Match) -> MatcherReturn:
+        rtrn = super()._convert_match(match)
+        if rtrn.params["year"] < 100:
+            year = datetime.now().year
+            current_century = year // 100
+            if rtrn.params["year"] < year % 100:
+                current_century += 1
+            rtrn.params["year"] = (current_century * 100) + rtrn.params["year"]
+        return rtrn
+
+
 class WeekdayMatcher(Matcher):
     regex: Pattern
     map: Dict[str, Union[int, WeekdayType]]
diff --git a/reminder/locales.py b/reminder/locales.py
index 2ef590b..4b5471f 100644
--- a/reminder/locales.py
+++ b/reminder/locales.py
@@ -15,7 +15,8 @@
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 from dateutil.relativedelta import MO, TU, WE, TH, FR, SA, SU
 
-from .locale_util import Locales, Locale, RegexMatcher, WeekdayMatcher, TimeMatcher
+from .locale_util import (Locales, Locale, RegexMatcher,
+                          WeekdayMatcher, TimeMatcher, ShortYearMatcher)
 
 locales: Locales = {}
 
@@ -58,11 +59,11 @@ time_12_en = TimeMatcher(r"\s?(?:at\s)?"
 
 locales["en_us"] = locales["en_iso"].replace(
     name="English (US)", time=time_12_en,
-    date=RegexMatcher(r"(?P<month>\d{1,2})/(?P<day>\d{1,2})(?:/(?P<year>\d{4}))?"))
+    date=ShortYearMatcher(r"(?P<month>\d{1,2})/(?P<day>\d{1,2})(?:/(?P<year>\d{2}(?:\d{2})?))?"))
 
 locales["en_uk"] = locales["en_iso"].replace(
     name="English (UK)", time=time_12_en,
-    date=RegexMatcher(r"(?P<day>\d{1,2})/(?P<month>\d{1,2})(?:/(?P<year>\d{4}))?"))
+    date=ShortYearMatcher(r"(?P<day>\d{1,2})/(?P<month>\d{1,2})(?:/(?P<year>\d{2}(?:\d{2})?))?"))
 
 td_sep_fi = r"(?:[\s,]{1,3}(?:ja\s)?)"
 locales["fi_fi"] = Locale(
@@ -75,7 +76,7 @@ locales["fi_fi"] = Locale(
                            rf"(?:(?P<minutes>[-+]?\d+)\s?m(?:in(?:uut(?:in?|tia))?)?{td_sep_fi})?"
                            r"(?:(?P<seconds>[-+]?\d+)\s?s(?:ek(?:un(?:nin?|tia))?)?)?"
                            r"(?:\s(?:kuluttua|päästä?))?"),
-    date=RegexMatcher(r"(?P<day>\d{1,2})\.(?P<month>\d{1,2})\.(?P<year>\d{4})"),
+    date=ShortYearMatcher(r"(?P<day>\d{1,2})\.(?P<month>\d{1,2})\.(?P<year>\d{2}(?:\d{2})?)"),
     weekday=WeekdayMatcher(pattern=r"(?:tänään"
                                    r"|(?:yli)?huomen"
                                    r"|ma(?:aanantai)?"
@@ -107,7 +108,7 @@ locales["de_de"] = Locale(
                            rf"(?:(?P<hours>[-+]?\d+)\s?stunden?{td_sep_de})?"
                            rf"(?:(?P<minutes>[-+]?\d+)\s?minuten?{td_sep_de})?"
                            r"(?:(?P<seconds>[-+]?\d+)\s?sekunden?)?"),
-    date=RegexMatcher(r"(?P<day>\d{1,2})\.(?P<month>\d{1,2})\.(?P<year>\d{4})"),
+    date=ShortYearMatcher(r"(?P<day>\d{1,2})\.(?P<month>\d{1,2})\.(?P<year>\d{2}(?:\d{2})?)"),
     weekday=WeekdayMatcher(pattern=r"(?:heute"
                                    r"|(?:über)?morgen"
                                    r"|mo(?:ntag)?"
-- 
GitLab