%DIFF(op1:op2:*MSECONDS|*SECONDS|*MINUTES|*HOURS|*DAYS|*MONTHS|*YEARS) %DIFF(op1:op2:*MS|*S|*MN|*H|*D|*M|*Y)
%DIFF は、2 つの日付、または時刻の値の間の差 (期間) を生成します。最初のパラメーターと 2 番目のパラメーターは、同じタイプかまたは互換性のあるタイプでなければなりません。 可能な組み合わせは以下のとおりです。
3 番目のパラメーターには単位を指定します。次の単位が有効です。
差異は、第 1 オペランドから第 2 オペランドを減算することによって計算されます。
結果は、すべての剰余を廃棄して切り捨てられます。たとえば、61 分は 1 時間と等しく、59 分は 0 時間に等しくなります。
関数によって戻り値は、数字タイプと期間タイプの両方と互換性があります。 結果は、数 (数値タイプ) あるいは日付、時刻、またはタイム・スタンプ (期間タイプ) に加算することができます。
32 年 9 カ月よりも離れている 2 つのタイム・スタンプの間の差をマイクロ秒単位で得たい場合は、期間の値の限界である 15 桁を超えます。この結果はエラーになるかまたは切り捨てが行なわれます。
詳細については、日付命令または 組み込み関数を参照してください。
D due_date S D INZ(D'2005-06-01') D today S D INZ(D'2004-09-23') D num_days S 15P 0 D start_time S Z D time_taken S 15P 0 /FREE // Determine the number of days between two dates. // If due_date has the value 2005-06-01 and // today has the value 2004-09-23, then // num_days will have the value 251. num_days = %DIFF (due_date: today: *DAYS); // If the arguments are coded in the reverse order, // num_days will have the value -251. num_days = %DIFF (today: due_date: *DAYS); // Determine the number of seconds required to do a task: // 1. Get the starting timestamp // 2. Do the task // 3. Calculate the difference between the current // timestamp and the starting timestamp start_time = %timestamp(); process(); time_taken = %DIFF (%timestamp() : start_time : *SECONDS); /END-FREE
D estimated_end... D S D D prev_start S D INZ(D'2003-06-21') D prev_end S D INZ(D'2003-06-24') /FREE // Add the number of days between two dates // to a third date // prev_start is the date a previous task began // prev_end is the date a previous task ended. // The following calculation will estimate the // date a similar task will end, if it begins // today. // If the current date, returned by %date(), is // 2003-08-15, then estimated_end will be // 2003-08-18. estimated_end = %date() + %DIFF(prev_end : prev_start : *days); /END-FREE