Skip to content

Commit

Permalink
Fix get_period_id() with dayPeriodRule across 0:00
Browse files Browse the repository at this point in the history
  • Loading branch information
jun66j5 authored and akx committed May 10, 2022
1 parent b203c67 commit 128322c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
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

0 comments on commit 128322c

Please sign in to comment.