This website is not affiliated with, sponsored by, or approved by SAP AG.

0029 - Manipulating Timestamps

Moderators: Snowy, thx4allthefish, Rich, ilya

0029 - Manipulating Timestamps

Postby Rich » Sat Mar 28, 2009 8:40 am

Manipulating Timestamps.

Actually, we should use time stamp in our abap programs instead of the traditional date and time fields. When we have to do some calculation on a time stamp is not as easy as on a date field.

For example, to add an hour to a timestamp, many people will:

    convert the time stamp into a standard date and field.
    add an hour.
    convert back the date and time to a timestamp.


However, it is the slowest method you can use. Instead, use class CL_ABAP_TSTMP. It enables you to make whatever calculation you want on a time stamp.

In the following example, 1 hour is added


Code: Select all
REPORT zdany_tstamp.
DATA : l_tstamp         TYPE timestamp,
       l_tstamp_out TYPE timestamp.

GET TIME STAMP FIELD l_tstamp.
TRY.
  CALL METHOD cl_abap_tstmp=>add
     EXPORTING
        tstmp = l_tstamp
        secs = 3600 <<<===--- 1 hour = 3600 seconds
     RECEIVING
                r_tstmp = l_tstamp_out.
     CATCH cx_parameter_invalid_range .
                WRITE 'invalid range'.
                EXIT.
     CATCH cx_parameter_invalid_type .
               WRITE 'invalid type'.
               EXIT.
ENDTRY.

WRITE l_tstamp time zone 'UTC '.
SKIP.
WRITE l_tstamp_out time zone 'UTC '.


(Courtesy Dany Charbonneau)
Regards

Rich

Image
Abap KC:http://www.richard-harper.me.uk/Kb
SFMDR:http://www.se37.com
Rich
 
Posts: 7112
Joined: Thu Oct 31, 2002 4:47 pm
Location: Liverpool

Return to ABAPers

Who is online

Users browsing this forum: No registered users and 1 guest





loading...


This website is not affiliated with, sponsored by, or approved by SAP AG.