Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Remove seconds from output to prevent rounding errors

Aaron Contrerasdeb542e30b2f
diff --git a/app/services/duration_converter.rb b/app/services/duration_converter.rb
index 6e632812a1b..8d8a00e4893 100644
--- a/app/services/duration_converter.rb
+++ b/app/services/duration_converter.rb
@@ -81,12 +81,10 @@ class DurationConverter
class << self
def parse(duration_string)
- # Keep 0 values and convert the extracted duration to hours
- # by dividing by 3600.
-
+ # Assume the next logical unit to allow users to write
+ # durations such as "2h 1" assuming "1" is "1 minute"
last_unit_in_string = duration_string.scan(/[a-zA-Z]+/)
.last
-
default_unit = if last_unit_in_string
last_unit_in_string
.then { |last_unit| UNIT_ABBREVIATION_MAP[last_unit.downcase] }
@@ -104,7 +102,11 @@ class DurationConverter
def output(duration_in_hours)
return duration_in_hours if duration_in_hours.nil?
- duration_in_seconds = convert_duration_to_seconds(duration_in_hours)
+ # Prevents rounding errors when including seconds by chopping
+ # off the overflow seconds and keeping the nearest minute.
+ seconds = ((duration_in_hours * 3600) + 30).to_i
+ seconds_overflow = seconds % 60
+ seconds_to_the_nearest_minute = seconds - seconds_overflow
# return "0 h" if parsing 0.
# ChronicDuration returns nil when parsing 0.
@@ -112,10 +114,15 @@ class DurationConverter
# keeping zeroes, we'd format this as "0 secs".
#
# We want to override this behavior.
- if ChronicDuration.output(duration_in_seconds, default_unit: "hours", **duration_length_options).nil?
+ if ChronicDuration.output(seconds_to_the_nearest_minute,
+ default_unit: "hours",
+ **duration_length_options).nil?
"0h"
else
- ChronicDuration.output(duration_in_seconds, default_unit: "hours", format: :short, **duration_length_options)
+ ChronicDuration.output(seconds_to_the_nearest_minute,
+ default_unit: "hours",
+ format: :short,
+ **duration_length_options)
end
end
diff --git a/frontend/src/app/core/datetime/timezone.service.ts b/frontend/src/app/core/datetime/timezone.service.ts
index 64c49f5386d..8900b7e9edc 100644
--- a/frontend/src/app/core/datetime/timezone.service.ts
+++ b/frontend/src/app/core/datetime/timezone.service.ts
@@ -116,6 +116,10 @@ export class TimezoneService {
];
}
+ public toSeconds(durationString:string):number {
+ return Number(moment.duration(durationString).asSeconds().toFixed(2));
+ }
+
public toHours(durationString:string):number {
return Number(moment.duration(durationString).asHours().toFixed(2));
}
@@ -153,7 +157,12 @@ export class TimezoneService {
daysPerMonth: this.configurationService.daysPerMonth(),
weeks: true,
}):string {
- return outputChronicDuration(this.toHours(durationString) * 3600, opts) || '0h';
+ // Keep in sync with app/services/duration_converter#output
+ const seconds = this.toSeconds(durationString) + 30;
+ const secondsOverflow = seconds % 60;
+ const secondsToTheNearestMinute = seconds - secondsOverflow;
+
+ return outputChronicDuration(secondsToTheNearestMinute, opts) || '0h';
}
public formattedISODate(date:any):string {
diff --git a/spec/services/duration_converter_spec.rb b/spec/services/duration_converter_spec.rb
index 0ca04115c25..ae41cc833a2 100644
--- a/spec/services/duration_converter_spec.rb
+++ b/spec/services/duration_converter_spec.rb
@@ -67,9 +67,11 @@ RSpec.describe DurationConverter do
.to eq("5h 45m")
end
- it "handles floating point numbers gracefully" do
+ it "ignores seconds and keep the nearest minute" do
expect(described_class.output(0.28))
- .to eq("16m 48s")
+ .to eq("17m")
+ expect(described_class.output(2.23))
+ .to eq("2h 14m")
end
end
end