@@ -188,10 +188,10 @@ func (t datetimeType) Convert(ctx context.Context, v interface{}) (interface{},
188188 return nil , sql .InRange , nil
189189 }
190190 res , err := ConvertToTime (ctx , v , t )
191- if err != nil {
191+ if err != nil && ! sql . ErrTruncatedIncorrect . Is ( err ) {
192192 return nil , sql .OutOfRange , err
193193 }
194- return res , sql .InRange , nil
194+ return res , sql .InRange , err
195195}
196196
197197// precisionConversion is a conversion ratio to divide time.Second by to truncate the appropriate amount for the
@@ -206,7 +206,7 @@ func ConvertToTime(ctx context.Context, v interface{}, t datetimeType) (time.Tim
206206 }
207207
208208 res , err := t .ConvertWithoutRangeCheck (ctx , v )
209- if err != nil {
209+ if err != nil && ! sql . ErrTruncatedIncorrect . Is ( err ) {
210210 return time.Time {}, err
211211 }
212212
@@ -227,7 +227,7 @@ func ConvertToTime(ctx context.Context, v interface{}, t datetimeType) (time.Tim
227227 if validated == nil {
228228 return time.Time {}, ErrConvertingToTimeOutOfRange .New (v , t )
229229 }
230- return validated .(time.Time ), nil
230+ return validated .(time.Time ), err
231231 }
232232
233233 switch t .baseType {
@@ -245,7 +245,7 @@ func ConvertToTime(ctx context.Context, v interface{}, t datetimeType) (time.Tim
245245 }
246246 }
247247
248- return res , nil
248+ return res , err
249249}
250250
251251// ConvertWithoutRangeCheck converts the parameter to time.Time without checking the range.
@@ -267,7 +267,7 @@ func (t datetimeType) ConvertWithoutRangeCheck(ctx context.Context, v interface{
267267 }
268268 // TODO: consider not using time.Parse if we want to match MySQL exactly ('2010-06-03 11:22.:.:.:.:' is a valid timestamp)
269269 var parsed bool
270- res , parsed = parseDatetime (value )
270+ res , parsed , err = t . parseDatetime (value )
271271 if ! parsed {
272272 return zeroTime , ErrConvertingToTime .New (v )
273273 }
@@ -361,20 +361,24 @@ func (t datetimeType) ConvertWithoutRangeCheck(ctx context.Context, v interface{
361361 res = res .Truncate (24 * time .Hour )
362362 }
363363
364- return res , nil
364+ return res , err
365365}
366366
367- func parseDatetime (value string ) (time.Time , bool ) {
368- end := len (value )
367+ func (t datetimeType ) parseDatetime (value string ) (time.Time , bool , error ) {
368+ valueLen := len (value )
369+ end := valueLen
369370 for end > 0 {
370371 for _ , layout := range TimestampDatetimeLayouts {
371372 if t , err := time .Parse (layout , value [0 :end ]); err == nil {
372- return t .UTC (), true
373+ if end != valueLen {
374+ err = sql .ErrTruncatedIncorrect .New (t , value )
375+ }
376+ return t .UTC (), true , err
373377 }
374378 }
375379 end --
376380 }
377- return time.Time {}, false
381+ return time.Time {}, false , nil
378382}
379383
380384// Equals implements the Type interface.
0 commit comments