// Pseudo-type encoders
// Encode a time.Time value to the TIME RQL type
494928
func timePseudoTypeEncoder(v reflect.Value) (interface{}, error) {
	t := v.Interface().(time.Time)

	timeVal := float64(t.UnixNano()) / float64(time.Second)

	// use seconds-since-epoch precision if time.Time `t`
	// is before the oldest nanosecond time
	if t.Before(time.Unix(0, math.MinInt64)) {
		timeVal = float64(t.Unix())
	}

	return map[string]interface{}{
		"$reql_type$": "TIME",
		"epoch_time":  timeVal,
		"timezone":    t.Format("-07:00"),
	}, nil
}
57631
func DateToTime(d *google_type.Date) time.Time {
	if d == nil {
		return time.Unix(0, 0).UTC()
	}
	return time.Date(int(d.Year), time.Month(d.Month), int(d.Day), 0, 0, 0, 0, time.UTC)
}
1102564
func (jsonEncoder) Encode(v ...interface{}) string {
	var data interface{} = v
	if v == nil {
		// So that empty results produces `[]` and not `null`
		data = []interface{}{}
	} else if len(v) == 1 {
		data = v[0]
	}
	b, err := json.Marshal(data)
	if err != nil {
		panic(err)
	}
	log.Printf("%s", b)
	return string(b)
}
94831
func (up *Unsuback) Encode(dst []byte) (int, error) {
	return identifiedEncode(dst, up.ID, UNSUBACK)
}
286475
func (pp *Pingreq) Encode(dst []byte) (int, error) {
	return nakedEncode(dst, PINGREQ)
}
434151
func Convert_Slice_string_To_v1_Time(input *[]string, out *Time, s conversion.Scope) error {
	str := ""
	if len(*input) > 0 {
		str = (*input)[0]
	}
	return out.UnmarshalQueryParameter(str)
}
715051
func ValueToTimeAnchor(val Value, anchor time.Time) (time.Time, bool) {
	switch v := val.(type) {
	case TimeValue:
		return v.Val(), true
	case StringValue:
		return StringToTimeAnchor(v.Val(), anchor)
	case StringsValue:
		vals := v.Val()
		if len(vals) < 1 {
			return time.Time{}, false
		}
		return StringToTimeAnchor(vals[0], anchor)
	case IntValue, NumberValue:
		t, err := dateparse.ParseIn(v.ToString(), time.Local)
		if err != nil {
			return time.Time{}, false
		}
		if t.Year() < 1800 || t.Year() > 2120 {
			return t, false
		}
		return t, true
	default:
		//u.Warnf("un-handled type to time? %#v", val)
	}
	return time.Time{}, false
}
633884
func (p *parser) Time(i int, context string) Time {
	s := p.String(i, context)
	if p.err != nil {
		return Time{}
	}
	v, err := ParseTime(s)
	if err != nil {
		p.SetErr(context, s)
	}
	return v
}
576039
func TimeForDt6(dt6 int32) (time.Time, error) {
	return time.Parse(DT6, strconv.FormatInt(int64(dt6), 10))
}
1092076
func ToTime(in interface{}) (time.Time, error) {
	switch val := in.(type) {
	case int64:
		return time.Unix(val, 0), nil
	default:
		t, err := cast.ToTimeE(val)
		if err != nil {
			return t, ErrBadType
		}
		return t, nil
	}
}
1045788
func (resp *AppendEntriesResponse) Encode(w io.Writer) (int, error) {
	b, err := proto.Marshal(resp.pb)
	if err != nil {
		return -1, err
	}

	return w.Write(b)
}
965763
func (d *defaulter) AsTime(v Valuer, layout string, dv time.Time) time.Time {
	value, err := v.Value()
	if err != nil {
		d.logValuerError(err)
		return dv
	}
	tvalue, err := time.Parse(layout, value)
	if err != nil {
		d.logFormatError("time", err)
		return dv
	}
	return tvalue
}
1036617
func (e *Encoder) Encode(v interface{}) (err error) {
	defer func() {
		if r := recover(); r != nil {
			if _, ok := r.(runtime.Error); ok {
				panic(r)
			}
			if s, ok := r.(string); ok {
				panic(s)
			}
			err = r.(error)
		}
	}()
	e.b.Clear()
	reflectValue(&e.b, reflect.ValueOf(v), encoderOptions{})
	if _, err := e.b.WriteTo(e.w); err != nil {
		return WithStack(err)
	}
	return nil
}
345460
func EncodeTimeout(t time.Duration) string {
	if d := div(t, time.Nanosecond); d <= maxTimeoutValue {
		return strconv.FormatInt(d, 10) + "n"
	}
	if d := div(t, time.Microsecond); d <= maxTimeoutValue {
		return strconv.FormatInt(d, 10) + "u"
	}
	if d := div(t, time.Millisecond); d <= maxTimeoutValue {
		return strconv.FormatInt(d, 10) + "m"
	}
	if d := div(t, time.Second); d <= maxTimeoutValue {
		return strconv.FormatInt(d, 10) + "S"
	}
	if d := div(t, time.Minute); d <= maxTimeoutValue {
		return strconv.FormatInt(d, 10) + "M"
	}
	// Note that maxTimeoutValue * time.Hour > MaxInt64.
	return strconv.FormatInt(div(t, time.Hour), 10) + "H"
}
273495
func (f *Values) Encode() string {
	var buf bytes.Buffer
	for _, v := range f.values {
		if buf.Len() > 0 {
			buf.WriteByte('&')
		}
		buf.WriteString(url.QueryEscape(v.Key))
		buf.WriteString("=")
		buf.WriteString(url.QueryEscape(v.Value))
	}
	return buf.String()
}
917694
func EncodeMySQLTime(sc *stmtctx.StatementContext, d types.Datum, tp byte, b []byte) (_ []byte, err error) {
	t := d.GetMysqlTime()
	// Encoding timestamp need to consider timezone. If it's not in UTC, transform to UTC first.
	// This is compatible with `PBToExpr > convertTime`, and coprocessor assumes the passed timestamp is in UTC as well.
	if tp == mysql.TypeUnspecified {
		tp = t.Type
	}
	if tp == mysql.TypeTimestamp && sc.TimeZone != time.UTC {
		err = t.ConvertTimeZone(sc.TimeZone, time.UTC)
		if err != nil {
			return nil, err
		}
	}
	var v uint64
	v, err = t.ToPackedUint()
	if err != nil {
		return nil, err
	}
	b = EncodeUint(b, v)
	return b, nil
}
213442
func Time(ms uint64) time.Time {
	s := int64(ms / 1e3)
	ns := int64((ms % 1e3) * 1e6)
	return time.Unix(s, ns)
}