27bb03bbc0
* adding copyright header * fix fmt and a test
43 lines
761 B
Go
43 lines
761 B
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package healthcheck
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
oneDay = 24 * time.Hour
|
|
oneWeek = 7 * oneDay
|
|
oneMonth = 30 * oneDay
|
|
oneYear = 365 * oneDay
|
|
)
|
|
|
|
var suffixDurationMap = map[string]time.Duration{
|
|
"y": oneYear,
|
|
"mo": oneMonth,
|
|
"w": oneWeek,
|
|
"d": oneDay,
|
|
}
|
|
var orderedSuffixes = []string{"y", "mo", "w", "d"}
|
|
|
|
func FormatDuration(d time.Duration) string {
|
|
var result string
|
|
for _, suffix := range orderedSuffixes {
|
|
unit := suffixDurationMap[suffix]
|
|
if d > unit {
|
|
quantity := int64(d / unit)
|
|
result = fmt.Sprintf("%v%v%v", quantity, suffix, result)
|
|
d = d - (time.Duration(quantity) * unit)
|
|
}
|
|
}
|
|
|
|
if d > 0 {
|
|
result = d.String() + result
|
|
}
|
|
|
|
return result
|
|
}
|