github.com/motyar/up@v0.2.10/config/duration.go (about) 1 package config 2 3 import ( 4 "bytes" 5 "strconv" 6 "time" 7 ) 8 9 // Duration may be specified as numerical seconds or 10 // as a duration string such as "1.5m". 11 type Duration time.Duration 12 13 // Seconds returns the duration in seconds. 14 func (d *Duration) Seconds() float64 { 15 return float64(time.Duration(*d) / time.Second) 16 } 17 18 // UnmarshalJSON implementation. 19 func (d *Duration) UnmarshalJSON(b []byte) error { 20 if i, err := strconv.ParseInt(string(b), 10, 64); err == nil { 21 *d = Duration(time.Second * time.Duration(i)) 22 return nil 23 } 24 25 v, err := time.ParseDuration(string(bytes.Trim(b, `"`))) 26 if err != nil { 27 return err 28 } 29 30 *d = Duration(v) 31 return nil 32 } 33 34 // MarshalJSON implement. 35 func (d *Duration) MarshalJSON() ([]byte, error) { 36 return []byte(strconv.Itoa(int(d.Seconds()))), nil 37 }