From 681b125c002cc8da2b065f124c7618363dfc498e Mon Sep 17 00:00:00 2001 From: Ronnie Dutta <61982285+MetRonnie@users.noreply.github.com> Date: Thu, 27 Jul 2023 13:56:48 +0100 Subject: [PATCH] Truncated timepoint addition returns next matching point instead of the original point if the two already match --- metomi/isodatetime/data.py | 25 ++++++++++++++++--------- metomi/isodatetime/tests/test_01.py | 12 +++++++++++- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/metomi/isodatetime/data.py b/metomi/isodatetime/data.py index 0ae9e4f..a7dd436 100644 --- a/metomi/isodatetime/data.py +++ b/metomi/isodatetime/data.py @@ -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: @@ -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_ diff --git a/metomi/isodatetime/tests/test_01.py b/metomi/isodatetime/tests/test_01.py index 1f6defe..91f313d 100644 --- a/metomi/isodatetime/tests/test_01.py +++ b/metomi/isodatetime/tests/test_01.py @@ -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), @@ -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(