Tuesday, 6 February 2007

ABAP Calculating Date-Ranges

Here is an example that given a date it returns the date of the same day one month later. It demonstrates the concepts of subfields and converting a date to an integer and vice versa. It also provides an implementation of the isLeap() function in ABAP. Note that ABAP does not allow intermediate calculations in logical expressions, so all the intermediate modules operations have to be performed beforehand and stored in separate variables.
FORM get_month_range CHANGING p_start_date TYPE d
                      p_end_date TYPE d.
DATA :
 month TYPE i,
 year TYPE i,
 y_m_4 TYPE i,
 y_m_100 TYPE i,
 y_m_400 TYPE i.

IF p_start_date IS INITIAL.
 p_start_date = sy-datum.
ENDIF.

month = p_start_date+4(2).
CASE month.
 WHEN 1 OR 3 OR 5 OR 7 OR 8 OR 10 OR 12.
   p_end_date = p_start_date + 31.
 WHEN 2.
   year = p_start_date(4).
   y_m_4 = year MOD 4.
   y_m_100 = year MOD 100.
   y_m_400 = year MOD 400.
   IF ( y_m_4 = 0 ) AND
      ( ( y_m_100 <> 0 ) OR ( y_m_400 = 0 )
        ).
     p_end_date = p_start_date + 29.
   ELSE.
     p_end_date = p_start_date + 28.
   ENDIF.
 WHEN OTHERS.
   p_end_date = p_start_date + 30.
ENDCASE.

ENDFORM.                    " get_month_range

No comments :