Skip to content

Commit

Permalink
Truncated timepoint addition returns next matching point instead of t…
Browse files Browse the repository at this point in the history
…he original point if the two already match
  • Loading branch information
MetRonnie committed Jul 27, 2023
1 parent caa9e0e commit 2833ad2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
25 changes: 16 additions & 9 deletions metomi/isodatetime/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1572,7 +1572,7 @@ def find_next_month_and_day(
def find_next_month_and_day(
self, month: Optional[int], day: Optional[int]
) -> 'TimePoint':
"""Return the next TimePoint after this one (inclusive) that has the
"""Return the next TimePoint after this one that has the
same month and/or day as specified.
Args:
Expand All @@ -1594,18 +1594,25 @@ def _next_month_and_day(
for i, year in enumerate(years_to_check):
self._year = year
if month:
if month >= self._month_of_year and (
day is None or
self._day_of_month <= day <= get_days_in_month(month, year)
):
self._month_of_year = month
self._day_of_month = day or 1
return
if day is None:
if month > self._month_of_year:
self._month_of_year = month
self._day_of_month = 1
return
else:
if month >= self._month_of_year and (
self._day_of_month < day <= get_days_in_month(
month, year
)
):
self._month_of_year = month
self._day_of_month = day
return
else:
for month_ in range(
self._month_of_year, CALENDAR.MONTHS_IN_YEAR + 1
):
if self._day_of_month <= day <= get_days_in_month(
if self._day_of_month < day <= get_days_in_month(
month_, year
):
self._month_of_year = month_
Expand Down
12 changes: 11 additions & 1 deletion metomi/isodatetime/tests/test_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,13 +720,18 @@ def tp_add_param(timepoint, other, expected):
tp_add_param(
data.TimePoint(year=2000, day_of_month=15),
data.TimePoint(month_of_year=1, day_of_month=15, truncated=True),
data.TimePoint(year=2000, day_of_month=15),
data.TimePoint(year=2001, day_of_month=15),
),
tp_add_param(
data.TimePoint(year=2000, day_of_month=15),
data.TimePoint(month_of_year=1, day_of_month=14, truncated=True),
data.TimePoint(year=2001, day_of_month=14),
),
tp_add_param(
data.TimePoint(year=2000, day_of_month=31),
data.TimePoint(day_of_month=31, truncated=True),
data.TimePoint(year=2000, month_of_year=3, day_of_month=31),
),
tp_add_param(
data.TimePoint(year=2000, day_of_month=15),
data.TimePoint(day_of_month=14, truncated=True),
Expand Down Expand Up @@ -769,6 +774,11 @@ def test_timepoint_add(
'-11-02',
data.TimePoint(year=2011, month_of_year=2, day_of_month=1)
),
tp_add_param(
'2014-04-10T00Z',
'-14-04',
data.TimePoint(year=2114, month_of_year=4, day_of_month=1)
),
]
)
def test_timepoint_add__extra(
Expand Down

0 comments on commit 2833ad2

Please sign in to comment.