Skip to content

Commit 7423c3c

Browse files
WangXiangUSTCzz-jason
authored andcommitted
types: extract month support 0 (#10116)
1 parent 28e6294 commit 7423c3c

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
@@ -1545,19 +1545,15 @@ func checkDatetimeType(t MysqlTime, allowZeroInDate, allowInvalidDate bool) erro
15451545

15461546
// ExtractDatetimeNum extracts time value number from datetime unit and format.
15471547
func ExtractDatetimeNum(t *Time, unit string) (int64, error) {
1548+
// TODO: Consider time_zone variable.
15481549
switch strings.ToUpper(unit) {
15491550
case "DAY":
15501551
return int64(t.Time.Day()), nil
15511552
case "WEEK":
15521553
week := t.Time.Week(0)
15531554
return int64(week), nil
15541555
case "MONTH":
1555-
// TODO: Consider time_zone variable.
1556-
t1, err := t.Time.GoTime(gotime.Local)
1557-
if err != nil {
1558-
return 0, errors.Trace(err)
1559-
}
1560-
return int64(t1.Month()), nil
1556+
return int64(t.Time.Month()), nil
15611557
case "QUARTER":
15621558
m := int64(t.Time.Month())
15631559
// 1 - 3 -> 1

types/time_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,32 @@ func (s *testTimeSuite) TestExtractDatetimeNum(c *C) {
11891189
res, err = types.ExtractDatetimeNum(&in, "TEST_ERROR")
11901190
c.Assert(res, Equals, int64(0))
11911191
c.Assert(err, ErrorMatches, "invalid unit.*")
1192+
1193+
in = types.Time{
1194+
Time: types.FromDate(0000, 00, 00, 00, 00, 00, 0000),
1195+
Type: mysql.TypeTimestamp,
1196+
Fsp: types.DefaultFsp,
1197+
}
1198+
1199+
res, err = types.ExtractDatetimeNum(&in, "day")
1200+
c.Assert(err, IsNil)
1201+
c.Assert(res, Equals, int64(0))
1202+
1203+
res, err = types.ExtractDatetimeNum(&in, "week")
1204+
c.Assert(err, IsNil)
1205+
c.Assert(res, Equals, int64(0))
1206+
1207+
res, err = types.ExtractDatetimeNum(&in, "MONTH")
1208+
c.Assert(err, IsNil)
1209+
c.Assert(res, Equals, int64(0))
1210+
1211+
res, err = types.ExtractDatetimeNum(&in, "QUARTER")
1212+
c.Assert(err, IsNil)
1213+
c.Assert(res, Equals, int64(0))
1214+
1215+
res, err = types.ExtractDatetimeNum(&in, "YEAR")
1216+
c.Assert(err, IsNil)
1217+
c.Assert(res, Equals, int64(0))
11921218
}
11931219

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

0 commit comments

Comments
 (0)