Project

General

Profile

Actions

Defect #42172

closed

`format_hours` method produces incorrect output for negative time values when `Setting.timespan_format` is "minutes"

Added by Steven Jones 8 days ago. Updated 4 days ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Time tracking
Target version:
Start date:
Due date:
% Done:

0%

Estimated time:
Resolution:
Fixed
Affected version:

Description

Following on from #36897 we've spotted a strange formatting issue with time that is negative.

In our use-case we have some 'contracts' that are assigned a project and say have 40 hours a month.

As we log time against a project, we use-up this time assigned to the support contract. So suppose we have done 25 hours of work, then we want to display the obvious calculation:

Start of month 40:00
Used time 25:00
Balance 15:00

We use the format_hours method to display this.

When we've done more than 40 hours of work however, this causes issues, specifically, assume that we've done 40 hours 15 minutes of work in a month, the calculation with Redmine 6.0.2 (and our custom code) looks like this:

Start of month 40:00
Used time 40:15
Balance -1:45

Which to my eyes looks like we've done 1 hour 45 minutes over the contract.

This is because of the semantics of Ruby modulo arithmetic:

And this is purely because of the formatting of the number.

If we changed the display of Time spans to fractional our table looks like this:

Start of month 40.00
Used time 40.25
Balance -0.25

Which makes more sense to me.

I think the fix is pretty simple, I think that one could add a simple line like this:

# If the timespan is negative, perform the calculation on the positive value.
return "-" + format_hours(-hours) if hours < 0

To the top of the method.

And then my table would look like this:

Start of month 40:00
Used time 40:15
Balance -0:15

Which makes much more sense, I think, for a time span.


Files

42172.patch (1.43 KB) 42172.patch Go MAEDA, 2025-01-29 02:45

Related issues

Related to Redmine - Defect #36897: The minutes part of a time entry is displayed as 60 instead of being carried overClosedGo MAEDA

Actions
Actions #1

Updated by Go MAEDA 7 days ago

  • Status changed from New to Confirmed
Actions #2

Updated by Go MAEDA 7 days ago

  • Related to Defect #36897: The minutes part of a time entry is displayed as 60 instead of being carried over added
Actions #3

Updated by Go MAEDA 7 days ago

Thank you for reporting the issue.

The attached patch fixes the behavior of Redmine::I18n.format_hours when it is given a negative value. Previously, negative values were not handled correctly, resulting in incorrect outputs such as -1:45 for -0.25 hours. This patch ensures that negative values are formatted as expected, for example, -0:15 for -0.25 hours.

Actions #4

Updated by Steven Jones 6 days ago

I can confirm that this patch fixes the issue, thanks!

Actions #5

Updated by Go MAEDA 6 days ago

  • Subject changed from Negative time can be formatted incorrectly to `format_hours` method produces incorrect output for negative time values when Setting.timespan_format is "minutes"
Actions #6

Updated by Go MAEDA 6 days ago

  • Subject changed from `format_hours` method produces incorrect output for negative time values when Setting.timespan_format is "minutes" to `format_hours` method produces incorrect output for negative time values when `Setting.timespan_format` is "minutes"
Actions #7

Updated by Go MAEDA 5 days ago

  • Status changed from Confirmed to Resolved
  • Assignee set to Go MAEDA
  • Resolution set to Fixed

Committed the fix in r23482.

Actions #8

Updated by Go MAEDA 4 days ago

  • Status changed from Resolved to Closed

Merged the fix into the 6.0-stable branch in r23483.

Actions

Also available in: Atom PDF