New Feature and Technique: The Difference Between Two Times
By Noreen Redden
FOCUS for S/390, and WEBFOCUS 4.3.6 gave us the ability to work with Relational Date Time Stamps. In addition to the capability of describing an integer field containing not only date but also time in seconds or milliseconds, several other functions are available.
Furthermore, we now have a function that takes alpha fields and stores into date-time fields. That means we can subtract not only two dates using the subroutine DATEDIFF (which also supports the difference in years, months, working days, etc.), but also two date-times.
For more information on DATEDIFF, click here. You may find the date/time information here.
However, HDIFF returns an answer in the specified units. So when subtracting two times, if you specify the difference in hours, for instance, an answer might be 31.5 hours, which of course means 1 day, 7 hours and 30 minutes. It gets even worse if you request minutes or seconds
because the translation from seconds to days/hours:minutes:seconds is tedious at best.
What to do? Functions to the rescue.
In this case, I have set up a function containing the following arguments:
| Argument |
Argument Format |
| End Date |
HYYMDSA (YYMMDD HH:MM:SSA (AM/PM)) |
| Start Date |
HYYMDSA |
| Day Indicator |
A1 Y display days/N no day display |
| Seconds Indicator |
A1 Y display seconds/N no seconds |
| HDIFE (output) |
A25 [n DAYS] hh:mm:[:ss] |
The function may be in an EDASPROF, or FOCPROF (S/390), or any FOCEXEC. Take a look at the following example:
-* DAY IS Y IF YOU WANT TO SEE DAY
-* SECS IS Y IF THE CALCULATION AND
-* DISPLAY SHOULD CONTAIN SECONDS
-* DEFINE FUNCTION HDIFE/A25 (ENDT/HYYMDSA,
-* STDT/HYYMDSA, DAY/A1, SECS/A1)
-* PARM1/A7 = IF SECS EQ 'Y' THEN
-* 'SECONDS' ELSE 'MINUTE';
-* determine difference in minutes or
-* seconds based on argument 4
-* HDIFF/D12.2 = HDIFF
-* (ENDT,STDT,PARM1,'D12.2');
-* Calculate Days if argument 3 set to Y
-* DAYS/I5= IF DAY EQ 'Y' AND SECS EQ 'Y'
-* THEN HDIFF / (24 * 60 * 60)
-* ELSE IF DAY EQ 'Y'
-* THEN HDIFF / (24 * 60) ELSE 0;
-* DAYEQ/I9 = IF SECS EQ 'Y' THEN DAYS *
-* 3600 * 24 ELSE DAYS * 60 * 24;
-* DAYALPHA/A12 = IF DAY EQ 'Y' THEN
-* (FTOA(DAYS,'(D5)','A6')) || ' DAYS '
-* ELSE ' ';
-* Convert minutes or seconds remaining -
-* to Integer # hours
-* HRS/I5 = IF DAY EQ 'Y' AND SECS EQ 'Y'
-* THEN
-* (HDIFF - (DAYS * 24 * 3600)) / 3600
-* ELSE
-* IF SECS EQ 'Y' THEN HDIFF / 3600 ELSE
-* IF DAY EQ 'Y' THEN (HDIFF - (DAYS * 24
-* * 60))/60 ELSE
-* HDIFF / 60 ;
-* HRALPHA/A6 =
-* LJUST(5,(FTOA(HRS,'(D5c)','A5')),'A5');
-* HREQ/I9 = IF SECS EQ 'Y' THEN HRS *
-* 3600 ELSE HRS * 60;
-* Convert minutes or seconds remaining to
-* minutes
-* MINS/I2 = IF SECS EQ 'Y' THEN (HDIFF -
-* DAYEQ - HREQ) / 60 ELSE
-* HDIFF - DAYEQ - HREQ ;
-* MINEQ/I9 = IF SECS EQ 'Y' THEN MINS *
-* 60 ELSE MINS;
-* MINALPHA/A2 = EDIT(MINS);
-* remainder is seconds
-* (if argument 4 is Y)
-* SECONDS/I2S= IF SECS NE 'Y' THEN 0 ELSE
-* HDIFF - DAYEQ - HREQ - MINEQ;
-* SECALPHA/A3 = IF SECS EQ 'Y' THEN ':' |
-* EDIT(SECONDS) ELSE ' ';
-* establish the display
-* HDIFE/A25 = DAYALPHA || (' ' | HRALPHA)
-* || ':' || MINALPHA || SECALPHA;
-* END
Because this code is established as a function, it can be used against any file, and any fields, so long as the format complies with the specifications.
DEFINE FILE DELIVERY
-* DELIVERY is the Date/Time for the
actual delivery to the customer.
-* ORDERED is the Date/Time when the
product was ordered.
OUTTIME/A25 =
HDIFE(DELIVERY,ORDERED,'Y','Y');
END
TABLE FILE DELIVER
PRINT ORDERED AS ‘ORDERED’ DELIVERY AS
‘DATE,DELIVERED’
OUTTIME AS ‘TIME BETWEEN,ORDERED AND
DELIVERED’
BY DIVISION BY PART_NO
END
The SUBROUTINE HINPUT will create a date in the correct format from an alpha field. So let us suppose that there are actually four fields DELIVER DATE and DELIVERY TIME, and ORDER DATE and ORDER TIME. Then we can use the above function, but must first convert into DELIVERY and ORDER into date/time fields.
DEFINE FILE NEWDEL
DEL_DT/A8YYMD = DELIVER_DATE;
ORD_DT/A8YYMD = ORDER_DATE;
STARTA/A20 = ORD_DT | ORD_TIME;
ENDA/A20 = DEL_DT | DEL_TIME;
STARTT/HYYMDSA = HINPUT(20,STARTA,8,'HYYMDSA');
ENDT/HYYMDSA = HINPUT(20,ENDA,8,'HYYMDSA');
TME/A25 = HDIFE(ENDA,STARTT,'Y','Y');
END

|
|