Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Date and Time Processing →  Time Stamp 

Converting Time Stamps

This example demonstrates the statements CONVERT TIME STAMP and CONVERT INTO TIME STAMP.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA: date TYPE d,
          time TYPE t,
          ts   TYPE timestamp.
    date1 = sy-datlo.
    time1 = sy-timlo.
    tz1 = tz2 = 'UTC'.
    DO.
      IF sy-index > 1.
        CALL SELECTION-SCREEN 100 STARTING AT 10 10.
        IF sy-subrc <> 0.
          EXIT.
        ENDIF.
      ENDIF.
      date = date1.
      time = time1.
      IF dst_flag = abap_false.
        CONVERT DATE date TIME time
                INTO TIME STAMP ts TIME ZONE tz1.
      ELSE.
        CONVERT DATE date TIME time DAYLIGHT SAVING TIME dst1
                INTO TIME STAMP ts TIME ZONE tz1.
      ENDIF.
      CASE sy-subrc.
        WHEN 4.
          MESSAGE 'Time zone set to UTC'
                  TYPE 'I' DISPLAY LIKE 'W'.
        WHEN 8.
          MESSAGE 'Invalid time zone'
                  TYPE 'I' DISPLAY LIKE 'E'.
          CONTINUE.
        WHEN 12.
          MESSAGE 'Invalid input values for date,'
                & ' time or daylight saving time'
                   TYPE 'I' DISPLAY LIKE 'E'.
          CONTINUE.
      ENDCASE.
      CONVERT TIME STAMP ts TIME ZONE tz2
              INTO DATE date TIME time DAYLIGHT SAVING TIME dst2.
      CASE sy-subrc.
        WHEN 4.
          MESSAGE 'Time zone set to UTC'
                  TYPE 'I' DISPLAY LIKE 'W'.
        WHEN 8.
          MESSAGE 'Invalid time zone'
                  TYPE 'I' DISPLAY LIKE 'E'.
          CONTINUE.
        WHEN 12.
          MESSAGE 'Invalid time stamp'
                  TYPE 'I' DISPLAY LIKE 'E'.
          CONTINUE.
      ENDCASE.
      tsout = |{ ts TIMESTAMP = ISO TIMEZONE = 'UTC   ' }|.
      date2 = date.
      time2 = time.
    ENDDO.

Description

The program is given a date, a time, and a time zone and converts this information into a time stamp. The program can also include summer time in its calculations. The resulting time stamp is then converted to the local date and local time of another time zone. The following table shows several possible combinations of input and output, where spc stands for a space and - for no input or output.

date1 time1 tz1 dst1 ts tz2 date2 time2 dst2 sy-subrc
16.07.2010 09:10:00 CET - 2010-07-16T07:10:00 CET 16.07.2010 09:10:00 X -
16.07.2010 09:10:00 CET X 2010-07-16T07:10:00 CET 16.07.2010 09:10:00 X -
16.07.2010 09:10:00 CET spc - CET - - - 12
16.12.2010 09:10:00 CET spc 2010-12-16T08:10:00 CET 16.12.2010 09:10:00 - -
16.12.2010 09:10:00 CET X - CET - - - 12
16.12.2010 09:10:00 AUSTAS - 2010-12-15T22:10:00 AUSTAS 16.12.2010 09:10:00 X -

The third and fifth rows demonstrate that invalid summer time data produces the value 12 in sy-subrc.