SAP CDS TIMESTAMP FUNCTIONS V1



Get Example source ABAP code based on a different SAP table
  


• UTCL_CURRENT ABAP_CDS_FUNCTION_V1
• UTCL_ADD_SECONDS ABAP_CDS_FUNCTION_V1
• UTCL_SECONDS_BETWEEN ABAP_CDS_FUNCTION_V1
• TSTMP_IS_VALID ABAP_CDS_FUNCTION_V1
• TSTMP_CURRENT_UTCTIMESTAMP ABAP_CDS_FUNCTION_V1
• TSTMP_SECONDS_BETWEEN ABAP_CDS_FUNCTION_V1
• TSTMP_ADD_SECONDS ABAP_CDS_FUNCTION_V1

ABAP_CDS_DDL - DDIC-Based View, Time Stamp Functions

ABAP_SYNTAX
... UTCL_CURRENT()
$| UTCL_ADD_SECONDS(utcl,seconds)
$| UTCL_SECONDS_BETWEEN(utcl1,utcl2)
$| TSTMP_IS_VALID(tstmp)
$| TSTMP_CURRENT_UTCTIMESTAMP()
$| TSTMP_SECONDS_BETWEEN(tstmp1,tstmp2,on_error)
$| TSTMP_ADD_SECONDS(tstmp,seconds,on_error) ...

ABAP_VARIANTS:
1 ... UTCL_CURRENT()
2 ... UTCL_ADD_SECONDS(utcl,seconds)
3 ... UTCL_SECONDS_BETWEEN(utcl1,utcl2)
4 ... TSTMP_IS_VALID(tstmp)
5 ... TSTMP_CURRENT_UTCTIMESTAMP()
6 ... TSTMP_SECONDS_BETWEEN(tstmp1,tstmp2,on_error)
7 ... TSTMP_ADD_SECONDS(tstmp,seconds,on_error)

What does it do?
In a ABAP_CDS_V1_VIEW , these functions perform operations on time stamps.
With the exception of UTCL_CURRENT and TSTMP_CURRENT_UTCTIMESTAMP, these functions have positional parameters to which actual parameters must be assigned when called. There are currently no optional parameters. Suitable fields of a data source, literals, parameters, path expressions, built-in 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 except TSTMP_IS_VALID returns a null value.



Latest notes:

While the time stamp functions starting with UTCL process data that is stored as real time stamp on the database, the functions starting with TSTMP process time stamps on the database that are stored as a packed number in the ABAP-specific format in DDIC database tables. As in ABAP, other types of access to these fields interpret these time stamps as regular numeric values.
Conversion functions make it possible to transform a time stamp to date fields and time fields and to transform date fields and time fields to a time stamp.
NON_V5_HINTS
ABAP_HINT_END

ABAP_VARIANT_1 ... UTCL_CURRENT()

What does it do?
This function generates a UTC time stamp from the system time and the system date of AS ABAP in accordance with POSIX. The return value has the built-in dictionary type UTCLONG.

ABAP_VARIANT_2 ... UTCL_ADD_SECONDS(utcl,seconds)

What does it do?
The function UTCL_ADD_SECONDS adds seconds seconds to a time stamp utclong. It has two positional parameters. The actual parameter for the formal parameter utclong must have the built-in dictionary type UTCLONG and contain a valid UTC time stamp . An invalid time stamp produces an error. The actual parameter for the formal parameter seconds must have either the built-in data type INT4 or the data type DEC with length 21 and 7 decimal places. Any negative values are subtracted. If the result is invalid, an error occurs.
The result is of type UTCLONG.



Latest notes:

The actual parameter utcl can be any of the operands listed above in section Effect, including a literal. A literal can be specified in the following format: '0001-01-01 00:00:02.0000000'
If the positional parameter utclong is empty, it is set to its initial value.
The days missing from the Gregorian calendar (from 5.10.1582 to 15.10.1582) are handled as usual in ABAP.
NON_V5_HINTS
ABAP_HINT_END

ABAP_VARIANT_3 ... UTCL_SECONDS_BETWEEN(utcl1,utcl2)

What does it do?
The function UTCL_SECONDS_BETWEEN calculates the difference between two specified time stamps, utcl1 and utcl2, in seconds. It has two positional parameters. The actual parameters for the formal parameters utcl1 and utcl2 must have the built-in dictionary type UTCLONG and contain a valid UTC time stamp. Any invalid time stamps produce an error. If utcl2 is greater than utcl1, the result is positive. If the values are identical, the result is 0. In all other cases, the result is negative.
The result is of type DEC with length 21 and 7 decimal places.



Latest notes:

