Skip to content

Commit

Permalink
Add support for 2-digit years
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Mar 25, 2020
1 parent df968bd commit 02b3f7d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
12 changes: 12 additions & 0 deletions reminder/locale_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ def _convert_match(self, match: Match) -> MatcherReturn:
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]]
Expand Down
11 changes: 6 additions & 5 deletions reminder/locales.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -58,11 +59,11 @@

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(
Expand All @@ -75,7 +76,7 @@
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)?"
Expand Down Expand Up @@ -107,7 +108,7 @@
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)?"
Expand Down

0 comments on commit 02b3f7d

Please sign in to comment.