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: 
Read only

Duration Overlap Calculation

jogeswararao_kavala
Active Contributor
0 Likes
4,315

Dear Experts,

I have 6 fields in an internal table.

1. STDT - Start Date

2. STTM - Start Time

3. ENDT - End Date

4. ENTM - End Time

5. DURN - Duration

6. OVRLP -Overlap

I have calculated DURN through FM 'SWI_DURATION_DETERMINE' (using fields 1 to 4).

Then I have sorted the table in descending order of DURN.

Now the highest duration record is in the 1st line.

The requirement now is

I need to calculate the duration-overlap of each line item with reference to the 1st record, into field OVRLP  (in hours or minutes or seconds).

Your help is requested.

kind regards

Jogeswara Rao

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
3,640

Hi Jog,

try this logic.

There is a function module C14B_DATE_TIME_COMPARE , which outputs value X if datetime1 < datetime2.  In the code below, COMPARE refers to this FM. Duration refers to your  FM to calculate duration

(Also another FM is there SD_CAL_DURATION_FROM_DATETIME which outputs X if datetime 2 < datetime 1. The following logic is based on the first FM, if you want to use the second FM, change logic accordingly.  )

Suppose your internal table is itab.

ws1 like line of itab,

ws2 like line of itab.

res1 type c, res2 type c, res3 type c.

read itab index 1 into ws1.

loop at itab into ws2.

* When the start date of current record is between start and end date of first record

     compare (ws2-stdt, ws2-sttm, ws1-endt, ws1-entm)

     res1 = result.

     compare(ws1-stdt,ws1-sttm, ws2-std, ws2-sttm)

     res2 = result.

     if res1  = 'X' and res2 = 'X'.          " Start date of current record is between the start and end date of first record.

             compare(ws1-enddt,ws1-enddtm, ws2-enddt,ws2-endtm)    " get end date of overlap.

             if res3 = 'X

                    ws2-overlap = duration(ws2-stdt,ws2sttm, ws1-enddt,ws1-endtm.)

             else.

                    ws2-overlap = duration(ws2-stdt, ws2-sttm, ws2-enddt, ws2-endt.)

             endif.

            modify itab index sy-tabix from ws2.

            clear : res1, res2, res3, ws2.

           continue

     endif.

* When enddate of current record is betwen start and end date of first record.

     compare (ws2-enddt, ws2-endtm, ws1-endt, ws1-entm)

     res1 = result.

     compare(ws1-stdt,ws1-sttm, ws2-enddt, ws2-endtm)

     res2 = result.

     if res1  = 'X' and res2 = 'X'.          " End date of current record is between the start and end date of first record.

             compare(ws1-stdt,ws1-sttm, ws2-stdt,ws2-sttm)    " get start date of overlap.

             if res3 = 'X

                    ws2-overlap = duration(ws1-stdt,ws1sttm, ws2-enddt,ws2-endtm.)

             else.

                    ws2-overlap = duration(ws2-stdt, ws2-sttm, ws2-enddt, ws2-endtm.)

             endif.

            modify itab index sy-tabix from ws2.

            clear : res1, res2, res3, ws2.

           continue

     endif.

endloop.

Dont forget to clear the flags and ws2 after each loop.

Message was edited by: Susmitha Thomas

11 REPLIES 11
Read only

Former Member
0 Likes
3,640

What do you mean by duration overlap?

Does the timing and date also have coincide with the first record or you look only at the duration?

Read only

0 Likes
3,640

There is no role of DURN field, further after sorting.

I need the timing overlap of each line item with reference to the first one.

Perhaps I should have used the title as ' Timing Overlap'

Thank you Susmitha for response.

-Jogeswara Rao

Read only

Former Member
0 Likes
3,640

I think by Overlap you mean how much time is shared by interval of first record and interval of n'th record.

Assuming LT_REC is internal table, LS_REC is structure/workarea, LS_FIRST is another structure meant to store first record data.

SWI_DURATION_CALCULATE gives 0 if end date/time is less than start date/time.

Think of first interval as (2....7) and second interval as (4....12).

Durations between start of one interval and end of other interval would be 10 and 3.

I believe you are considering 3 to be the overlap value in this case.

Here is a pseudocode.

Read table LT_REC into LS_FIRST index 1.

Loop at LT_REC into LS_REC.

Calculate duration between (LS_REC-STDT, LS_REC-STTM, LS_FIRST-ENDT, LS_FIRST-ENTM).

Calculate duration between (LS_FIRST-STDT, LS_FIRST-STTM, LS_REC-ENDT, LS_REC-ENTM).

Out of 2 calculated durations, whichever is lower should be the overlap. Store in it LS_REC-OVRLP.


If the invervals don't overlap, lower calculated duration would be 0.

Modify LT_REC using LS_REC.

Endloop.

If the pseudocode is incorrect or does not make sense, let us know by example data what overlap value are you expecting.

