apple/pkl · error · VmException

cannotConvertToIsoDuration

cannotConvertToIsoDuration

Error message

cannotConvertToIsoDuration

What it means

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.

Source

Thrown at pkl-core/src/main/java/org/pkl/core/stdlib/base/DurationNodes.java:52

    @Specialization(guards = "isMathematicalInteger(self.getValue())")
    protected long evalInt(VmDuration self) {
      return (long) self.getValue();
    }

    @Specialization(guards = "!isMathematicalInteger(self.getValue())")
    protected double evalFloat(VmDuration self) {
      return self.getValue();
    }
  }

  public abstract static class isoString extends ExternalPropertyNode {
    @Specialization
    @TruffleBoundary
    protected String eval(VmDuration self) {
      try {
        return DurationUtils.toIsoString(self.getValue(), self.getUnit());
      } catch (ArithmeticException e) {
        throw exceptionBuilder().evalError("cannotConvertToIsoDuration", self).build();
      }
    }
  }

  public abstract static class unit extends ExternalPropertyNode {
    @Specialization
    protected String eval(VmDuration self) {
      return self.getUnit().getSymbol();
    }
  }

  public abstract static class isPositive extends ExternalPropertyNode {
    @Specialization
    protected boolean eval(VmDuration self) {
      return self.getValue() >= 0;
    }
  }

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Reduce the magnitude of the duration value before conversion.
  2. Break very large durations into a smaller unit composition before serialization.
  3. Check where the duration is computed; clamp or validate the input that produced the extreme value.
  4. If the ISO string is only for display, pre-format the value in a smaller unit (e.g. years) manually.

Example fix

// before
iso = Duration(1e18, "d").toIsoString()  // overflow
// after
iso = Duration(2.7e15, "y").toIsoString()  // express in a unit that fits
Defensive patterns

Strategy: validation

Validate before calling

// Pkl: check duration magnitude before toIsoString
isValidForIso = (d) -> d.isPositive && d.value < 1e15

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/36ead707a4b20655. Report an issue: GitHub.