Skip to Content

Getting UNIX/Epoch timestamp from the UI client is not reliable. Timestamps are generated based on the time set on the device. It is much better to let the ABAP application server create timestamps. To my knowledge, it is not possible to to generate a UNIX/Epoch timestamp in any SAP delivered method or function.

This little static method generates a timestamp that equals number of milliseconds since 01.01.1970.

class zcl_epoch definition
  public
  final
  create public .

  public section.

    class-methods get_time
      returning
        value(value) type numc13 .
  protected section.
  private section.
endclass.

class zcl_epoch implementation.

* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_EPOCH=>GET_TIME
* +-------------------------------------------------------------------------------------------------+
* | [<-()] VALUE                          TYPE        NUMC13
* +--------------------------------------------------------------------------------------</SIGNATURE>
  method get_time.

    data time type tzntstmpl.
    data str1 type string.
    data str2 type string.
    data tstmp1 type p.
    data tstmp2 type p.
    data secs type tzntstmpl.

    get time stamp field time.

    tstmp1 = time.
    tstmp2 = '19700101000000'.

    try.
        secs = cl_abap_tstmp=>subtract(
          tstmp1 = tstmp1
          tstmp2 = tstmp2
        ).
      catch cx_parameter_invalid_range .
      catch cx_parameter_invalid_type .
    endtry.

    str1 = secs.
    str2 = time.
    value = str1(10) && str2+15(3).

  endmethod.
endclass.
To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

  1. Shai Sinai

    Thanks for sharing.

    Actually, there is also SAP (standard?) method hiding out there:
    cl_pco_utility=>convert_java_timestamp_to_abap

    You may you use it the following:

    METHOD unix_time_to_timestamp.
    
      DATA: lv_timestamp_msec TYPE string,
            lv_date TYPE datum,
            lv_time TYPE uzeit.
    
      CLEAR rv_timestamp.
    
      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
    *     ev_msec      =     " Remaining Milliseconds
      ).
      CONVERT DATE lv_date TIME lv_time INTO TIME STAMP rv_timestamp
        TIME ZONE c_tzone_none. " ''
    
    ENDMETHOD.

     

    (0) 

Leave a Reply