mirror of
https://github.com/sablierapp/sablier.git
synced 2025-12-21 13:23:03 +01:00
41 lines
749 B
Go
41 lines
749 B
Go
package durations
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func Humanize(d time.Duration) string {
|
|
days := int64(d.Hours() / 24)
|
|
hours := int64(math.Mod(d.Hours(), 24))
|
|
minutes := int64(math.Mod(d.Minutes(), 60))
|
|
seconds := int64(math.Mod(d.Seconds(), 60))
|
|
|
|
chunks := []struct {
|
|
singularName string
|
|
amount int64
|
|
}{
|
|
{"day", days},
|
|
{"hour", hours},
|
|
{"minute", minutes},
|
|
{"second", seconds},
|
|
}
|
|
|
|
var parts []string
|
|
|
|
for _, chunk := range chunks {
|
|
switch chunk.amount {
|
|
case 0:
|
|
continue
|
|
case 1:
|
|
parts = append(parts, fmt.Sprintf("%d %s", chunk.amount, chunk.singularName))
|
|
default:
|
|
parts = append(parts, fmt.Sprintf("%d %ss", chunk.amount, chunk.singularName))
|
|
}
|
|
}
|
|
|
|
return strings.Join(parts, " ")
|
|
}
|