{"record":{"id":"0f6c3221a1e82380","repo":"puppetlabs/puppet","slug":"unable-to-parse-str-using-any-of-the-formats-0f6c32","errorCode":null,"errorMessage":"Unable to parse '%{str}' using any of the formats %{formats}","messagePattern":"Unable to parse '%(.+?)' using any of the formats %(.+?)","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/puppet/pops/time/timestamp.rb","lineNumber":87,"sourceCode":"    has_timezone = !(timezone.nil? || timezone.empty? || timezone == :default)\n    if format.nil? || format == :default\n      format = has_timezone ? DEFAULT_FORMATS_WO_TZ : DEFAULT_FORMATS\n    end\n\n    parsed = nil\n    if format.is_a?(Array)\n      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?","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/pops/time/timestamp.rb#L69-L105","documentation":"Timestamp.parse with an Array of formats (or :default, which expands to the DEFAULT_FORMATS array) tries each format via DateTime._strptime; a candidate is discarded when it returns nil, leaves :leftover (trailing unparsed text), or - when an explicit timezone argument was given - the parsed hash contains :zone. If every candidate is exhausted, ArgumentError 'Unable to parse ... using any of the formats ...' lists the joined formats.","triggerScenarios":"Timestamp.parse('2023-13-45T99:99') against defaults (out-of-range fields), '2023/01/31' with slash separators against %F dash formats, '2023-01-31 12:00 hello' (leftover), or a zone-bearing string plus an explicit timezone argument (each candidate rejected via the :zone rule).","commonSituations":"Feeding log lines, database dumps, or third-party API timestamps with unexpected separators or precision into Timestamp()/Timestamp.parse; DST-ambiguous strings; inputs with trailing timezone names while a timezone arg was also supplied.","solutions":["Pass your own Array of formats covering the real shapes: Timestamp.parse(str, ['%d/%m/%Y %T', '%m/%d/%Y %T'])","Normalize the string first - gsub('/', '-'), strip trailing zone names or whitespace","If you supply a timezone argument, drop %Z/%z from the candidate formats (the :zone rejection rule)","Pre-validate with a cheap regex per accepted shape before calling parse"],"exampleFix":"// before\nts = Puppet::Pops::Time::Timestamp.parse(api_date)   # '2023/01/31 12:00:00' -> raise listing defaults\n\n// after\nts = Puppet::Pops::Time::Timestamp.parse(\n  api_date.gsub('/', '-'),\n  ['%F %T', '%FT%T', '%F %T.%N', '%F']\n)","handlingStrategy":"try-catch","validationCode":"SHAPES = [/\\A\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(Z|[+-]\\d{2}:?\\d{2})?\\z/, /\\A\\d{4}-\\d{2}-\\d{2}( \\d{2}:\\d{2}:\\d{2})?\\z/]\nraise DataError, \"unrecognized timestamp shape\" unless SHAPES.any? { |r| str.match?(r) }","typeGuard":null,"tryCatchPattern":"begin\n  ts = Timestamp.parse(str, formats)\nrescue ArgumentError => e\n  raise DataError, \"input #{str.inspect} matches none of #{formats.join(', ')}\"\nend","preventionTips":["Supply your own format array matching the producer's real output, including separator style","Normalize separators and strip trailing text before parsing","When passing a timezone argument, keep %Z/%z out of the candidate formats"],"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"}