1

I have to format a DateTime which is in UTC based on user settings which include its time zone (CET/IST etc) and time format (24hr/12hr).

I could find CONVERT TIME STAMP statement which takes only TIME ZONE as a parameter and does that conversion:

DATA: lv_timestampl TYPE timestampl VALUE '20200219095959.0000000',
      lv_date       TYPE d,
      lv_time       TYPE t.
CONVERT TIME STAMP lv_timestampl TIME ZONE sy-zonlo INTO DATE lv_date TIME lv_time.

My objective is to convert this lv_timestampl based on TimeZone and TimeFormat together.

PS: I could just do that -12 manipulation on lv_time after CONVERT TIME STAMP statement and append PM/AM but I am looking for a standard way of doing it.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • 1
    The terms "DateTime", "TimeZone", "TimeFormat" mean nothing in ABAP, can you explain what you are referring to? Please provide an example with an input and expected result. – Sandra Rossi Feb 19 '20 at 08:10
  • Yep, provide samples what you want to receive from what – Suncatcher Feb 19 '20 at 08:33

2 Answers2

1

OK, let me be your living help today, if you were not able to find string templates help.

Template to convert timestamp to UTC:

DATA(ld_tims_utc) = |{ lv_timestampl TIMESTAMP = ENVIRONMENT }|.

returns

19.02.2020 09:59:59,0000000

Template to convert timestamp to explicitly specified timezone:

DATA(ld_tims_zone) = |{ lv_timestampl TIMESTAMP = ENVIRONMENT TIMEZONE = 'CET' }|.

returns

19.02.2020 10:59:59,0000000

Getting time and date from timestamp (not timestampl, so conversion needed):

cl_abap_tstmp=>systemtstmp_utc2syst( EXPORTING
                                       utc_tstmp = CONV timestamp( lv_timestampl )
                                     IMPORTING
                                       syst_date = lv_date
                                       syst_time = lv_time ).

Output in 12h format:

SET COUNTRY 'US'.
DATA(time_us) = |{ lv_time TIME = ENVIRONMENT }|. "gives 01:55:43 PM

Output in 24h format:

SET COUNTRY 'DE'.
DATA(time_de) = |{ lv_time TIME = ENVIRONMENT }|. "gives 13:55:43

For the output of AM/PM time formats, they need to be maintained in field TIMEFM of table USR01 aka User settings or table t005x aka Country settings (tcode OY01).

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
  • A few others: your answer assumes that `timestamp` and `timestampl` contain values in UTC. The `TIMESTAMP` option doesn't convert to UTC (it's already UTC), it applies a pattern (ENVIRONMENT means that it depends on the user or country. Typo: `timestamlp` -> `timestampl`. `cl_abap_tstmp=>systemtstmp_utc2syst` returns the date and time in the ABAP server time zone. – Sandra Rossi Feb 19 '20 at 15:27
  • `The TIMESTAMP option doesn't convert to UTC ` in help they say *If TIMEZONE = tz is initial, the time zone is set implicitly to "UTC"* and **the content is formatted as a UTC time stamp** They same happens when tz is absent in table TTZZ or when TIMEZONE addition is not defined – Suncatcher Feb 20 '20 at 07:55
  • `cl_abap_tstmp=>systemtstmp_utc2syst returns the date and time in the ABAP server time zone` yes, and that's why I set country explicitly before showing time – Suncatcher Feb 20 '20 at 08:33
  • It's all about the ambiguous meaning of "convert to UTC". It means either a rendering in a given format like in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601), or adding or subtracting a number of hours based from a source time zone to UTC (and in ABAP, a "[time stamp](https://help.sap.com/doc/abapdocu_753_index_htm/7.53/en-US/index.htm?file=abentime_stamps.htm)" is mostly referred as a date/time value in UTC (GMT time zone). So, I'd prefer the term from SAP "formatted as a UTC" rather than "converted to UTC", and **adding an example of result to make it clear**. – Sandra Rossi Feb 20 '20 at 08:43
  • 1
    it's a matter of taste, the displayable time value is changed so I prefer using *convert*, format is smth about changing the representation without changing value. Anyway I gave a link to documentation, everybody is free to read the primary source – Suncatcher Feb 20 '20 at 09:03
  • That's exactly what I was looking for. Thanks! – Vaibhav Malik Feb 20 '20 at 10:30
0

FM HRVE_CONVERT_TIME can be used to convert in both ways (Convert 12hr-to 24h and 24hr to 12hr).

You can combine the results with the FM you already found.

mxstml
  • 95
  • 2
  • 7