3

I have a program in ABAP where they use the type 'timestampl' in a variable, so they can get the time of certain events. They use it because they need the milliseconds.

I now have the mission of getting the difference between 2 of these variables, and I can't seem to find a function module or another solution.

Any help is much appreciated!

2
  • subtracts it ? what is the issue ? :-)
    – zod
    May 10, 2012 at 17:22
  • Well, I´m not sure if substracting directly is the way to go. Does that consider different days depending on the month and things like that?
    – Laloski
    May 10, 2012 at 17:35

4 Answers 4

5

Use the method CL_ABAP_TSTMP=>SUBTRACT, by passing two timestamps which must be of the type TIMESTAMPL so that to contain milliseconds, and the difference between the 2 timestamps will be returned in number of seconds, including the milliseconds.

Example:

DATA: lv_tstmp1 TYPE timestampl,
      lv_tstmp2 TYPE timestampl,
      lv_diff   TYPE tzntstmpl.

lv_tstmp1 = '20190704000010.999'. " July 4th, 00:00:10 and 999 ms
lv_tstmp2 = '20190703235950.001'. " July 3rd, 23:59:50 and 001 ms

CALL METHOD cl_abap_tstmp=>subtract
    EXPORTING
      tstmp1 = lv_tstmp1
      tstmp2 = lv_tstmp2
    RECEIVING
      r_secs = lv_diff.

ASSERT lv_diff = '20.998'. " expectation verified or run time error
1

A Google search turns up this recommendation: http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/840ad679-0601-0010-cd8e-9989fd650822#q-8: use the class CL_ABAP_TSTMP. You can also see an example of how to use the class in this thread: http://scn.sap.com/thread/85476.

 cl_abap_tstmp=>systemtstmp_syst2utc(
    exporting
      syst_date = <wa_joblist>-strtdate
      syst_time = <wa_joblist>-strttime
    importing
      utc_tstmp = start_stamp ).

  cl_abap_tstmp=>systemtstmp_syst2utc(
    exporting
      syst_date = sy-datum
      syst_time = sy-uzeit
    importing
      utc_tstmp = now_stamp ).

  seconds = cl_abap_tstmp=>subtract(
      tstmp1 = now_stamp
      tstmp2 = start_stamp ).
2
  • 1
    This actually does not answer the question, which requires the delta in milliseconds..
    – tomdemuyt
    May 15, 2013 at 17:45
  • The method SUBTRACT may accept timestamps with milliseconds, which would answer the question, but the example first uses methods which always return timestamps without milliseconds, so it ruins the answer... Jul 4, 2019 at 12:58
0

Use the FM *'CCU_TIMESTAMP_DIFFERENCE'*

After Checking which timestamp is greater call the FM.

IF TIMESTAMP_2 > TIMESTAMP_1.

 CALL FUNCTION 'CCU_TIMESTAMP_DIFFERENCE'
   EXPORTING
      TIMESTAMP1 = TIMESTAMP_2
      TIMESTAMP2 = TIMESTAMP_1 
   IMPORTING 
      DIFFERENCE = TIMESTAMP_DIFFERENCE.
EndIf.
0

The existing proposals ignore the milliseconds. Here's a solution for modern ABAP AS that also considers msecs:

r_secs = CONV #( cl_abap_tstmp=>subtract(
  tstmp1 = CONV timestamp( i_ts1 )
  tstmp2 = CONV timestamp( i_ts1 )
) ).
r_secs = r_secs + ( frac( i_ts1 ) - frac( i_ts2 ) ).

i_ts1 and i_ts2 are two timestamps, r_secs (type f) is the result.

Here's a small tester:

REPORT ztest_timestampl_dif.

CLASS lcl_timestampl DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS:
      test,
      dif
        IMPORTING
                  i_ts1         TYPE timestampl
                  i_ts2         TYPE timestampl
        RETURNING VALUE(r_secs) TYPE f.
ENDCLASS.

START-OF-SELECTION.
  lcl_timestampl=>test( ).

CLASS lcl_timestampl IMPLEMENTATION.
  METHOD test.
    DATA:
      l_ts1   TYPE timestampl,
      l_ts2   TYPE timestampl,
      l_msecs TYPE f.

    GET TIME STAMP FIELD l_ts1.
    WAIT UP TO '0.378' SECONDS.
    GET TIME STAMP FIELD l_ts2.

    l_msecs = dif( i_ts1 = l_ts1  i_ts2 = l_ts2 ).

    cl_demo_output=>display( l_msecs ).
  ENDMETHOD.

  METHOD dif.
    r_secs = CONV #( cl_abap_tstmp=>subtract(
      tstmp1 = CONV timestamp( i_ts1 )
      tstmp2 = CONV timestamp( i_ts1 )
    ) ).
    r_secs = r_secs + ( frac( i_ts1 ) - frac( i_ts2 ) ).
  ENDMETHOD.

ENDCLASS.

Consider to add handling for overflows, if you are using it productive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.