func (t timespanType) ConvertToTimespanImpl(v interface{}) (timespanImpl, error) {
	switch value := v.(type) {
	case int:
		return t.ConvertToTimespanImpl(int64(value))
	case uint:
		return t.ConvertToTimespanImpl(int64(value))
	case int8:
		return t.ConvertToTimespanImpl(int64(value))
	case uint8:
		return t.ConvertToTimespanImpl(int64(value))
	case int16:
		return t.ConvertToTimespanImpl(int64(value))
	case uint16:
		return t.ConvertToTimespanImpl(int64(value))
	case int32:
		return t.ConvertToTimespanImpl(int64(value))
	case uint32:
		return t.ConvertToTimespanImpl(int64(value))
	case int64:
		absValue := int64Abs(value)
		if absValue >= -59 && absValue <= 59 {
			return microsecondsToTimespan(value * microsecondsPerSecond), nil
		} else if absValue >= 100 && absValue <= 9999 {
			minutes := absValue / 100
			seconds := absValue % 100
			if minutes <= 59 && seconds <= 59 {
				microseconds := (seconds * microsecondsPerSecond) + (minutes * microsecondsPerMinute)
				if value < 0 {
					return microsecondsToTimespan(-1 * microseconds), nil
				}
				return microsecondsToTimespan(microseconds), nil
			}
		} else if absValue >= 10000 && absValue <= 9999999 {
			hours := absValue / 10000
			minutes := (absValue / 100) % 100
			seconds := absValue % 100
			if minutes <= 59 && seconds <= 59 {
				microseconds := (seconds * microsecondsPerSecond) + (minutes * microsecondsPerMinute) + (hours * microsecondsPerHour)
				if value < 0 {
					return microsecondsToTimespan(-1 * microseconds), nil
				}
				return microsecondsToTimespan(microseconds), nil
			}
		}
	case uint64:
		return t.ConvertToTimespanImpl(int64(value))
	case float32:
		return t.ConvertToTimespanImpl(float64(value))
	case float64:
		intValue := int64(value)
		microseconds := int64Abs(int64(math.Round((value - float64(intValue)) * float64(microsecondsPerSecond))))
		absValue := int64Abs(intValue)
		if absValue >= -59 && absValue <= 59 {
			totalMicroseconds := (absValue * microsecondsPerSecond) + microseconds
			if value < 0 {
				return microsecondsToTimespan(-1 * totalMicroseconds), nil
			}
			return microsecondsToTimespan(totalMicroseconds), nil
		} else if absValue >= 100 && absValue <= 9999 {
			minutes := absValue / 100
			seconds := absValue % 100
			if minutes <= 59 && seconds <= 59 {
				totalMicroseconds := (seconds * microsecondsPerSecond) + (minutes * microsecondsPerMinute) + microseconds
				if value < 0 {
					return microsecondsToTimespan(-1 * totalMicroseconds), nil
				}
				return microsecondsToTimespan(totalMicroseconds), nil
			}
		} else if absValue >= 10000 && absValue <= 9999999 {
			hours := absValue / 10000
			minutes := (absValue / 100) % 100
			seconds := absValue % 100
			if minutes <= 59 && seconds <= 59 {
				totalMicroseconds := (seconds * microsecondsPerSecond) + (minutes * microsecondsPerMinute) + (hours * microsecondsPerHour) + microseconds
				if value < 0 {
					return microsecondsToTimespan(-1 * totalMicroseconds), nil
				}
				return microsecondsToTimespan(totalMicroseconds), nil
			}
		}
	case string:
		impl, err := stringToTimespan(value)
		if err == nil {
			return impl, nil
		}
		if strings.Contains(value, ".") {
			strAsDouble, err := strconv.ParseFloat(value, 64)
			if err != nil {
				return timespanImpl{}, ErrConvertingToTimeType.New(v)
			}
			return t.ConvertToTimespanImpl(strAsDouble)
		} else {
			strAsInt, err := strconv.ParseInt(value, 10, 64)
			if err != nil {
				return timespanImpl{}, ErrConvertingToTimeType.New(v)
			}
			return t.ConvertToTimespanImpl(strAsInt)
		}
	case time.Duration:
		microseconds := value.Nanoseconds() / 1000
		return microsecondsToTimespan(microseconds), nil
	}
	return timespanImpl{}, ErrConvertingToTimeType.New(v)
}
Convert implements Type interface.
package sql
import (
	"fmt"
	"math"
	"regexp"
	"strconv"
	"strings"
	"time"
	"github.com/dolthub/vitess/go/sqltypes"
	"github.com/dolthub/vitess/go/vt/proto/query"
	"gopkg.in/src-d/go-errors.v1"
)
var (
	Time TimeType = timespanType{}
	ErrConvertingToTimeType = errors.NewKind("value %v is not a valid Time")
	timespanRegex                   = regexp.MustCompile(`^-?(\d{1,3}):(\d{1,2})(:(\d{1,2})(\.(\d{1,9}))?)?$`)
	timespanMinimum           int64 = -3020399000000
	timespanMaximum           int64 = 3020399000000
	microsecondsPerSecond     int64 = 1000000
	microsecondsPerMinute     int64 = 60000000
	microsecondsPerHour       int64 = 3600000000
	nanosecondsPerMicrosecond int64 = 1000
)
type TimeType interface {
	Type
	ConvertToTimeDuration(v interface{}) (time.Duration, error)
	Marshal(v interface{}) (int64, error)
	Unmarshal(v int64) string
}
type timespanType struct{}
type timespanImpl struct {
	negative     bool
	hours        int16
	minutes      int8
	seconds      int8
	microseconds int32
}
func (t timespanType) Compare(a interface{}, b interface{}) (int, error) {
	if hasNulls, res := compareNulls(a, b); hasNulls {
		return res, nil
	}
	as, err := t.ConvertToTimespanImpl(a)
	if err != nil {
		return 0, err
	}
	bs, err := t.ConvertToTimespanImpl(b)
	if err != nil {
		return 0, err
	}
	ai := as.AsMicroseconds()
	bi := bs.AsMicroseconds()
	if ai < bi {
		return -1, nil
	} else if ai > bi {
		return 1, nil
	}
	return 0, nil
}
func (t timespanType) Convert(v interface{}) (interface{}, error) {
	if v == nil {
		return nil, nil
	}
	if ti, err := t.ConvertToTimespanImpl(v); err != nil {
		return nil, err
	} else {
		return ti.String(), nil
	}
}
func (t timespanType) MustConvert(v interface{}) interface{} {
	value, err := t.Convert(v)
	if err != nil {
		panic(err)
	}
	return value
}