From 34c92cb220a6eeac40f1cb5b60241ddffca6c3ff Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Tue, 17 May 2016 09:21:46 +0300 Subject: [PATCH] Fix day periods to work with CLDR 29 data --- babel/dates.py | 12 ++++++++---- scripts/import_cldr.py | 2 ++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/babel/dates.py b/babel/dates.py index 4a0bbd3e..523e4562 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -1080,12 +1080,16 @@ def get_period_id(time, tzinfo=None, type=None, locale=LC_TIME): locale = Locale.parse(locale) # The LDML rules state that the rules may not overlap, so iterating in arbitrary - # order should be alright. - for rule_id, rules in locale.day_period_rules.get(type, {}).items(): + # order should be alright, though `at` periods should be preferred. + rulesets = locale.day_period_rules.get(type, {}).items() + + for rule_id, rules in rulesets: for rule in rules: if "at" in rule and rule["at"] == seconds_past_midnight: return rule_id + for rule_id, rules in rulesets: + for rule in rules: start_ok = end_ok = False if "from" in rule and seconds_past_midnight >= rule["from"]: @@ -1096,8 +1100,8 @@ def get_period_id(time, tzinfo=None, type=None, locale=LC_TIME): end_ok = True if "before" in rule and seconds_past_midnight < rule["before"]: end_ok = True - if "after" in rule and seconds_past_midnight > rule["after"]: - start_ok = True + if "after" in rule: + raise NotImplementedError("'after' is deprecated as of CLDR 29.") if start_ok and end_ok: return rule_id diff --git a/scripts/import_cldr.py b/scripts/import_cldr.py index 8de70436..b8041193 100755 --- a/scripts/import_cldr.py +++ b/scripts/import_cldr.py @@ -845,6 +845,8 @@ def parse_day_period_rules(tree): locales = rules.attrib["locales"].split() for rule in rules.findall("dayPeriodRule"): type = rule.attrib["type"] + if type in ("am", "pm"): # These fixed periods are handled separately by `get_period_id` + continue rule = _compact_dict(dict( (key, _time_to_seconds_past_midnight(rule.attrib.get(key))) for key in ("after", "at", "before", "from", "to") -- 2.47.2