Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
This blog belongs to series blog post Master handling Date/Time in E2E Fiori Scenario.

You can view the index page by click following page.

Master handling Date/Time in E2E Fiori Scenario

 

In this blog, we will summarise how ABAP deal with data of type date/time.

 

1.Datatypes for date/time


ABAP has 4 datatypes for date/time, as the following table

*The table has 9 columns, if you can not see them all, please select the table and drag it to the right using your mouse.

* Can somebody tell me how to make it visible?




























































Name ABAP Type DataElement Domain DDIC Predefined Type Internal Stroage Type Example Description Format
Date d  N/A  N/A DATS TYPE c Length 8 '20180121' Describe a Date by 'YYYYMMDD'
Time t  N/A  N/A TIMS TYPE c Length 6 '104355' Describe a time, always using 24 hours method.  'HHMMSS'
Short Timestamp N/A TEMPSTAMP  TZNTSTMPS DEC(15) TYPE p length 8 '20170121104355' Date + Time, represented precisely to the second.  'YYYYMMDDHHMMSS'
Long Timestamp N/A  TIMESTAMPL  TZNTSTMPL DEC(21,7) TYPE p length 11 decimal 7 '20170121104355.8765432' The resolution is 10E-7 second. 'YYYYMMDDHHMMSS.sssssss'



