aboutsummaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorGusted <williamzijl7@hotmail.com>2022-08-08 05:07:42 +0000
committerGitHub <noreply@github.com>2022-08-08 13:07:42 +0800
commit498352c210fc3adf0ab4243e2c56138657fce73b (patch)
treec7c975d50606c7d8b92ea26075570dd14108139d /modules
parent73e9854040917cbed3a0319098c3c4d4b54debcc (diff)
downloadgitea-498352c210fc3adf0ab4243e2c56138657fce73b.tar.gz
gitea-498352c210fc3adf0ab4243e2c56138657fce73b.zip
Fix SecToTime edge-cases (#20610)
Diffstat (limited to 'modules')
-rw-r--r--modules/util/sec_to_time.go20
-rw-r--r--modules/util/sec_to_time_test.go23
2 files changed, 33 insertions, 10 deletions
diff --git a/modules/util/sec_to_time.go b/modules/util/sec_to_time.go
index 9ce6fe1a13..13461915e6 100644
--- a/modules/util/sec_to_time.go
+++ b/modules/util/sec_to_time.go
@@ -18,10 +18,22 @@ import (
// 45677465s -> 1 year 6 months
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
+
+ // The following four variables are calculated by taking
+ // into account the previously calculated variables, this avoids
+ // pitfalls when using remainders. As that could lead to incorrect
+ // results when the calculated number equals the quotient number.
+ remainingDays := duration / (60 * 60 * 24)
+ years := remainingDays / 365
+ remainingDays -= years * 365
+ months := remainingDays * 12 / 365
+ remainingDays -= months * 365 / 12
+ weeks := remainingDays / 7
+ remainingDays -= weeks * 7
+ days := remainingDays
+
+ // The following three variables are calculated without depending
+ // on the previous calculated variables.
hours := (duration / 3600) % 24
minutes := (duration / 60) % 60
seconds := duration % 60
diff --git a/modules/util/sec_to_time_test.go b/modules/util/sec_to_time_test.go
index 854190462b..1e256aa865 100644
--- a/modules/util/sec_to_time_test.go
+++ b/modules/util/sec_to_time_test.go
@@ -11,10 +11,21 @@ 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")
+ second := int64(1)
+ minute := 60 * second
+ hour := 60 * minute
+ day := 24 * hour
+ year := 365 * day
+
+ assert.Equal(t, "1 minute 6 seconds", SecToTime(minute+6*second))
+ assert.Equal(t, "1 hour", SecToTime(hour))
+ assert.Equal(t, "1 hour", SecToTime(hour+second))
+ assert.Equal(t, "14 hours 33 minutes", SecToTime(14*hour+33*minute+30*second))
+ assert.Equal(t, "6 days 12 hours", SecToTime(6*day+12*hour+30*minute+18*second))
+ assert.Equal(t, "2 weeks 4 days", SecToTime((2*7+4)*day+2*hour+16*minute+58*second))
+ assert.Equal(t, "4 weeks", SecToTime(4*7*day))
+ assert.Equal(t, "4 weeks 1 day", SecToTime((4*7+1)*day))
+ assert.Equal(t, "1 month 2 weeks", SecToTime((6*7+3)*day+13*hour+38*minute+45*second))
+ assert.Equal(t, "11 months", SecToTime(year-25*day))
+ assert.Equal(t, "1 year 5 months", SecToTime(year+163*day+10*hour+11*minute+5*second))
}