From ca09d6f966415246761765c66614b0bb420ce2ed Mon Sep 17 00:00:00 2001 From: bel Date: Tue, 4 Apr 2023 19:20:09 -0600 Subject: [PATCH] yaml marshal fat durations --- db.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/db.go b/db.go index f407a39..16a34d6 100644 --- a/db.go +++ b/db.go @@ -3,7 +3,6 @@ package main import ( "errors" "fmt" - "log" "strconv" "time" @@ -91,8 +90,20 @@ func (db db) lastTS(user, q string) time.Time { } func (d duration) MarshalYAML() (interface{}, error) { - log.Println("marshalling duration", time.Duration(d)) - return time.Duration(d).String(), nil + return d.String(), nil +} + +func (d duration) String() string { + result := "" + if weeks := time.Duration(d) / (time.Hour * 24 * 7); weeks > 0 { + result += fmt.Sprintf("%dw", weeks) + d -= duration(weeks * time.Hour * 24 * 7) + } + if days := time.Duration(d) / (time.Hour * 24); days > 0 { + result += fmt.Sprintf("%dd", days) + d -= duration(days * time.Hour * 24) + } + return result + time.Duration(d).String() } func (d *duration) UnmarshalYAML(unmarshal func(interface{}) error) error {