Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Date and Time Processing →  Time Stamps →  Time Stamp Field with Time Stamp Type →  Conversion Statements for Time Stamp Fields 

Convert Time Stamp in Time Stamp Field

This example demonstrates the statements CONVERT INTO UTCLONG and CONVERT UTCLONG.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA: date TYPE d,
          time TYPE t,
          ts   TYPE utclong.
    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.
      TRY.
          IF dst_flag = abap_false.
            CONVERT DATE date
                    TIME time
                    TIME ZONE tz1
                    INTO UTCLONG ts.
          ELSE.
            CONVERT DATE date
                    TIME time
                    DAYLIGHT SAVING TIME dst1
                    TIME ZONE tz1
                    INTO UTCLONG ts.
          ENDIF.
        CATCH cx_sy_conversion_no_date_time INTO DATA(exc1).
          MESSAGE exc1->get_text( )
                  TYPE 'I' DISPLAY LIKE 'E'.
          CONTINUE.
        CATCH cx_sy_conversion_no_date INTO DATA(exc2).
          MESSAGE exc2->get_text( )
                  TYPE 'I' DISPLAY LIKE 'E'.
          CONTINUE.
        CATCH cx_sy_conversion_no_time INTO DATA(exc3).
          MESSAGE exc3->get_text( )
                  TYPE 'I' DISPLAY LIKE 'E'.
          CONTINUE.
        CATCH cx_parameter_invalid_range INTO DATA(exc4).
          MESSAGE exc4->get_text( )
                  TYPE 'I' DISPLAY LIKE 'E'.
          CONTINUE.
      ENDTRY.
      TRY.
          CONVERT UTCLONG ts
                  INTO DATE date
                       TIME time
                       DAYLIGHT SAVING TIME dst2
                       TIME ZONE tz2.
        CATCH cx_sy_conversion_no_date_time INTO exc1.
          MESSAGE exc1->get_text( )
                  TYPE 'I' DISPLAY LIKE 'E'.
          CONTINUE.
      ENDTRY.
      tsout = ts.
      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 cx_...
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 - - - X
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 - - - X
16.12.2010 09:10:00 AUSTAS - 2010-12-15T22:10:00 AUSTAS 16.12.2010 09:10:00 X -
01.01.0001 01:00:00 UTC+4 - - UTC+4 - - - X
31.12.9999 23:59:59 UTC-4 - 9999-12-31T23:59:59 UTC 31.12.9999 23:59:59 - -

The third and fifth rows demonstrate that invalid daylight saving time data raises an exception. The last two rows show how values behave at the limits of the value range. Row 7 produces an invalid time stamp and hence raises an exception. The maximum value in row 8, however, does not calculate a time change and no exception is raised. If specified, 23:59:58 would raise an exception.