1. 程式人生 > >SAP ABAP CDS FUNCTION OF date/time

SAP ABAP CDS FUNCTION OF date/time

ABAP CDS - Date/Time Conversions

Syntax

... TSTMP_TO_DATS(tstmp,tzone,clnt,on_error) 
  | TSTMP_TO_TIMS(tstmp,tzone,clnt,on_error) 
  | TSTMP_TO_DST(tstmp,tzone,clnt,on_error) 
  | DATS_TIMS_TO_TSTMP(date,time,tzone,clnt,on_error) ... 
 

Variants:

Effect

These functions convert time stamps into dates or times, and the other way round. The argument tstmp must have the built-in data type DEC

 with length 15, or the type of the data element TIMESTAMP. The content of this argument is interpreted as an ABAP-specifictime stamp.

The functions have positional parameters to which actual parameters need to be assigned when called. There are currently no optional parameters. Suitable fields of a data source

literalsparameterspath expressions, predefined functions, or expressions can all be specified as actual parameters. Only literals can be passed to the parameter on_error. If an actual parameter contains the null value, every function returns a null value.

The conversions follow the ABAP-specific rules for time zones

. The associated database tables must be filled correctly.

Note

These functions move the functionality of the ABAP statements CONVERT TIME STAMP and CONVERT INTO TIME STAMP to the database. 
 

Variant 1

... TSTMP_TO_DATS(tstmp,tzone,clnt,on_error) 

 

Effect

The function TSTMP_TO_DATS extracts the local date for the time zone specified in tzone from a time stamp in the argument tstmp.

The actual parameter tstmp must have the predefined data type DEC with length 15 and no decimal places and contain a valid time stamp in the format YYYYMMDDHHMMSS. tzone expects an actual parameter of the type CHAR with length 6 and containing a valid time zone. The actual parameter clnt must have the built-in dictionary type CLNT and must contain a valid client ID. This client ID is used in the evaluation of the system tables of the rules for time zones. The return value has the built-in data type DATS.

The actual parameter on_error controls error handling. It must have the predefined data type CHAR with the length 10 and must have one of the following values:

  • "FAIL" (an error raises an exception)
  • "NULL" (an error returns the null value)
  • "INITIAL" (an error returns the initial value)

The format is not case-sensitive. Any incorrectly specified values raise an exception. 
 

Variant 2

... TSTMP_TO_TIMS(tstmp,tzone,clnt,on_error) 

 

Effect

The function TSTMP_TO_TIMS extracts the local time for the time zone specified in tzone from a time stamp in the argument tstmp.

For the actual parameters tstmp, tzone, clnt, and on_error, the same applies as for the function TSTMP_TO_DATS. The return value has the built-in data type TIMS
 

Variant 3

... TSTMP_TO_DST(tstmp,tzone,clnt,on_error) 

 

Effect

The function TSTMP_TO_DST extracts the summer time marker for the time zone specified in tzone from a time stamp in the argument tstmp. This is "X" if the time stamp for the time zone is in the summer time, otherwise it is initial.

For the actual parameters tstmp, tzone, clnt, and on_error, the same applies as for the function TSTMP_TO_DATS. The return value has the built-in data type CHAR with length 1. 
 

Variant 4

... DATS_TIMS_TO_TSTMP(date,time,tzone,clnt,on_error) 

 

Effect

The function DATS_TIMS_TO_TSTMP constructs a time stamp from a local date specified in date and a local time specified in time in the time zone specified in tzone. The summer time is respected implicitly.

The actual parameter date must have the built-in data type DATS and should contain a valid date. The actual parameter time must have the built-in data type TIMS and should contain a valid time. For the actual parameters tzone, clnt, and on_error, the same applies as for the function TSTMP_TO_DATS.

The return value has the built-in data type DEC with length 15 and represents an ABAP-specific time stamp.

Example

In the following CDS view, the date, the time, and the summer time marker of the current UTC time stamp are extracted using the conversion functionsTSTMP_TO_DATS, TSTMP_TO_TIMS, and TSTMP_TO_DST. The values of the columns DATS1 and TIMS1 of the database table DEMO_EXPRESSIONS are combined into a time stamp by the conversion function DATS_TIMS_TO_TSTMP. The program DEMO_CDS_DATE_TIME accesses the view and displays the result.

@AbapCatalog.sqlViewName: 'demo_cds_datim' 
@AccessControl.authorizationCheck: #NOT_REQUIRED 
define view demo_cds_date_time 
  as select from 
    demo_expressions 
    { 
      tstmp_current_utctimestamp() as tstmp, 
      tstmp_to_dats( tstmp_current_utctimestamp(), 
                     abap_system_timezone( $session.client,'NULL' ), 
                     $session.client, 
                     'NULL' )      as dat, 
      tstmp_to_tims( tstmp_current_utctimestamp(), 
                     abap_system_timezone( $session.client,'NULL' ), 
                     $session.client, 
                     'NULL' )      as tim, 
      tstmp_to_dst( tstmp_current_utctimestamp(), 
                    abap_system_timezone( $session.client,'NULL' ), 
                    $session.client, 
                    'NULL' )       as dst, 
      dats_tims_to_tstmp( dats1, 
                          tims1, 
                          abap_system_timezone( $session.client,'NULL' ), 
                          $session.client, 
                         'NULL' )  as dat_tim 
    }