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: 

Converting timestamp from local time to UTC

Former Member
0 Kudos

Is there a smooth way to convert a timestamp containing local time to UTC time?

The statement:

CONVERT TIME STAMP time_stamp TIME ZONE tz

INTO [DATE dat] [TIME tim]

[DAYLIGHT SAVING TIME dst].

Consideres time_tamp to be UTC time and then tries to convert it to the time zone specified in tz. What I would like to have done is the opposite, to have time_stamp represent the local time and then convert it back to UTC.

TIA!

/Armin

1 ACCEPTED SOLUTION

Former Member

Hi Armin.

Just turn your statement and you will be fine

DATA: date TYPE sydatum VALUE '20070525',
      time TYPE syuzeit VALUE '173030',
      cet  TYPE tzonref-tzone VALUE 'CET',
      utc  TYPE tzonref-tzone VALUE 'UTC',
      tstp TYPE timestamp.

CONVERT DATE date TIME time INTO TIME STAMP tstp TIME ZONE cet .

The output will be 15:30:30 at 25th of May 2007.

I used CET as example, just use the timezone your date is representing. The result is formatted as UTC.

Actually if you only have the timestamp, for this solution you have to convert it into date/time first.

Use


CONVERT TIME STAMP tstp TIME ZONE utc INTO DATE date TIME time.

to do so.

Regards,

Timo.

8 REPLIES 8

Former Member
0 Kudos
Say you have time stamp for local time, split that to date and time

DATA:
  tstamp  TYPE timestamp value '20070525183545',
  d       TYPE D,
  t       TYPE T,
  tstamp_UTC type timestamp.

d =  tstamp+0(8).
t = tstamp+8(8).

CONVERT DATE d TIME t INTO
        TIME STAMP tstamp_UTC TIME ZONE 'UTC'.

write : / tstamp_UTC.

Former Member
0 Kudos

Sorry, but it doesn't work.

It just converts from UTC to UTC, ergo nothing changes, the timestamp remains the same!

Also

d = tstamp+0(8).

t = tstamp+8(8).

is not possible since you can't reference timestamp like that!

Former Member

Hi Armin.

Just turn your statement and you will be fine

DATA: date TYPE sydatum VALUE '20070525',
      time TYPE syuzeit VALUE '173030',
      cet  TYPE tzonref-tzone VALUE 'CET',
      utc  TYPE tzonref-tzone VALUE 'UTC',
      tstp TYPE timestamp.

CONVERT DATE date TIME time INTO TIME STAMP tstp TIME ZONE cet .

The output will be 15:30:30 at 25th of May 2007.

I used CET as example, just use the timezone your date is representing. The result is formatted as UTC.

Actually if you only have the timestamp, for this solution you have to convert it into date/time first.

Use


CONVERT TIME STAMP tstp TIME ZONE utc INTO DATE date TIME time.

to do so.

Regards,

Timo.

0 Kudos

Hi Timo, thank you very much for your valuable post!

As far as I have understood, if one has only a timestamp, we have to generate Date und Time first (with the last stated CONVERT instruction) and then convert again with the first stated CONVERT instruction. Is this right?

When converting for the second time, does ABAP interpret Date and Time that way that it already has been converted to UTC before? What I mean is: Don't we convert twice with UTC, so that the 2nd conversion considers a local time?

0 Kudos
Thomas Mundt I doubt that "Timo"/"Former member" would answer after 12 years. Please ask a Question instead. Based on your question, you might receive more adequate answers, i.e. for instance based on the class CL_ABAP_TSTMP.

Former Member
0 Kudos

Thank you Time for the helpful answer!

Former Member
0 Kudos

Timo, sorry

Blokmeister
Discoverer
0 Kudos

You have to go through a few conversions, but eventually it works.

* Target: Get latest timestamp from ADSO and give it to variable. But due summertime issues the time gets two hours extra when opening the query. The format in BW is 14 NUMC and cannot easily be converted. So I firstly retrieved the timestamp, than converted into a timestamp added with a timezone. Then converted UTC to CET and than back into the output of the variable. Its a bit a mess, but it works.

IF i_step = 1.
DATA: max_tstamp TYPE /bi0/oitctlstload,
time_stamp TYPE timestamp,
cet TYPE tzonref-tzone VALUE 'CET',
utc TYPE tzonref-tzone VALUE 'UTC',
tz TYPE ttzz-tzone,
date TYPE sydatum,
time TYPE syuzeit.

SELECT SINGLE MAX( tctlstload ) FROM /bic/azldatmon2 INTO max_tstamp.

tz = 'CET'.
time_stamp = max_tstamp.

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

CONVERT TIME STAMP time_stamp TIME ZONE utc INTO DATE date TIME time.
CONVERT DATE date TIME time INTO TIME STAMP time_stamp TIME ZONE cet.

max_tstamp = time_stamp.

l_s_range-sign = 'I'.
l_s_range-opt = 'EQ'.
l_s_range-low = max_tstamp.
APPEND l_s_range TO e_t_range.
ENDIF.