libyear/pkg/libyear/libyear.go

32 lines
654 B
Go
Raw Normal View History

2021-04-10 02:01:41 +00:00
package libyear
2021-04-11 02:14:45 +00:00
import (
"fmt"
"time"
)
2021-04-10 02:01:41 +00:00
type Info struct {
Version string // version string
Time time.Time // commit time
}
type Pair struct {
Name string
Latest Info
Current Info
}
2021-04-11 02:14:45 +00:00
// TODO: sum can only represent ~290 years before overflowing, but we don't actually need nanosecond precision!
// Probably worth switching to hour-based summing here.
2021-04-10 02:01:41 +00:00
func Calc(p ...Pair) time.Duration {
sum := time.Duration(0)
for i := range p {
sum = sum + p[i].Latest.Time.Sub(p[i].Current.Time)
}
return sum
}
2021-04-11 02:14:45 +00:00
func DecimalYear(d time.Duration) string {
return fmt.Sprintf("%.2f", float64(d.Truncate(time.Hour)/time.Hour)/float64(8760))
}