Skip to content
Snippets Groups Projects
Commit 4ed17251 authored by Tulir Asokan's avatar Tulir Asokan
Browse files

Add support for AM/PM in US and UK locales

parent 0382b9e8
No related branches found
No related tags found
No related merge requests found
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
# #
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import NamedTuple, Union, Pattern, Dict, Type, Optional, TYPE_CHECKING from typing import NamedTuple, Union, Pattern, Match, Dict, Type, Optional, TYPE_CHECKING
from datetime import datetime from datetime import datetime
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
import re import re
...@@ -73,11 +73,31 @@ class RegexMatcher(Matcher): ...@@ -73,11 +73,31 @@ class RegexMatcher(Matcher):
def match(self, val: str, start: int = 0) -> Optional[MatcherReturn]: def match(self, val: str, start: int = 0) -> Optional[MatcherReturn]:
match = self.regex.match(val, pos=start) match = self.regex.match(val, pos=start)
if match and match.end() > 0: if match and match.end() > 0:
return MatcherReturn(params={key: self.value_type(value) return self._convert_match(match)
for key, value in match.groupdict().items() if value},
end=match.end())
return None return None
def _convert_match(self, match: Match) -> MatcherReturn:
return MatcherReturn(params=self._convert_groups(match.groupdict()),
end=match.end())
def _convert_groups(self, groups: Dict[str, str]) -> 'RelativeDeltaParams':
return {key: self.value_type(value) for key, value in groups.items() if value}
class TimeMatcher(RegexMatcher):
def _convert_match(self, match: Match) -> MatcherReturn:
groups = match.groupdict()
try:
meridiem = groups.pop("meridiem").lower()
except KeyError:
meridiem = None
params = self._convert_groups(groups)
if meridiem == "pm":
params["hour"] += 12
elif meridiem == "am" and params["hour"] == 12:
params["hour"] = 0
return MatcherReturn(params=params, end=match.end())
class WeekdayMatcher(Matcher): class WeekdayMatcher(Matcher):
regex: Pattern regex: Pattern
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from dateutil.relativedelta import MO, TU, WE, TH, FR, SA, SU from dateutil.relativedelta import MO, TU, WE, TH, FR, SA, SU
from .locale_util import Locales, Locale, RegexMatcher, WeekdayMatcher from .locale_util import Locales, Locale, RegexMatcher, WeekdayMatcher, TimeMatcher
locales: Locales = {} locales: Locales = {}
...@@ -47,15 +47,21 @@ locales["en_iso"] = Locale( ...@@ -47,15 +47,21 @@ locales["en_iso"] = Locale(
time=RegexMatcher(r"\s?(?:at\s)?" time=RegexMatcher(r"\s?(?:at\s)?"
r"(?P<hour>\d{2})" r"(?P<hour>\d{2})"
r"[:.](?P<minute>\d{2})" r"[:.](?P<minute>\d{2})"
r"(?:[:.](?P<second>\d{2}))?") r"(?:[:.](?P<second>\d{2}))?"),
) )
time_12_en = TimeMatcher(r"\s?(?:at\s)?"
r"(?P<hour>\d{2})"
r"(?:[:.](?P<minute>\d{2}))?"
r"(?:[:.](?P<second>\d{2}))?"
r"(?:\s(?P<meridiem>a\.?m|p\.?m)\.?)?")
locales["en_us"] = locales["en_iso"].replace( locales["en_us"] = locales["en_iso"].replace(
name="English (US)", 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=RegexMatcher(r"(?P<month>\d{1,2})/(?P<day>\d{1,2})(?:/(?P<year>\d{4}))?"))
locales["en_uk"] = locales["en_iso"].replace( locales["en_uk"] = locales["en_iso"].replace(
name="English (UK)", 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=RegexMatcher(r"(?P<day>\d{1,2})/(?P<month>\d{1,2})(?:/(?P<year>\d{4}))?"))
td_sep_fi = r"(?:[\s,]{1,3}(?:ja\s)?)" td_sep_fi = r"(?:[\s,]{1,3}(?:ja\s)?)"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment