diff options
author | Aarni Koskela <akx@iki.fi> | 2016-05-17 09:21:46 +0300 |
---|---|---|
committer | Aarni Koskela <akx@iki.fi> | 2016-06-22 10:05:57 +0300 |
commit | 34c92cb220a6eeac40f1cb5b60241ddffca6c3ff (patch) | |
tree | a4fb404c667398a62c17c4d58ccbc1161c3b8820 | |
parent | cf1e50648c4840c405dfffa874801e5ff70b1a48 (diff) | |
download | babel-34c92cb220a6eeac40f1cb5b60241ddffca6c3ff.tar.gz |
Fix day periods to work with CLDR 29 data
-rw-r--r-- | babel/dates.py | 12 | ||||
-rwxr-xr-x | scripts/import_cldr.py | 2 |
2 files changed, 10 insertions, 4 deletions
diff --git a/babel/dates.py b/babel/dates.py index 4a0bbd3..523e456 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 8de7043..b804119 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") |