Difference between time



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Difference between time

Postby deva_048 » Mon May 20, 2013 9:52 pm

Hi

My input file contains the date such as following format mentioned below

Start time 01:00
End Time 01:30

I have written a cobol program to moving these two times into two working storage variable in ready now. Now i have to calculate the difference between two times i.e) Endtime - start time to store in new variable and to add 3 hours in start time to display elapsed time as 4:00 to store in new variable.

Can anyone give solution to implement this in cobol. Please suggest me the solution.

Thanks in advance
deva_048
 
Posts: 72
Joined: Thu Feb 02, 2012 9:28 pm
Has thanked: 1 time
Been thanked: 0 time

Re: Difference between time

Postby dick scherrer » Mon May 20, 2013 10:07 pm

Hello,

First you need to be sure the times are in the same 24-hour day. If not, then you need to include the dates.

One way is to:
. Convert both to seconds
. Subtract the start seconds from the end seconds (after ensuring the end is greater than the start) to get elapsed seconds
. Convert the elapsed seconds back to HH:MM:SS to show the results
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: Difference between time

Postby deva_048 » Mon May 20, 2013 10:10 pm

Thanks.I forgot to say that the times in format HH:MM:SS in input file for both start and endtime. Is compute function will work for to subtract these ??
deva_048
 
Posts: 72
Joined: Thu Feb 02, 2012 9:28 pm
Has thanked: 1 time
Been thanked: 0 time

Re: Difference between time

Postby dick scherrer » Mon May 20, 2013 10:26 pm

Hello,

No - You need to comvert the times to seconds or use something that can do arithmetic with formatted date/time values.
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: Difference between time

Postby Robert Sample » Mon May 20, 2013 10:42 pm

Compute starting seconds as hours * 3600 + minutes * 60 + seconds. Compute ending seconds as hours * 3600 + minutes * 60 + seconds. The difference in seconds can be converted back to hours / minutes / seconds easily enough.

Things to consider:
1) will your data span a day boundary (start at 11 PM, end at 1 AM)?
2) is your data in 12-hour or 24-hour format?
3) can your data span more than one day? If so, how do you detect that?
4) is the data computer-generated, or human-generated? If the latter, do you need to think about one-digit versus 2-digit hours, minutes, seconds?
5) do you need to be concerned with daylight savings time? If so, how do you detect that?

That should start you thinking in the right direction.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Difference between time

Postby Akatsukami » Mon May 20, 2013 10:48 pm

Deva-kun, I recommend that you investigate the Language Environment services CEESECS and CEEDATM for ways to handle many of Mr. Sample's points.
"You have sat too long for any good you have been doing lately ... Depart, I say; and let us have done with you. In the name of God, go!" -- what I say to a junior programmer at least once a day
User avatar
Akatsukami
Global moderator
 
Posts: 1058
Joined: Sat Oct 16, 2010 2:31 am
Location: Bloomington, IL
Has thanked: 6 times
Been thanked: 51 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post