Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix get_period_id() with dayPeriodRule across 0:00 #871

Merged
merged 1 commit into from May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions babel/dates.py
Expand Up @@ -1113,6 +1113,12 @@ def get_period_id(time, tzinfo=None, type=None, locale=LC_TIME):
>>> get_period_names(locale="de")[get_period_id(time(7, 42), locale="de")]
u'Morgen'

>>> get_period_id(time(0), locale="en_US")
u'midnight'

>>> get_period_id(time(0), type="selection", locale="en_US")
u'night1'

:param time: The time to inspect.
:param tzinfo: The timezone for the time. See ``format_time``.
:param type: The period type to use. Either "selection" or None.
Expand All @@ -1136,6 +1142,16 @@ def get_period_id(time, tzinfo=None, type=None, locale=LC_TIME):

for rule_id, rules in rulesets:
for rule in rules:
if "from" in rule and "before" in rule:
if rule["from"] < rule["before"]:
if rule["from"] <= seconds_past_midnight < rule["before"]:
return rule_id
else:
# e.g. from="21:00" before="06:00"
if rule["from"] <= seconds_past_midnight < 86400 or \
0 <= seconds_past_midnight < rule["before"]:
return rule_id

start_ok = end_ok = False

if "from" in rule and seconds_past_midnight >= rule["from"]:
Expand Down
3 changes: 3 additions & 0 deletions tests/test_day_periods.py
Expand Up @@ -10,6 +10,9 @@
("de", time(3, 11), "night1"), # (after, before)
("fi", time(0), "midnight"), # (at)
("en_US", time(12), "noon"), # (at)
("en_US", time(21), "night1"), # (from, before) across 0:00
("en_US", time(5), "night1"), # (from, before) across 0:00
("en_US", time(6), "morning1"), # (from, before)
("agq", time(10), "am"), # no periods defined
("agq", time(22), "pm"), # no periods defined
("am", time(14), "afternoon1"), # (before, after)
Expand Down