zeit/z/util.go

29 lines
514 B
Go
Raw Normal View History

package z
import (
2022-02-28 16:14:27 +00:00
"fmt"
"time"
2022-02-28 16:14:27 +00:00
"github.com/shopspring/decimal"
)
var fractional bool
func fmtDuration(dur time.Duration) (string) {
2022-02-28 16:14:27 +00:00
return fmtHours(decimal.NewFromFloat(dur.Hours()))
}
func fmtHours(hours decimal.Decimal) (string) {
2022-02-28 16:14:27 +00:00
if fractional {
return hours.StringFixed(2)
} else {
return fmt.Sprintf(
"%s:%02s",
hours.Floor(), // hours
hours.Sub(hours.Floor()).
2022-02-28 16:14:27 +00:00
Mul(decimal.NewFromFloat(.6)).
Mul(decimal.NewFromInt(100)).
Floor())
}
}