Highlighted
Absent Member.
Absent Member.
351 views

[archive] Elapsed Time

[Migrated content. Thread originally posted on 05 April 2005]

Hello,
Does anyone have a quick routine for converting elapsed time to minutes & seconds? What I'm doing is accepting a beginning time and an ending time and subtracting the beginning from the ending time. How do I convert this result to minutes & seconds?

Thanks!
0 Likes
2 Replies
Highlighted
Absent Member.
Absent Member.

RE: [archive] Elapsed Time

05 WS-TIME-START PIC 9(08) VALUE ZERO.
05 WS-TIME-START-1 REDEFINES WS-TIME-START.
10 WS-START-HOURS PIC 9(02).
10 WS-START-MINUTES PIC 9(02).
10 WS-START-SECONDS PIC 9(02).
10 FILLER PIC 9(02).
05 WS-TIME-STOP PIC 9(08) VALUE ZERO.
05 WS-TIME-STOP-1 REDEFINES WS-TIME-STOP.
10 WS-STOP-HOURS PIC 9(02).
10 WS-STOP-MINUTES PIC 9(02).
10 WS-STOP-SECONDS PIC 9(02).
10 FILLER PIC 9(02).
05 WS-TIME-ELAPSE PIC 9(08) VALUE ZERO.
05 WS-TIME-ELAPSE-1 REDEFINES WS-TIME-ELAPSE.
10 WS-ELAPSE-HOURS PIC 9(02).
10 WS-ELAPSE-MINUTES PIC 9(02).
10 WS-ELAPSE-SECONDS PIC 9(02).
10 FILLER PIC 9(02).

ACCEPT WS-TIME-STOP FROM TIME.
COMPUTE WS-TIME-ELAPSE = WS-TIME-STOP - WS-TIME-START.
IF WS-STOP-SECONDS >= WS-START-SECONDS
COMPUTE WS-ELAPSE-SECONDS = WS-STOP-SECONDS - WS-START-SECONDS
ELSE
IF WS-STOP-MINUTES = 0
SUBTRACT 1 FROM WS-STOP-HOURS
ADD 59 TO WS-STOP-MINUTES
ELSE
SUBTRACT 1 FROM WS-STOP-MINUTES
END-IF
COMPUTE WS-ELAPSE-SECONDS = 60 + WS-STOP-SECONDS - WS-START-SECONDS.
IF WS-STOP-MINUTES >= WS-START-MINUTES
COMPUTE WS-ELAPSE-MINUTES = WS-STOP-MINUTES - WS-START-MINUTES
ELSE
SUBTRACT 1 FROM WS-STOP-HOURS
COMPUTE WS-ELAPSE-MINUTES = 60 + WS-STOP-MINUTES - WS-START-MINUTES.
IF WS-STOP-HOURS >= WS-START-HOURS
COMPUTE WS-ELAPSE-HOURS = WS-STOP-HOURS - WS-START-HOURS
ELSE
COMPUTE WS-ELAPSE-HOURS = 24 + WS-STOP-HOURS - WS-START-HOURS.

Regards,
Patty
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Elapsed Time

Another approach is to convert both the start and finish times to seconds (or hundredths of seconds), subtract, and then re-convert back to hours/mins/secs.

My example:

05 START-TIME.
10 START-HOURS PIC 99.
10 START-MINS PIC 99.
10 START-SECS PIC 99.
10 START-HDTHS PIC 99.
05 FINISH-TIME.
10 FINISH-HOURS PIC 99.
10 FINISH-MINS PIC 99.
10 FINISH-SECS PIC 99.
10 FINISH-HDTHS PIC 99.
05 START-NUM PIC 9(9).
05 FINISH-NUM PIC 9(9).

......

ACCEPT START-TIME FROM TIME.

...[do something here] ....

ACCEPT FINISH-TIME FROM TIME.

IF FINISH-TIME
ADD 24 TO FINISH-HOURS
END-IF.

COMPUTE START-NUM = START-HDTHS +
(START-SECS * 100) +
(START-MINS * 6000) +
(START-HOURS * 360000).
COMPUTE FINISH-NUM = FINISH-HDTHS +
(FINISH-SECS * 100) +
(FINISH-MINS * 6000) +
(FINISH-HOURS * 360000).

SUBTRACT START-NUM FROM FINISH-NUM.

DIVIDE FINISH-NUM BY 100
GIVING FINISH-NUM REMAINDER FINISH-HDTHS.
DIVIDE FINISH-NUM BY 60
GIVING FINISH-NUM REMAINDER FINISH-SECS.
DIVIDE FINISH-NUM BY 60
GIVING FINISH-NUM REMAINDER FINISH-MINS.
DIVIDE FINISH-NUM BY 24
GIVING FINISH-NUM REMAINDER FINISH-HOURS.

Of course at the end FINISH-NUM should be zero (number of days), and the FINISH-TIME contains your elapsed time.

This assumes (as does the previous post) that your elapsed time will never be greater than 24 hours.

Hope this helps.

Cheers
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.