LOGIC
----------------------------------------------------------------------
Purpose: Get Local Time according to the user's time zone, or time zone provided.
----------------------------------------------------------------------
SAMPLE CODE
----------------------------------------------------------------------
FUNCTION zut_get_local_time_by_uname.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(IM_UNAME) TYPE SYUNAME OPTIONAL
*" REFERENCE(IM_TZONE) TYPE TZNZONE OPTIONAL
*" REFERENCE(IM_TIMESTAMP) TYPE TZNTIMESTP OPTIONAL
*" EXPORTING
*" REFERENCE(EX_LOCAL_DATE) TYPE SYSTDATLO
*" REFERENCE(EX_LOCAL_TIME) TYPE SYSTTIMLO
*" REFERENCE(EX_LOCAL_TIMESTAMP) TYPE TZNTIMESTP
*"----------------------------------------------------------------------
*--------------------------------------*
* DECLARATION *
*--------------------------------------*
DATA: lv_tzone LIKE ttzdata-tzone,
lv_timestamp TYPE ttzdata-timestamp.
*--------------------------------------*
* TIME ZONE PREPARATION *
*--------------------------------------*
IF im_uname IS NOT INITIAL.
SELECT SINGLE tzone
INTO lv_tzone
FROM usr02
WHERE bname EQ im_uname.
IF sy-subrc NE 0.
IF im_tzone IS NOT INITIAL.
lv_tzone = im_tzone.
ELSE.
lv_tzone = 'UTC'.
ENDIF.
----------------------------------------------------------------------
Purpose: Get Local Time according to the user's time zone, or time zone provided.
----------------------------------------------------------------------
- Get Time Zone
- IF im_uname is provided, get tzone from table usr02.
- ELSEIF im_tzone is provided, tzone = im_tzone.
- ELSE default tzone to 'UTC'
- Get Current Time Stamp
- IF im_timestamp is not provided, call FM 'TZ_UTC_SYSTEMCLOCK' => UTC timestamp
- Get Local Time
- Call FM 'TZ_GLOBAL_TO_LOCAL' to get local time.
SAMPLE CODE
----------------------------------------------------------------------
FUNCTION zut_get_local_time_by_uname.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(IM_UNAME) TYPE SYUNAME OPTIONAL
*" REFERENCE(IM_TZONE) TYPE TZNZONE OPTIONAL
*" REFERENCE(IM_TIMESTAMP) TYPE TZNTIMESTP OPTIONAL
*" EXPORTING
*" REFERENCE(EX_LOCAL_DATE) TYPE SYSTDATLO
*" REFERENCE(EX_LOCAL_TIME) TYPE SYSTTIMLO
*" REFERENCE(EX_LOCAL_TIMESTAMP) TYPE TZNTIMESTP
*"----------------------------------------------------------------------
*--------------------------------------*
* DECLARATION *
*--------------------------------------*
DATA: lv_tzone LIKE ttzdata-tzone,
lv_timestamp TYPE ttzdata-timestamp.
*--------------------------------------*
* TIME ZONE PREPARATION *
*--------------------------------------*
IF im_uname IS NOT INITIAL.
SELECT SINGLE tzone
INTO lv_tzone
FROM usr02
WHERE bname EQ im_uname.
IF sy-subrc NE 0.
IF im_tzone IS NOT INITIAL.
lv_tzone = im_tzone.
ELSE.
lv_tzone = 'UTC'.
ENDIF.
ELSE.
IF lv_tzone IS INITIAL.
IF im_tzone IS NOT INITIAL.
lv_tzone = im_tzone.
ELSE.
lv_tzone = 'UTC'.
ENDIF.
ENDIF.
ENDIF.
ELSEIF im_tzone IS NOT INITIAL.
lv_tzone = im_tzone.
ELSE.
lv_tzone = 'UTC'.
ENDIF.
*--------------------------------------*
* TIMESTAMP PREPARATION *
*--------------------------------------*
IF im_timestamp IS NOT INITIAL.
lv_timestamp = im_timestamp.
ELSE.
CALL FUNCTION 'TZ_UTC_SYSTEMCLOCK'
IMPORTING
* UTC_DATE =
* UTC_TIME =
utc_timestamp = lv_timestamp
EXCEPTIONS
OTHERS = 1.
IF sy-subrc NE 0.
lv_timestamp = sy-datum.
lv_timestamp+8 = sy-uzeit.
ENDIF.
ENDIF.
*--------------------------------------*
* GET LOCAL TIME *
*--------------------------------------*
CALL FUNCTION 'TZ_GLOBAL_TO_LOCAL'
EXPORTING
* DATE_GLOBAL = '00000000'
timestamp_global = lv_timestamp
timezone = lv_tzone
* TIME_GLOBAL = '000000'
IMPORTING
date_local = ex_local_date
timestamp_local = ex_local_timestamp
time_local = ex_local_time
EXCEPTIONS
OTHERS = 4.
ENDFUNCTION.
IF lv_tzone IS INITIAL.
IF im_tzone IS NOT INITIAL.
lv_tzone = im_tzone.
ELSE.
lv_tzone = 'UTC'.
ENDIF.
ENDIF.
ENDIF.
ELSEIF im_tzone IS NOT INITIAL.
lv_tzone = im_tzone.
ELSE.
lv_tzone = 'UTC'.
ENDIF.
*--------------------------------------*
* TIMESTAMP PREPARATION *
*--------------------------------------*
IF im_timestamp IS NOT INITIAL.
lv_timestamp = im_timestamp.
ELSE.
CALL FUNCTION 'TZ_UTC_SYSTEMCLOCK'
IMPORTING
* UTC_DATE =
* UTC_TIME =
utc_timestamp = lv_timestamp
EXCEPTIONS
OTHERS = 1.
IF sy-subrc NE 0.
lv_timestamp = sy-datum.
lv_timestamp+8 = sy-uzeit.
ENDIF.
ENDIF.
*--------------------------------------*
* GET LOCAL TIME *
*--------------------------------------*
CALL FUNCTION 'TZ_GLOBAL_TO_LOCAL'
EXPORTING
* DATE_GLOBAL = '00000000'
timestamp_global = lv_timestamp
timezone = lv_tzone
* TIME_GLOBAL = '000000'
IMPORTING
date_local = ex_local_date
timestamp_local = ex_local_timestamp
time_local = ex_local_time
EXCEPTIONS
OTHERS = 4.
ENDFUNCTION.
No comments:
Post a Comment