From 7532e439b6b99b3fd191098990a34b01863e0c9e Mon Sep 17 00:00:00 2001 From: MAEDA Go Date: Wed, 14 Oct 2026 15:37:31 +0900 Subject: [PATCH 1/2] Fix add_working_days returning a date one working day too late when starting from a non-working day --- lib/redmine/utils/date_calculation.rb | 10 +++++++++- test/unit/lib/redmine/utils/date_calculation_test.rb | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/redmine/utils/date_calculation.rb b/lib/redmine/utils/date_calculation.rb index e0c045271..14dbfcde6 100644 --- a/lib/redmine/utils/date_calculation.rb +++ b/lib/redmine/utils/date_calculation.rb @@ -84,8 +84,16 @@ module Redmine # Adds working days to the given date, # taking only non working week days into account def add_working_week_days(date, working_days) - weeks = working_days / (7 - non_working_week_days.size) + # Advance by whole weeks first. This lands on the same week day as + # date, so it is a non-working day if date is a non-working day. + # Subtracting 1 prevents days_left from being 0 when working_days is + # a multiple of the working days per week, so the remainder loop + # below always runs and the result ends on the last working day it + # counts. + weeks = (working_days - 1) / (7 - non_working_week_days.size) result = weeks * 7 + # Remainder loop: advance day by day until the remaining working days + # are counted days_left = working_days - weeks * (7 - non_working_week_days.size) cwday = date.cwday while days_left > 0 diff --git a/test/unit/lib/redmine/utils/date_calculation_test.rb b/test/unit/lib/redmine/utils/date_calculation_test.rb index b4e276106..6ba39aff7 100644 --- a/test/unit/lib/redmine/utils/date_calculation_test.rb +++ b/test/unit/lib/redmine/utils/date_calculation_test.rb @@ -65,6 +65,9 @@ class Redmine::Utils::DateCalculationTest < ActiveSupport::TestCase assert_add_working_days '2012-10-31', '2012-10-10', 15 assert_add_working_days '2012-10-19', '2012-10-09', 8 assert_add_working_days '2012-10-23', '2012-10-11', 8 + # From Saturday and Sunday to the 5th working day, Friday 2012-10-26 + assert_add_working_days '2012-10-26', '2012-10-20', 5 + assert_add_working_days '2012-10-26', '2012-10-21', 5 end end -- 2.55.0