{"record":{"id":"4a27845f20744638","repo":"pentaho/pentaho-kettle","slug":"argument-of-trunc-of-date-has-to-be-between-0-and-5-4a2784","errorCode":null,"errorMessage":"Argument of TRUNC of date has to be between 0 and 5","messagePattern":"Argument of TRUNC of date has to be between 0 and 5","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java","lineNumber":2462,"sourceCode":"        cal.set( Calendar.MONTH, 0 );\n        // DAYS\n      case 4:\n        cal.set( Calendar.DAY_OF_MONTH, 1 );\n        // HOURS\n      case 3:\n        cal.set( Calendar.HOUR_OF_DAY, 0 );\n        // MINUTES\n      case 2:\n        cal.set( Calendar.MINUTE, 0 );\n        // SECONDS\n      case 1:\n        cal.set( Calendar.SECOND, 0 );\n        // MILI-SECONDS\n      case 0:\n        cal.set( Calendar.MILLISECOND, 0 );\n        break;\n      default:\n        throw new RuntimeException( \"Argument of TRUNC of date has to be between 0 and 5\" );\n    }\n    return cal.getTime();\n  }\n\n\n  public static void moveFile( Bowl bowl, ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,\n    Object FunctionContext ) {\n\n    try {\n      if ( ArgList.length == 3\n        && !isNull( ArgList[0] ) && !isNull( ArgList[1] ) && !isUndefined( ArgList[0] )\n        && !isUndefined( ArgList[1] ) ) {\n        FileObject fileSource = null, fileDestination = null;\n\n        try {\n          // Source file to move\n          fileSource = KettleVFS.getInstance( bowl ).getFileObject( (String) ArgList[0] );\n          // Destination filename","sourceCodeStart":2444,"sourceCodeEnd":2480,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java#L2444-L2480","documentation":"The static truncDate(Date, Integer) helper uses an intentional fall-through switch over the level; any level outside 0-5 hits the default branch and throws this RuntimeException. The level selects the truncation precision (0=millisecond, 1=second, 2=minute, 3=hour, 4=day, 5=month), and there is no year option despite what one might assume.","triggerScenarios":"Calling truncDate(d, 6), truncDate(d, -1), or any non-0..5 integer level from JavaScript script code.","commonSituations":"Assuming 6 means 'year' truncation (it is not supported); computing the level dynamically and producing an out-of-range value; confusing this API with ones where level counts up to year; off-by-one when mapping names like 'month' to 6 instead of 5.","solutions":["Use a level between 0 and 5 only (5=month is the coarsest supported)","Clamp or validate the level: level = Math.max(0, Math.min(5, level))","Map precision names explicitly: {0:'ms',1:'s',2:'min',3:'hour',4:'day',5:'month'}","For year truncation, build it manually with new Date(y, 0, 1) logic"],"exampleFix":"// before\nvar td = truncDate(d, 6); // intended year\n// after\nif (level < 0 || level > 5) level = 5;\nvar td = truncDate(d, level); // 5 = month is the coarsest supported","handlingStrategy":"validation","validationCode":"function safeLevel(level) {\n  var L = parseInt(level, 10);\n  if (isNaN(L) || L < 0 || L > 5) throw new Error('TRUNC date level must be 0-5');\n  return L;\n}\nvar td = truncDate(d, safeLevel(level));","typeGuard":"function isValidTruncLevel(v) { return Number.isInteger(v) && v >= 0 && v <= 5; }","tryCatchPattern":"try {\n  var td = truncDate(d, level);\n} catch (e) {\n  if (String(e.message).indexOf('between 0 and 5') >= 0) {\n    var td = truncDate(d, 0); // or correct the level\n  } else { throw e; }\n}","preventionTips":["Memorize the valid levels: 0=ms, 1=s, 2=min, 3=hour, 4=day, 5=month — there is no year level","Validate dynamically computed levels against [0,5] before calling","Do not assume 1-based indexing; the range is 0-5, not 1-5","Use named constants instead of raw numbers in scripts"],"tags":["javascript","scripting","date","argument-out-of-range"],"backgroundTag":"value-out-of-range","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}