这是indexloc提供的服务,不要输入任何密码
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,8 @@ func (b *Bar) TimeElapsed() time.Duration {
func (b *Bar) TimeElapsedString() string {
return strutil.PrettyTime(b.TimeElapsed())
}

// TimeElapsedStringFormat returns the formatted string represenation of the time elapsed in the specified time unit
func (b *Bar) TimeElapsedStringFormat(f time.Duration) string {
return strutil.PrettyTimeFormat(b.TimeElapsed(), f)
}
8 changes: 8 additions & 0 deletions util/strutil/strutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,11 @@ func PrettyTime(t time.Duration) string {
}
return (t - (t % time.Second)).String()
}

// PrettyTimeFormat returns the string representation of the duration. It rounds the time duration to the specified unit and returns a "---" when duration is 0
func PrettyTimeFormat(t time.Duration, f time.Duration) string {
if t == 0 {
return "---"
}
return (t - (t % f)).String()
}
16 changes: 16 additions & 0 deletions util/strutil/strutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,19 @@ func TestPrettyTime(t *testing.T) {
t.Fatal("want", "---", "got", got)
}
}

func TestPrettyTimeFormat(t *testing.T) {
d, _ := time.ParseDuration("300ms")
got := PrettyTimeFormat(d, time.Millisecond)
if got != "300ms" {
t.Fatal("want", "300ms", "got", got)
}
got = PrettyTimeFormat(d, time.Second)
if got != "0s" {
t.Fatal("want", "0s", "got", got)
}
got = PrettyTimeFormat(d, time.Hour)
if got != "0s" {
t.Fatal("want", "0s", "got", got)
}
}