Wednesday, September 19, 2018

Easy handling of time zones and time stamps

Sooner or later every developer has to work with time zones and time stamps. Luckily the SAP system provides plenty of tools to support you in this area. In this blog I want to highlight some of the tools I've used in the past to make work a little easier.

The local time within a time zone is defined by its offset (difference) from Coordinated Universal Time (UTC), the world's time standard. In SAP this piece of information can be stored in the user's master data. You can adjust your own time zone from the menu: System » User profile » Own data and then the tab Defaults (or transaction SU3/SU50).

Conversion exits

The following conversion exits come in handy:
  • TSTPS - Displays a time stamp in the UTC time zone
  • TSTLC - Displays a time stamp in the user's local time zone
Just attach these conversion exits to a domain to profit from advanced display handling. Instead of creating your own data elements you can also reuse LCA_TIMESTAMP (UTC time) or SALV_DE_TSTLC (local time). When these are used on a (selection-)screen, the attached conversion exits automatically render the values. Note that in the example below the internal values of both fields are equal in my situation where the local time zone is CET.

Transactions SE16 and SE16N also take into account possible conversion exits. For SE16, with default settings, the detail screen shows the converted values. By the way, the internal value is also rendered but according to its internal type, decimal, which is rather meaningless for time stamps.

The same applies to ALV. No additional effort is needed for proper display of time stamps, not even if you decide it's time to move to Hawaii...

Other tools

Another tool that might be useful is search help BGRFC_F4_TIMESTAMP. It provides a simple user interface for entering a time stamp. I have used this one in a report that needed a selection on time stamp.

Some more:
  • If you're a fan of functional methods you can use CL_ATRADT_TRACES_RES_TRACES=>GET_CURRENT_TIMESTAMP( )or CL_KF_HELPER=>CURRENT_TIMESTAMP_GET( ) as an alternative for the statement GET TIME STAMP
  • Class CL_ABAP_TSTMP provides methods to manipulate or compare time stamps. The strange abbreviated class name puzzles me and certainly makes it harder to find compared to e.g. CL_ABAP_TIMESTAMP which is still available in my system…

Daylight saving time (DST) – Summer time

UTC does not change with a change of seasons but local time may change if a time zone observes daylight saving time. Whether or not DST is active on a specific time in a particular time zone can be requested in the CONVERT TIME STAMP statement:

        INTO [DATE dat] 
             [TIME tim] [DAYLIGHT SAVING TIME dst]. 

As an example some time stamps around the moment summer time ends:

When converting a local time to a timestamp (using the CONVERT INTO TIME STAMP statement) the DST-indicator is an essential piece of information during the short period when you set the clock back one hour when DST ends in the fall.

Did you know?

And by the way, did you know that...

  • France currently holds the world record for the country with the most time zones (12!) because of its overseas territories, beating even the United States and Russia
  • China, roughly similar size to the continental United States, has one time zone
  • While most time zones differ from UTC by a number of full hours, there are also a few time zones with both 30-minute and 45-minute offsets like India (+5:30) and Nepal (+5:45)