Note that:

  • For short timestamp, 14 digit( 8+6 ) is enough. But the minmum menmory space for pack number is 2 digit. So the data type itself can save 15 digits plus 1 digit for the sign ( not used for timestamp

  • I will explain details about timezone in another blog of this series


2.Get current date / time.


Following code example shows how to get current date time.
DATA(server_date) = sy-datum. "Get current ABAP server date and return a variable of type d

DATA(server_time) = sy-uzeit. "Get current ABAP server time and return a variable of type t

DATA(user_date) = sy-datlo. "Get date based on timezone configured in user master record and return a variable of type d

DATA(user_time) = sy-timlo "Get date based on timezone configured in user master record and return a variable of type t.

GET TIME STAMP FIELD DATA(timestamp). "Get UTC short timestamp and return a variable of type p length 8.

DATA tsl TYPE timestampl.
GET TIME STAMP FIELD tsl."Get UTC long timestamp and return a variable of type p length 11 desimal 7.

Things to keep in mind


When we are talking about I want current date/time, there are several possibilities

  • (1)Date/Time of ABAP application server

  • (2)Date/Time of Database server

  • (3)Date/Time of the computer SAPGUI is installed

  • (4)Date/Time based on timezone settings in user master record

  • (4)UTC Date/Time


ABAP server always synchronize its time to database server, so generally, we can assume (1) = (2),

When you are creating Date/Time fields, you need a timezone. Which timezone is used for the variable is determined by your program.

Timestamp field always stores UTC time.

User local time means the time set in user master record. It can be set in SU01 by the administrator or in SU3 by the user itself. If no user local timezone is set, default user local timezone in table TTZCU is used. Generally, it is more meaningful than date/time in user's computer because the latter is easy to be affected by client environment.

For now, I haven't found a convenient way of getting time from user's computer. Do you have a good idea?

The current date/time value is evaluated at some point in a program like when you are starting a program, a screen is showing. So, if you want to get exact time in a long-run ABAP code. You need to run GET TIME to refresh all system fields about date/time.

 

3.Process Date/Time


3.1 Process Date/Time type as a string


Date and time can be converted to a char variable and vice versa.

So generally all string operation can be used for those types. But normally you should only use string operation for getting parts you need from date/time variables.

Here are some examples got from ABAP Keyword Documents
date = sy-datlo. 
time = sy-timlo.

year = substring( val = date off = 0 len = 4 ).
month = substring( val = date off = 4 len = 2 ).
day = substring( val = date off = 6 len = 2 ).
hour = substring( val = time off = 0 len = 2 ).
minute = substring( val = time off = 2 len = 2 ).
second = substring( val = time off = 4 len = 2 ).

3.2 Process Date/Time as an integer


For math calculation Date/Time convert to an integer.

The table below summarized meaningful options for date/time calculation


































Type of Oprand1 Type of Oprand2 Meaningful Operation Meaning
d d - Calculate days between two dates
d t - Calculate seconds between two times
d i +/- Add / subtract days to a date variable
t i +/- Add / subtract seconds to a time variable


*This series blogs won't cover factory calendar and advanced calculate requirements based on business scenarios

In addition, if you want to calculate seconds between two date/time, the best way is converting them to timestamps before calculating. We will discuss the conversion later.

3.3 Process timestamp


Although timestamps are stored as packed numbers, you should not use them in such way.

For timestamp calculation, you need to calculate them using class cl_abap_tstmp.

Reference the class in SE24 for further information.

 

3.4 Process Date/Time in Core Data Service or Open SQL


The table below show all possible SQL functions for date/time type














































SQL Function Result ABAP CDS Open SQL
DATS_DAYS_BETWEEN( date1, date2 ) Difference between two dates. x x
DATS_ADD_DAYS( date, days , on_error ) Total of days and a date. x x
DATS_ADD_MONTHS( date, months, on_error ) Total of months and a date. x x
TSTMP_CURRENT_UTCTIMESTAMP( ) Current UTC time stamp. x -
TSTMP_SECONDS_BETWEEN( tstmp1, tstmp2, on_error ) Difference between two time stamps in seconds. x -
TSTMP_ADD_SECONDS( tstmp, seconds, on_error ) Total of seconds and a time stamp. x -


You need to mention:



  • Only few function can be used in OpenSQL, most of Date/Time SQL function can only work in CDS

  • Use calculated fields as filter or group condition will have a very bad impact on performance, especially for HANA Database.( You can think HANA store data in a zip file when you search data, HANA can find them in the zip file and extract files you need. But if you filter by a calculation field , it needs to extract all data for calculation first. )

  • #TODO, add a blog in this series of best practices for data calculation in CDS.( Data Function and Date/Time Dimension )


 

4.Conversion


You can convert a date/time combination to a timestamp and vice versa.

4.1 Convert a date/time combination to timestamp


As I mentioned, a date/time combination is only meaningful if there are a timezone and timestamp always save time as UTC.

To convert a date/time combination to a timestamp, you must provide a timezone. Then the runtime will help you to convert it to UTC.

  • I will explain details about timezone in another blog of this series


Here is a sample:
DATA: dat TYPE d, "date
tim TYPE t, "time
tz TYPE ttzz-tzone. "timezone


"To get timezone settings in user master record, using sy-zonlo
"To get system timezone using following OPEN SQL
"SELECT SINGLE tzonesys
" FROM ttzcu
" INTO @DATA(tz).
tz = 'EST'.
dat = '20161030'.
tim = '013000'.

CONVERT DATE dat TIME tim
INTO TIME STAMP DATA(time_stamp) TIME ZONE tz.



4.2 Convert timestamp to date/time combination


A timestamp always using UTC, so when converting to date/time variables, you need to tell ABAP which timezone the variables belongs you want.
CONVERT TIME STAMP time_stamp TIME ZONE tz 
INTO DATE DATA(dat) TIME DATA(tim)

4.3 Conversion in OpenSQL and Core Data Service


In OPEN SQL, there is no way to convert date/time to a timestamp

For timestamp to data/time, you can convert a timestamp to a char first, then get digits you need and convert it to date/time.

For CDS view, following function can help you.








































SQL Function Result ABAP CDS Open SQL
TSTMP_TO_DATS( tstmp, tzone, clnt, on_error ) Local date of a time stamp. x -
TSTMP_TO_TIMS( tstmp, tzone, clnt, on_error ) Local time of a time stamp. x -
DATS_TIMS_TO_TSTMP( date, time, tzone, clnt, on_error ) Time stamp for a local date and a local time. x -
ABAP_SYSTEM_TIMEZONE( clnt, on_error ) System time zone of AS ABAP. x -
ABAP_USER_TIMEZONE( user, clnt, on_error ) User time zone of AS ABAP. x -


5.Output


Output of date/time/timestamp means

  • Output those variables to screen

  • Convert them into a formatted char/string


Now, the best way to format the output is using string templates.

If you have no idea about this, please reference those two amazing blogs.

https://blogs.sap.com/2017/03/25/string-template-in-abap-es6-angular-and-react/

This blog is a great blog introduce you the string template.

 

https://blogs.sap.com/2017/03/25/string-template-in-abap-es6-angular-and-react/

This blog comes from Jerry. He is also a member of SAP Mentor. In this blog, he compared string templates across ABAP, ES6 and React.

There are useful format options for date/time.

  • Format option DATE is for date type

  • Format option TIME is for time type

  • Format option TIMESTAMP is for timestamp type

  • Format option TIMEZONE is used along with TIMESTAMP to convert timestamp to a specific timezone

  • Format COUNTRY is used along with option ENVIRONMENT to display as format set by developer


Here is an example to demonstrate usage of format options.
*&---------------------------------------------------------------------*
*& Report zdtformatdemo
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zdtformatdemo.
data:my_date type d,
my_time type t,
my_timestamp type timestamp,
my_long_timestamp type timestampl,
my_tzone type ttzz-TZONE,
my_country type t005x-land.


"SYSTEM TIMEZONE = CET
"USER TIMEZONE = CET
my_date = sy-datum.
my_time = sy-uzeit.
get time stamp field my_timestamp.
get time stamp field my_long_timestamp.
my_tzone = 'UTC+8'."Query table TTZZ to find avaliable time zones
my_country = 'US'."Query table t005x to find avaliable countries

"This is the plain output for all fields
write: / |{ my_date }|. "20180122
write: / |{ my_time }|."092339
write: / |{ my_timestamp }|."20180122082339
write: / |{ my_long_timestamp }|."20180122082339.6490410

"Out put as ISO formatts
"This is the best way to get stable output format regardless any enviroment considerations
write: / |{ my_date DATE = ISO }|."2018-01-22
write: / |{ my_time TIME = ISO }|."09:23:39
write: / |{ my_timestamp TIMESTAMP = ISO }|."2018-01-22T08:23:39 Mention that there is a T between date and time part
write: / |{ my_long_timestamp TIMESTAMP = ISO }|."2018-01-22T08:23:39,6490410 Mention that use ',' to separate integer and decimal part


"Output according user settings in SU3
write: / |{ my_date DATE = USER }|."22.01.2018
write: / |{ my_time TIME = USER }|."09:23:39
write: / |{ my_timestamp TIMESTAMP = USER }|."22.01.2018 08:23:39 Mention that there is a space between date and time part
write: / |{ my_long_timestamp TIMESTAMP = USER }|."22.01.2018 08:23:39,6490410


*You can get contry specific format by using set country and use format option ENVIRONMENT
SET COUNTRY my_country.
write: / |{ my_date DATE = ENVIRONMENT }|."01/22/2018
write: / |{ my_time TIME = ENVIRONMENT }|."09:23:39
write: / |{ my_timestamp TIMESTAMP = ENVIRONMENT }|."01/22/2018 08:23:39
write: / |{ my_long_timestamp TIMESTAMP = ENVIRONMENT }|."01/22/2018 08:23:39.6490410
*Don't forget to set it back. Or it will affact following code
SET COUNTRY ' '.

*In 7.52, you can add country into formation options to get country specific output temprorily. It avoid the side effect of forgetting set it back.
*I tested it in 7.51 but not work, but this useage is described in 7.52's document.
*write: / |{ my_date DATE = ENVIRONMENT COUNTRY = my_country }|.
*write: / |{ my_time TIME = ENVIRONMENT COUNTRY = my_country }|.
*write: / |{ my_timestamp TIMESTAMP = ENVIRONMENT COUNTRY = my_country }|.
*write: / |{ my_long_timestamp TIMESTAMP = ENVIRONMENT COUNTRY = my_country }|.

*Output as timezone as developer set. Only work for timestamp.
"Mention the time is changed.
write: / |{ my_timestamp TIMESTAMP = ISO TIMEZONE = my_tzone }|."2018-01-22T16:23:39
write: / |{ my_long_timestamp TIMESTAMP = ISO TIMEZONE = my_tzone }|."2018-01-22T16:23:39,6490410

 

6.Reference to the document


Now I think you've got a lot about ABAP and date/time.

If you want to dig it more and keep track on new features, you also need to look up ABAP Keyword Document.

Here are some useful links to ABAP Keyword Document for topics related to this blog.

The like to ABAP Document is

https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/index.htm

( I believe you can change 752 in this URL to other versions you want, it may be work ....  )

 

Date / Time Processing: ABAP - Keyword Documentation →  ABAP - Reference →  Processing Internal Data → Date and Time Processing

 

Deal with date/time in SQL / Core Data Service: ABAP - Dictionary →  Built-In Functions in ABAP Dictionary →  Special Functions   →   Date Functions and Time Functions

 

Format Options for Date and Time  ABAP - Reference →  Processing Internal Data →  Character String and Byte String Processing →  Expressions and Functions for String Processing →  string_exp - String Expressions →  string_exp - String Templates →  String Templates - embedded_expressions → Embedded Expressions - format_options

 

 

7.Next Step


In my next blog, I will show you what will happen when publishing ABAP date/time to an OData service!

See you next week!

 
Labels in this area