pentaho/pentaho-kettle · error

Argument of TRUNC of date has to be between 0 and 5

Error message

Argument of TRUNC of date has to be between 0 and 5

What it means

truncDate's level argument must be an integer between 0 and 5 (millisecond, second, minute, hour, day, month). The switch's default branch throws this error for any other value. It is a value-range validation failure inside the valid 2-argument call.

Solutions

  1. Use a level in 0..5: 0=ms,1=sec,2=min,3=hour,4=day,5=month.
  2. Validate before calling: if (level >= 0 && level <= 5) { ... } else fall back.
  3. If you need year truncation or other granularities, use Calendar/date functions in the script instead of truncDate.
  4. Clamp or map external level data: level = Math.max(0, Math.min(5, level)).

Example fix

// before
var d = truncDate(orderDate, 7); // invalid level
// after
var d = truncDate(orderDate, 5); // truncate to month start
Defensive patterns

Strategy: validation

Validate before calling

if (lvl === undefined || lvl === null || lvl < 0 || lvl > 5) { lvl = 4; } // default to day truncation
var d = truncDate(orderDate, lvl);

Type guard

function isValidLevel(v) { return Number.isInteger(v) && v >= 0 && v <= 5; }

Try / catch

try { d = truncDate(dt, lvl); } catch (e) { if (e.message.indexOf('between 0 and 5') >= 0) { d = dt; } else { throw e; } }

Prevention

When it happens

Trigger: Calling truncDate(d, 6) or higher, a negative level, or a JS number that converts to an Integer outside 0-5 (e.g. level computed from variables or a field containing 99).

Common situations: Misremembering the level scale (expecting 1-12 for months or Calendar constants like Calendar.MONTH=2 semantics confusion); passing a level read from a lookup table with bad data.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of pentaho/pentaho-kettle@f3058517a1 (2026-09-13). Data as JSON: /api/errors/8227876471129f5d. Report an issue: GitHub.

Appendix: source

Thrown at engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java:2552

        cal.set( Calendar.MONTH, 0 );
        // DAYS
      case 4:
        cal.set( Calendar.DAY_OF_MONTH, 1 );
        // HOURS
      case 3:
        cal.set( Calendar.HOUR_OF_DAY, 0 );
        // MINUTES
      case 2:
        cal.set( Calendar.MINUTE, 0 );
        // SECONDS
      case 1:
        cal.set( Calendar.SECOND, 0 );
        // MILI-SECONDS
      case 0:
        cal.set( Calendar.MILLISECOND, 0 );
        break;
      default:
        throw Context.reportRuntimeError( "Argument of TRUNC of date has to be between 0 and 5" );
    }
    return cal.getTime();
  }

  public static void moveFile( Context actualContext, Scriptable actualObject, Object[] ArgList,
    Function FunctionContext ) {

    try {
      if ( ArgList.length == 3
        && !isNull( ArgList[0] ) && !isNull( ArgList[1] ) && !isUndefined( ArgList[0] )
        && !isUndefined( ArgList[1] ) ) {
        FileObject fileSource = null, fileDestination = null;

        try {
          // Source file to move
          Bowl bowl = getBowl( actualObject );
          fileSource = KettleVFS.getInstance( bowl ).getFileObject( Context.toString( ArgList[0] ) );
          // Destination filename

View on GitHub (pinned to f3058517a1)