{"record":{"id":"549aa6c3d9937fa0","repo":"apple/pkl","slug":"cannot-convert-pkl-duration-this-to-java-time-d","errorCode":null,"errorMessage":"Cannot convert Pkl duration `this` to `java.time.Duration`.","messagePattern":"Cannot convert Pkl duration `this` to `java\\.time\\.Duration`\\.","errorType":"exception","errorClass":"ArithmeticException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/Duration.java","lineNumber":165,"sourceCode":"\n  /** Returns the value of this duration measured in whole {@link DurationUnit#HOURS}. */\n  public long inWholeHours() {\n    return Math.round(inHours());\n  }\n\n  /** Returns the value of this duration measured in whole {@link DurationUnit#DAYS}. */\n  public long inWholeDays() {\n    return Math.round(inDays());\n  }\n\n  /**\n   * Converts this duration to a {@link java.time.Duration}. If {@link #getValue()} is NaN,\n   * +/-Infinity or too large to fit into {@link java.time.Duration}, {@link ArithmeticException} is\n   * thrown.\n   */\n  public java.time.Duration toJavaDuration() {\n    if (!Double.isFinite(value)) {\n      throw new ArithmeticException(\n          \"Cannot convert Pkl duration `\" + this + \"` to `java.time.Duration`.\");\n    }\n\n    var l = (long) value;\n    if (l == value) {\n      // `value` is a mathematical integer that fits into a long.\n      // Hence this duration is easy to convert without risk of rounding errors.\n      // Throws ArithmeticException if this duration doesn't fit into java.time.Duration (e.g.,\n      // Long.MAX_VALUE days).\n      return java.time.Duration.of(l, unit.toChronoUnit());\n    }\n\n    var seconds = convertValueTo(DurationUnit.SECONDS);\n    var secondsPart = (long) seconds;\n    var nanosPart = (long) ((seconds - secondsPart) * 1_000_000_000);\n    // If `seconds` is infinite or too large to fit into java.time.Duration,\n    // this throws ArithmeticException because one the following holds:\n    //   secondsPart == Long.MAX_VALUE && nanosPart >= 1_000_000_000","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/Duration.java#L147-L183","documentation":"Pkl's Duration.toJavaDuration() converts the duration value to java.time.Duration, but java.time.Duration cannot represent NaN or +/-Infinity seconds. When the Pkl duration's value is non-finite, an ArithmeticException is thrown with the message naming this Pkl duration.","triggerScenarios":"Calling toJavaDuration() on a Pkl duration whose value is NaN, +Infinity, or -Infinity (e.g. produced from 1/0-style arithmetic or from a Pkl `Infinity*min` expression).","commonSituations":"Pkl computed durations involving division by zero; durations imported from configs with extreme values; tests exercising edge cases of duration conversion.","solutions":["Check Double.isFinite(duration.value) before calling toJavaDuration().","Fix the upstream Pkl expression producing the non-finite duration.","Clamp or substitute a finite sentinel duration before converting."],"exampleFix":"// before\nvar d = pklDuration.toJavaDuration();\n// after\nif (Double.isFinite(pklDuration.value)) {\n  var d = pklDuration.toJavaDuration();\n} else {\n  var d = Duration.ZERO; // or handle explicitly\n}","handlingStrategy":"validation","validationCode":"if (!Double.isFinite(pklDuration.value)) throw new ArithmeticException(\"non-finite duration\");","typeGuard":"static boolean isConvertible(Duration d) { return Double.isFinite(d.value); }","tryCatchPattern":"try { return pklDuration.toJavaDuration(); } catch (ArithmeticException e) { return Duration.ZERO; /* or propagate */ }","preventionTips":["Validate Pkl durations at config load time for finiteness","Fix upstream Pkl expressions that divide by zero or produce Infinity"],"tags":["java","duration","arithmetic-exception","non-finite"],"backgroundTag":"value-out-of-range","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}