Skip to content

Commit

Permalink
Clarify existing upper bound behavior of AmbiguousDate.range()
Browse files Browse the repository at this point in the history
  • Loading branch information
victorlin committed Mar 29, 2023
1 parent ca0f042 commit 7a7d3b7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 6 additions & 0 deletions augur/dates/ambiguous_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def __init__(self, uncertain_date, fmt="%Y-%m-%d", min_max_year=None):
self.assert_only_less_significant_uncertainty()

def range(self):
"""Return the range of possible dates defined by the ambiguous date.
Impose an upper limit of today's date.
"""
min_date = tuple_to_date(
resolve_uncertain_int(self.uncertain_date_components["Y"], "min"),
resolve_uncertain_int(self.uncertain_date_components["m"], "min"),
Expand All @@ -60,6 +64,8 @@ def range(self):
resolve_uncertain_int(self.uncertain_date_components["m"], "max"),
resolve_uncertain_int(self.uncertain_date_components["d"], "max"),
)

# Limit the max date to be no later than today's date.
max_date = min(max_date, datetime.date.today())

return (min_date, max_date)
Expand Down
7 changes: 4 additions & 3 deletions tests/dates/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ def test_get_numerical_date_from_value_ambiguous_month_and_day(self):

@freeze_time("2000-02-20")
def test_get_numerical_date_from_value_current_day_limit(self):
min_date, max_date = dates.get_numerical_date_from_value("2000-02-XX", "%Y-%m-%d")
min_date, max_date = dates.get_numerical_date_from_value("2000-03-XX", "%Y-%m-%d")
# min_date is not subject to the upper limit
assert (min_date
== pytest.approx(dates.numeric_date(datetime.date(year=2000, month=2, day=1)), abs=1e-3)
== pytest.approx(2000.086, abs=1e-3)
== pytest.approx(dates.numeric_date(datetime.date(year=2000, month=3, day=1)), abs=1e-3)
== pytest.approx(2000.165, abs=1e-3)
)
assert (max_date
== pytest.approx(dates.numeric_date(datetime.date(year=2000, month=2, day=20)), abs=1e-3)
Expand Down

0 comments on commit 7a7d3b7

Please sign in to comment.