Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: extract month support 0 (#10116) #10702

Merged
merged 1 commit into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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