go-sql-driver/mysql · error
invalid timeTruncate value
Error message
invalid timeTruncate value: %v, error: %w
What it means
The 'timeTruncate' DSN parameter is parsed with time.ParseDuration and controls truncation of scanned time values to the given granularity (e.g. "1s" drops sub-second precision). This error wraps the ParseDuration failure when the value is not a valid Go duration string.
Solutions
- Use a valid Go duration such as '1s', '500ms', or '1m'.
- Remove the timeTruncate parameter entirely if you do not need time truncation.
- Confirm the value against time.ParseDuration rules (number + optional fraction + unit: ns/us/ms/s/m/h).
Example fix
// before
sql.Open("mysql", "user@tcp(127.0.0.1:3306)/db?parseTime=true&timeTruncate=second")
// after
sql.Open("mysql", "user@tcp(127.0.0.1:3306)/db?parseTime=true&timeTruncate=1s") Defensive patterns
Strategy: validation
Validate before calling
// Validate the duration string before building the DSN.
import "time"
if _, err := time.ParseDuration(timeTruncateVal); err != nil {
return fmt.Errorf("timeTruncate must be a duration: %w", err)
} Type guard
null
Try / catch
// ParseDSN/DSN-Format errors are returned from ParseDSN; check the error.
if _, err := mysql.ParseDSN(dsn); err != nil {
return err
} Prevention
- Remember timeTruncate takes a Go duration (e.g. 1s), not a word.
- Reuse time.ParseDuration in a unit test for any DSN you assemble.
- Omit timeTruncate unless you specifically need sub-second truncation.
When it happens
Trigger: A DSN containing '?timeTruncate=...' with a value time.ParseDuration rejects, e.g. 'timeTruncate=second', 'timeTruncate=', or 'timeTruncate=1.5' (no unit).
Common situations: Confusing the parameter for a human-readable word like 'seconds' or 'minute'; passing a numeric without a unit; copy-paste from documentation that omits the unit.
Related errors
- invalid connectionAttributes value
- invalid dbname
- can't convert %T to time.Time
- invalid value for server pub key name
- invalid value for TLS config name
AI-assisted analysis of go-sql-driver/mysql@03d76c7e07 (2026-08-07).
Data as JSON: /api/errors/fe1a917941960cc0.
Report an issue: GitHub.
Appendix: source
Thrown at dsn.go:610
var isBool bool
cfg.MultiStatements, isBool = readBool(value)
if !isBool {
return errors.New("invalid bool value: " + value)
}
// time.Time parsing
case "parseTime":
var isBool bool
cfg.ParseTime, isBool = readBool(value)
if !isBool {
return errors.New("invalid bool value: " + value)
}
// time.Time truncation
case "timeTruncate":
cfg.timeTruncate, err = time.ParseDuration(value)
if err != nil {
return fmt.Errorf("invalid timeTruncate value: %v, error: %w", value, err)
}
// I/O read Timeout
case "readTimeout":
cfg.ReadTimeout, err = time.ParseDuration(value)
if err != nil {
return
}
// Reject read-only connections
case "rejectReadOnly":
var isBool bool
cfg.RejectReadOnly, isBool = readBool(value)
if !isBool {
return errors.New("invalid bool value: " + value)
}
// Server public keyView on GitHub (pinned to 03d76c7e07)