Skip to content

Commit 6f44c59

Browse files
committed
duration: extract roundDuration helper and round consistently
Consolidates ad-hoc rounding (int(d.Hours()+0.5)) and integer truncation into a single roundDuration helper, so days/weeks/months/years all use round-half-up semantics. Adds day/week/month/year constants and extends tests for the new rounding behavior. Refs #6891 Signed-off-by: Sahil Singh <sahiilsiingh37@gmail.com>
1 parent 2bd057e commit 6f44c59

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

duration.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ import (
77
"time"
88
)
99

10+
const (
11+
day = 24 * time.Hour
12+
week = 7 * day
13+
month = 30 * day
14+
year = 365 * day
15+
)
16+
17+
func roundDuration(d, unit time.Duration) int {
18+
return int(float64(d)/float64(unit) + 0.5)
19+
}
20+
1021
// HumanDuration returns a human-readable approximation of a duration
1122
// (eg. "About a minute", "4 hours ago", etc.).
1223
func HumanDuration(d time.Duration) string {
@@ -20,16 +31,16 @@ func HumanDuration(d time.Duration) string {
2031
return "About a minute"
2132
} else if minutes < 60 {
2233
return fmt.Sprintf("%d minutes", minutes)
23-
} else if hours := int(d.Hours() + 0.5); hours == 1 {
34+
} else if hours := roundDuration(d, time.Hour); hours == 1 {
2435
return "About an hour"
2536
} else if hours < 48 {
2637
return fmt.Sprintf("%d hours", hours)
27-
} else if hours < 24*7*2 {
28-
return fmt.Sprintf("%d days", hours/24)
29-
} else if hours < 24*30*2 {
30-
return fmt.Sprintf("%d weeks", hours/24/7)
31-
} else if hours < 24*365*2 {
32-
return fmt.Sprintf("%d months", hours/24/30)
38+
} else if d < 2*week {
39+
return fmt.Sprintf("%d days", roundDuration(d, day))
40+
} else if d < 2*month {
41+
return fmt.Sprintf("%d weeks", roundDuration(d, week))
42+
} else if d < 2*year {
43+
return fmt.Sprintf("%d months", roundDuration(d, month))
3344
}
34-
return fmt.Sprintf("%d years", int(d.Hours())/24/365)
45+
return fmt.Sprintf("%d years", roundDuration(d, year))
3546
}

duration_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func ExampleHumanDuration() {
5757
// 7 days
5858
// 13 days
5959
// 2 weeks
60-
// 2 weeks
60+
// 3 weeks
6161
// 3 weeks
6262
// 4 weeks
6363
// 4 weeks
@@ -108,21 +108,28 @@ func TestHumanDuration(t *testing.T) {
108108
assertEquals(t, "2 days", HumanDuration(2*day))
109109
assertEquals(t, "7 days", HumanDuration(7*day))
110110
assertEquals(t, "13 days", HumanDuration(13*day+5*time.Hour))
111+
assertEquals(t, "3 days", HumanDuration(2*day+17*time.Hour))
112+
assertEquals(t, "14 days", HumanDuration(13*day+12*time.Hour))
111113
assertEquals(t, "2 weeks", HumanDuration(2*week))
112-
assertEquals(t, "2 weeks", HumanDuration(2*week+4*day))
114+
assertEquals(t, "3 weeks", HumanDuration(2*week+4*day))
113115
assertEquals(t, "3 weeks", HumanDuration(3*week))
116+
assertEquals(t, "4 weeks", HumanDuration(3*week+6*day))
114117
assertEquals(t, "4 weeks", HumanDuration(4*week))
115118
assertEquals(t, "4 weeks", HumanDuration(4*week+3*day))
116119
assertEquals(t, "4 weeks", HumanDuration(1*month))
120+
assertEquals(t, "8 weeks", HumanDuration(8*week+3*day))
117121
assertEquals(t, "6 weeks", HumanDuration(1*month+2*week))
118122
assertEquals(t, "2 months", HumanDuration(2*month))
119123
assertEquals(t, "2 months", HumanDuration(2*month+2*week))
120124
assertEquals(t, "3 months", HumanDuration(3*month))
121125
assertEquals(t, "3 months", HumanDuration(3*month+1*week))
126+
assertEquals(t, "4 months", HumanDuration(3*month+20*day))
122127
assertEquals(t, "5 months", HumanDuration(5*month+2*week))
123128
assertEquals(t, "13 months", HumanDuration(13*month))
124129
assertEquals(t, "23 months", HumanDuration(23*month))
125130
assertEquals(t, "24 months", HumanDuration(24*month))
126131
assertEquals(t, "2 years", HumanDuration(24*month+2*week))
127132
assertEquals(t, "3 years", HumanDuration(3*year+2*month))
133+
assertEquals(t, "4 years", HumanDuration(3*year+8*month))
134+
assertEquals(t, "4 years", HumanDuration(3*year+200*day))
128135
}

0 commit comments

Comments
 (0)