zeit/z/util.go
2022-02-28 11:14:27 -05:00

29 lines
514 B
Go

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