github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/jsonhelper/duration.go (about) 1 package jsonhelper 2 3 import "time" 4 5 // Duration is [time.Duration] but implements [encoding.TextMarshaler] and [encoding.TextUnmarshaler]. 6 type Duration time.Duration 7 8 // MarshalText implements [encoding.TextMarshaler.MarshalText]. 9 func (d Duration) MarshalText() ([]byte, error) { 10 return []byte(time.Duration(d).String()), nil 11 } 12 13 // UnmarshalText implements [encoding.TextUnmarshaler.UnmarshalText]. 14 func (d *Duration) UnmarshalText(text []byte) error { 15 duration, err := time.ParseDuration(string(text)) 16 if err != nil { 17 return err 18 } 19 *d = Duration(duration) 20 return nil 21 }