go-sql-driver/mysql · error
can't convert %T to time.Time
Error message
can't convert %T to time.Time
What it means
NullTime.Scan accepts only nil, time.Time, []byte (a date/time string), or string. Any other concrete type reaches the fallback and yields this error naming the offending type. It means the value being scanned into a NullTime is incompatible.
Solutions
- Scan into the column's actual type (e.g. int64) and convert to time.Time yourself.
- Write a custom sql.Scanner that converts the source type before assigning.
- Ensure the source column is a MySQL DATE/TIME/DATETIME/TIMESTAMP so the driver yields a time.Time (with parseTime=true) or a scannable string.
Example fix
// before
var nt mysql.NullTime
rows.Scan(&nt) // column is BIGINT epoch -> error
// after (scan int, convert manually)
var epoch int64
rows.Scan(&epoch)
nt := mysql.NullTime{Valid: epoch != 0}
if nt.Valid {
nt.Time = time.Unix(epoch, 0)
} Defensive patterns
Strategy: validation
Validate before calling
// Check the concrete type before scanning when the source is uncertain.
switch v := src.(type) {
case nil, time.Time, []byte, string:
nt.Scan(v)
default:
return fmt.Errorf("unsupported NullTime source: %T", v)
} Type guard
func isNullTimeScannable(v any) bool {
switch v.(type) {
case nil, time.Time, []byte, string:
return true
}
return false
} Try / catch
// NullTime.Scan returns this error; check it.
if err := nt.Scan(val); err != nil {
// convert val to a supported type and retry
} Prevention
- Confirm the source column type matches a temporal MySQL type.
- Use parseTime=true so temporal columns yield time.Time directly.
- Wrap non-standard sources in a custom Scanner.
When it happens
Trigger: Scanning a numeric/decimal/integer column into a *mysql.NullTime, or calling nt.Scan with an int64/float64/struct value. E.g. a column stored as a unix epoch integer scanned directly into NullTime.
Common situations: Column-type mismatch (times stored as BIGINT epoch or DECIMAL); feeding a driver.Value of the wrong shape into Scan; schema drift.
Related errors
- cannot convert type: %T
- invalid timeTruncate value
- non-Value type %T returned from Value
- unsupported type %T, a
- unsupported type %T, a slice of
AI-assisted analysis of go-sql-driver/mysql@03d76c7e07 (2026-08-07).
Data as JSON: /api/errors/966cc21fab4a54e9.
Report an issue: GitHub.
Appendix: source
Thrown at nulltime.go:62
return
}
switch v := value.(type) {
case time.Time:
nt.Time, nt.Valid = v, true
return
case []byte:
nt.Time, err = parseDateTime(v, time.UTC)
nt.Valid = (err == nil)
return
case string:
nt.Time, err = parseDateTime([]byte(v), time.UTC)
nt.Valid = (err == nil)
return
}
nt.Valid = false
return fmt.Errorf("can't convert %T to time.Time", value)
}
// Value implements the driver Valuer interface.
func (nt NullTime) Value() (driver.Value, error) {
if !nt.Valid {
return nil, nil
}
return nt.Time, nil
}
View on GitHub (pinned to 03d76c7e07)