-
-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(deps): remove qdm12/golibs dependency
- Implement friendly duration formatting locally
- Loading branch information
Showing
8 changed files
with
105 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,9 @@ | ||
package cli | ||
|
||
import "github.com/qdm12/golibs/logging" | ||
|
||
type noopLogger struct{} | ||
|
||
func newNoopLogger() *noopLogger { | ||
return new(noopLogger) | ||
} | ||
|
||
func (l *noopLogger) Debug(string) {} | ||
func (l *noopLogger) Info(string) {} | ||
func (l *noopLogger) Warn(string) {} | ||
func (l *noopLogger) Error(string) {} | ||
func (l *noopLogger) PatchLevel(logging.Level) {} | ||
func (l *noopLogger) PatchPrefix(string) {} | ||
func (l *noopLogger) Info(string) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package format | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// FriendlyDuration formats a duration in an approximate, human friendly duration. | ||
// For example 55 hours will result in "2 days". | ||
func FriendlyDuration(duration time.Duration) string { | ||
const twoDays = 48 * time.Hour | ||
switch { | ||
case duration < time.Minute: | ||
seconds := int(duration.Round(time.Second).Seconds()) | ||
const two = 2 | ||
if seconds < two { | ||
return fmt.Sprintf("%d second", seconds) | ||
} | ||
return fmt.Sprintf("%d seconds", seconds) | ||
case duration <= time.Hour: | ||
minutes := int(duration.Round(time.Minute).Minutes()) | ||
if minutes == 1 { | ||
return "1 minute" | ||
} | ||
return fmt.Sprintf("%d minutes", minutes) | ||
case duration < twoDays: | ||
hours := int(duration.Truncate(time.Hour).Hours()) | ||
return fmt.Sprintf("%d hours", hours) | ||
default: | ||
const hoursInDay = 24 | ||
days := int(duration.Truncate(time.Hour).Hours() / hoursInDay) | ||
return fmt.Sprintf("%d days", days) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package format | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_FriendlyDuration(t *testing.T) { | ||
t.Parallel() | ||
testCases := map[string]struct { | ||
duration time.Duration | ||
friendly string | ||
}{ | ||
"zero": { | ||
friendly: "0 second", | ||
}, | ||
"one_second": { | ||
duration: time.Second, | ||
friendly: "1 second", | ||
}, | ||
"59_seconds": { | ||
duration: 59 * time.Second, | ||
friendly: "59 seconds", | ||
}, | ||
"1_minute": { | ||
duration: time.Minute, | ||
friendly: "1 minute", | ||
}, | ||
"2_minutes": { | ||
duration: 2 * time.Minute, | ||
friendly: "2 minutes", | ||
}, | ||
"1_hour": { | ||
duration: time.Hour, | ||
friendly: "60 minutes", | ||
}, | ||
"2_hours": { | ||
duration: 2 * time.Hour, | ||
friendly: "2 hours", | ||
}, | ||
"26_hours": { | ||
duration: 26 * time.Hour, | ||
friendly: "26 hours", | ||
}, | ||
"28_hours": { | ||
duration: 28 * time.Hour, | ||
friendly: "28 hours", | ||
}, | ||
"55_hours": { | ||
duration: 55 * time.Hour, | ||
friendly: "2 days", | ||
}, | ||
} | ||
for name, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
s := FriendlyDuration(testCase.duration) | ||
|
||
assert.Equal(t, testCase.friendly, s) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters