If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Determine difference between two times

Hi,
I have two timestamps; a beginning and an ending.
I need to determine the elapsed time from the beginning to the ending.

There must be an easy way of doing this but cannot put my finger on it.

It seems that all the operation codes only allow processing on a specific portion; hours, minutes, seconds....
Is there a way to do what I want without 30 lines of code to extract each portion and then calculate the difference?

I thought RPGLE was suppose to be easier.
I could convert the hours and minutes to seconds of both values and then calculate like I used to do in RPG III but that seems archaic and if anyone saw that in my code, I would be embarrassed to say the least.

Comment

If I wanted to subtract 30 days (30:*D) from today or 45 minutes (45:*MN) from now, yeah, that's doable

But not for a whole time subtracted from another whole time.

Per the docs (although maybe I'm misunderstanding what you're asking):Calculate a duration
The SUBDUR operation can also be used to calculate a duration between:
1. Two dates
2. A date and a timestamp
3. Two times
4. A time and a timestamp
5. Two timestamps

1) Free-form IS RPGLE. If you want to restrict free-form uses in the responses then you need to either say "fixed form only" or at least say what "flavor" of free-form you mean.

For example: X = Y; style free-form has been available since V5R1 - that is SEVENTEEN (17) years ago. Hardly modern so it is not really surprising that most people use it for their examples. Why would anyone code in a version older than that? It makes no sense. If you mean is what is often referred to as **Free then that is a mere EIGHT (8) years old but as far a calcs are concerned only affects the column in which you start coding.

2) SUBDUR will calculate a duration - you just have to tell it what format the duration is in. Minutes? Seconds? If you want the interval in (say) an H:M:S type format then you must calculate the duration to the lowest level and then use math to convert the seconds to that format. There are examples out on the web. Most of us would create a subprocedure to perform that function but I'm guessing that those are probably verbotten in your shop as well.

3) If you want to directly produce an interval then that can be done easily using embedded SQL in your RPG - but again I'm guessing that that also a no-no.

For those of you on the list used to "kindler, gentler" responses from me - yes - I am getting old and cranky and increasingly intolerant of stupid shops that can't get out of their own way.

redvan I have no idea why you thought I was referring to you being sarcastic. You weren't and I wasn't. I was talking about MYSELF. I didn't want you to take anything I said as a personal criticism. Does that clarify?

There is a difference between free format and fixed format but that doesn't stop you from using functions.
You can use functions in fixed format without problems.
In this case I would use the %DIFF function.

For example ( the example does not align to the specific columns of the D- and C-specifications. You have to ajust them yourself )
D start_time S Z
D time_taken S 15P 0
C eval time_taken = %DIFF (%timestamp() : start_time : *SECONDS)

The 3. parameter tells the function what unit it should return the period in.
For two timestamps the valid values are: *MSECONDS, *SECONDS, *MINUTES, *HOURS, *DAYS, *MONTHS, and *YEARS