SAP 日期时间处理总结

 


  DATA: lv_date        TYPE sy-datum,
        lv_date2       TYPE sy-datum,
        lv_time        TYPE sy-uzeit,
        lv_time2       TYPE sy-uzeit,
        lv_timestampl  TYPE timestampl,      " UTC Time Stamp in Long  Form (YYYYMMDDhhmmssmmmuuun)
        lv_timestamp_1 TYPE timestamp,       " UTC Time Stamp in Short Form (YYYYMMDDhhmmss)
        lv_timestamp_2 TYPE timestamp,       " UTC Time Stamp in Short Form (YYYYMMDDhhmmss)
        lv_timestamp_3 TYPE timestamp,       " UTC Time Stamp in Short Form (YYYYMMDDhhmmss)

        lv_date3       TYPE char10,
        lv_char10      TYPE char10.

  GET TIME STAMP FIELD lv_timestamp_1.
  GET TIME STAMP FIELD lv_timestampl.

  lv_date = sy-datum.
  lv_time = sy-uzeit.


*----------------------------------------------------------------------*
* YYYYMMDD Short Date Format Convert to Short Timestamp 79768771
*----------------------------------------------------------------------*
  CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
    EXPORTING
      date_internal            = sy-datum  " YYYYMMDD
    IMPORTING
      date_external            = lv_date3  " YYYY.MM.DD
    EXCEPTIONS
      date_internal_is_invalid = 1
      OTHERS                   = 2.

  CALL FUNCTION 'CONVERSION_EXIT_INVDT_INPUT'
    EXPORTING
      input  = lv_date3     " YYYY.MM.DD
    IMPORTING
      output = lv_char10.   " 79768771

  CLEAR: lv_date3.
  CALL FUNCTION 'CONVERSION_EXIT_INVDT_OUTPUT'
    EXPORTING
      input  = lv_char10    " 79768771
    IMPORTING
      output = lv_date3.    " YYYY.MM.DD


*----------------------------------------------------------------------*
* UTC Time Stamp in Short Form (YYYYMMDDhhmmss)
*----------------------------------------------------------------------*
  CLEAR: lv_timestamp_1.
  CONVERT DATE lv_date  TIME lv_time INTO TIME STAMP lv_timestamp_1
                                          TIME ZONE  'UTC'.
  CLEAR: lv_date, lv_time.
  CONVERT TIME STAMP lv_timestamp_1 TIME ZONE 'UTC'
                                    INTO DATE lv_date TIME lv_time.


*----------------------------------------------------------------------*
* UTC Time Stamp in Short Form (YYYYMMDDhhmmss)
*----------------------------------------------------------------------*
  CLEAR: lv_timestamp_2.
  CALL FUNCTION 'CIF_GEN4_CONVERT_DATETIME'
    EXPORTING
      iv_date                = lv_date
      iv_time                = lv_time
      iv_timezone            = 'UTC'
    IMPORTING
      ev_timestamp           = lv_timestamp_2
    EXCEPTIONS
      time_conversion_failed = 1
      OTHERS                 = 2.

  CLEAR: lv_date, lv_time.
  CALL FUNCTION 'CIF_GEN4_CONVERT_TIMESTAMP'
    EXPORTING
      iv_timestamp           = lv_timestamp_2
      iv_timezone            = 'UTC'
    IMPORTING
      ev_date                = lv_date
      ev_time                = lv_time
    EXCEPTIONS
      time_conversion_failed = 1
      OTHERS                 = 2.


*----------------------------------------------------------------------*
* Convert ABAP Timestamp to Java Timestamp(毫秒级别)
*----------------------------------------------------------------------*
  DATA:lv_str_timestampl TYPE char22,
       lv_java_timestamp TYPE string.

  CLEAR: lv_timestampl.
  GET TIME STAMP FIELD lv_timestampl.                    " 20231228140928.3582690

  lv_str_timestampl = lv_timestampl.

  cl_pco_utility=>convert_abap_timestamp_to_java(
    EXPORTING
      iv_date      = CONV #( lv_str_timestampl(8)    )   " 20231228
      iv_time      = CONV #( lv_str_timestampl+8(6)  )   " 140928
      iv_msec      = CONV #( lv_str_timestampl+15(3) )   " 358
    IMPORTING
      ev_timestamp = lv_java_timestamp  ).               " 1703772568358

  cl_pco_utility=>convert_java_timestamp_to_abap(
    EXPORTING
      iv_timestamp = lv_java_timestamp                   " 1703772568358
    IMPORTING
      ev_date = lv_date                                  " 20231228
      ev_time = lv_time  ).                              " 140928

* UTC Time Stamp in Short Form (YYYYMMDDhhmmss)
  CONVERT DATE lv_date  TIME lv_time INTO TIME STAMP lv_timestamp_1  " 20231228140928
                                          TIME ZONE  'UTC'.
  CLEAR: lv_date, lv_time.
  CONVERT TIME STAMP lv_timestamp_1 TIME ZONE  'UTC+8'
                                    INTO DATE lv_date TIME lv_time.  " 20231228  220928


*----------------------------------------------------------------------*
* Calculate + - Minutes or Seconds
*----------------------------------------------------------------------*
  DATA: lv_duration TYPE f.
  lv_duration = 10.

  CLEAR: lv_date2, lv_time2.
  CALL FUNCTION 'START_TIME_DETERMINE'
    EXPORTING
      duration                   = lv_duration
      unit                       = 'MIN'      " Minute
*     FACTORY_CALENDAR           =
    IMPORTING
      start_date                 = lv_date2   " 20231228   OUT
      start_time                 = lv_time2   " 215928     OUT
    CHANGING
      end_date                   = lv_date    " 20231228   IN
      end_time                   = lv_time    " 220928     IN
    EXCEPTIONS
      factory_calendar_not_found = 1
      date_out_of_calendar_range = 2
      date_not_valid             = 3
      unit_conversion_error      = 4
      si_unit_missing            = 5
      parameters_not_valid       = 6
      OTHERS                     = 7.

  lv_duration = 10.

  CLEAR: lv_date, lv_time.
  CALL FUNCTION 'END_TIME_DETERMINE'
    EXPORTING
      duration                   = lv_duration
      unit                       = 'MIN'      " Minute
*     factory_calendar           =
    IMPORTING
      end_date                   = lv_date    " 20231228   OUT
      end_time                   = lv_time    " 220928     OUT
    CHANGING
      start_date                 = lv_date2   " 20231228   IN
      start_time                 = lv_time2   " 215928     IN
    EXCEPTIONS
      factory_calendar_not_found = 1
      date_out_of_calendar_range = 2
      date_not_valid             = 3
      unit_conversion_error      = 4
      si_unit_missing            = 5
      parameters_no_valid        = 6
      OTHERS                     = 7.

  lv_duration = 10.

  CLEAR: lv_date2, lv_time2.
  CALL FUNCTION 'START_TIME_DETERMINE'
    EXPORTING
      duration                   = lv_duration
      unit                       = 'S'        " Second
*     FACTORY_CALENDAR           =
    IMPORTING
      start_date                 = lv_date2   " 20231228   OUT
      start_time                 = lv_time2   " 220918     OUT
    CHANGING
      end_date                   = lv_date    " 20231228   IN
      end_time                   = lv_time    " 220928     IN
    EXCEPTIONS
      factory_calendar_not_found = 1
      date_out_of_calendar_range = 2
      date_not_valid             = 3
      unit_conversion_error      = 4
      si_unit_missing            = 5
      parameters_not_valid       = 6
      OTHERS                     = 7.

*----------------------------------------------------------------------*
* Calculate Timestamp Duration - Timestamp
*----------------------------------------------------------------------*
  DATA: lv_tstmp1 TYPE p,
        lv_tstmp2 TYPE p,
        lv_secs   TYPE timestamp.

  CLEAR: lv_timestamp_1.
  CONVERT DATE lv_date TIME lv_time INTO TIME STAMP lv_timestamp_1
                                         TIME ZONE  sy-zonlo.
  lv_tstmp1 = lv_timestamp_1.
  lv_tstmp2 = '19700101000000'.
  TRY.
      CALL METHOD cl_abap_tstmp=>subtract
        EXPORTING
          tstmp1 = lv_tstmp1   " 20231229003642
          tstmp2 = lv_tstmp2   " 19700101000000
        RECEIVING
          r_secs = lv_secs.    " 1703810167

    CATCH cx_parameter_invalid_range .
    CATCH cx_parameter_invalid_type .
  ENDTRY.

*----------------------------------------------------------------------*
* Calculate Timestamp Duration - Seconds / Hours
*----------------------------------------------------------------------*
  DATA: lv_seconds TYPE /sdf/cmo_time-seconds,
        lv_hours   TYPE /sdf/cmo_time-hours.

  lv_date  = sy-datum.
  lv_date2 = sy-datum.
  lv_time  = sy-uzeit.
  lv_time2 = sy-uzeit + 3600.

  CLEAR: lv_timestamp_1.
  CONVERT DATE lv_date  TIME lv_time  INTO TIME STAMP lv_timestamp_1
                                           TIME ZONE  'UTC'.
  CLEAR: lv_timestamp_2.
  CONVERT DATE lv_date2 TIME lv_time2 INTO TIME STAMP lv_timestamp_2
                                           TIME ZONE  'UTC'.
  CALL FUNCTION '/SDF/CMO_TIME_DIFF_GET'
    EXPORTING
      utc_start = lv_timestamp_1  " 20231228140928
      utc_end   = lv_timestamp_2  " 20231228150928
    IMPORTING
      second    = lv_seconds      " 3600
      hours     = lv_hours        " 1.0000000000000000E+00
    EXCEPTIONS
      paramerr  = 1
      OTHERS    = 2.


*----------------------------------------------------------------------*
* Calculate Time Duration - Minutes
*----------------------------------------------------------------------*
  DATA: lv_minutes TYPE i.

  lv_date  = sy-datum.
  lv_date2 = sy-datum.
  lv_time  = sy-uzeit.
  lv_time2 = sy-uzeit + 3600.

  CALL FUNCTION 'DELTA_TIME_DAY_HOUR'
    EXPORTING
      t1      = lv_time
      t2      = lv_time2
      d1      = lv_date
      d2      = lv_date2
    IMPORTING
      minutes = lv_minutes.   " 60 minutes


*----------------------------------------------------------------------*
* Calculate Time Duration - Seconds
*----------------------------------------------------------------------*
  DATA: lv_duration_2 TYPE sytabix.

  lv_date  = sy-datum.
  lv_date2 = sy-datum.
  lv_time  = sy-uzeit.
  lv_time2 = sy-uzeit + 3600.

  CALL FUNCTION 'SWI_DURATION_DETERMINE'
    EXPORTING
      start_date = lv_date
      end_date   = lv_date2
      start_time = lv_time
      end_time   = lv_time2
    IMPORTING
      duration   = lv_duration_2.  " 3600 seconds

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值