Read only

0 Likes
3,638

Thank you Manish,

I feel you've understood the issue. We are studying the code.

But the example you've give above the code ( 2..7 & 4....12), I could not follow.

So to clear any understanding-gap, I am attaching the graphical representation of my requirement.

Hope this helps you to understand the requirement fully.


Read only

former_member184455
Active Participant
0 Likes
3,638

What about (in pseudo-code):

OVRLP(I1, I2) = DURATION( MAX(START_I1, START_I2) ... MIN(END_I1, END_I2) )

Read only

former_member193464
Contributor
0 Likes
3,638

i dont know if this is your query , but from what i understand...


overlap line n -->

check start date + start time of line n if greater than end date + end time of line1 , overlap = 0
if not then overlap = end date + end time of line 1 - start date + start time of line n...

you can use F.M. SD_CALC_DURATION_FROM_DATETIME to calculate the difference in hours , it also sets a flag to X if the hours are negative , so you can put your first condition of overlap being 0 if the flag is set(i.e. start date and time is after end date and time of the first line)

Read only

0 Likes
3,638

ok so you can have overlap at the starting also , sorry skipped my mind...
so based on the start date + time , you need to find where does it lie

1)less than start date +time of 1st item,then check end date +time should lie in between , else over lap 0.
2)in between start date + time and end date + time of  1st  item
    
3)greater than end date + time of 1st item ,

for the third case its 0,


for the second case, and first case,
when passing date to FM sd_calc_duration_from_datetime
keep variable lv_to_date, lv_from_date , same for time

if its in the first case -  if it satisfies the above condition

lv_from_date = start date of 1st item

IF end date + time of nth line item is GT end date + time of 1st.

lv_to_date =  end date of 1st item (same for time).

ELSE.

lv_to_date = end date of nth item.

ENDIF.

Now pass this to the FM

Similar condition check you need to do for second case... I hope it make sense...

Read only

Former Member
0 Likes
3,641

Hi Jog,

try this logic.

There is a function module C14B_DATE_TIME_COMPARE , which outputs value X if datetime1 < datetime2.  In the code below, COMPARE refers to this FM. Duration refers to your  FM to calculate duration

(Also another FM is there SD_CAL_DURATION_FROM_DATETIME which outputs X if datetime 2 < datetime 1. The following logic is based on the first FM, if you want to use the second FM, change logic accordingly.  )

Suppose your internal table is itab.

ws1 like line of itab,

ws2 like line of itab.

res1 type c, res2 type c, res3 type c.

read itab index 1 into ws1.

loop at itab into ws2.

* When the start date of current record is between start and end date of first record

     compare (ws2-stdt, ws2-sttm, ws1-endt, ws1-entm)

     res1 = result.

     compare(ws1-stdt,ws1-sttm, ws2-std, ws2-sttm)

     res2 = result.

     if res1  = 'X' and res2 = 'X'.          " Start date of current record is between the start and end date of first record.

             compare(ws1-enddt,ws1-enddtm, ws2-enddt,ws2-endtm)    " get end date of overlap.

             if res3 = 'X

                    ws2-overlap = duration(ws2-stdt,ws2sttm, ws1-enddt,ws1-endtm.)

             else.

                    ws2-overlap = duration(ws2-stdt, ws2-sttm, ws2-enddt, ws2-endt.)

             endif.

            modify itab index sy-tabix from ws2.

            clear : res1, res2, res3, ws2.

           continue

     endif.

* When enddate of current record is betwen start and end date of first record.

     compare (ws2-enddt, ws2-endtm, ws1-endt, ws1-entm)

     res1 = result.

     compare(ws1-stdt,ws1-sttm, ws2-enddt, ws2-endtm)

     res2 = result.

     if res1  = 'X' and res2 = 'X'.          " End date of current record is between the start and end date of first record.

             compare(ws1-stdt,ws1-sttm, ws2-stdt,ws2-sttm)    " get start date of overlap.

             if res3 = 'X

                    ws2-overlap = duration(ws1-stdt,ws1sttm, ws2-enddt,ws2-endtm.)

             else.

                    ws2-overlap = duration(ws2-stdt, ws2-sttm, ws2-enddt, ws2-endtm.)

             endif.

            modify itab index sy-tabix from ws2.

            clear : res1, res2, res3, ws2.

           continue

     endif.

endloop.

Dont forget to clear the flags and ws2 after each loop.

Message was edited by: Susmitha Thomas

Read only

0 Likes
3,638

Thank you Susmitha, You're Great !

Read only

Former Member
0 Likes
3,638

I have edited the code a little. Let me know if its giving the right output.

Read only

0 Likes
3,638

Hi ,

check FM TTE_CHK_DTRNG_DATETAB_2(checl Funtion Group for more FMs) along with FM's mentioned by Susmitha.

Prabhu