How to convert an UTC timestamp to system date and time in ABAP

seso picture seso · Apr 3, 2012 · Viewed 45.9k times · Source

does anyone of you have suggestions how to convert a given UTC timestamp into the date and time of the system time zone?

Converting from an UTC timestamp to the users local time zone is easy, you could just do:

CONVERT TIME STAMP lv_utc_timestamp TIME ZONE sy-zonlo
          INTO DATE lv_local_date TIME lv_local_time.

But how to do it for the system time - system time is needed in many situations e.g. when calling the JOB_CLOSE function module. The only solution I have come up so far is like that:

SELECT SINGLE * FROM TTZCU INTO ls_ttzcu.
CONVERT TIME STAMP lv_utc_timestamp TIME ZONE ls_ttzcu-tzonesys
          INTO DATE lv_system_date TIME lv_system_time.

Is this already the best solution or can the system time zone be retrieved in another way? Is there always a valid time zone to be expected from the entry in table TTZCU? Any ideas?

UPDATE: @rmtiwari suggested on twitter, that the FLAGACTIVE flag of TTZCU should also be checked, so the modified statement would be

SELECT SINGLE * FROM TTZCU INTO ls_ttzcu WHERE flagactive = abap_true.
CONVERT TIME STAMP lv_utc_timestamp TIME ZONE ls_ttzcu-tzonesys
          INTO DATE lv_system_date TIME lv_system_time.

UPDATE2: I have found another way, which is probably the best:

    cl_abap_tstmp=>systemtstmp_utc2syst(
           EXPORTING  utc_tstmp = lv_utc_timestamp  
           IMPORTING  syst_date = lv_system_date    " System Date
                      syst_time = lv_system_time    " System Time
           ).

Answer

seso picture seso · Jul 4, 2012

The best way seems to be:

cl_abap_tstmp=>systemtstmp_utc2syst(
       EXPORTING  utc_tstmp = lv_utc_timestamp  
       IMPORTING  syst_date = lv_system_date    " System Date
                  syst_time = lv_system_time    " System Time
       ).