Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Convert longhand UTC timestamp to longhand LOCAL timestamp

luis_rod
Participant
0 Kudos

Hi all,

We have a Z table where we write UTC timestamps (e.g.20230105130756.0372170).

How can we get the local timezone value of this time stamp (e.g.20230105090756.0372170 for UTC-4)?


TIA,

Luis

1 ACCEPTED SOLUTION

Sandra_Rossi
Active Contributor
0 Kudos

Considering that you want to subtract 4 hours to the UTC timestamp, a workaround would be to subtract 14400 seconds with cl_abap_tstmp=>subtractsecs:

DATA(tstmp) = CONV TZNTSTMPL( '20230105130756.0372170' ).

data(hours) = 4.
data(secs) = hours * 3600.
tstmp = cl_abap_tstmp=>subtractsecs( EXPORTING tstmp = tstmp secs = secs ).

ASSERT tstmp = '20230105090756.0372170'.

As you say you want it to display the time to the user via SE16, a better solution would be to adapt the conversion exit TSTLC (display UTC time in user's time zone), so that it also displays the microseconds.

4 REPLIES 4

matt
Active Contributor
0 Kudos

Use

CONVERT TIME STAMP time_stamp TIME ZONE tz
INTO [DATE dat]

[TIME tim]

Followed by

CONVERT DATE dat
[TIME tim ]
INTO TIME STAMP time_stamp TIME ZONE tz.

However. In practice it's best to have ALL timestamps in UTC and only change them into the local time zone when display is needed.

0 Kudos

Matthew:

Thanks for your post. The problem with that approach is that we would lose the "decimal" part of the timestamp. Many of our records have the same date and time, and we need the full timestamp info to be able to set the correct order for them.

Agree about the UTC comment but our QA people, when querying the data with SE16N, would prefer to have it in local time (for the record, it is a constant UTC-4 for us, so there is no worry about DST times).


Thanks again,

Luis

Sandra_Rossi
Active Contributor
0 Kudos

Considering that you want to subtract 4 hours to the UTC timestamp, a workaround would be to subtract 14400 seconds with cl_abap_tstmp=>subtractsecs:

DATA(tstmp) = CONV TZNTSTMPL( '20230105130756.0372170' ).

data(hours) = 4.
data(secs) = hours * 3600.
tstmp = cl_abap_tstmp=>subtractsecs( EXPORTING tstmp = tstmp secs = secs ).

ASSERT tstmp = '20230105090756.0372170'.

As you say you want it to display the time to the user via SE16, a better solution would be to adapt the conversion exit TSTLC (display UTC time in user's time zone), so that it also displays the microseconds.

Sandra.

The conversion exit is exactly what we need!!


Thanks,

Luis