Merge pull request #1047 from overvenus/fix-cast-date-to-int
mysql: fix incorrect cast date to int.
This commit is contained in:
@ -215,9 +215,11 @@ func (t *Time) UnmarshalInLocation(b []byte, loc *time.Location) error {
|
||||
}
|
||||
|
||||
const numberFormat = "20060102150405"
|
||||
const dateFormat = "20060102"
|
||||
|
||||
// ToNumber returns a formatted number.
|
||||
// e.g,
|
||||
// 2012-12-12 -> 20121212
|
||||
// 2012-12-12T10:10:10 -> 20121212101010
|
||||
// 2012-12-12T10:10:10.123456 -> 20121212101010.123456
|
||||
func (t Time) ToNumber() Decimal {
|
||||
@ -225,7 +227,15 @@ func (t Time) ToNumber() Decimal {
|
||||
return ZeroDecimal
|
||||
}
|
||||
|
||||
tfStr := numberFormat
|
||||
// Fix issue #1046
|
||||
// Prevents from converting 2012-12-12 to 20121212000000
|
||||
var tfStr string
|
||||
if t.Type == TypeDate {
|
||||
tfStr = dateFormat
|
||||
} else {
|
||||
tfStr = numberFormat
|
||||
}
|
||||
|
||||
if t.Fsp > 0 {
|
||||
tfStr = fmt.Sprintf("%s.%s", tfStr, strings.Repeat("0", t.Fsp))
|
||||
}
|
||||
|
||||
@ -474,6 +474,29 @@ func (s *testTimeSuite) TestToNumber(c *C) {
|
||||
c.Assert(t.ToNumber().String(), Equals, test.Expect)
|
||||
}
|
||||
|
||||
// Fix issue #1046
|
||||
tblDate := []struct {
|
||||
Input string
|
||||
Fsp int
|
||||
Expect string
|
||||
}{
|
||||
{"12-12-31 11:30:45", 0, "20121231"},
|
||||
{"12-12-31 11:30:45", 6, "20121231"},
|
||||
{"12-12-31 11:30:45.123", 6, "20121231"},
|
||||
{"12-12-31 11:30:45.123345", 0, "20121231"},
|
||||
{"12-12-31 11:30:45.123345", 3, "20121231"},
|
||||
{"12-12-31 11:30:45.123345", 5, "20121231"},
|
||||
{"12-12-31 11:30:45.123345", 6, "20121231"},
|
||||
{"12-12-31 11:30:45.1233457", 6, "20121231"},
|
||||
{"12-12-31 11:30:45.823345", 0, "20121231"},
|
||||
}
|
||||
|
||||
for _, test := range tblDate {
|
||||
t, err := ParseTime(test.Input, TypeDate, 0)
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(t.ToNumber().String(), Equals, test.Expect)
|
||||
}
|
||||
|
||||
tblDuration := []struct {
|
||||
Input string
|
||||
Fsp int
|
||||
|
||||
Reference in New Issue
Block a user