mirror of
https://github.com/nxshock/gwp.git
synced 2024-11-27 03:31:02 +05:00
Add custom duration formatter
This commit is contained in:
parent
2a342398b9
commit
35416cd502
46
durationformat.go
Normal file
46
durationformat.go
Normal file
@ -0,0 +1,46 @@
|
||||
package gwp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func fmtDuration(d time.Duration) string {
|
||||
d = d.Round(time.Second)
|
||||
|
||||
days := d / time.Hour / 24
|
||||
d -= days * 24 * time.Hour
|
||||
|
||||
hours := d / time.Hour
|
||||
d -= hours * time.Hour
|
||||
|
||||
minites := d / time.Minute
|
||||
d -= minites * time.Minute
|
||||
|
||||
seconds := d / time.Second
|
||||
d -= seconds * time.Second
|
||||
|
||||
var resultStr string
|
||||
|
||||
if days > 0 {
|
||||
resultStr += fmt.Sprintf("%3dd", days)
|
||||
} else {
|
||||
resultStr += " "
|
||||
}
|
||||
|
||||
if hours > 0 {
|
||||
resultStr += fmt.Sprintf(" %2dh", hours)
|
||||
} else {
|
||||
resultStr += " "
|
||||
}
|
||||
|
||||
if minites > 0 {
|
||||
resultStr += fmt.Sprintf(" %2dm", minites)
|
||||
} else {
|
||||
resultStr += " "
|
||||
}
|
||||
|
||||
resultStr += fmt.Sprintf(" %2ds", seconds)
|
||||
|
||||
return resultStr
|
||||
}
|
16
durationformat_test.go
Normal file
16
durationformat_test.go
Normal file
@ -0,0 +1,16 @@
|
||||
package gwp
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFmtDuration(t *testing.T) {
|
||||
assert.Equal(t, " 1s", fmtDuration(time.Second))
|
||||
assert.Equal(t, " 1m 0s", fmtDuration(time.Minute))
|
||||
assert.Equal(t, " 1h 0s", fmtDuration(time.Hour))
|
||||
assert.Equal(t, " 1d 0s", fmtDuration(time.Hour*24))
|
||||
assert.Equal(t, "365d 0s", fmtDuration(time.Hour*24*365))
|
||||
}
|
4
gwp.go
4
gwp.go
@ -101,7 +101,7 @@ func (workerPool *WorkerPool) printProgress() {
|
||||
}
|
||||
if workerPool.EstimateCount > 0 && workerPool.currentSpeed > 0 {
|
||||
fmt.Fprintf(os.Stderr, " ETA: %s at %.2f rps",
|
||||
time.Second*time.Duration(float64(workerPool.EstimateCount-workerPool.processedCount)/workerPool.currentSpeed), workerPool.currentSpeed)
|
||||
fmtDuration(time.Second*time.Duration(float64(workerPool.EstimateCount-workerPool.processedCount)/workerPool.currentSpeed)), workerPool.currentSpeed)
|
||||
}
|
||||
fmt.Fprint(os.Stderr, endLine)
|
||||
}
|
||||
@ -121,7 +121,7 @@ func (workerPool *WorkerPool) CloseAndWait() {
|
||||
workerPool.printProgress()
|
||||
}
|
||||
|
||||
// ErrorCount returns total error count
|
||||
// ErrorCount returns total error count.
|
||||
func (workerPool *WorkerPool) ErrorCount() int {
|
||||
return workerPool.errorCount
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user