Convert date time between UTC and different timezones
*& Report ZTIMEZONE_UPDATE
*&---------------------------------------------------------------------*
*& Date time conversion between local time, UTC time can be a bit
*& confusing at times then you add in different timezones and different
*& representations of UTC time:
*&
*& UTC FORMAT
*& ----------
*& UTC with a Z means its in UTC time i.e. 2021-01-01T10:30:01Z
*&
*& UTC without a Z means local time and the offset to UTC will be added
*& to the end i.e. 2021-05-01T10:30:01+05:00
*&---------------------------------------------------------------------*
REPORT ztimezone_update.
DATA: gd_date TYPE timezone.
PARAMETERS: p_vbeln TYPE vbap-vbeln.
PERFORM timezone_update USING p_vbeln
CHANGING gd_date.
*&---------------------------------------------------------------------*
*& Form UPD DATE BASED ON TIMEZONE
*&---------------------------------------------------------------------*
* text
*---------------------------------------------------------------------------------*
FORM timezone_update USING p_vbeln
CHANGING p_date .
DATA: wa_vbpawe TYPE vbpa,
wa_kna1 TYPE kna1,
wa_adrc TYPE adrc.
DATA: ld_utcdiff TYPE tznutcdiff,
ld_utcsign TYPE tznutcsign,
ld_is_in_dst TYPE xflag,
ld_utcdiffmin(2) TYPE c.
*Get Ship to data (receiver)
SELECT SINGLE *
FROM vbpa
INTO CORRESPONDING FIELDS OF wa_vbpawe
WHERE vbeln EQ p_vbeln
AND posnr EQ '00000'
AND parvw EQ zif_sd_constants=>gc_partner_func_ship_to. "'WE'. "ship to
SELECT SINGLE *
FROM kna1
INTO wa_kna1
WHERE kunnr EQ wa_vbpawe-kunnr. " ship to
* Get customer address data
SELECT SINGLE * "TIME_ZONE
FROM adrc
INTO wa_adrc
WHERE addrnumber EQ wa_kna1-adrnr.
*
IF wa_adrc-time_zone NE sy-zonlo.
CALL FUNCTION 'TZON_GET_OFFSET'
EXPORTING
if_timezone = wa_adrc-time_zone
if_local_date = sy-datum
if_local_time = sy-uzeit
IMPORTING
ef_utcdiff = ld_utcdiff
ef_utcsign = ld_utcsign
ef_is_in_dst = ld_is_in_dst
EXCEPTIONS
conversion_error = 1
OTHERS = 2.
IF sy-subrc EQ 0.
DATA: ld_time TYPE sy-uzeit,
ld_texttz(20) TYPE c,
ld_date TYPE sy-datum,
ld_seconds TYPE i.
ld_texttz = p_date.
SHIFT ld_texttz LEFT DELETING LEADING space.
ld_time = ld_texttz+8(6).
ld_date = ld_texttz(8).
* Convert hours to seconds
ld_seconds = ld_seconds + ( ld_utcdiff(2) * 3600 ).
* Convert minutes to seconds
ld_seconds = ld_seconds + ( ld_utcdiff+2(2) * 60 ).
* Add seconds
ld_seconds = ld_seconds + ld_utcdiff+4(2) .
ld_seconds = ld_seconds * -1.
CALL METHOD cl_abap_tstmp=>td_add
EXPORTING
date = ld_date
time = ld_time
secs = ld_seconds "'36000' "seconds " 10 hours
IMPORTING
res_date = ld_date
res_time = ld_time.
* If no timezone conversion needed you can just concatenate
" CONCATENATE ld_date ld_time INTO ld_texttz.
" p_date = ld_texttz.
* If the date is in utc you can use function module ISU_DATE_TIME_CONVERT_TIMEZONE to convert the date
CALL FUNCTION 'ISU_DATE_TIME_CONVERT_TIMEZONE'
EXPORTING
x_date_utc = ld_date
x_time_utc = ld_time
x_timezone = sy-zonlo
IMPORTING
y_date_lcl = ld_date
y_time_lcl = ld_time.
* Then Concatenate date time into timestamp field via text field
CONCATENATE ld_date ld_time INTO ld_texttz.
p_date = ld_texttz.
* If the date is in your local timezone (i.e. sy-ZONLO) you can use ABAP statement CONVERT DATE
* to alter the date and build a timestamp in the appropriate time zone
" CONVERT DATE ld_date TIME ld_time INTO TIME STAMP p_date TIME ZONE 'AUSVIC'.
ENDIF.
ENDIF.
ENDFORM.