yaml marshal fat durations

master
bel 2023-04-04 19:20:09 -06:00
parent 5b2d89a73a
commit ca09d6f966
1 changed files with 14 additions and 3 deletions

17
db.go
View File

@ -3,7 +3,6 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"log"
"strconv" "strconv"
"time" "time"
@ -91,8 +90,20 @@ func (db db) lastTS(user, q string) time.Time {
} }
func (d duration) MarshalYAML() (interface{}, error) { func (d duration) MarshalYAML() (interface{}, error) {
log.Println("marshalling duration", time.Duration(d)) return d.String(), nil
return time.Duration(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 { func (d *duration) UnmarshalYAML(unmarshal func(interface{}) error) error {