feat: round timing style

This commit is contained in:
Jan De Dobbeleer 2021-05-20 19:53:42 +02:00 committed by Jan De Dobbeleer
parent 676da9e4d1
commit 8642b7bfd9
2 changed files with 22 additions and 1 deletions

View file

@ -40,12 +40,13 @@ The [installation guide][install] shows how to include this argument for PowerSh
Style specifies the format in which the time will be displayed. The table below shows some example times in each option. Style specifies the format in which the time will be displayed. The table below shows some example times in each option.
| format | 0.001s | 2.1s | 3m2.1s | 4h3m2.1s | | format | 0.001s | 2.1s | 3m2.1s | 4h3m2.1s |
|-----------|----------------|--------------|---------------|------------------| | --------- | -------------- | ------------ | ------------- | ---------------- |
| austin | `1ms` | `2.1s` | `3m 2.1s` | `4h 3m 2.1s` | | austin | `1ms` | `2.1s` | `3m 2.1s` | `4h 3m 2.1s` |
| roundrock | `1ms` | `2s 100ms` | `3m 2s 100ms` | `4h 3m 2s 100ms` | | roundrock | `1ms` | `2s 100ms` | `3m 2s 100ms` | `4h 3m 2s 100ms` |
| dallas | `0.001` | `2.1` | `3:2.1` | `4:3:2.1` | | dallas | `0.001` | `2.1` | `3:2.1` | `4:3:2.1` |
| galveston | `00:00:00` | `00:00:02` | `00:03:02` | `04:03:02` | | galveston | `00:00:00` | `00:00:02` | `00:03:02` | `04:03:02` |
| houston | `00:00:00.001` | `00:00:02.1` | `00:03:02.1` | `04:03:02.1` | | houston | `00:00:00.001` | `00:00:02.1` | `00:03:02.1` | `04:03:02.1` |
| amarillo | `0.001s` | `2.1s` | `182.1s` | `14,582.1s` | | amarillo | `0.001s` | `2.1s` | `182.1s` | `14,582.1s` |
| round | `1ms` | `2s` | `3m` | `4h` |
[install]: /docs/installation [install]: /docs/installation

View file

@ -32,6 +32,8 @@ const (
Houston DurationStyle = "houston" Houston DurationStyle = "houston"
// Amarillo seconds // Amarillo seconds
Amarillo DurationStyle = "amarillo" Amarillo DurationStyle = "amarillo"
// Round will round the output of the format
Round DurationStyle = "round"
second = 1000 second = 1000
minute = 60000 minute = 60000
@ -78,6 +80,8 @@ func (t *executiontime) formatDuration(ms int64, style DurationStyle) string {
return t.formatDurationHouston(ms) return t.formatDurationHouston(ms)
case Amarillo: case Amarillo:
return t.formatDurationAmarillo(ms) return t.formatDurationAmarillo(ms)
case Round:
return t.formatDurationRound(ms)
default: default:
return fmt.Sprintf("Style: %s is not available", style) return fmt.Sprintf("Style: %s is not available", style)
} }
@ -175,3 +179,19 @@ func (t *executiontime) formatDurationAmarillo(ms int64) string {
return result return result
} }
func (t *executiontime) formatDurationRound(ms int64) string {
if ms >= day {
return fmt.Sprintf("%dd", ms/day)
}
if ms >= hour {
return fmt.Sprintf("%dh", ms/hour%hoursPerDay)
}
if ms >= minute {
return fmt.Sprintf("%dm", ms/minute%secondsPerMinute)
}
if ms >= second {
return fmt.Sprintf("%ds", (ms%minute)/second)
}
return fmt.Sprintf("%dms", ms%second)
}