{"record":{"id":"42183e9b5b7c748a","repo":"puppetlabs/puppet","slug":"unable-to-parse-str-using-format-format","errorCode":null,"errorMessage":"Unable to parse '%{str}' using format '%{format}'","messagePattern":"Unable to parse '%(.+?)' using format '%(.+?)'","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/puppet/pops/time/timestamp.rb","lineNumber":94,"sourceCode":"      format.each do |fmt|\n        parsed = DateTime._strptime(str, fmt)\n        next if parsed.nil?\n\n        if parsed.include?(:leftover) || (has_timezone && parsed.include?(:zone))\n          parsed = nil\n          next\n        end\n        break\n      end\n      if parsed.nil?\n        raise ArgumentError, _(\n          \"Unable to parse '%{str}' using any of the formats %{formats}\"\n        ) % { str: str, formats: format.join(', ') }\n      end\n    else\n      parsed = DateTime._strptime(str, format)\n      if parsed.nil? || parsed.include?(:leftover)\n        raise ArgumentError, _(\"Unable to parse '%{str}' using format '%{format}'\") % { str: str, format: format }\n      end\n\n      if has_timezone && parsed.include?(:zone)\n        raise ArgumentError, _(\n          'Using a Timezone designator in format specification is mutually exclusive to providing an explicit timezone argument'\n        )\n      end\n    end\n    unless has_timezone\n      timezone = parsed[:zone]\n      has_timezone = !timezone.nil?\n    end\n    fraction = parsed[:sec_fraction]\n\n    # Convert msec rational found in _strptime hash to usec\n    fraction *= 1_000_000 unless fraction.nil?\n\n    # Create the Time instance and adjust for timezone","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/pops/time/timestamp.rb#L76-L112","documentation":"Timestamp.parse with a single (non-Array) format String delegates to DateTime._strptime and raises 'Unable to parse ... using format ...' when the result is nil (no match at all) or contains :leftover (the format matched a prefix but trailing text remains). This is the single-format counterpart of error 554.","triggerScenarios":"Timestamp.parse('31-01-2023', '%F') (day-first vs year-first), '2023-01-31 12:00:00 extra', '%F' against a string with time when format omits %T, or a format directive the string does not satisfy.","commonSituations":"Hard-coding one format for heterogeneous input (EU vs US date order); strings with trailing whitespace/units; fractional seconds present but format lacks .%N; ISO8601 with 'Z' or offset parsed by a format without a zone directive.","solutions":["Match the format to the actual string: '2023-01-31T12:00:00.123' needs '%FT%T.%N'","Strip the input: str.strip, and remove trailing junk before parsing","Switch to an Array of formats to tolerate variants (the :default set covers common ISO shapes)","Ambiguous day/month order cannot be fixed by formats - enforce an input contract upstream"],"exampleFix":"// before\nts = Timestamp.parse(raw, '%F %T')   # raw '2023-01-31T12:00:00.500'\n\n// after\nts = Timestamp.parse(raw.strip, '%FT%T.%N')","handlingStrategy":"try-catch","validationCode":"re = /\\A\\d{4}-\\d{2}-\\d{2}[ T]\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?\\z/\nraise DataError, \"bad shape #{str.inspect}\" unless str.to_s.strip.match?(re)\nts = Timestamp.parse(str.strip, '%F %T.%N')","typeGuard":null,"tryCatchPattern":"begin\n  ts = Timestamp.parse(str, fmt)\nrescue ArgumentError => e\n  ts = Timestamp.parse(str, :default)   # fall back to the built-in set\n  raise DataError, \"unparseable #{str.inspect}\" if ts.nil?\nend","preventionTips":["Prefer an Array of formats (or :default) over one brittle format for external input","Include .%N when inputs may carry fractional seconds","strip inputs; leftover text is the second most common cause after wrong directives"],"tags":["puppet","timestamp","parse-error","format-string","argument-error"],"backgroundTag":"datetime-parse-failed","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}