IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

How to calculate the time difference?


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Geo

New User


Joined: 25 May 2006
Posts: 1

PostPosted: Thu May 25, 2006 10:00 am
Reply with quote

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.
Back to top
View user's profile Send private message
gskulkarni

New User


Joined: 01 Mar 2006
Posts: 70

PostPosted: Thu May 25, 2006 11:09 am
Reply with quote

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!)

parse your time HH:MM:SS:NN as
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 ':'.
Back to top
View user's profile Send private message
DavidatK

Active Member


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

PostPosted: Wed May 31, 2006 3:43 am
Reply with quote

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
Back to top
View user's profile Send private message
DavidatK

Active Member


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

PostPosted: Wed May 31, 2006 3:46 am
Reply with quote

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
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts To get the the current time DFSORT/ICETOOL 13
No new posts RC query -Time column CA Products 3
No new posts C Compile time time stamps Java & MQSeries 10
No new posts how to calculate SUM value for VB fil... DFSORT/ICETOOL 1
No new posts Parallelization in CICS to reduce res... CICS 4
Search our Forums:

Back to Top