Skip to content

Commit fb0a2f0

Browse files
committed
Optimise addition of truncated TimePoint
1 parent 604b3a9 commit fb0a2f0

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

metomi/isodatetime/data.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,17 +1511,22 @@ def add_truncated(self, other: 'TimePoint') -> 'TimePoint':
15111511
if second_of_minute is not None or minute_of_hour is not None:
15121512
new = new.to_hour_minute_second()
15131513
if second_of_minute is not None:
1514-
while new._second_of_minute != second_of_minute:
1515-
new._second_of_minute += 1.0
1516-
new._tick_over()
1514+
new._second_of_minute += (
1515+
(second_of_minute - new._second_of_minute)
1516+
% CALENDAR.SECONDS_IN_MINUTE
1517+
)
1518+
new._tick_over()
15171519
if minute_of_hour is not None:
1518-
while new._minute_of_hour != minute_of_hour:
1519-
new._minute_of_hour += 1.0
1520-
new._tick_over()
1520+
new._minute_of_hour += (
1521+
(minute_of_hour - new._minute_of_hour)
1522+
% CALENDAR.MINUTES_IN_HOUR
1523+
)
1524+
new._tick_over()
15211525
if hour_of_day is not None:
1522-
while new._hour_of_day != hour_of_day:
1523-
new._hour_of_day += 1.0
1524-
new._tick_over()
1526+
new._hour_of_day += (
1527+
(hour_of_day - new._hour_of_day) % CALENDAR.HOURS_IN_DAY
1528+
)
1529+
new._tick_over()
15251530

15261531
if day_of_week is not None:
15271532
new = new.to_week_date()

0 commit comments

Comments
 (0)