Skip to content

Commit

Permalink
add buildin function UTC_TIME (#3145)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkk2003rty authored and shenli committed Apr 26, 2017
1 parent 31bc108 commit 4822322
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
16 changes: 15 additions & 1 deletion expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -2330,5 +2330,19 @@ type builtinUTCTimeSig struct {
// eval evals a builtinUTCTimeSig.
// See https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_utc-time
func (b *builtinUTCTimeSig) eval(row []types.Datum) (d types.Datum, err error) {
return d, errFunctionNotExists.GenByArgs("UTC_TIME")
const utctimeFormat = "15:04:05.000000"
args, err := b.evalArgs(row)
if err != nil {
return types.Datum{}, errors.Trace(err)
}
fsp := 0
sc := b.ctx.GetSessionVars().StmtCtx
if len(args) == 1 && !args[0].IsNull() {
if fsp, err = checkFsp(sc, args[0]); err != nil {
d.SetNull()
return d, errors.Trace(err)
}
}
d.SetString(time.Now().UTC().Format(utctimeFormat))
return convertToDuration(b.ctx.GetSessionVars().StmtCtx, d, fsp)
}
27 changes: 27 additions & 0 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,33 @@ func (s *testEvaluatorSuite) TestCurrentTime(c *C) {
c.Assert(err, NotNil)
}

func (s *testEvaluatorSuite) TestUTCTime(c *C) {
defer testleak.AfterTest(c)()

tfStr := "15:04:05"
last := time.Now().UTC()
fc := funcs[ast.UTCTime]

tests := []struct {
param interface{}
expect int
}{{nil, 8}, {0, 8}, {3, 12}, {6, 15}, {-1, 0}, {7, 0}}

for _, test := range tests {
f, err := fc.getFunction(datumsToConstants(types.MakeDatums(test.param)), s.ctx)
c.Assert(err, IsNil)
v, err := f.eval(nil)
if test.expect > 0 {
c.Assert(err, IsNil)
n := v.GetMysqlDuration()
c.Assert(n.String(), HasLen, test.expect)
c.Assert(n.String(), GreaterEqual, last.Format(tfStr))
} else {
c.Assert(err, NotNil)
}
}
}

func (s *testEvaluatorSuite) TestUTCDate(c *C) {
defer testleak.AfterTest(c)()
last := time.Now().UTC()
Expand Down
2 changes: 1 addition & 1 deletion plan/typeinferer.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func (v *typeInferrer) handleFuncCallExpr(x *ast.FuncCallExpr) {
tp = types.NewFieldType(mysql.TypeLonglong)
tp.Flag |= mysql.UnsignedFlag
// time related
case ast.Curtime, ast.CurrentTime, ast.TimeDiff, ast.MakeTime, ast.SecToTime:
case ast.Curtime, ast.CurrentTime, ast.TimeDiff, ast.MakeTime, ast.SecToTime, ast.UTCTime:
tp = types.NewFieldType(mysql.TypeDuration)
tp.Decimal = v.getFsp(x)
case ast.Curdate, ast.CurrentDate, ast.Date, ast.FromDays, ast.MakeDate:
Expand Down
2 changes: 2 additions & 0 deletions plan/typeinferer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ func (ts *testTypeInferrerSuite) TestInferType(c *C) {
{"maketime(12, 15, 30)", mysql.TypeDuration, charset.CharsetBin, mysql.BinaryFlag},
{"sec_to_time(2378)", mysql.TypeDuration, charset.CharsetBin, mysql.BinaryFlag},
{"current_timestamp()", mysql.TypeDatetime, charset.CharsetBin, mysql.BinaryFlag},
{"utc_time()", mysql.TypeDuration, charset.CharsetBin, mysql.BinaryFlag},
{"utc_time(3)", mysql.TypeDuration, charset.CharsetBin, mysql.BinaryFlag},
{"utc_timestamp()", mysql.TypeDatetime, charset.CharsetBin, mysql.BinaryFlag},
{"microsecond('2009-12-31 23:59:59.000010')", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag},
{"second('2009-12-31 23:59:59.000010')", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag},
Expand Down

0 comments on commit 4822322

Please sign in to comment.