{"record":{"id":"34f301c333805d16","repo":"apache/cassandra","slug":"timetype-unable-to-coerce-s-to-a-formatted-ti","errorCode":null,"errorMessage":"(TimeType) Unable to coerce '%s' to a formatted time (long)","messagePattern":"\\(TimeType\\) Unable to coerce '(.+?)' to a formatted time \\(long\\)","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/serializers/TimeSerializer.java","lineNumber":67,"sourceCode":"                long result = Long.parseLong(source);\n                if (result < 0 || result >= TimeUnit.DAYS.toNanos(1))\n                    throw new NumberFormatException(\"Input long out of bounds: \" + source);\n                return result;\n            }\n            catch (NumberFormatException e)\n            {\n                throw new MarshalException(String.format(\"Unable to make long (for time) from: '%s'\", source), e);\n            }\n        }\n\n        // Last chance, attempt to parse as time string\n        try\n        {\n            return parseTimeStrictly(source);\n        }\n        catch (IllegalArgumentException e1)\n        {\n            throw new MarshalException(String.format(\"(TimeType) Unable to coerce '%s' to a formatted time (long)\", source), e1);\n        }\n    }\n\n    public <V> void validate(V value, ValueAccessor<V> accessor) throws MarshalException\n    {\n        if (accessor.size(value) != 8)\n            throw new MarshalException(String.format(\"Expected 8 byte long for time (%d)\", accessor.size(value)));\n    }\n\n    @Override\n    public boolean shouldQuoteCQLLiterals()\n    {\n        return true;\n    }\n\n    public String toString(Long value)\n    {\n        if (value == null)","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/serializers/TimeSerializer.java#L49-L85","documentation":"When the input is not a plain long, TimeSerializer attempts strict time-string parsing via parseTimeStrictly (formats like hh:mm:ss[.fraction], optionally with suffixes). IllegalArgumentException from that parser is wrapped as this MarshalException, meaning the string could not be coerced to a time-of-day long.","triggerScenarios":"Inserting strings like '25:00:00' (hour out of range), '12:60:00', '1pm', '2020-01-01 12:00:00' (full datetime), or '12:00' variants unsupported by the strict parser into a time column.","commonSituations":"12-hour clock strings with AM/PM; datetime strings pasted where only time-of-day is allowed; locale formats like '12.00 Uhr'; hour/min/sec out of bounds from user input.","solutions":["Use the strict format HH:MM:SS[.fffffffff] with 24-hour hours (00-23), e.g. '13:30:54.234'","Clamp/validate hour<=23, minute<=59, second<=59 before sending","Pass a plain long of nanoseconds since midnight instead of a string","Use a driver DateRange/Time codec or LocalTime object so the driver formats it correctly"],"exampleFix":"// before\nsession.execute(\"INSERT INTO t (tm) VALUES (?)\", \"1:30 PM\");\n// after\nsession.execute(\"INSERT INTO t (tm) VALUES (?)\", \"13:30:00\");","handlingStrategy":"validation","validationCode":"// strict 24h time-of-day check\nif (!input.matches(\"^([01]\\\\d|2[0-3]):[0-5]\\\\d(:[0-5]\\\\d(\\.\\\\d{1,9})?)?$\")) throw new IllegalArgumentException(\"unsupported time format: \" + input);","typeGuard":"boolean isValidCqlTimeString(String s) {\n  try { LocalTime.parse(s); return true; } catch (DateTimeParseException e) { return false; }\n}","tryCatchPattern":"try {\n  long nanos = TimeSerializer.timeStringToLong(input);\n} catch (MarshalException e) {\n  throw new BadRequestException(\"use 24-hour HH:MM:SS[.fraction] time strings\");\n}","preventionTips":["Convert 12-hour AM/PM input to 24-hour before sending","Strip date components from datetime strings destined for time columns","Validate hour/min/sec bounds in the UI layer","Use LocalTime binding instead of raw strings"],"tags":["cassandra","serialization","time","invalid-date-format"],"backgroundTag":"invalid-date-format","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}