summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJun Omae <jun66j5@gmail.com>2022-05-01 16:33:41 +0900
committerAarni Koskela <akx@iki.fi>2022-05-10 08:53:12 +0200
commit128322c6b9ad23f4c1cd0b3996dd00fc48306750 (patch)
tree20c6511b9e7fb51d8a81c9caae97aa21c73fcb68
parentb203c672a265bc9d4e6b44cc46359d9414252736 (diff)
downloadbabel-128322c6b9ad23f4c1cd0b3996dd00fc48306750.tar.gz
Fix get_period_id() with `dayPeriodRule` across 0:00
-rw-r--r--babel/dates.py16
-rw-r--r--tests/test_day_periods.py3
2 files changed, 19 insertions, 0 deletions
diff --git a/babel/dates.py b/babel/dates.py
index 1386316..74fc6db 100644
--- a/babel/dates.py
+++ b/babel/dates.py
@@ -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.
@@ -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"]:
diff --git a/tests/test_day_periods.py b/tests/test_day_periods.py
index eb12b07..52cbc5e 100644
--- a/tests/test_day_periods.py
+++ b/tests/test_day_periods.py
@@ -10,6 +10,9 @@ import pytest
("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)