From 261063e69e80193ad563e086c515fd6e22e48464 Mon Sep 17 00:00:00 2001 From: Eike Rathke Date: Thu, 18 May 2023 23:30:51 +0200 Subject: Eliminate 24h loops in DateTime::operator+=/-=(tools::Time&) ... and repeated Date::operator++/--() that each calculate lcl_DaysToDate( GetAsNormalizedDays() + 1 ) where Date::AddDays() does it once. Also, that probably never worked correctly with negative time results >=24h Change-Id: Ic67aaa3d93e0d6533501d52671acf765e2d9bbdd Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151984 Reviewed-by: Eike Rathke Tested-by: Jenkins --- tools/source/datetime/datetime.cxx | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/tools/source/datetime/datetime.cxx b/tools/source/datetime/datetime.cxx index 00790ff78dd4..efdb928986c3 100644 --- a/tools/source/datetime/datetime.cxx +++ b/tools/source/datetime/datetime.cxx @@ -101,19 +101,20 @@ DateTime& DateTime::operator +=( const tools::Time& rTime ) sal_uInt16 nHours = aTime.GetHour(); if ( aTime.GetTime() > 0 ) { - while ( nHours >= 24 ) + if (nHours >= 24) { - Date::operator++(); - nHours -= 24; + AddDays( nHours / 24 ); + nHours %= 24; + aTime.SetHour( nHours ); } - aTime.SetHour( nHours ); } else if ( aTime.GetTime() != 0 ) { - while ( nHours >= 24 ) + if (nHours >= 24) { - Date::operator--(); - nHours -= 24; + AddDays( -static_cast(nHours) / 24 ); + nHours %= 24; + aTime.SetHour( nHours ); } Date::operator--(); aTime = Time( 24, 0, 0 )+aTime; @@ -130,19 +131,20 @@ DateTime& DateTime::operator -=( const tools::Time& rTime ) sal_uInt16 nHours = aTime.GetHour(); if ( aTime.GetTime() > 0 ) { - while ( nHours >= 24 ) + if (nHours >= 24) { - Date::operator++(); - nHours -= 24; + AddDays( nHours / 24 ); + nHours %= 24; + aTime.SetHour( nHours ); } - aTime.SetHour( nHours ); } else if ( aTime.GetTime() != 0 ) { - while ( nHours >= 24 ) + if (nHours >= 24) { - Date::operator--(); - nHours -= 24; + AddDays( -static_cast(nHours) / 24 ); + nHours %= 24; + aTime.SetHour( nHours ); } Date::operator--(); aTime = Time( 24, 0, 0 )+aTime; -- cgit