[英]Difference between 2 timestamps
I have a program in ABAP where they use the type 'timestampl' in a variable, so they can get the time of certain events.我在 ABAP 中有一个程序,他们在变量中使用“timestamp”类型,因此他们可以获得某些事件的时间。 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.我现在的任务是获取其中 2 个变量之间的差异,但我似乎找不到功能模块或其他解决方案。
Any help is much appreciated!任何帮助深表感谢!
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.使用方法CL_ABAP_TSTMP=>SUBTRACT
,通过传递两个时间戳必须是TIMESTAMPL
类型以便包含毫秒,并且两个时间戳之间的差异将以秒数返回,包括毫秒。
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
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.谷歌搜索出现了这个建议: http ://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/840ad679-0601-0010-cd8e-9989fd650822# q-8 :使用类CL_ABAP_TSTMP。 You can also see an example of how to use the class in this thread: http://scn.sap.com/thread/85476 .您还可以在此线程中查看如何使用该类的示例: 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 ).
Use the FM *'CCU_TIMESTAMP_DIFFERENCE'*使用 FM *'CCU_TIMESTAMP_DIFFERENCE'*
After Checking which timestamp is greater call the FM.在检查哪个时间戳更大之后调用 FM。
IF TIMESTAMP_2 > TIMESTAMP_1.
CALL FUNCTION 'CCU_TIMESTAMP_DIFFERENCE'
EXPORTING
TIMESTAMP1 = TIMESTAMP_2
TIMESTAMP2 = TIMESTAMP_1
IMPORTING
DIFFERENCE = TIMESTAMP_DIFFERENCE.
EndIf.
The existing proposals ignore the milliseconds.现有的提案忽略了毫秒。 Here's a solution for modern ABAP AS that also considers msecs:这是现代 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 and i_ts2 are two timestamps, r_secs (type f) is the result. i_ts1 和 i_ts2 是两个时间戳,r_secs(类型 f)是结果。
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.如果您正在高效地使用它,请考虑添加溢出处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.