func (d *Duration) UnmarshalText(text []byte) error {
	var err error
	*d, err = ParseDuration(string(text))
	return err
}
UnmarshalText implements the encoding.TextUnmarshaler interface.
package model
import (
	"fmt"
	"math"
	"regexp"
	"strconv"
	"strings"
	"time"
)
const (
	minimumTick = time.Millisecond
	second = int64(time.Second / minimumTick)
	nanosPerTick = int64(minimumTick / time.Nanosecond)
	Earliest = Time(math.MinInt64)
	Latest = Time(math.MaxInt64)
)
type Time int64
type Interval struct {
	Start, End Time
}
func Now() Time {
	return TimeFromUnixNano(time.Now().UnixNano())
}
func TimeFromUnix(t int64) Time {
	return Time(t * second)
}
func TimeFromUnixNano(t int64) Time {
	return Time(t / nanosPerTick)
}
func (t Time) Equal(o Time) bool {
	return t == o
}
func (t Time) Before(o Time) bool {
	return t < o
}
func (t Time) After(o Time) bool {
	return t > o
}
func (t Time) Add(d time.Duration) Time {
	return t + Time(d/minimumTick)
}
func (t Time) Sub(o Time) time.Duration {
	return time.Duration(t-o) * minimumTick
}
func (t Time) Time() time.Time {
	return time.Unix(int64(t)/second, (int64(t)%second)*nanosPerTick)
}
func (t Time) Unix() int64 {
	return int64(t) / second
}
func (t Time) UnixNano() int64 {
	return int64(t) * nanosPerTick
}
var dotPrecision = int(math.Log10(float64(second)))
func (t Time) String() string {
	return strconv.FormatFloat(float64(t)/float64(second), 'f', -1, 64)
}
func (t Time) MarshalJSON() ([]byte, error) {
	return []byte(t.String()), nil
}
func (t *Time) UnmarshalJSON(b []byte) error {
	p := strings.Split(string(b), ".")
	switch len(p) {
	case 1:
		v, err := strconv.ParseInt(string(p[0]), 10, 64)
		if err != nil {
			return err
		}
		*t = Time(v * second)
	case 2:
		v, err := strconv.ParseInt(string(p[0]), 10, 64)
		if err != nil {
			return err
		}
		v *= second
		prec := dotPrecision - len(p[1])
		if prec < 0 {
			p[1] = p[1][:dotPrecision]
		} else if prec > 0 {
			p[1] = p[1] + strings.Repeat("0", prec)
		}
		va, err := strconv.ParseInt(p[1], 10, 32)
		if err != nil {
			return err
		}
		if len(p[0]) > 0 && p[0][0] == '-' && v+va > 0 {
			*t = Time(v+va) * -1
		} else {
			*t = Time(v + va)
		}
	default:
		return fmt.Errorf("invalid time %q", string(b))
	}
	return nil
}
type Duration time.Duration
func (d *Duration) Set(s string) error {
	var err error
	*d, err = ParseDuration(s)
	return err
}
func (d *Duration) Type() string {
	return "duration"
}
var durationRE = regexp.MustCompile("^(([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?(([0-9]+)ms)?$")
func ParseDuration(durationStr string) (Duration, error) {
	switch durationStr {
	case "0":
		return 0, nil
	case "":
		return 0, fmt.Errorf("empty duration string")
	}
	matches := durationRE.FindStringSubmatch(durationStr)
	if matches == nil {
		return 0, fmt.Errorf("not a valid duration string: %q", durationStr)
	}
	var dur time.Duration
	m := func(pos int, mult time.Duration) {
		if matches[pos] == "" {
			return
		}
		n, _ := strconv.Atoi(matches[pos])
		d := time.Duration(n) * time.Millisecond
		dur += d * mult
	}
	m(2, 1000*60*60*24*365)  
	m(4, 1000*60*60*24*7)    
	m(6, 1000*60*60*24)      
	m(8, 1000*60*60)         
	m(10, 1000*60)           
	m(12, 1000)              
	m(14, 1)                 
	return Duration(dur), nil
}
func (d Duration) String() string {
	var (
		ms = int64(time.Duration(d) / time.Millisecond)
		r  = ""
	)
	if ms == 0 {
		return "0s"
	}
	f := func(unit string, mult int64, exact bool) {
		if exact && ms%mult != 0 {
			return
		}
		if v := ms / mult; v > 0 {
			r += fmt.Sprintf("%d%s", v, unit)
			ms -= v * mult
		}
	}
	f("y", 1000*60*60*24*365, true)
	f("w", 1000*60*60*24*7, true)
	f("d", 1000*60*60*24, false)
	f("h", 1000*60*60, false)
	f("m", 1000*60, false)
	f("s", 1000, false)
	f("ms", 1, false)
	return r
}
func (d *Duration) MarshalText() ([]byte, error) {
	return []byte(d.String()), nil
}