Skip to Content
Author's profile photo Former Member

ABAP UNIX timestamp

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.

Assigned tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      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.

       

      Author's profile photo Ashok Kumar M
      Ashok Kumar M

      Hi,

      How about this?

      DATA: lv_time_utc TYPE i.
      
      CALL 'ALERTS' ID 'ADMODE'   FIELD 20
                    ID 'OPCODE'   FIELD 30
                    ID 'ACT_TIME' FIELD lv_time_utc.
      
      WRITE: lv_time_utc. "in seconds. (seconds since Jan  01 1970).

      Best Regards,

      Ashok.