Skip to content

Commit

Permalink
types: extract month support 0 (#10116) (#10702)
Browse files Browse the repository at this point in the history
  • Loading branch information
qw4990 authored and zz-jason committed Jun 4, 2019
1 parent 54d8e85 commit 4e45485
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
8 changes: 2 additions & 6 deletions types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -1538,19 +1538,15 @@ func checkDatetimeType(t MysqlTime, allowZeroInDate, allowInvalidDate bool) erro

// ExtractDatetimeNum extracts time value number from datetime unit and format.
func ExtractDatetimeNum(t *Time, unit string) (int64, error) {
// TODO: Consider time_zone variable.
switch strings.ToUpper(unit) {
case "DAY":
return int64(t.Time.Day()), nil
case "WEEK":
week := t.Time.Week(0)
return int64(week), nil
case "MONTH":
// TODO: Consider time_zone variable.
t1, err := t.Time.GoTime(gotime.Local)
if err != nil {
return 0, errors.Trace(err)
}
return int64(t1.Month()), nil
return int64(t.Time.Month()), nil
case "QUARTER":
m := int64(t.Time.Month())
// 1 - 3 -> 1
Expand Down
26 changes: 26 additions & 0 deletions types/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,32 @@ func (s *testTimeSuite) TestExtractDatetimeNum(c *C) {
res, err = types.ExtractDatetimeNum(&in, "TEST_ERROR")
c.Assert(res, Equals, int64(0))
c.Assert(err, ErrorMatches, "invalid unit.*")

in = types.Time{
Time: types.FromDate(0000, 00, 00, 00, 00, 00, 0000),
Type: mysql.TypeTimestamp,
Fsp: types.DefaultFsp,
}

res, err = types.ExtractDatetimeNum(&in, "day")
c.Assert(err, IsNil)
c.Assert(res, Equals, int64(0))

res, err = types.ExtractDatetimeNum(&in, "week")
c.Assert(err, IsNil)
c.Assert(res, Equals, int64(0))

res, err = types.ExtractDatetimeNum(&in, "MONTH")
c.Assert(err, IsNil)
c.Assert(res, Equals, int64(0))

res, err = types.ExtractDatetimeNum(&in, "QUARTER")
c.Assert(err, IsNil)
c.Assert(res, Equals, int64(0))

res, err = types.ExtractDatetimeNum(&in, "YEAR")
c.Assert(err, IsNil)
c.Assert(res, Equals, int64(0))
}

func (s *testTimeSuite) TestExtractDurationNum(c *C) {
Expand Down

0 comments on commit 4e45485

Please sign in to comment.