Skip to content

Commit

Permalink
util/types: convertToMysqlTime will truncate hh:mm:ss part for Date t…
Browse files Browse the repository at this point in the history
…ype (#2808)
  • Loading branch information
tiancaiamao authored and coocood committed Mar 10, 2017
1 parent 27405e8 commit b8b84dd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
6 changes: 1 addition & 5 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ func (s *testEvaluatorSuite) TestDate(c *C) {
c.Assert(err, IsNil)
v, err := f.eval(nil)
c.Assert(err, IsNil)
if v.Kind() != types.KindMysqlTime {
c.Assert(v, testutil.DatumEquals, t["Expect"][0])
} else {
c.Assert(v.GetMysqlTime().String(), Equals, t["Expect"][0].GetString())
}
c.Assert(v, testutil.DatumEquals, t["Expect"][0])
}

// test year, month and day
Expand Down
38 changes: 17 additions & 21 deletions util/types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,45 +877,41 @@ func (d *Datum) convertToMysqlTime(sc *variable.StatementContext, target *FieldT
if target.Decimal != UnspecifiedLength {
fsp = target.Decimal
}
var ret Datum
var (
ret Datum
t Time
err error
)
switch d.k {
case KindMysqlTime:
t, err := d.GetMysqlTime().Convert(tp)
t, err = d.GetMysqlTime().Convert(tp)
if err != nil {
ret.SetValue(t)
return ret, errors.Trace(err)
}
t, err = t.roundFrac(fsp)
ret.SetValue(t)
if err != nil {
return ret, errors.Trace(err)
}
case KindMysqlDuration:
t, err := d.GetMysqlDuration().ConvertToTime(tp)
t, err = d.GetMysqlDuration().ConvertToTime(tp)
if err != nil {
ret.SetValue(t)
return ret, errors.Trace(err)
}
t, err = t.roundFrac(fsp)
ret.SetValue(t)
if err != nil {
return ret, errors.Trace(err)
}
case KindString, KindBytes:
t, err := ParseTime(d.GetString(), tp, fsp)
ret.SetValue(t)
if err != nil {
return ret, errors.Trace(err)
}
t, err = ParseTime(d.GetString(), tp, fsp)
case KindInt64:
t, err := ParseTimeFromNum(d.GetInt64(), tp, fsp)
ret.SetValue(t)
if err != nil {
return ret, errors.Trace(err)
}
t, err = ParseTimeFromNum(d.GetInt64(), tp, fsp)
default:
return invalidConv(d, tp)
}
if tp == mysql.TypeDate {
// Truncate hh:mm:ss part if the type is Date.
t.Time = FromDate(t.Time.Year(), t.Time.Month(), t.Time.Day(), 0, 0, 0, 0)
}
ret.SetValue(t)
if err != nil {
return ret, errors.Trace(err)
}
return ret, nil
}

Expand Down

0 comments on commit b8b84dd

Please sign in to comment.