accounting: limit length of ETA string
No need to report hours, minutes, and even seconds when the ETA is several years, e.g. "292y24w3d23h47m16s". Now only reports the 3 most significant units, sacrificing precision, e.g. "292y24w3d", "24w3d23h", "3d23h47m", "23h47m16s". Fixes #6381
This commit is contained in:
parent
67132ecaec
commit
0328878e46
4 changed files with 56 additions and 27 deletions
|
@ -108,38 +108,44 @@ func TestDurationString(t *testing.T) {
|
|||
|
||||
func TestDurationReadableString(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
negative bool
|
||||
in time.Duration
|
||||
want string
|
||||
negative bool
|
||||
in time.Duration
|
||||
wantLong string
|
||||
wantShort string
|
||||
}{
|
||||
// Edge Cases
|
||||
{false, time.Duration(DurationOff), "off"},
|
||||
{false, time.Duration(DurationOff), "off", "off"},
|
||||
// Base Cases
|
||||
{false, time.Duration(0), "0s"},
|
||||
{true, time.Millisecond, "1ms"},
|
||||
{true, time.Second, "1s"},
|
||||
{true, time.Minute, "1m"},
|
||||
{true, (3 * time.Minute) / 2, "1m30s"},
|
||||
{true, time.Hour, "1h"},
|
||||
{true, time.Hour * 24, "1d"},
|
||||
{true, time.Hour * 24 * 7, "1w"},
|
||||
{true, time.Hour * 24 * 365, "1y"},
|
||||
{false, time.Duration(0), "0s", "0s"},
|
||||
{true, time.Millisecond, "1ms", "1ms"},
|
||||
{true, time.Second, "1s", "1s"},
|
||||
{true, time.Minute, "1m", "1m"},
|
||||
{true, (3 * time.Minute) / 2, "1m30s", "1m30s"},
|
||||
{true, time.Hour, "1h", "1h"},
|
||||
{true, time.Hour * 24, "1d", "1d"},
|
||||
{true, time.Hour * 24 * 7, "1w", "1w"},
|
||||
{true, time.Hour * 24 * 365, "1y", "1y"},
|
||||
// Composite Cases
|
||||
{true, time.Hour + 2*time.Minute + 3*time.Second, "1h2m3s"},
|
||||
{true, time.Hour * 24 * (365 + 14), "1y2w"},
|
||||
{true, time.Hour*24*4 + time.Hour*3 + time.Minute*2 + time.Second, "4d3h2m1s"},
|
||||
{true, time.Hour * 24 * (365*3 + 7*2 + 1), "3y2w1d"},
|
||||
{true, time.Hour*24*(365*3+7*2+1) + time.Hour*2 + time.Second, "3y2w1d2h1s"},
|
||||
{true, time.Hour*24*(365*3+7*2+1) + time.Second, "3y2w1d1s"},
|
||||
{true, time.Hour*24*(365+7*2+3) + time.Hour*4 + time.Minute*5 + time.Second*6 + time.Millisecond*7, "1y2w3d4h5m6s7ms"},
|
||||
{true, time.Hour + 2*time.Minute + 3*time.Second, "1h2m3s", "1h2m3s"},
|
||||
{true, time.Hour * 24 * (365 + 14), "1y2w", "1y2w"},
|
||||
{true, time.Hour*24*4 + time.Hour*3 + time.Minute*2 + time.Second, "4d3h2m1s", "4d3h2m"},
|
||||
{true, time.Hour * 24 * (365*3 + 7*2 + 1), "3y2w1d", "3y2w1d"},
|
||||
{true, time.Hour*24*(365*3+7*2+1) + time.Hour*2 + time.Second, "3y2w1d2h1s", "3y2w1d"},
|
||||
{true, time.Hour*24*(365*3+7*2+1) + time.Second, "3y2w1d1s", "3y2w1d"},
|
||||
{true, time.Hour*24*(365+7*2+3) + time.Hour*4 + time.Minute*5 + time.Second*6 + time.Millisecond*7, "1y2w3d4h5m6s7ms", "1y2w3d"},
|
||||
{true, time.Duration(DurationOff) / time.Millisecond * time.Millisecond, "292y24w3d23h47m16s853ms", "292y24w3d"}, // Should have been 854ms but some precision are lost with floating point calculations
|
||||
} {
|
||||
got := Duration(test.in).ReadableString()
|
||||
assert.Equal(t, test.want, got)
|
||||
assert.Equal(t, test.wantLong, got)
|
||||
got = Duration(test.in).ShortReadableString()
|
||||
assert.Equal(t, test.wantShort, got)
|
||||
|
||||
// Test Negative Case
|
||||
if test.negative {
|
||||
got = Duration(-test.in).ReadableString()
|
||||
assert.Equal(t, "-"+test.want, got)
|
||||
assert.Equal(t, "-"+test.wantLong, got)
|
||||
got = Duration(-test.in).ShortReadableString()
|
||||
assert.Equal(t, "-"+test.wantShort, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue