在ABAP编程语言中,如何在TIMESTAMP
类型的时间内添加1天并正确翻转月份/年?
例如:
data lv_time type TIMESTAMP value '20180228000000'.
data(lv_new_time) = lv_time + 1. " should be '20180301000000', but instead is '20180228000001'
data(lv_new_time2) = lv_time + 1000000. " should be '20180301000000', but instead is '20180229000000'
答案 0 :(得分:6)
在查看了Jagger的答案之后,我查看了TIMESTAMP_DURATION_ADD
函数的内幕并发现了一些ABAP语法来完成这项工作而根本不需要函数调用。
constants: lc_time_zone type timezone value 'UTC'.
data lv_timestamp_before type timestamp value '20180228001234'.
data lv_timestamp_after type timestamp.
data lv_date like sy-datum.
data lv_time like sy-uzeit.
convert time stamp lv_timestamp_before time zone lc_time_zone
into date lv_date time lv_time.
lv_date = lv_date + 1.
convert date lv_date time lv_time
into time stamp lv_timestamp_after time zone lc_time_zone.
答案 1 :(得分:3)
这是一个适合您的解决方案。系统中应该可用的功能模块是TIMESTAMP_DURATION_ADD
。
REPORT zzz.
DATA lv_time TYPE timestamp VALUE '20180228000000'.
START-OF-SELECTION.
DATA timestamp_out TYPE timestamp.
CALL FUNCTION 'TIMESTAMP_DURATION_ADD'
EXPORTING
timestamp_in = lv_time
timezone = 'UTC'
duration = 1
unit = 'TAG' " day (in German)
IMPORTING
timestamp_out = timestamp_out
EXCEPTIONS
timestamp_error = 1
OTHERS = 2.
ASSERT sy-subrc = 0.
WRITE timestamp_out.
答案 2 :(得分:2)
如果您不想恢复旧的程序编程,可以使用documentation中描述的课程CL_ABAP_TSTMP
。
DATA some_timestamp TYPE timestamp VALUE '20180228000000'.
DATA(new_timestamp) = cl_abap_tstmp=>add(
tstmp = some_timestamp
secs = ( 365 * 24 * 60 * 60 )
).
(太糟糕了CL_ABAP_TSTMP=>SECSOFDAY
是私人的...但是,至少这会让你想到如何处理闰年......)
答案 3 :(得分:-1)
您可以使用FM IAM_TIMESTAMP_CALC将一天添加到时间戳,结果将是20180301000000:
i
答案 4 :(得分:-2)
我不确定它是否有效,但也许值得一试。
提取时间戳的日期,将日期添加一天,然后使用新日期和时间戳的其余部分创建新的时间戳。
像
这样的东西data: stamp type timestamp value '20180301000000000',
stamp_date type dats,
new_stamp type timestamp.
stamp_date = stamp(8). "if it doesn't work, just look for a FM
add 1 to stamp_date.
new_stamp = stamp_date && stamp+8.
我不确定它是否会起作用"按照#34;但是我很确定你能找到一种让它在那里工作的方法......我们都在等待为了正确的答案;)