Skip to Content
Technical Articles

ABAP – Time period intersection tool

After several times I encounterd the need for calculating the intersection of two time periods, I finally decided to create a reusable tool.

The tool not only decides wether two periods intersect or not (see also https://blogs.sap.com/tag/timeslice/) but returns also the intersection period. You can use it only with clocktime, dates or both.

Here’s the class coding:

class zcl_tool_time_slices definition
  public
  create public .

  public section.

    types:
      begin of ty_period,
        date_from type d,
        time_from type t,
        date_to   type d,
        time_to   type t,
      end of ty_period .

    class-methods intersect
      importing
        first_period      type ty_period
        second_period     type ty_period
      returning
        value(r_intersect) type ty_period.
  protected section.
  private section.
ENDCLASS.



CLASS ZCL_TOOL_TIME_SLICES IMPLEMENTATION.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_TOOL_TIME_SLICES=>INTERSECT
* +-------------------------------------------------------------------------------------------------+
* | [--->] FIRST_PERIOD                   TYPE        TY_PERIOD
* | [--->] SECOND_PERIOD                  TYPE        TY_PERIOD
* | [<-()] R_INTERSECT                    TYPE        TY_PERIOD
* +--------------------------------------------------------------------------------------</SIGNATURE>
  method intersect.

    r_intersect = cond #(
      let
        " concatenate date and time for easier comparison
        first_from = first_period-date_from && first_period-time_from
        second_from = second_period-date_from && second_period-time_from
        first_to = first_period-date_to && first_period-time_to
        second_to = second_period-date_to && second_period-time_to in
      when
        " very slim decision wether an intersection exists or not
        first_from < second_to and
        second_from < first_to
      then value #(
        let
          from = cond #(
            when first_from > second_from then first_from else second_from )
          to = cond #(
            when first_to > second_to then second_to else first_to ) in
        date_from = from(8)
        time_from = from+8
        date_to = to(8)
        time_to = to+8 ) ).

  endmethod.
ENDCLASS.

You can play around with the class using this report

report zp_tmp_intersect.

parameters:
  p1_datef type d, p1_timef type t, p1_datet type d, p1_timet type t,
  p2_datef type d, p2_timef type t, p2_datet type d, p2_timet type t.

start-of-selection.
  cl_demo_output=>display( zcl_tool_time_slices=>intersect(
    first_period  = value #(
      date_from = p1_datef
      time_from = p1_timef
      date_to = p1_datet
      time_to = p1_timet )
    second_period  = value #(
      date_from = p2_datef
      time_from = p2_timef
      date_to = p2_datet
      time_to = p2_timet ) ) ).

 

 

2 Comments
You must be Logged on to comment or reply to a post.
    • Interesting – I never used this command. However, from the key word documentation, I could not get to know how to use  PROVIDE in this case. Could you please explain?