{"record":{"id":"80f0e21011499bb2","repo":"go-sql-driver/mysql","slug":"invalid-time-bytes-s","errorCode":null,"errorMessage":"invalid time bytes: %s","messagePattern":"invalid time bytes: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"utils.go","lineNumber":179,"sourceCode":"\n\t\tsec, err := parseByte2Digits(b[17], b[18])\n\t\tif err != nil {\n\t\t\treturn time.Time{}, err\n\t\t}\n\t\tif len(b) == 19 {\n\t\t\treturn time.Date(year, month, day, hour, min, sec, 0, loc), nil\n\t\t}\n\n\t\tif b[19] != '.' {\n\t\t\treturn time.Time{}, fmt.Errorf(\"bad value for field: `%c`\", b[19])\n\t\t}\n\t\tnsec, err := parseByteNanoSec(b[20:])\n\t\tif err != nil {\n\t\t\treturn time.Time{}, err\n\t\t}\n\t\treturn time.Date(year, month, day, hour, min, sec, nsec, loc), nil\n\tdefault:\n\t\treturn time.Time{}, fmt.Errorf(\"invalid time bytes: %s\", b)\n\t}\n}\n\nfunc parseByteYear(b []byte) (int, error) {\n\tyear, n := 0, 1000\n\tfor i := range 4 {\n\t\tv, err := bToi(b[i])\n\t\tif err != nil {\n\t\t\treturn 0, err\n\t\t}\n\t\tyear += v * n\n\t\tn /= 10\n\t}\n\treturn year, nil\n}\n\nfunc parseByte2Digits(b1, b2 byte) (int, error) {\n\td1, err := bToi(b1)","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/go-sql-driver/mysql/blob/03d76c7e07908e255ce62d126d07ede3f2365d86/utils.go#L161-L197","documentation":"Raised by parseDateTime (utils.go:179) in its default branch: the byte length of the text datetime value is none of the canonical lengths {10 (date), 19 (datetime), 21-26 (datetime with 1-6 fractional digits)}. The driver therefore has no parse template for the value and returns it verbatim in the error. Length is checked before any separator, so this fires for structurally wrong values (truncated, padded, or extra chars).","triggerScenarios":"Scanning a DATE/DATETIME/TIMESTAMP column into time.Time (parseTime=true) or sql.NullTime when the server's text value length is not 10/19/21-26 — e.g. '2020-1-1' (single digits, len 8), '2020-01-01 00:00' (len 16), values with trailing whitespace/garbage, or an empty-but-non-null value. Reached via parseDateTime at packets.go:875 / nulltime.go:52.","commonSituations":"Single-digit month/day (no zero padding), DATETIME values with seconds omitted, rows containing trailing characters or whitespace, mis-typed columns (VARCHAR holding a near-date), or hand-written import files with inconsistent formatting.","solutions":["Identify the bad value: scan into string to capture it, then fix the stored data to a canonical length.","If variable formats are unavoidable, disable parseTime and parse with the correct layout per row.","Pad/normalize on write (LPAD month/day, enforce seconds) so values are always 10 or 19+ chars.","Audit the table: SELECT col, LENGTH(col) FROM t WHERE LENGTH(col) NOT IN (10,19,21,22,23,24,25,26) to find offenders."],"exampleFix":"-- find offending rows\nSELECT id, the_col, LENGTH(the_col) AS len\nFROM events\nWHERE LENGTH(the_col) NOT IN (10,19,21,22,23,24,25,26);\n\n// Go: read raw and coerce\nvar raw string\nrows.Scan(&raw)\nt, err := time.Parse(\"2006-1-2 15:04:05\", strings.TrimSpace(raw))","handlingStrategy":"validation","validationCode":"func isValidMySQLDateTimeLen(s string) bool {\n    switch len(s) {\n    case 10, 19, 21, 22, 23, 24, 25, 26:\n        return true\n    }\n    return false\n}","typeGuard":"func hasCanonicalDateTimeLength(b []byte) bool {\n    switch len(b) {\n    case 10, 19, 21, 22, 23, 24, 25, 26:\n        return true\n    }\n    return false\n}","tryCatchPattern":"var t time.Time\nif err := rows.Scan(&t); err != nil && strings.Contains(err.Error(), \"invalid time bytes\") {\n    var raw string\n    // inspect raw value/length and repair\n}","preventionTips":["Audit tables: SELECT col, LENGTH(col) WHERE LENGTH NOT IN (10,19,21,22,23,24,25,26).","Zero-pad month/day and always include seconds on insert.","Trim whitespace from imported values before insert.","Disable parseTime for tables with known inconsistent formatting."],"tags":["mysql","datetime","parsing","text-protocol","validation","parsetime"],"backgroundTag":null,"analyzedSha":"03d76c7e07908e255ce62d126d07ede3f2365d86","analyzedAt":"2026-08-07T10:39:17.340Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}