Skip to content

Incorrect handling of durations in SecToTime() #20589

Closed
@edgar-bonet

Description

@edgar-bonet

Description

Pull request #18863 introduced the following code

years := duration / (3600 * 24 * 7 * 4 * 12)
months := (duration / (3600 * 24 * 30)) % 12
weeks := (duration / (3600 * 24 * 7)) % 4
days := (duration / (3600 * 24)) % 7

This is wrong for a couple of reasons:

  1. the week count reverts to zero (because of the modulus operation) after 4 weeks, which is not quite the same as a month

  2. these “years” are 336 days long, which is about 11 months

The consequence is that this function displays:

  • a duration of 340 days (about 11.2 months) as “1 year 11 months”

  • a duration of 29 days as “1 day”

  • a duration of 28 days as the empty string.

See the attached screenshot for the effect of this bug on the display of tracked time.

I suggest to instead use something along these lines:

years := (duration / 86400) / 365
months := (duration / 86400 - years * 365)  / 30
weeks := (duration / 86400 - years * 365 - month * 30) / 7
days := duration / 86400 - years * 365 - month * 30 - weeks * 7

I am not submitting a pull request because I have zero experience with the Go programming language.

Gitea Version

1.18.0+dev-196-ge56005f90

Can you reproduce the bug on the Gitea demo site?

Yes

Log Gist

No response

Screenshots

time_tracker
Screenshot from this test issue.

Git Version

No response

Operating System

No response

How are you running Gitea?

using https://try.gitea.io/

Database

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions