我在ABAP中有一个程序,他们在变量中使用类型"timestampl",因此他们可以获取某些事件的时间。他们使用它是因为他们需要毫秒。
我现在的任务是获取其中 2 个变量之间的差异,但我似乎找不到函数模块或其他解决方案。
任何帮助都非常感谢!
使用方法 CL_ABAP_TSTMP=>SUBTRACT
,通过传递两个时间戳,这些时间戳必须是 TIMESTAMPL
类型,以便包含毫秒,并且 2 个时间戳之间的差异将以秒数(包括毫秒)返回。
例:
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
谷歌搜索显示以下建议: 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 ).
使用 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.
现有提案忽略毫秒。以下是现代 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(类型 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.
如果高效使用它,请考虑添加溢出处理。