libyear/pkg/libyear/libyear.go

36 lines
987 B
Go

package libyear
import (
"fmt"
"time"
)
// Info defines a version and a commit time for that version
type Info struct {
Version string // version string
Time time.Time // commit time
}
// Pair is a set of versions for a dependency (current and newest possible)
type Pair struct {
Name string
Latest Info
Current Info
}
// Calc returns the summed time distance (as a time.Duration) between a set of Pairs of modules.
// 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.
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
}
// DecimalYear conversts a time.Duration (which is in nsec) to a two-decimal-place count of years.
func DecimalYear(d time.Duration) string {
return fmt.Sprintf("%.2f", float64(d.Truncate(time.Hour)/time.Hour)/float64(8760))
}