Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

[ABAP] How to calculate difference between two timestamps excluding break time

0 Kudos
15,972

Hi ABAP experts,

There's a special requirement, below picture shows a class schedule table, class start time(ZAACH1381) and class end time(ZAACH1382) are stored in timestamps, there will be two input parameters: student join time and leaving time in timestamps format also, and need to output the total time the student participated excluding the break time (output in seconds).

PS. The student may join in break time, also may leave in break time.

Let's take this as an example:

The student joined at 20150408084000 and left at 201504081005, so the output should be:

[Interval between 20150408100500 and 20150408084000] - [ Interval between 20150408101000 and 20150408100500 which is break time ]

= 5,100 seconds - 300 seconds

= 4,800 seconds

How can I achieve this complicated scenario?

Million thanks,

Galland

6 REPLIES 6

retired_member
Product and Topic Expert
Product and Topic Expert
0 Kudos
7,216

First of all, understand timestamps by reading

https://help.sap.com/http.svc/rc/abapdocu_752_index_htm/7.52/en-US/index.htm?file=abentime_stamp_ovi...

There you find CL_ABAP_TSTMP,

https://help.sap.com/http.svc/rc/abapdocu_752_index_htm/7.52/en-US/index.htm?file=abencl_abap_tstmp....

With that knowledge you should solve that problem easily yourself.

Rashid_Javed
Contributor
7,216

Hi Golland,

what is the actual question here? If you want to subtract two timestamps, you can use ABAP class cl_abap_tstmp that can do various timestamp operations. Following is an example of subtract where the difference is returned in seconds.

data: r_secs type TZNTSTMPL.

CALL METHOD cl_abap_tstmp=>subtract

  EXPORTING

    tstmp1 = tstmp1

    tstmp2 = tstmp2

  RECEIVING

    r_secs = r_secs

    .



 CATCH cx_parameter_invalid_range .

   "relevant error handling code here

 CATCH cx_parameter_invalid_type .

   "relevant error handling code here

ENDTRY.

RJv

0 Kudos
7,216

Hello Rashid,

Thanks for your reply, I know how to calculate the interval between two timestamps, but the scenario is very complicated as I explained, I need to calculate the interval but excluding the break time based on the class schedule table, I don't know how to achieve this logic.


Thanks,

Galland

0 Kudos
7,216

Hi Galland Peng,

As mentioned by Matthew Billingham, to explain simply I used SY-UZEIT instead of timestamp. Please find the logic below. If required for TIMESTAMP, convert the logic accordingly.

SELECT-OPTIONS: s_time FOR l_time DEFAULT '084000' TO '100500'.
SELECT-OPTIONS: s_break FOR l_time DEFAULT '090000' TO '091000'.
DATA: l_calc_time TYPE sy-uzeit.
IF s_time-low LE s_break-high AND s_time-high GE s_break-low.
IF s_break-low IN s_time.
l_calc_time = s_break-low - s_time-low.
ENDIF.
IF s_break-high IN s_time.
l_calc_time = l_calc_time + s_time-high - s_break-high.
ENDIF.
ENDIF.
WRITE L_CALC_TIME.

matt
Active Contributor
7,216

You know, it'd be a lot easier for people to understand if you simplified your example to just using days and dates instead of timestamps. The logic for the solution would be the same - just different datatypes - so you could easily apply it to your timestamp actual requirements.

It's very hard to read 14 digit figures. Evidenced by the fact you have written: The student joined at 20150408084000 and left at 201504081005 Your last timestamp there has only 12 digits.

Please comment on your question with a simpler example, using more readable datatypes. This would make it easier for people to help you, so you're more likely to get a solution.

Ganesh_Pandian
Explorer
0 Kudos
7,216

Look for any specific key field to determine the Break time and the class time.

So that you can get the solution i hope!