// ParamDuration parses a duration string, example: "1m30s"
// that will be 1 minute and 30 seconds
518601
func (r *Request) ParamDuration(name string) *time.Duration {
	v, set := r.Req.URL.Query()[name]
	if !set {
		return nil
	}

	d, err := time.ParseDuration(v[0])
	if err != nil {
		return nil
	}
	return &d
}
884765
func (c *Constant) EvalDuration(ctx sessionctx.Context, _ chunk.Row) (val types.Duration, isNull bool, err error) {
	if c.DeferredExpr != nil {
		dt, err := c.DeferredExpr.Eval(chunk.Row{})
		if err != nil {
			return types.Duration{}, true, err
		}
		if dt.IsNull() {
			return types.Duration{}, true, nil
		}
		val, err := dt.ToString()
		if err != nil {
			return types.Duration{}, true, err
		}
		dur, err := types.ParseDuration(ctx.GetSessionVars().StmtCtx, val, types.MaxFsp)
		if err != nil {
			return types.Duration{}, true, err
		}
		c.Value.SetMysqlDuration(dur)
	} else {
		if c.GetType().Tp == mysql.TypeNull || c.Value.IsNull() {
			return types.Duration{}, true, nil
		}
	}
	return c.Value.GetMysqlDuration(), false, nil
}
399007
func (k *Key) Duration() (time.Duration, error) {
	return time.ParseDuration(k.String())
}
597023
func truncateDuration(d time.Duration) time.Duration {
	for _, t := range durTransactions {
		if d > t.threshold {
			return d.Round(t.round)
		}
	}
	return d
}
83116
func (col *CorrelatedColumn) EvalDuration(ctx sessionctx.Context, row chunk.Row) (types.Duration, bool, error) {
	if col.Data.IsNull() {
		return types.Duration{}, true, nil
	}
	return col.Data.GetMysqlDuration(), false, nil
}
919578
func ShortHumanDuration(d time.Duration) string {
	// Allow deviation no more than 2 seconds(excluded) to tolerate machine time
	// inconsistence, it can be considered as almost now.
	if seconds := int(d.Seconds()); seconds < -1 {
		return fmt.Sprintf("<invalid>")
	} else if seconds < 0 {
		return fmt.Sprintf("0s")
	} else if seconds < 60 {
		return fmt.Sprintf("%ds", seconds)
	} else if minutes := int(d.Minutes()); minutes < 60 {
		return fmt.Sprintf("%dm", minutes)
	} else if hours := int(d.Hours()); hours < 24 {
		return fmt.Sprintf("%dh", hours)
	} else if hours < 24*365 {
		return fmt.Sprintf("%dd", hours/24)
	}
	return fmt.Sprintf("%dy", int(d.Hours()/24/365))
}
417867
func (b *builtinUTCTimeWithoutArgSig) evalDuration(row chunk.Row) (types.Duration, bool, error) {
	var nowTs = &b.ctx.GetSessionVars().StmtCtx.NowTs
	if nowTs.Equal(time.Time{}) {
		*nowTs = time.Now()
	}
	v, err := types.ParseDuration(b.ctx.GetSessionVars().StmtCtx, nowTs.UTC().Format(types.TimeFormat), 0)
	return v, false, err
}
640858
func (b *builtinValuesDurationSig) evalDuration(_ chunk.Row) (types.Duration, bool, error) {
	if !b.ctx.GetSessionVars().StmtCtx.InInsertStmt {
		return types.Duration{}, true, nil
	}
	row := b.ctx.GetSessionVars().CurrInsertValues
	if row.IsEmpty() {
		return types.Duration{}, true, errors.New("Session current insert values is nil")
	}
	if b.offset < row.Len() {
		if row.IsNull(b.offset) {
			return types.Duration{}, true, nil
		}
		duration := row.GetDuration(b.offset, b.getRetTp().Decimal)
		return duration, false, nil
	}
	return types.Duration{}, true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", row.Len(), b.offset)
}
1004812
func ParseDuration(value string) (time.Duration, error) {
	var match []string
	var regex *regexp.Regexp

	if week.MatchString(value) {
		match = week.FindStringSubmatch(value)
		regex = week
	} else if full.MatchString(value) {
		match = full.FindStringSubmatch(value)
		regex = full
	} else {
		return time.Duration(0), ErrBadFormat
	}

	d := time.Duration(0)
	day := time.Hour * 24
	week := day * 7
	year := day * 365

	for i, name := range regex.SubexpNames() {
		part := match[i]
		if i == 0 || name == "" || part == "" {
			continue
		}

		value, err := strconv.Atoi(part)
		if err != nil {
			return time.Duration(0), err
		}
		switch name {
		case "year":
			d += year * time.Duration(value)
		case "month":
			return time.Duration(0), ErrNoMonth
		case "week":
			d += week * time.Duration(value)
		case "day":
			d += day * time.Duration(value)
		case "hour":
			d += time.Hour * time.Duration(value)
		case "minute":
			d += time.Minute * time.Duration(value)
		case "second":
			d += time.Second * time.Duration(value)
		}
	}

	return d, nil
}
809935
func (f *TimeFuncs) ParseDuration(n interface{}) (gotime.Duration, error) {
	return gotime.ParseDuration(conv.ToString(n))
}
1248411
func ParseDuration(str string) (dur time.Duration, err error) {
	str = strings.TrimSpace(str)
	orig := str
	// Check sign
	sign := int64(1)
	switch str[0] {
	case '-':
		sign = -1
		fallthrough
	case '+':
		str = str[1:]
	}
	var i, d int64
	// Find houre
	if nn := strings.IndexRune(str, ':'); nn != -1 {
		if i, err = strconv.ParseInt(str[0:nn], 10, 64); err != nil {
			return
		}
		d = i * 3600
		str = str[nn+1:]
	} else {
		goto invalid
	}
	if len(str) != 5 && len(str) != 15 || str[2] != ':' {
		goto invalid
	}
	if i, err = strconv.ParseInt(str[0:2], 10, 64); err != nil {
		return
	}
	if i < 0 || i > 59 {
		goto invalid
	}
	d += i * 60
	if i, err = strconv.ParseInt(str[3:5], 10, 64); err != nil {
		return
	}
	if i < 0 || i > 59 {
		goto invalid
	}
	d += i
	d *= 1e9
	if len(str) == 15 {
		if str[5] != '.' {
			goto invalid
		}
		if i, err = strconv.ParseInt(str[6:15], 10, 64); err != nil {
			return
		}
		d += i
	}
	dur = time.Duration(d * sign)
	return

invalid:
	err = errors.New("invalid MySQL TIME string: " + orig)
	return

}
1201468
func strDatetimeAddDuration(sc *stmtctx.StatementContext, d string, arg1 types.Duration) (string, error) {
	arg0, err := types.ParseTime(sc, d, mysql.TypeDatetime, types.MaxFsp)
	if err != nil {
		return "", err
	}
	ret, err := arg0.Add(sc, arg1)
	if err != nil {
		return "", err
	}
	fsp := types.MaxFsp
	if ret.Time.Microsecond() == 0 {
		fsp = types.MinFsp
	}
	ret.Fsp = fsp
	return ret.String(), nil
}
292288
func (c Config) ParseInterval() (time.Duration, error) {
	return time.ParseDuration(c.Interval)
}
353638
func (b *builtinStringStringTimeDiffSig) evalDuration(row chunk.Row) (d types.Duration, isNull bool, err error) {
	lhs, isNull, err := b.args[0].EvalString(b.ctx, row)
	if isNull || err != nil {
		return d, isNull, err
	}

	rhs, isNull, err := b.args[1].EvalString(b.ctx, row)
	if isNull || err != nil {
		return d, isNull, err
	}

	sc := b.ctx.GetSessionVars().StmtCtx
	fsp := b.tp.Decimal
	lhsDur, lhsTime, lhsIsDuration, err := convertStringToDuration(sc, lhs, fsp)
	if err != nil {
		return d, true, err
	}

	rhsDur, rhsTime, rhsIsDuration, err := convertStringToDuration(sc, rhs, fsp)
	if err != nil {
		return d, true, err
	}

	if lhsIsDuration != rhsIsDuration {
		return d, true, nil
	}

	if lhsIsDuration {
		d, isNull, err = calculateDurationTimeDiff(b.ctx, lhsDur, rhsDur)
	} else {
		d, isNull, err = calculateTimeDiff(sc, lhsTime, rhsTime)
	}

	return d, isNull, err
}
947200
func (config AudioConfig) params() (map[string]string, error) {
	params, _ := config.BaseFile.params()

	if config.Duration != 0 {
		params["duration"] = strconv.Itoa(config.Duration)
	}

	if config.Performer != "" {
		params["performer"] = config.Performer
	}
	if config.Title != "" {
		params["title"] = config.Title
	}
	if config.Caption != "" {
		params["caption"] = config.Caption
		if config.ParseMode != "" {
			params["parse_mode"] = config.ParseMode
		}
	}

	return params, nil
}
1015935
func (b *builtinNullTimeDiffSig) evalDuration(row chunk.Row) (d types.Duration, isNull bool, err error) {
	return d, true, nil
}
396548
func ParseDuration(dinput time.Duration, args ...interface{}) *Durafmt {
	input := dinput.String()
	return &Durafmt{dinput, input, getDurationUnits(args)}
}