2018-01-12 16:30:54 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2018-05-14 15:32:27 +00:00
|
|
|
"fmt"
|
|
|
|
"math"
|
2018-01-12 16:30:54 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Duration is a time.Duration with some more parsing options
|
|
|
|
type Duration time.Duration
|
|
|
|
|
2018-03-12 20:52:42 +00:00
|
|
|
// DurationOff is the default value for flags which can be turned off
|
|
|
|
const DurationOff = Duration((1 << 63) - 1)
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// Turn Duration into a string
|
|
|
|
func (d Duration) String() string {
|
2018-03-12 20:52:42 +00:00
|
|
|
if d == DurationOff {
|
|
|
|
return "off"
|
|
|
|
}
|
2018-05-14 15:32:27 +00:00
|
|
|
for i := len(ageSuffixes) - 2; i >= 0; i-- {
|
|
|
|
ageSuffix := &ageSuffixes[i]
|
|
|
|
if math.Abs(float64(d)) >= float64(ageSuffix.Multiplier) {
|
|
|
|
timeUnits := float64(d) / float64(ageSuffix.Multiplier)
|
|
|
|
return strconv.FormatFloat(timeUnits, 'f', -1, 64) + ageSuffix.Suffix
|
|
|
|
}
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
return time.Duration(d).String()
|
|
|
|
}
|
|
|
|
|
2018-03-12 20:52:42 +00:00
|
|
|
// IsSet returns if the duration is != DurationOff
|
|
|
|
func (d Duration) IsSet() bool {
|
|
|
|
return d != DurationOff
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// We use time conventions
|
|
|
|
var ageSuffixes = []struct {
|
|
|
|
Suffix string
|
|
|
|
Multiplier time.Duration
|
|
|
|
}{
|
|
|
|
{Suffix: "d", Multiplier: time.Hour * 24},
|
|
|
|
{Suffix: "w", Multiplier: time.Hour * 24 * 7},
|
|
|
|
{Suffix: "M", Multiplier: time.Hour * 24 * 30},
|
|
|
|
{Suffix: "y", Multiplier: time.Hour * 24 * 365},
|
|
|
|
|
|
|
|
// Default to second
|
|
|
|
{Suffix: "", Multiplier: time.Second},
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseDuration parses a duration string. Accept ms|s|m|h|d|w|M|y suffixes. Defaults to second if not provided
|
|
|
|
func ParseDuration(age string) (time.Duration, error) {
|
|
|
|
var period float64
|
|
|
|
|
2018-03-12 20:52:42 +00:00
|
|
|
if age == "off" {
|
|
|
|
return time.Duration(DurationOff), nil
|
|
|
|
}
|
|
|
|
|
2018-05-14 15:32:27 +00:00
|
|
|
// Attempt to parse as a time.Duration first
|
|
|
|
d, err := time.ParseDuration(age)
|
|
|
|
if err == nil {
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
for _, ageSuffix := range ageSuffixes {
|
|
|
|
if strings.HasSuffix(age, ageSuffix.Suffix) {
|
|
|
|
numberString := age[:len(age)-len(ageSuffix.Suffix)]
|
|
|
|
var err error
|
|
|
|
period, err = strconv.ParseFloat(numberString, 64)
|
|
|
|
if err != nil {
|
|
|
|
return time.Duration(0), err
|
|
|
|
}
|
|
|
|
period *= float64(ageSuffix.Multiplier)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.Duration(period), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a Duration
|
|
|
|
func (d *Duration) Set(s string) error {
|
|
|
|
duration, err := ParseDuration(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*d = Duration(duration)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type of the value
|
|
|
|
func (d Duration) Type() string {
|
2019-02-07 11:57:26 +00:00
|
|
|
return "Duration"
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
2018-05-14 15:32:27 +00:00
|
|
|
|
|
|
|
// Scan implements the fmt.Scanner interface
|
|
|
|
func (d *Duration) Scan(s fmt.ScanState, ch rune) error {
|
|
|
|
token, err := s.Token(true, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return d.Set(string(token))
|
|
|
|
}
|