more functionality

This commit is contained in:
2021-04-10 22:14:45 -04:00
parent ece8baeaac
commit 91ff1a10d4
7 changed files with 200 additions and 15 deletions

View File

@@ -1,6 +1,9 @@
package libyear
import "time"
import (
"fmt"
"time"
)
type Info struct {
Version string // version string
@@ -13,6 +16,8 @@ type Pair struct {
Current Info
}
// 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 {
@@ -20,3 +25,7 @@ func Calc(p ...Pair) time.Duration {
}
return sum
}
func DecimalYear(d time.Duration) string {
return fmt.Sprintf("%.2f", float64(d.Truncate(time.Hour)/time.Hour)/float64(8760))
}

View File

@@ -0,0 +1,23 @@
package libyear
import (
"testing"
"time"
)
func TestDecimalYear(t *testing.T) {
values := map[time.Duration]string{
time.Hour: "0.00",
time.Hour * 8760: "1.00",
time.Hour * 4380: "0.50",
time.Hour * 12264: "1.40",
time.Hour * 27520: "3.14",
time.Hour * 1000000: "114.16",
}
for d, s := range values {
if r := DecimalYear(d); r != s {
t.Logf("Expected %s, got %s for %d", s, r, d)
t.Fail()
}
}
}