Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref

Author Message
Geo

New User

Joined: 25 May 2006
Posts: 1

 Posted: Thu May 25, 2006 10:00 am    Post subject: How to calculate the time difference? Hi, How to calculate the difference between 2 given times. Eg. Input: HHMMSSXX 04:45:12:45 04:42:54:23 How to write a program to calculate the difference between 2 given time like the above mentioned? Is there any built-in function? It's quite emergency.Someone plz give me a reply as early as possible.

gskulkarni

New User

Joined: 01 Mar 2006
Posts: 70

Posted: Thu May 25, 2006 11:09 am    Post subject:

This is a tricky one. The date difference can be calculated using COBOL functions. I have stated an example below.
The following example shows how to calculate a due date that is 90
days from today. The first eight characters returned by the CURRENT-DATE function
represent the date in a 4-digit year, 2-digit month, and 2-digit day format
(YYYYMMDD). In the example, this date is converted to its integer value. Then 90
is added to this value, and the integer is converted back to the YYYYMMDD format.
 Code: 01 Date-1-YYYYMMDD Pic 9(8). 01 Date-2-YYYYMMDD Pic 9(8). 01 Integer-Form Pic S9(9). ... Move Function Current-Date(1:8) to Date-1-YYYYMMDD Compute Integer-Form-Date-1 = Function Integer-of-Date(Date-1-YYYYMMDD) Compute Integer-Form-Date-2 = Function Integer-of-Date(Date-2-YYYYMMDD) Compute day-diff-dates = Integer-Form-Date-2 - Integer-Form-Date-1

This gives difference of days between dates.
However, time difference will have to be calculated with program logic (I think!)

 Code: 01 WS-TIME.       05 WS-HR PIC 9(02)       05 FILLER PIC X(01) VALUE ':'       05 WS-MM PIC 9(02)       05 FILLER PIC X(01) VALUE ':'       05 WS-SS PIC 9(02)       05 FILLER PIC X(01) VALUE ':'       05 WS-NN PIC 9(02) MOVE INPUT-TIME TO WS-TIME. MOVE INPUT-OTHER-TIME TO WS-TIME-2 (defined in similar fashion as WS-TIME).

Now calculated HH, MM, SS and NN difference separately. and club output in WS-TIME-RESULT which should be defined in similar fashion as WS-TIME. Fillers would be ':'.
DavidatK

Active Member

Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

Posted: Wed May 31, 2006 3:43 am    Post subject: Re: How to calculate the time difference?

Geo,

