go-sql-driver/mysql · error

invalid timeTruncate value: %v, error: %w

Error message

invalid timeTruncate value: %v, error: %w

What it means

Returned by the DSN parser when the `timeTruncate` query parameter cannot be parsed by Go's time.ParseDuration. The `timeTruncate` feature truncates time.Time values to a fixed duration (e.g. to the second) before writing them to the wire via appendDateTime (connection.go:380, packets.go:1211). A malformed duration string aborts DSN parsing entirely.

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 key

View on GitHub (pinned to c426bd9379)

Solutions

  1. Use a valid Go duration string with a unit: `timeTruncate=1s`, `1m`, `500ms`, `1h`, or `1h30m`.
  2. Remove the timeTruncate parameter entirely if you do not need truncation; it is optional.
  3. Validate the duration in Go with time.ParseDuration before interpolating it into the DSN.

Example fix

// before
sql.Open("mysql", "u:p@tcp(127.0.0.1:3306)/db?parseTime=true&timeTruncate=1sec")
// after
sql.Open("mysql", "u:p@tcp(127.0.0.1:3306)/db?parseTime=true&timeTruncate=1s")
Defensive patterns

Strategy: validation

Validate before calling

if _, err := time.ParseDuration(rawTimeTruncate); err != nil {
    return fmt.Errorf("timeTruncate must be a Go duration (e.g. 1s, 500ms): %w", err)
}

Type guard

func validDuration(s string) bool { _, err := time.ParseDuration(s); return err == nil }

Prevention

When it happens

Trigger: Calling mysql.ParseDSN or sql.Open with a DSN whose timeTruncate value is not a valid Go duration, e.g. `?timeTruncate=abc`, `?timeTruncate=1` (bare number with no unit), or `?timeTruncate=1hour` (Go wants `1h`). The error is produced at dsn.go:610 when time.ParseDuration returns non-nil.

Common situations: Translating a duration from another language/notation into the DSN; forgetting that Go durations require a unit suffix; typos like `timeTruncate=1h30m` is valid but `timeTruncate=1:30` is not; copying a DSN template that used a non-Go duration format.

Related errors


AI-assisted analysis of go-sql-driver/mysql@c426bd9379 (2026-08-04). Data as JSON: /data/errors/fe1a917941960cc0.json. Report an issue: GitHub.