3

我在 ABAP 中有一个程序,他们在变量中使用“timestamp”类型,因此他们可以获得某些事件的时间。他们使用它是因为他们需要毫秒。

我现在的任务是获取其中两个变量之间的差异,但我似乎找不到功能模块或其他解决方案。

任何帮助深表感谢!

4

4 回答 4

5

使用该方法CL_ABAP_TSTMP=>SUBTRACT,通过传递两个时间戳,它们必须是TIMESTAMPL包含毫秒的类型,并且两个时间戳之间的差异将以秒数返回,包括毫秒。

例子:

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
于 2013-09-09T09:09:28.523 回答
1

谷歌搜索出现了这个建议:http ://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/840ad679-0601-0010-cd8e-9989fd650822#q-8 :使用类CL_ABAP_TSTMP。您还可以在此线程中查看如何使用该类的示例: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 ).
于 2012-05-10T17:51:36.830 回答
0

使用 FM *'CCU_TIMESTAMP_DIFFERENCE'*

检查哪个时间戳更大后,调用 FM。

IF TIMESTAMP_2 > TIMESTAMP_1.

 CALL FUNCTION 'CCU_TIMESTAMP_DIFFERENCE'
   EXPORTING
      TIMESTAMP1 = TIMESTAMP_2
      TIMESTAMP2 = TIMESTAMP_1 
   IMPORTING 
      DIFFERENCE = TIMESTAMP_DIFFERENCE.
EndIf.
于 2012-05-11T05:59:08.763 回答
0

现有的提议忽略了毫秒。这是现代 ABAP AS 的解决方案,它也考虑了毫秒:

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 和 i_ts2 是两个时间戳,r_secs (type f) 是结果。

这是一个小测试器:

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.

如果您正在使用它,请考虑添加溢出处理。

于 2019-03-01T12:48:06.950 回答