Try this code for the difference in time. If the start time is greater than the end time, the code assumes passage of one day.

 Code: 01  START-HHMMSSDD.                              05  START-HH                PIC 9(2).        05  FILLER                  PIC X.            05  START-MM                PIC 9(2).        05  FILLER                  PIC X.            05  START-SS                PIC 9(2).        05  FILLER                  PIC X.            05  START-DD                PIC 9(2).     01  END-HHMMSSDD.                                05  END-HH                  PIC 9(2).        05  FILLER                  PIC X.          05  END-MM                  PIC 9(2).        05  FILLER                  PIC X.          05  END-SS                  PIC 9(2).        05  FILLER                  PIC X.          05  END-DD                  PIC 9(2).                                                                 01  NO-DAYS                     PIC 9(3).                                                                               01  START-TIME                  PIC 9(11).                 01  END-TIME                    PIC 9(11).                 01  DIFF-TIME                   PIC 9(11).                                                                             01  DIFF-HHMMSSDD.                                            05  DIFF-HH                 PIC 99.                        05  FILLER                  PIC X       VALUE ':'.        05  DIFF-MM                 PIC 99.                        05  FILLER                  PIC X       VALUE ':'.        05  DIFF-SS                 PIC 99.                        05  FILLER                  PIC X       VALUE '.'.        05  DIFF-DD                 PIC 99.                   PROCEDURE DIVISION.                                                                                                                    MOVE '04:45:12.45'          TO START-HHMMSSDD.                MOVE '04:42:54.23'          TO END-HHMMSSDD.                                                                                    IF END-HHMMSSDD < START-HHMMSSDD                                  THEN                                                                  MOVE 1                  TO NO-DAYS                            ELSE                                                                  MOVE 0                  TO NO-DAYS                            END-IF.                                                                                                                            MOVE 0                      TO START-TIME                                                        END-TIME.                                                                              COMPUTE START-TIME = START-HH * 60              COMPUTE START-TIME = (START-TIME                                      + START-MM) * 60          COMPUTE START-TIME = (START-TIME                                      + START-SS) * 100          COMPUTE START-TIME = (START-TIME                                      + START-DD)                                                                  COMPUTE END-TIME = NO-DAYS * 24                  COMPUTE END-TIME = (END-TIME                                          + END-HH) * 60            COMPUTE END-TIME = (END-TIME                                          + END-MM) * 60            COMPUTE END-TIME = (END-TIME                                          + END-SS) * 100            COMPUTE END-TIME = (END-TIME                                           + END-DD                                                                                                                                            COMPUTE DIFF-TIME = END-TIME - START-TIME.                                                                                  DIVIDE DIFF-TIME BY 100     GIVING DIFF-TIME                                              REMAINDER DIFF-DD.                DIVIDE DIFF-TIME BY 60      GIVING DIFF-TIME                                              REMAINDER DIFF-SS.                DIVIDE DIFF-TIME BY 60      GIVING DIFF-TIME                                              REMAINDER DIFF-MM.                MOVE DIFF-TIME              TO DIFF-HH.                                                                                    DISPLAY 'START-HHMMSSDD : ' START-HHMMSSDD.                  DISPLAY 'END-HHMMSSDD   : ' END-HHMMSSDD.                    DISPLAY 'DIFF-HHMMSSDD  : ' DIFF-HHMMSSDD.                                                                                  GOBACK.

With START-HHMMSSDD = '04:45:12.45' and END-HHMMSSDD = '04:42:54.23', result is:

 Code: .START-HHMMSSDD : 04:45:12.45 .END-HHMMSSDD   : 04:42:54.23 .DIFF-HHMMSSDD  : 23:57:41.78

With START-HHMMSSDD = '04:42:54.23' and END-HHMMSSDD = '04:45:12.45', result is:

 Code: .START-HHMMSSDD : 04425423    .END-HHMMSSDD   : 04451245    .DIFF-HHMMSSDD  : 00:26:22.00

If you have problems wit the code, let me know.

Dave
DavidatK

Active Member

Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

Posted: Wed May 31, 2006 3:46 am    Post subject: Re: How to calculate the time difference?

OOPs,

With START-HHMMSSDD = '04:42:54.23' and END-HHMMSSDD = '04:45:12.45', result is:

 Code: .START-HHMMSSDD : 04:42:54.23 .END-HHMMSSDD   : 04:45:12.45 .DIFF-HHMMSSDD  : 00:02:18.22
 All times are GMT + 6 Hours
 Page 1 of 1

Search our Forum:

 Topic Author Forum Replies Posted Similar Topics Difference between EQUAL and EQUAL TO... jithinraghavan COBOL Programming 3 Thu Apr 19, 2018 2:22 am How syntax check at Bind time is diff... Poha Eater DB2 5 Wed Mar 21, 2018 9:16 pm Merging records, but not all the time prino DFSORT/ICETOOL 17 Sat Feb 17, 2018 4:20 pm Need to identify top 10 costly or tim... ashek15 DB2 4 Wed Feb 14, 2018 3:20 am Logic to skip Maximum time-stamp and ... vickey_dw DB2 2 Thu Feb 01, 2018 11:15 pm

 © 2003-2017 IBM MAINFRAME Software Support Division
 Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us