Skip to content

Commit

Permalink
Fix SecToTime edge-cases (go-gitea#20610)
Browse files Browse the repository at this point in the history
- Backport of go-gitea#20610
  - Currently there are edge-cases where the code fails to produce the correct results, this is mainly due to the misusage of the modulo operator. Well this could be fixed, it's better to use a more performant way and simply distract the previous number's cost to calculate the variables.
  - Resolves go-gitea#20589
  • Loading branch information
Gusted committed Aug 2, 2022
1 parent 51c8c0f commit 06738ea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
20 changes: 16 additions & 4 deletions modules/util/sec_to_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@ import (
// 1563418 -> 2 weeks 4 days
// 3937125s -> 1 month 2 weeks
// 45677465s -> 1 year 6 months
//
// Magic numbers:
// 3600 = 60 * 60 (amount of seconds in a hour)
// 86400 = 60 * 60 * 24 (amount of seconds in a day)
func SecToTime(duration int64) string {
formattedTime := ""
years := duration / (3600 * 24 * 7 * 4 * 12)
months := (duration / (3600 * 24 * 30)) % 12
weeks := (duration / (3600 * 24 * 7)) % 4
days := (duration / (3600 * 24)) % 7
years := (duration / 86400) / 365

// The following three variables are calculated with taking
// into account the previous calculated variables, this avoids
// pitfalls when using remainders. As that could lead to incorrect
// results when the calculated number equals the quotient number.
months := (duration/86400 - years*365) / 30
weeks := (duration/86400 - years*365 - months*30) / 7
days := duration/86400 - years*365 - months*30 - weeks*7

// The following three variables are calculated without depending
// on the previous calculated variables.
hours := (duration / 3600) % 24
minutes := (duration / 60) % 60
seconds := duration % 60
Expand Down
17 changes: 11 additions & 6 deletions modules/util/sec_to_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ import (
)

func TestSecToTime(t *testing.T) {
assert.Equal(t, SecToTime(66), "1 minute 6 seconds")
assert.Equal(t, SecToTime(52410), "14 hours 33 minutes")
assert.Equal(t, SecToTime(563418), "6 days 12 hours")
assert.Equal(t, SecToTime(1563418), "2 weeks 4 days")
assert.Equal(t, SecToTime(3937125), "1 month 2 weeks")
assert.Equal(t, SecToTime(45677465), "1 year 5 months")
assert.Equal(t, "1 minute 6 seconds", SecToTime(66))
assert.Equal(t, "1 hour", SecToTime(3600))
assert.Equal(t, "1 hour", SecToTime(3601))
assert.Equal(t, "14 hours 33 minutes", SecToTime(52410))
assert.Equal(t, "6 days 12 hours", SecToTime(563418))
assert.Equal(t, "2 weeks 4 days", SecToTime(1563418))
assert.Equal(t, "4 weeks", SecToTime(2419200))
assert.Equal(t, "4 weeks 1 day", SecToTime(2505600))
assert.Equal(t, "1 month 2 weeks", SecToTime(3937125))
assert.Equal(t, "11 months 1 week", SecToTime(29376000))
assert.Equal(t, "1 year 5 months", SecToTime(45677465))
}

0 comments on commit 06738ea

Please sign in to comment.