Skip to content

Commit a0b7544

Browse files
committed
fix UNIX_TIMESTAMP issue
1 parent 9fec304 commit a0b7544

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

sql/types/datetime.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,14 @@ func ConvertToTime(ctx context.Context, v interface{}, t datetimeType) (time.Tim
214214
return zeroTime, nil
215215
}
216216

217+
// Round the date to the precision of this type
218+
if t.precision < 6 {
219+
truncationDuration := time.Second / time.Duration(precisionConversion[t.precision])
220+
res = res.Round(truncationDuration)
221+
} else {
222+
res = res.Round(time.Microsecond)
223+
}
224+
217225
if t == DatetimeMaxLimit {
218226
validated := ValidateTime(res)
219227
if validated == nil {
@@ -260,7 +268,6 @@ func (t datetimeType) ConvertWithoutRangeCheck(ctx context.Context, v interface{
260268
// TODO: consider not using time.Parse if we want to match MySQL exactly ('2010-06-03 11:22.:.:.:.:' is a valid timestamp)
261269
var parsed bool
262270
res, parsed = parseDatetime(value)
263-
res = res.Round(time.Microsecond)
264271
if !parsed {
265272
return zeroTime, ErrConvertingToTime.New(v)
266273
}

0 commit comments

Comments
 (0)