The actual parameter utcl can be any of the operands listed above in section Effect, including a literal. A literal can be specified in the following format: '0001-01-01 00:00:02.0000000'
If an actual parameter is empty, then this parameter is set to its initial value.
The days missing from the Gregorian calendar (from 5.10.1582 to 15.10.1582) are handled as usual in ABAP.
NON_V5_HINTS
ABAP_HINT_END

ABAP_VARIANT_4 ... TSTMP_IS_VALID(tstmp)

What does it do?
The function TSTMP_IS_VALID determines whether an argument tstmp contains a valid time stamp in the format YYYYMMDDHHMMSS. The actual parameter must have the built-in data type DEC with length 15 and no decimal places. The result has the data type INT4. A valid time stamp produces the value 1 and all other input values (including the null value) produce the value 0.



Latest notes:

The actual parameter tstmp cannot be specified as literal. This is only possible indirectly by using a cast expression. For example, this is valid syntax: tstmp_is_valid(cast(20200102334455 as abap.dec(15,0)) as demo
NON_V5_HINTS
ABAP_HINT_END

ABAP_VARIANT_5 ... TSTMP_CURRENT_UTCTIMESTAMP()

What does it do?
The function TSTMP_CURRENT_UTCTIMESTAMP returns a UTC time stamp in accordance with the POSIX standard. The result has the data type DEC with length 15 and no decimal places.



Latest notes:

The UTC time stamp is created from the clock on the database server. The system date and the system time in AS ABAP from which its time stamp is created using GET TIME STAMP are synchronized with the database server clock.
If the function TSTMP_CURRENT_UTCTIMESTAMP is used more thane once within a view, it cannot be guaranteed that the every call within a database access produces the same result. Hence, later calls can produce later time stamps.
NON_V5_HINTS
ABAP_HINT_END

ABAP_VARIANT_6 ... TSTMP_SECONDS_BETWEEN(tstmp1,tstmp2,on_error)

What does it do?
The function TSTMP_SECONDS_BETWEEN calculates the difference between two specified time stamps, tstmp1 and tstmp2 in seconds. The actual parameter must have the built-in data type DEC with length 15 and no decimal places and contain valid time stamps in the format YYYYMMDDHHMMSS. Any invalid time stamps produce an error. If tstmp2 is greater than tstmp1, the result is positive. In the reverse case, it is negative.
The actual parameter on_error controls error handling. It must have the built-in 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 values are case-sensitive. Any incorrectly specified values raise an exception.



Latest notes:

tstmp1 and tstmp2 cannot be specified as literals. This is only possible indirectly by using a cast expression. For example, this is valid syntax: tstmp_seconds_between(cast(20200102122345 as abap.dec(15,0)), cast(20200401161034 as abap.dec(15,0)), 'FAIL') as demo
NON_V5_HINTS
ABAP_HINT_END

ABAP_VARIANT_7 ... TSTMP_ADD_SECONDS(tstmp,seconds,on_error)

What does it do?
The function TSTMP_ADD_SECONDS adds seconds seconds to a time stamp tstmp. The actual parameter tstmp must have the built-in data type DEC with length 15 and no decimal places and contain a valid time stamp in the format YYYYMMDDHHMMSS. An invalid time stamp produces an error. The actual parameter seconds must also have the built-in data type DEC with length 15 and no decimal places. Any negative values are subtracted. If the result is invalid, an error occurs.
The actual parameter on_error controls error handling. The same applies as to the function TSTMP_SECONDS_BETWEEN. The additional value UNCHANGED can be used to specify that an error caused the unchanged value of tstmp to be returned.



Latest notes:

tstmp and seconds cannot be specified as literals. This is only possible indirectly by using cast expressions. For example, this is valid syntax: tstmp_add_seconds(cast(20200102122345 as abap.dec(15,0)), cast(5 as abap.dec(15,0)), 'FAIL') as demo
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
The following CDS view applies time stamp functions in the SELECT list to columns of the DDIC database table DEMO_EXPRESSIONS. The class CL_DEMO_CDS_TIMESTAMP_FNCTNS uses SELECT to access the view. A current UTCLONG time stamp is generated, seconds are added to a UTCLONG time stamp and the difference in seconds between two UTCLONG time stamps is calculated. The column NUM1 of the DDIC database table is given a value that is added to a time stamp in the column TIMESTAMP1 as seconds. The difference is found between this sum and a time stamp retrieved on the database by the function TSTMP_CURRENT_UTCTIMESTAMP. A delay, wait, can be integrated between the time stamp in the ABAP program and the time stamp created on the database.
DDLS DEMO_CDS_TIMESTAMP_FUNCTIONS
ABAP_EXAMPLE_END