// ExtractDatetimeNum extracts time value number from datetime unit and format.
512878
func ExtractDatetimeNum(t *Time, unit string) (int64, error) {
	// TODO: Consider time_zone variable.
	switch strings.ToUpper(unit) {
	case "DAY":
		return int64(t.Time.Day()), nil
	case "WEEK":
		week := t.Time.Week(0)
		return int64(week), nil
	case "MONTH":
		return int64(t.Time.Month()), nil
	case "QUARTER":
		m := int64(t.Time.Month())
		// 1 - 3 -> 1
		// 4 - 6 -> 2
		// 7 - 9 -> 3
		// 10 - 12 -> 4
		return (m + 2) / 3, nil
	case "YEAR":
		return int64(t.Time.Year()), nil
	case "DAY_MICROSECOND":
		h, m, s := t.Clock()
		d := t.Time.Day()
		return int64(d*1000000+h*10000+m*100+s)*1000000 + int64(t.Time.Microsecond()), nil
	case "DAY_SECOND":
		h, m, s := t.Clock()
		d := t.Time.Day()
		return int64(d)*1000000 + int64(h)*10000 + int64(m)*100 + int64(s), nil
	case "DAY_MINUTE":
		h, m, _ := t.Clock()
		d := t.Time.Day()
		return int64(d)*10000 + int64(h)*100 + int64(m), nil
	case "DAY_HOUR":
		h, _, _ := t.Clock()
		d := t.Time.Day()
		return int64(d)*100 + int64(h), nil
	case "YEAR_MONTH":
		y, m := t.Time.Year(), t.Time.Month()
		return int64(y)*100 + int64(m), nil
	default:
		return 0, errors.Errorf("invalid unit %s", unit)
	}
}
1093005
func (t *Translator) FormatNumber(number float64) string {
	return t.formatNumber(t.parseFormat(t.rules.Numbers.Formats.Decimal, true), number)
}
879412
func TrimToNumber(text string) string {
	leftTrimmed := strings.TrimLeftFunc(text, func(r rune) bool {
		return (r < '0' || r > '9') && r != '-'
	})

	return strings.TrimRightFunc(leftTrimmed, func(r rune) bool {
		return r < '0' || r > '9'
	})
}
1086004
func DatetimeField(name string) *Field {
	ret := FieldWithType(name, formcommon.DATETIME)
	return ret
}
41614
func (d DateTime) Timestamp(v reflect.Value) (interface{}, error) {
	return d.timestamp(), nil
}
1275716
func extractDayMicrosecond(format string) (int64, int64, int64, gotime.Duration, error) {
	fields := strings.Split(format, " ")
	if len(fields) != 2 {
		return 0, 0, 0, 0, errors.Errorf("invalid time format - %s", format)
	}

	days, err := strconv.ParseInt(fields[0], 10, 64)
	if err != nil {
		return 0, 0, 0, 0, errors.Errorf("invalid time format - %s", format)
	}

	_, _, _, value, err := extractHourMicrosecond(fields[1])
	if err != nil {
		return 0, 0, 0, 0, errors.Errorf("invalid time format - %s", format)
	}

	return 0, 0, days, value, nil
}
715010
func ParseTimeFromInt64(sc *stmtctx.StatementContext, num int64) (Time, error) {
	return parseDateTimeFromNum(sc, num)
}
963600
func getnum(s string, fixed bool) (int, string, error) {
	if !isDigit(s, 0) {
		return 0, s, errBad
	}
	if !isDigit(s, 1) {
		if fixed {
			return 0, s, errBad
		}
		return int(s[0] - '0'), s[1:], nil
	}
	return int(s[0]-'0')*10 + int(s[1]-'0'), s[2:], nil
}
245783
func (dt DateTime) Value() (driver.Value, error) {
	year, month, day := dt.Date()
	hour, minute, second := dt.Clock()
	return time.Date(year, month, day, hour, minute, second, 0, time.UTC), nil
}
61729
func ExtractCount(s string) (int64, error) {
	s = regexNum.FindString(s)
	if len(s) == 0 {
		return 0, ErrNotFound
	}
	val, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		return 0, err
	}
	return val, nil
}
762244
func parseNumber(tok *lexer.Token) (result float64, isInt bool) {
	var valInt int
	var err error

	valInt, err = strconv.Atoi(tok.Val)
	if err == nil {
		isInt = true

		result = float64(valInt)
	} else {
		isInt = false

		result, err = strconv.ParseFloat(tok.Val, 64)
		if err != nil {
			errToken(tok, fmt.Sprintf("Failed to parse number: %s", tok.Val))
		}
	}

	// named returned values
	return
}
1246263
func (t *Translator) FormatDateTime(format int, datetime time.Time) (string, error) {
	pattern := ""
	switch format {
	case DateFormatFull:
		pattern = t.rules.DateTime.Formats.Date.Full
	case DateFormatLong:
		pattern = t.rules.DateTime.Formats.Date.Long
	case DateFormatMedium:
		pattern = t.rules.DateTime.Formats.Date.Medium
	case DateFormatShort:
		pattern = t.rules.DateTime.Formats.Date.Short
	case TimeFormatFull:
		pattern = t.rules.DateTime.Formats.Time.Full
	case TimeFormatLong:
		pattern = t.rules.DateTime.Formats.Time.Long
	case TimeFormatMedium:
		pattern = t.rules.DateTime.Formats.Time.Medium
	case TimeFormatShort:
		pattern = t.rules.DateTime.Formats.Time.Short
	case DateTimeFormatFull:
		datePattern := strings.Trim(t.rules.DateTime.Formats.Date.Full, " ,")
		timePattern := strings.Trim(t.rules.DateTime.Formats.Time.Full, " ,")
		pattern = getDateTimePattern(t.rules.DateTime.Formats.DateTime.Full, datePattern, timePattern)
	case DateTimeFormatLong:
		datePattern := strings.Trim(t.rules.DateTime.Formats.Date.Long, " ,")
		timePattern := strings.Trim(t.rules.DateTime.Formats.Time.Long, " ,")
		pattern = getDateTimePattern(t.rules.DateTime.Formats.DateTime.Long, datePattern, timePattern)
	case DateTimeFormatMedium:
		datePattern := strings.Trim(t.rules.DateTime.Formats.Date.Medium, " ,")
		timePattern := strings.Trim(t.rules.DateTime.Formats.Time.Medium, " ,")
		pattern = getDateTimePattern(t.rules.DateTime.Formats.DateTime.Medium, datePattern, timePattern)
	case DateTimeFormatShort:
		datePattern := strings.Trim(t.rules.DateTime.Formats.Date.Short, " ,")
		timePattern := strings.Trim(t.rules.DateTime.Formats.Time.Short, " ,")
		pattern = getDateTimePattern(t.rules.DateTime.Formats.DateTime.Short, datePattern, timePattern)
	default:
		return "", translatorError{message: "unknown datetime format" + pattern[0:1]}
	}

	parsed, err := t.parseDateTimeFormat(pattern)
	if err != nil {
		return "", err
	}

	return t.formatDateTime(datetime, parsed)
}
1119896
func extractYearMonth(format string) (int64, int64, int64, gotime.Duration, error) {
	fields := strings.Split(format, "-")
	if len(fields) != 2 {
		return 0, 0, 0, 0, errors.Errorf("invalid time format - %s", format)
	}

	years, err := strconv.ParseInt(fields[0], 10, 64)
	if err != nil {
		return 0, 0, 0, 0, errors.Errorf("invalid time format - %s", format)
	}

	months, err := strconv.ParseInt(fields[1], 10, 64)
	if err != nil {
		return 0, 0, 0, 0, errors.Errorf("invalid time format - %s", format)
	}

	return years, months, 0, 0, nil
}
191184
func (d DateTime) TimeZone(v reflect.Value) (interface{}, error) {
	return d.timezone(), nil
}
664407
func FormatDateTimeField(key string, val time.Time) (string, string) {
	return key + ";VALUE=DATE-TIME", val.Format("20060102T150405")
}
1265084
func splitDateTime(format string) (seps []string, fracStr string) {
	index := GetFracIndex(format)
	if index > 0 {
		fracStr = format[index+1:]
		format = format[:index]
	}

	seps = ParseDateFormat(format)
	return
}
501144
func ExtractType(opts ...interface{}) *opt.TypeOption {
	for _, o := range opts {
		if v, ok := o.(*opt.TypeOption); ok {
			return v
		}
	}
	return nil
}