From 2f75c131d9ee75073e2b1193b782addf596ee3a9 Mon Sep 17 00:00:00 2001 From: Ronnie Dutta <61982285+MetRonnie@users.noreply.github.com> Date: Tue, 16 Jan 2024 16:21:49 +0000 Subject: [PATCH] Optimise addition of truncated TimePoint --- metomi/isodatetime/data.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/metomi/isodatetime/data.py b/metomi/isodatetime/data.py index 533e967..faf4771 100644 --- a/metomi/isodatetime/data.py +++ b/metomi/isodatetime/data.py @@ -1511,17 +1511,22 @@ def add_truncated(self, other: 'TimePoint') -> 'TimePoint': if second_of_minute is not None or minute_of_hour is not None: new = new.to_hour_minute_second() if second_of_minute is not None: - while new._second_of_minute != second_of_minute: - new._second_of_minute += 1.0 - new._tick_over() + new._second_of_minute += ( + (second_of_minute - new._second_of_minute) + % CALENDAR.SECONDS_IN_MINUTE + ) + new._tick_over() if minute_of_hour is not None: - while new._minute_of_hour != minute_of_hour: - new._minute_of_hour += 1.0 - new._tick_over() + new._minute_of_hour += ( + (minute_of_hour - new._minute_of_hour) + % CALENDAR.MINUTES_IN_HOUR + ) + new._tick_over() if hour_of_day is not None: - while new._hour_of_day != hour_of_day: - new._hour_of_day += 1.0 - new._tick_over() + new._hour_of_day += ( + (hour_of_day - new._hour_of_day) % CALENDAR.HOURS_IN_DAY + ) + new._tick_over() if day_of_week is not None: new = new.to_week_date()