From c987110b8b14e45b267f35c4b0db3b17538e2329 Mon Sep 17 00:00:00 2001 From: Jo Vandeginste Date: Tue, 20 Feb 2024 15:06:30 +0100 Subject: [PATCH] Loose some detail in human duration Signed-off-by: Jo Vandeginste --- pkg/templatehelpers/duration.go | 22 +++++++++++++--------- pkg/templatehelpers/duration_test.go | 24 ++++++++++++++++-------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/pkg/templatehelpers/duration.go b/pkg/templatehelpers/duration.go index 3b18b25d..61223a2e 100644 --- a/pkg/templatehelpers/duration.go +++ b/pkg/templatehelpers/duration.go @@ -41,12 +41,16 @@ func (d *duration) String() string { components = append(components, fmt.Sprintf("%dh", d.Hours)) } - if d.Minutes > 0 { - components = append(components, fmt.Sprintf("%dm", d.Minutes)) - } - - if d.Seconds > 0 { - components = append(components, fmt.Sprintf("%ds", d.Seconds)) + if d.Days == 0 { + if d.Minutes > 0 { + components = append(components, fmt.Sprintf("%dm", d.Minutes)) + } + + if d.Hours == 0 { + if d.Seconds > 0 { + components = append(components, fmt.Sprintf("%ds", d.Seconds)) + } + } } return strings.Join(components, " ") @@ -64,7 +68,7 @@ func (d *duration) calculate() { return } - if d.duration < 60 { + if d.duration < 80 { d.Seconds = d.duration return } @@ -74,7 +78,7 @@ func (d *duration) calculate() { } func (d *duration) calculateMinutes(minutes int) { - if d.duration < 60 { + if minutes < 80 { d.Minutes = minutes return } @@ -84,7 +88,7 @@ func (d *duration) calculateMinutes(minutes int) { } func (d *duration) calculateHours(hours int) { - if d.duration < 24 { + if hours < 30 { d.Hours = hours return } diff --git a/pkg/templatehelpers/duration_test.go b/pkg/templatehelpers/duration_test.go index f4745102..9be7a4f0 100644 --- a/pkg/templatehelpers/duration_test.go +++ b/pkg/templatehelpers/duration_test.go @@ -9,18 +9,26 @@ import ( func TestHumanDuration(t *testing.T) { tests := map[time.Duration]string{ - 0: "0s", - 1 * time.Second: "1s", - 1 * time.Minute: "1m", - 1 * time.Hour: "1h", - 1*time.Hour + 1*time.Minute: "1h 1m", + 0: "0s", + 1 * time.Second: "1s", + 1 * time.Minute: "60s", + 1 * time.Hour: "60m", + 24 * time.Hour: "24h", - 1*time.Hour + 1*time.Minute + 1*time.Second: "1h 1m 1s", - 72*time.Hour + 30*time.Minute + 5*time.Second: "3d 30m 5s", - 100*time.Hour + 100*time.Second: "4d 4h 1m 40s", + 2 * time.Second: "2s", + 2 * time.Minute: "2m", + 2 * time.Hour: "2h", + 48 * time.Hour: "2d", + + 1*time.Hour + 1*time.Minute: "61m", + 1*time.Hour + 1*time.Minute + 1*time.Second: "61m 1s", + 25*time.Hour + 1*time.Minute + 1*time.Second: "25h 1m", + 72*time.Hour + 30*time.Minute + 5*time.Second: "3d", + 100*time.Hour + 100*time.Second: "4d 4h", } for d, expected := range tests { assert.Equal(t, expected, HumanDuration(d)) + assert.Equal(t, expected, HumanDuration(-d)) } }