SAP Tutorials Blog


 

 


DATA date2utcl TYPE d VALUE '20240101'.

DATA time2utcl TYPE t VALUE '112458'.

DATA utcl TYPE utclong.


"2024-01-01 16:24:58.0000000

CONVERT DATE date2utcl

        TIME time2utcl

        TIME ZONE 'EST'

        INTO UTCLONG utcl.


"Using optional additions

"Check the details in the ABAP Keyword Documentation.

"Specifying 'X' for DAYLIGHT SAVING TIME in the following

"example raises a catchable exception (CX_SY_CONVERSION_NO_DATE_TIME).

"2024-01-01 16:24:58.7681270

CONVERT DATE date2utcl

        TIME time2utcl

        FRACTIONAL SECONDS CONV decfloat34( '0.768127' )

        DAYLIGHT SAVING TIME ''

        TIME ZONE 'EST'

        INTO UTCLONG DATA(utcl_inl1).


"There is no time shift in case of an initial time zone specification.

"2024-01-01 11:24:58.0000000

CONVERT DATE date2utcl

        TIME time2utcl

        TIME ZONE VALUE #( )

        INTO UTCLONG DATA(utcl_inl2).


"Ensure that valid values are passed

"The following example explores ABAP statements with invalid values 

"that are passed. The valid time stamp, the exceptions raised and 

"the error messages are added to an internal table.

DATA error_checks TYPE string_table.

DATA date_test TYPE d.

DATA time_test TYPE t.

DATA frac_sec_test TYPE decfloat34.

DATA dls_test TYPE abap_bool.

DATA tz_test TYPE string.

DO 6 TIMES.

  date_test = '20240101'.

  time_test = '112458'.

  frac_sec_test = '0.768127'.

  dls_test = abap_false.

  tz_test = `EST`.


  "Note: In newer ABAP releases, some of the following statements 

  "show a syntax warning. The values of some literals (intentionally 

  "specified here like this) are not admissable values for the target

  "types. 

  CASE sy-index.

    WHEN 1.

      "No data object change. The statement below should return a valid time stamp.

    WHEN 2.

      "Invalid date

      date_test = '20249999'.

    WHEN 3.

      "Invalid time

      time_test = '992458'.

    WHEN 4.

      "Invalid fractions of seconds

      frac_sec_test = '1'.

    WHEN 5.

      "Invalid specification for this particular example.

      dls_test = 'X'.

    WHEN 6.

      "Invalid time zone

      dls_test = `NOPE`.

  ENDCASE.


  TRY.

      CONVERT DATE date_test

              TIME time_test

              FRACTIONAL SECONDS frac_sec_test

              DAYLIGHT SAVING TIME dls_test

              TIME ZONE tz_test

              INTO UTCLONG DATA(utcl_inl3).

      error_checks = VALUE #( BASE error_checks ( |({ sy-index }) Valid time stamp: { utcl_inl3 }| ) ).

    CATCH cx_root INTO DATA(err).

      error_checks = VALUE #( BASE error_checks

        ( |({ sy-index }) Exception | &&

          |{ replace( val = cl_abap_typedescr=>describe_by_object_ref( err )->absolute_name

                      sub = `\CLASS=`

                      with = `` ) } was raised: | &&

          |{ err->get_text( ) }| ) ).

  ENDTRY.

ENDDO.


Serkan AKKAVAK

Computer Engineer BSc

Head of SAP & Software Department

Contact : serkurumsal@yandex.com