Skip to content

Commit 4e45485

Browse files
qw4990zz-jason
authored andcommitted
types: extract month support 0 (#10116) (#10702)
1 parent 54d8e85 commit 4e45485

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

types/time.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,19 +1538,15 @@ func checkDatetimeType(t MysqlTime, allowZeroInDate, allowInvalidDate bool) erro
15381538

15391539
// ExtractDatetimeNum extracts time value number from datetime unit and format.
15401540
func ExtractDatetimeNum(t *Time, unit string) (int64, error) {
1541+
// TODO: Consider time_zone variable.
15411542
switch strings.ToUpper(unit) {
15421543
case "DAY":
15431544
return int64(t.Time.Day()), nil
15441545
case "WEEK":
15451546
week := t.Time.Week(0)
15461547
return int64(week), nil
15471548
case "MONTH":
1548-
// TODO: Consider time_zone variable.
1549-
t1, err := t.Time.GoTime(gotime.Local)
1550-
if err != nil {
1551-
return 0, errors.Trace(err)
1552-
}
1553-
return int64(t1.Month()), nil
1549+
return int64(t.Time.Month()), nil
15541550
case "QUARTER":
15551551
m := int64(t.Time.Month())
15561552
// 1 - 3 -> 1

types/time_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,32 @@ func (s *testTimeSuite) TestExtractDatetimeNum(c *C) {
11651165
res, err = types.ExtractDatetimeNum(&in, "TEST_ERROR")
11661166
c.Assert(res, Equals, int64(0))
11671167
c.Assert(err, ErrorMatches, "invalid unit.*")
1168+
1169+
in = types.Time{
1170+
Time: types.FromDate(0000, 00, 00, 00, 00, 00, 0000),
1171+
Type: mysql.TypeTimestamp,
1172+
Fsp: types.DefaultFsp,
1173+
}
1174+
1175+
res, err = types.ExtractDatetimeNum(&in, "day")
1176+
c.Assert(err, IsNil)
1177+
c.Assert(res, Equals, int64(0))
1178+
1179+
res, err = types.ExtractDatetimeNum(&in, "week")
1180+
c.Assert(err, IsNil)
1181+
c.Assert(res, Equals, int64(0))
1182+
1183+
res, err = types.ExtractDatetimeNum(&in, "MONTH")
1184+
c.Assert(err, IsNil)
1185+
c.Assert(res, Equals, int64(0))
1186+
1187+
res, err = types.ExtractDatetimeNum(&in, "QUARTER")
1188+
c.Assert(err, IsNil)
1189+
c.Assert(res, Equals, int64(0))
1190+
1191+
res, err = types.ExtractDatetimeNum(&in, "YEAR")
1192+
c.Assert(err, IsNil)
1193+
c.Assert(res, Equals, int64(0))
11681194
}
11691195

11701196
func (s *testTimeSuite) TestExtractDurationNum(c *C) {

0 commit comments

Comments
 (0)