ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Date and Time Processing → Time Stamps
Time Stamps in Packed Numbers
In this format, time stamps
are represented using packed numbers of the type p
.
Other versions:
7.31 | 7.40 | 7.54
Representation of Time Stamps in Packed Numbers
There is a short form and a long form.
- In the short form, a time stamp is represented precisely to the second using a packed number with length 8, and the ABAP Dictionary type TIMESTAMP. The digits of the packed number show the time stamp in the format "yyyymmddhhmmss", where "yyyy" is the year, "mm" is the month, "dd" is the day, "hh" is the hour, "mm" are the minutes, and "ss" are the seconds.
- In the long form, a time stamp is represented precisely to 100 ns using a packed number with length 11 and seven decimal places, and the ABAP Dictionary type TIMESTAMPL The digits of the packed number show the time stamp in the format "yyyymmddhhmmss.sssssss", where, in addition to the short form, the seven decimal places "sssssss" are the fractions of a second. The maximum time resolution depends on the operating system of the host computer of the current AS Instance and can be less.
In its integer part, a valid time stamp must contain valid dates and times:
- When specifying the date, only the values 01 to 9999 for the year, 01 to 12 for the month, and 01 to 31 for the day are valid.
- When specifying the time, only the values 00 to 23 for the hours, and 00 to 59 for the minutes and seconds are valid.
A time valid in the Gregorian calendar must be represented. Leap seconds are not supported.
Notes
- An initial packed number with the value 0 does not contain a valid time stamp.
- The method NORMALIZE of class CL_ABAP_TSTMP can be used to convert invalid values in time stamps to valid values.
Accessing Time Stamps in Packed Numbers
Only a few statements recognize that packed numbers of the types TIMESTAMP and TIMESTAMPL are time stamps. All other statements interpret the content of these data types numerically and, with the exception of direct comparisons, are not suitable for handling time stamps. The statements for handling time stamps in packed numbers are as follows:
GET TIME STAMP
creates a current time stamp.
CONVERT TIME STAMP
converts a time stamp to a local date and a local time.
CONVERT INTO TIME STAMP
converts a local date and a local time to a time stamp.
Furthermore, time stamps in packed numbers are handled in specified ways in the following output formats:
- Use of the options
TIMESTAMP and
TIMEZONE
for embedded expressions in string templates
- Use of the addition
TIME ZONE
of the statementWRITE [TO]
.
- The domains XSDDATETIME_Z and XSDDATETIME_LONG_Z support the serialization and deserialization of ABAP time stamps in asXML and asJSON.
The system class CL_ABAP_TSTMP provides methods for adding, subtracting, converting, and comparing time stamps in packed numbers.
Note
Special time stamp functions can be used in and the CDS DDL of the ABAP CDS for editing time stamps saved as packed numbers in database tables.
Notes on Handling Time Stamps in Packed Numbers
Time stamps in packed numbers are only interpreted as such when they are accessed by the statements,
methods, and functions listed above. When being assigned or converted, they behave like packed numbers
of the type p
, which means they are not suitable for direct calculations.
Comparisons produce a meaningful result only when two time stamps are compared with each other. In programs for which the program attribute
Fixed Point Arithmetic
is not set, note the rules applying to the data type p
. In particular, direct
comparisons of time stamps in the long form with the short form produce a meaningful result only when
the program attribute fixed point arithmetic is set. Otherwise, the system class CL_ABAP_TSTMP must be used for comparisons as well.
A time stamp in its short form is the integer part of a time stamp in its long form. When assigning
time stamps in the long form to time stamps in the short form, unwanted rounding effects occur. For this reason, always use the method MOVE
of the system class
CL_ABAP_TSTMP.
When time stamps in packed numbers are used in operand positions where they are not supposed to be used, no warnings are given by the syntax check or by the extended program check (not even in lossless assignments).
Examples for Time Stamps in Packed Numbers
Example
Directly compares time stamps in packed numbers with the same data type.
GET TIME STAMP FIELD DATA(ts2).
WAIT UP TO 1 SECONDS.
GET TIME STAMP FIELD DATA(ts1).
ASSERT ts2 < ts1.
Example
Converts a time stamp in a packed number to a date and a time field, and determines the summer time marker.
GET TIME STAMP FIELD DATA(ts).
CONVERT TIME STAMP ts TIME ZONE sy-zonlo
INTO DATE DATA(date) TIME DATA(time)
DAYLIGHT SAVING TIME DATA(dst).
cl_demo_output=>display( |{ date }\n{
time }\n{
dst } | ).
Example
Formats a time stamp in a packed number in a string template in a type-friendly way.
GET TIME STAMP FIELD DATA(ts).
cl_demo_output=>display( |{ ts TIMESTAMP = ISO } | ).
Example
Serializes a time stamp in a packed number by using a special domain.
DATA ts TYPE xsddatetime_z.
GET TIME STAMP FIELD ts.
CALL TRANSFORMATION id SOURCE ts = ts
RESULT XML DATA(xml).
cl_demo_output=>display_xml( xml ).
Example
Calculates the difference between two time stamps in packed numbers using the class CL_ABAP_TSTMP.
DATA: ts1 TYPE timestampl,
ts2 TYPE timestampl.
GET TIME STAMP FIELD ts2.
WAIT UP TO 1 SECONDS.
GET TIME STAMP FIELD ts1.
DATA(seconds) = cl_abap_tstmp=>subtract(
EXPORTING
tstmp1 = ts1
tstmp2 = ts2 ).
cl_demo_output=>display( seconds ).
Example
Calculates directly using time stamps in packed numbers. If, for example, ts1
has the value 20161004130733, adding 3600 s in ts2
produces the value 20161004140733. Since the time stamps are interpreted
as numbers of type p
in the calculation, the result is 10000, which would generally be unexpected.
GET TIME STAMP FIELD DATA(ts1).
DATA(ts2) = cl_abap_tstmp=>add( tstmp = ts1
secs = 3600 ).
cl_demo_output=>display( ts2 - ts1 ).
Example
Calculates incorrectly using time stamps in packed numbers. The assumption here is that time stamps are interpreted as a number of seconds in calculations. This is not the case here. The result does not meet expectations and is generally not a valid time stamp. For example, if 20161004131906 is calculated, this produces the invalid value 20161004315506.
GET TIME STAMP FIELD DATA(ts).
ts = ts + 86400 * 2 + 3600 * 3.
cl_demo_output=>display( ts ).