{"record":{"id":"36ead707a4b20655","repo":"apple/pkl","slug":"cannotconverttoisoduration","errorCode":"cannotConvertToIsoDuration","errorMessage":"cannotConvertToIsoDuration","messagePattern":"cannotConvertToIsoDuration","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/stdlib/base/DurationNodes.java","lineNumber":52,"sourceCode":"    @Specialization(guards = \"isMathematicalInteger(self.getValue())\")\n    protected long evalInt(VmDuration self) {\n      return (long) self.getValue();\n    }\n\n    @Specialization(guards = \"!isMathematicalInteger(self.getValue())\")\n    protected double evalFloat(VmDuration self) {\n      return self.getValue();\n    }\n  }\n\n  public abstract static class isoString extends ExternalPropertyNode {\n    @Specialization\n    @TruffleBoundary\n    protected String eval(VmDuration self) {\n      try {\n        return DurationUtils.toIsoString(self.getValue(), self.getUnit());\n      } catch (ArithmeticException e) {\n        throw exceptionBuilder().evalError(\"cannotConvertToIsoDuration\", self).build();\n      }\n    }\n  }\n\n  public abstract static class unit extends ExternalPropertyNode {\n    @Specialization\n    protected String eval(VmDuration self) {\n      return self.getUnit().getSymbol();\n    }\n  }\n\n  public abstract static class isPositive extends ExternalPropertyNode {\n    @Specialization\n    protected boolean eval(VmDuration self) {\n      return self.getValue() >= 0;\n    }\n  }\n","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/stdlib/base/DurationNodes.java#L34-L70","documentation":"DurationNodes' toIso specialization converts a Pkl Duration to its ISO-8601 string form via DurationUtils.toIsoString. If the conversion overflows (ArithmeticException from Math.multiplyExact/addExact when scaling to nanoseconds), Pkl throws the `cannotConvertToIsoDuration` eval error naming the offending duration.","triggerScenarios":"Calling `duration.toIsoString()` (DurationNodes.java:52) with a duration whose value cannot be represented when converted to the ISO representation — e.g. `Duration(1e18, \"d\")` where days-to-nanoseconds multiplication overflows long arithmetic.","commonSituations":"Computed durations from large inputs (sums of intervals, astronomical time spans) or a misconfigured value with an inflated unit/value in config that later gets serialized to output.","solutions":["Reduce the magnitude of the duration value before conversion.","Break very large durations into a smaller unit composition before serialization.","Check where the duration is computed; clamp or validate the input that produced the extreme value.","If the ISO string is only for display, pre-format the value in a smaller unit (e.g. years) manually."],"exampleFix":"// before\niso = Duration(1e18, \"d\").toIsoString()  // overflow\n// after\niso = Duration(2.7e15, \"y\").toIsoString()  // express in a unit that fits","handlingStrategy":"validation","validationCode":"// Pkl: check duration magnitude before toIsoString\nisValidForIso = (d) -> d.isPositive && d.value < 1e15","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp computed durations to sane ranges","Express very long spans in larger units (y) before serializing","Validate the inputs that feed duration arithmetic"],"tags":["pkl","duration","iso-8601","overflow"],"backgroundTag":"argument-out-of-range","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}