Skip to content

Commit

Permalink
evaluator, parser: support UTC_DATE()
Browse files Browse the repository at this point in the history
* evaluator, parser: support UTC_DATE()

Fix issue: #236.
  • Loading branch information
overvenus authored and shenli committed Apr 5, 2016
1 parent 4b4f29d commit d01d516
Show file tree
Hide file tree
Showing 8 changed files with 5,707 additions and 5,583 deletions.
1 change: 1 addition & 0 deletions evaluator/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ var Funcs = map[string]Func{
"now": {builtinNow, 0, 1},
"second": {builtinSecond, 1, 1},
"sysdate": {builtinSysDate, 0, 1},
"utc_date": {builtinUTCDate, 0, 0},
"week": {builtinWeek, 1, 2},
"weekday": {builtinWeekDay, 1, 1},
"weekofyear": {builtinWeekOfYear, 1, 1},
Expand Down
10 changes: 10 additions & 0 deletions evaluator/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,16 @@ func builtinCurrentTime(args []types.Datum, _ context.Context) (d types.Datum, e
return convertToDuration(d, fsp)
}

// See https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_utc-date
func builtinUTCDate(args []types.Datum, _ context.Context) (d types.Datum, err error) {
year, month, day := time.Now().UTC().Date()
t := mysql.Time{
Time: time.Date(year, month, day, 0, 0, 0, 0, time.UTC),
Type: mysql.TypeDate, Fsp: mysql.UnspecifiedFsp}
d.SetMysqlTime(t)
return d, nil
}

// See https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_extract
func builtinExtract(args []types.Datum, _ context.Context) (d types.Datum, err error) {
unit := args[0].GetString()
Expand Down
8 changes: 8 additions & 0 deletions evaluator/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,14 @@ func (s *testEvaluatorSuite) TestCurrentTime(c *C) {
c.Assert(err, NotNil)
}

func (s *testEvaluatorSuite) TestUTCDate(c *C) {
last := time.Now().UTC()
v, err := builtinUTCDate(types.MakeDatums(nil), nil)
c.Assert(err, IsNil)
n := v.GetMysqlTime()
c.Assert(n.String(), GreaterEqual, last.Format(mysql.DateFormat))
}

func (s *testEvaluatorSuite) TestDateArith(c *C) {
ctx := mock.NewContext()

Expand Down
Loading

0 comments on commit d01d516

Please sign in to comment.