Skip to content

Commit

Permalink
feat: round timing style
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed May 20, 2021
1 parent 676da9e commit 8642b7b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/docs/segment-executiontime.md
Expand Up @@ -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.

| format | 0.001s | 2.1s | 3m2.1s | 4h3m2.1s |
|-----------|----------------|--------------|---------------|------------------|
| --------- | -------------- | ------------ | ------------- | ---------------- |
| austin | `1ms` | `2.1s` | `3m 2.1s` | `4h 3m 2.1s` |
| roundrock | `1ms` | `2s 100ms` | `3m 2s 100ms` | `4h 3m 2s 100ms` |
| 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` |
| 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` |
| round | `1ms` | `2s` | `3m` | `4h` |

[install]: /docs/installation
20 changes: 20 additions & 0 deletions src/segment_executiontime.go
Expand Up @@ -32,6 +32,8 @@ const (
Houston DurationStyle = "houston"
// Amarillo seconds
Amarillo DurationStyle = "amarillo"
// Round will round the output of the format
Round DurationStyle = "round"

second = 1000
minute = 60000
Expand Down Expand Up @@ -78,6 +80,8 @@ func (t *executiontime) formatDuration(ms int64, style DurationStyle) string {
return t.formatDurationHouston(ms)
case Amarillo:
return t.formatDurationAmarillo(ms)
case Round:
return t.formatDurationRound(ms)
default:
return fmt.Sprintf("Style: %s is not available", style)
}
Expand Down Expand Up @@ -175,3 +179,19 @@ func (t *executiontime) formatDurationAmarillo(ms int64) string {

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)
}

0 comments on commit 8642b7b

Please sign in to comment.