{"record":{"id":"6d15caa110b8c928","repo":"apache/cassandra","slug":"can-t-interpret-r-as-a-date-with-format-s-or-as","errorCode":null,"errorMessage":"can't interpret %r as a date with format %s or as int","messagePattern":"can't interpret %r as a date with format (.+?) or as int","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pylib/cqlshlib/copyutil.py","lineNumber":2032,"sourceCode":"        p = re.compile(r\"(\\d{4})-(\\d{2})-(\\d{2})\\s?(?:'T')?\"  # YYYY-MM-DD[( |'T')]\n                       + r\"(?:(\\d{2}):(\\d{2})(?::(\\d{2})(?:\\.(\\d{1,6}))?))?\"  # [HH:MM[:SS[.NNNNNN]]]\n                       + r\"(?:([+\\-])(\\d{2}):?(\\d{2}))?\")  # [(+|-)HH[:]MM]]\n\n        def convert_datetime(val, **_):\n            try:\n                dtval = datetime.datetime.strptime(val, self.date_time_format)\n                return dtval.timestamp() * 1000\n            except ValueError:\n                pass  # if it's not in the default format we try CQL formats\n\n            m = p.match(val)\n            if not m:\n                try:\n                    # in case of overflow COPY TO prints dates as milliseconds from the epoch, see\n                    # deserialize_date_fallback_int in cqlsh.py\n                    return int(val)\n                except ValueError:\n                    raise ValueError(\"can't interpret %r as a date with format %s or as int\" % (val,\n                                                                                                self.date_time_format))\n\n            # https://docs.python.org/3/library/time.html#time.struct_time\n            tval = time.struct_time((int(m.group(1)), int(m.group(2)), int(m.group(3)),  # year, month, day\n                                    int(m.group(4)) if m.group(4) else 0,  # hour\n                                    int(m.group(5)) if m.group(5) else 0,  # minute\n                                    int(m.group(6)) if m.group(6) else 0,  # second\n                                    0, 1, -1))  # day of week, day of year, dst-flag\n\n            # convert sub-seconds (a number between 1 and 6 digits) to milliseconds\n            milliseconds = 0 if not m.group(7) else int(m.group(7)) * pow(10, 3 - len(m.group(7)))\n\n            if m.group(8):\n                offset = (int(m.group(9)) * 3600 + int(m.group(10)) * 60) * int(m.group(8) + '1')\n            else:\n                offset = -time.timezone\n\n            # scale seconds to millis for the raw value","sourceCodeStart":2014,"sourceCodeEnd":2050,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/pylib/cqlshlib/copyutil.py#L2014-L2050","documentation":"Date columns in COPY FROM accept either a date string matching the configured date format or (as a COPY TO fallback) a millisecond count from the epoch as an int. If the value matches neither, this ValueError is raised.","triggerScenarios":"COPY FROM a CSV cell for a date column whose text doesn't match the DATEFORMAT/timestamp pattern and also isn't parseable as an integer — e.g. '01/02/2026' when the format expects '%Y-%m-%d', or a locale-formatted date.","commonSituations":"Exports from tools using a different date convention (US vs ISO), COPY TO round-trips where dates were printed as epoch millis and hand-edited, mismatch between the WITH DATEFORMAT option and the actual CSV contents.","solutions":["Reformat the date in the CSV to match the configured date_time_format (default ISO '%Y-%m-%d')","Pass WITH DATEFORMAT='<pattern matching your csv>' to the COPY FROM statement","Convert the date to milliseconds-from-epoch as an integer alternative"],"exampleFix":"// before\n1,01/02/2026\n// after\nCOPY t FROM 'f.csv' WITH DATEFORMAT='%m/%d/%Y';\n// or reformat the cell to 2026-01-02","handlingStrategy":"validation","validationCode":"import time\nfmt = '%Y-%m-%d'\nfor row in rows:\n    v = row['d']\n    if not v.isdigit():\n        try:\n            time.strptime(v, fmt)\n        except ValueError:\n            raise ValueError(f\"bad date {v!r}, expected {fmt} or epoch millis\")","typeGuard":null,"tryCatchPattern":"try:\n    run_copy_from(...)\nexcept ValueError as e:\n    print(f\"Date parse failed: {e}\")  # reformat or set DATEFORMAT","preventionTips":["Keep exports and imports using the same DATEFORMAT","Normalize dates to ISO 8601 before import","Round-trip a small sample COPY TO/FROM to verify formats"],"tags":["cqlsh","copy","date","parsing"],"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-14T21:17:11.552Z"}