你们有人建议如何将Unix时间戳转换为ABAP MEZ/MESZ时间和日期吗?
以下代码来自ABAP参考,该代码用于长度为15或21的时间戳,但Unix时间戳当前为10位数。
DATA: time_stamp TYPE timestamp,
tz TYPE ttzz-tzone.
tz = 'MESZ'.
time_stamp = 15319830890000.
CONVERT TIME STAMP time_stamp TIME ZONE tz
INTO DATE DATA(dat) TIME DATA(tim)
DAYLIGHT SAVING TIME DATA(dst).
cl_demo_output=>write( |{ dat DATE = ISO } {
tim TIME = ISO } { dst }| ).
time_stamp = 15319830890000.
CONVERT TIME STAMP time_stamp TIME ZONE tz
INTO DATE dat TIME tim
DAYLIGHT SAVING TIME dst.
cl_demo_output=>write( |{ dat DATE = ISO } {
tim TIME = ISO } { dst }| ).
cl_demo_output=>display( ).
我用这段代码解决了这个问题。我现在使用13位数字划时代。。。
data: i(20) type n.
data: d type sy-datum."
data: t type sy-uzeit.
data: epoche type int8.
data: test type int8.
data: test2 type int8.
epoche = 1522836000000.
i = epoche / 1000.
d = '19700101'.
d = d + i div 86400.
t = i mod 86400.
write: d, t.
如果您想使用SAP时区进行转换,这个时区也可以工作(没有时间将其转换为ABAP对象(:
FORM unixtime_2_date_time
USING
i_unixtime TYPE numeric
i_timezone TYPE timezone
CHANGING
e_date TYPE d
e_time TYPE t.
DATA l_tstmp_unix_era TYPE TZNTSTMPL.
DATA l_tstmp TYPE TZNTSTMPL.
CONSTANTS utc TYPE timezone value IS INITIAL.
CONVERT DATE '19700101' TIME '000000' INTO TIME STAMP l_tstmp_unix_era TIME ZONE utc.
l_tstmp = CL_ABAP_TSTMP=>add( tstmp = l_tstmp_unix_era secs = i_unixtime ).
CONVERT TIME STAMP l_tstmp TIME ZONE i_TIMEZONE INTO DATE e_date TIME e_time .
ENDFORM.
您可以使用cl_pco_utility
Java实用程序类,特别是方法convert_Java_timestamp_to_abap。它完全接受10个字符的Unix时间。
CLASS zcl_epoch DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
unix_time_to_timestamp
IMPORTING iv_timestamp TYPE timestamp.
ENDCLASS.
CLASS zcl_epoch IMPLEMENTATION.
METHOD unix_time_to_timestamp.
CONSTANTS: c_tzone TYPE ttzz-tzone VALUE 'CET'.
DATA: lv_timestamp_msec TYPE string,
lv_timestamp TYPE timestamp,
lv_date TYPE datum,
lv_time TYPE uzeit.
lv_timestamp_msec = iv_timestamp * 1000.
cl_pco_utility=>convert_java_timestamp_to_abap(
EXPORTING
iv_timestamp = lv_timestamp_msec
IMPORTING
ev_date = lv_date
ev_time = lv_time
).
lv_timestamp = lv_date && lv_time.
CONVERT TIME STAMP lv_timestamp TIME ZONE c_tzone INTO DATE lv_date TIME lv_time.
cl_demo_output=>display( |{ lv_date DATE = ISO } { lv_time TIME = ISO }| ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
zcl_epoch=>unix_time_to_timestamp( '1532196799' ).
值得一提的是,MEZ/MSEZ是NOT标准缩写,不应使用。例如,MEZ与CET相同。在这里或这里查看常见时区列表。
此外,最好还是坚持使用系统中可用时区的列表,即TTZZ表。
只需将epoch时间添加到带有初始时间的"19700101"日期。
DATA lv_initial_timestamp TYPE timestamp.
CONVERT DATE '19700101' TIME '000000' INTO TIME STAMP lv_initial_timestamp TIME ZONE sy-zonlo.
ev_timestamp = cl_abap_tstmp=>add( tstmp = lv_initial_timestamp secs = iv_epoch_timestamp ).
CONVERT TIME STAMP ev_timestamp TIME ZONE sy-zonlo INTO DATE ev_date TIME ev_time.
对于MEZ/MESZ,您可以相应地选择时区。