# Bug OP-19969 - time_entries hours-to-duration serialization truncates instead of rounds

Project: OpenProject
Status: closed
URL: https://community.openproject.org/work_packages/OP-19969

## Open points

_No work packages_

## Details

| Attribute | Value |
| --- | --- |
| Date |  |
| Priority | Medium |
| Module | Modules and project features / Time tracking, API |
| Target versions | 17.9.0 |
| Observed in versions |  |
| Sprint |  |
| Backlog bucket |  |
| Bug found in version (deprecated) |  |
| Found in mobile app version |  |
| Follow-up feedback e-mail |  |
| Requires doc change | No |
| Accessibility | No |
| Pull preview |  |
| List |  |
| Labels |  |

## People

| Attribute | Value |
| --- | --- |
| Assignee |  |
| Accountable |  |
| Product manager |  |
| Developers | Oliver Günther |
| Designer |  |
| Ordered by |  |
| Signed off by |  |
| Interest by |  |

## Planning

| Attribute | Value |
| --- | --- |
| Relevance |  |

## Costs

| Attribute | Value |
| --- | --- |
| Votes |  |
| Work |  |
| Labor costs |  |

## Description

### Before filing a report

Confirmed reproducible on the latest available patch release of the 17.7 minor line (17.7.1, the latest at the time of testing) via direct Rails-console reproduction and a full API round-trip. Searched community.openproject.org for existing reports (by "time entries duration rounding hours", "Duration.new truncate seconds", "format_duration_from_hours precision") — found no existing report covering this. A minimal, verified fix is proposed below, also submitted as a pull request: https://github.com/opf/openproject/pull/24776

### Steps to reproduce

1. Login as a user with permissions to log time on a work package.
2. Send `POST /api/v3/time_entries` with `hours` set to a fractional-second ISO 8601 duration string, e.g. `hours: "PT5H30M3.6S"` (parses to 5.501 hours -- this exposes the truncation during serialization when the value is read back).
3. Server-side (Rails console) reproduction, independent of any client formatting quirks:
   ```ruby
   ::API::V3::Utilities::DateTimeFormatter.format_duration_from_hours(5.501)
   # => "PT5H30M3S"
   ```

### What is the buggy behavior?

`5.501` hours is exactly `19803.6` seconds (`5.501 * 3600`). The correct rounded value is `19804` seconds (`PT5H30M4S`); the API instead returns `"PT5H30M3S"` -- the fractional `0.6` second is silently dropped rather than rounded, losing up to just under 1 full second of precision on any affected value that has a fractional-second remainder.

Root cause: `lib/api/v3/utilities/date_time_formatter.rb`'s `format_duration_from_hours` calls `Duration.new(seconds: hours * 3600).iso8601`. The third-party `ruby-duration` gem's `Duration#initialize`, when given a Hash argument, computes `@seconds += args[unit].to_i * multiple` for each unit key -- i.e. it calls Ruby's `Float#to_i` (truncation toward zero) on the raw seconds value, not `.round`. This is a truncation bug in the `ruby-duration` gem's own hash-based constructor path, which OpenProject's own formatter does not compensate for.

This affects every caller of `format_duration_from_hours`, not just time entries: `lib/api/v3/work_packages/work_package_representer.rb`, `lib/api/v3/work_packages/work_package_sums_representer.rb`, `modules/meeting/lib/api/v3/meetings/meeting_representer.rb`, `modules/costs/lib/api/v3/time_entries/time_entry_representer.rb`, and `app/controllers/work_packages/progress_controller.rb` all use the same shared formatter and are all equally affected.

### What is the expected behavior?

`format_duration_from_hours` should round to the nearest second rather than truncating, so `5.501` hours (`19803.6s`) correctly serializes as `"PT5H30M4S"`, matching the value that would be stored/expected from straightforward rounding.

### Logs

Not applicable — no error is raised; the bug is silent precision loss in a formatted value, not an exception.

### Screenshots and other files

None — this is a pure backend/API bug with no UI reproduction path tested; the Rails-console and REST API reproductions above are sufficient to demonstrate it independent of any client.

### Environment information

**OpenProject installation type**

* Docker All-in-one container (official `openproject/openproject` image: `17.7.1`)

**OpenProject version**

Reproduced on `17.7.1` (the latest release in the 17.7 line at the time of testing). Confirmed present unchanged in source on the `dev` branch (checked against the full source checkout).

**Browser**

Not applicable — reproduced via direct Rails console and REST API calls, no browser involved.

**Operating System**

Not applicable — server-side bug, reproduced against a Linux-based Docker container; the client OS is irrelevant to the reproduction.

**Language**

Not applicable — no UI/browser interaction involved in the reproduction.
