// TimeFieldFromInstance creates and initializes a time field based on its name, the reference object instance and field number.
// This method looks for "form_min", "form_max" and "form_value" tags to add additional parameters to the field.
974939
func TimeFieldFromInstance(val reflect.Value, t reflect.Type, fieldNo int, name string) *Field {
	ret := TimeField(name)
	// check tags
	if v := formcommon.TagVal(t, fieldNo, "form_min"); v != "" {
		if !validateTime(v) {
			panic(errors.New(fmt.Sprintf("Invalid time value (min) for field", name)))
		}
		ret.SetParam("min", v)
	}
	if v := formcommon.TagVal(t, fieldNo, "form_max"); v != "" {
		if !validateTime(v) {
			panic(errors.New(fmt.Sprintf("Invalid time value (max) for field", name)))
		}
		ret.SetParam("max", v)
	}
	if v := val.Field(fieldNo).Interface().(time.Time); !v.IsZero() {
		ret.SetValue(v.Format(TIME_FORMAT))
	} else if v := formcommon.TagVal(t, fieldNo, "form_value"); v != "" {
		ret.SetValue(v)
	}
	return ret
}
234229
func TimeAgoWithTime(start, end time.Time) (string, error) {
	duration := start.Sub(end)
	return stringForDuration(duration), nil
}
957642
func ParseDatetimeFromNum(num int64) (Time, error) {
	return ParseTimeFromNum(num, mysql.TypeDatetime, DefaultFsp)
}
186606
func CreateFromTime(h, m, s, ns int, location string) (*Carbon, error) {
	y, mon, d := Now().Date()

	return Create(y, mon, d, h, m, s, ns, location)
}
1284022
func TimeSince(t time.Time, lang string) template.HTML {
	return template.HTML(fmt.Sprintf(`<span class="time-since" title="%s">%s</span>`, t.Format(setting.TimeFormat), timeSince(t, lang)))
}
223751
func HiddenFieldFromInstance(val reflect.Value, t reflect.Type, fieldNo int, name string) *Field {
	ret := HiddenField(name)
	ret.SetValue(fmt.Sprintf("%v", val.Field(fieldNo).Interface()))
	return ret
}
1320155
func CreateInputField(x, y, w, h int, fg, bg termbox.Attribute) *InputField {
	c := InputField{x: x, y: y, width: w, height: h,
		fg: fg, bg: bg, cursorFg: bg, cursorBg: fg, activeFg: fg, activeBg: bg,
	}
	return &c
}
666823
func RangeField(name string, min, max, step int) *Field {
	ret := FieldWithType(name, formcommon.RANGE)
	ret.SetParam("min", fmt.Sprintf("%d", min))
	ret.SetParam("max", fmt.Sprintf("%d", max))
	ret.SetParam("step", fmt.Sprintf("%d", step))
	return ret
}
141462
func (s *DeleteByQueryService) DefaultField(defaultField string) *DeleteByQueryService {
	s.df = defaultField
	return s
}
1138063
func reqlTimeToNativeTime(timestamp float64, timezone string) (time.Time, error) {
	sec, ms := math.Modf(timestamp)

	// Convert to native time rounding to milliseconds
	t := time.Unix(int64(sec), int64(math.Floor(ms*1000+0.5))*1000*1000)

	// Caclulate the timezone
	if timezone != "" {
		hours, err := strconv.Atoi(timezone[1:3])
		if err != nil {
			return time.Time{}, err
		}
		minutes, err := strconv.Atoi(timezone[4:6])
		if err != nil {
			return time.Time{}, err
		}
		tzOffset := ((hours * 60) + minutes) * 60
		if timezone[:1] == "-" {
			tzOffset = 0 - tzOffset
		}

		t = t.In(time.FixedZone(timezone, tzOffset))
	}

	return t, nil
}
1339164
func TextFieldFromInstance(val reflect.Value, t reflect.Type, fieldNo int, name string, typ ...string) *Field {
	ret := TextField(name, typ...)
	ret.SetValue(fmt.Sprintf("%v", val.Field(fieldNo).Interface()))
	return ret
}
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
}
736339
func (t *SimpleTrack) TimeDuration() time.Duration {
	return time.Duration(t.Duration) * time.Millisecond
}
923577
func (b *builtinTimeStringTimeDiffSig) evalDuration(row chunk.Row) (d types.Duration, isNull bool, err error) {
	lhs, isNull, err := b.args[0].EvalTime(b.ctx, row)
	if isNull || err != nil {
		return d, isNull, err
	}

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

	sc := b.ctx.GetSessionVars().StmtCtx
	_, rhs, isDuration, err := convertStringToDuration(sc, rhsStr, b.tp.Decimal)
	if err != nil || isDuration {
		return d, true, err
	}

	d, isNull, err = calculateTimeDiff(sc, lhs, rhs)
	return d, isNull, err
}
355603
func StaticFieldFromInstance(val reflect.Value, t reflect.Type, fieldNo int, name string) *Field {
	ret := StaticField(name, fmt.Sprintf("%s", val.Field(fieldNo).Interface()))
	return ret
}
961162
func MinNonZeroTime(a, b time.Time) time.Time {
	if a.IsZero() {
		return b
	}
	if b.IsZero() {
		return a
	}
	return MinTime(a, b)
}
867587
func DateFromTime(t time.Time) Date {
	year, month, day := t.Date()
	return DateFor(year, month, day)
}