Skip to Content
Author's profile photo Horst Keller

About Timestamps

A timestamp is a sequence of characters or encoded information identifying when a certain event occurred, usually giving date and time of day, sometimes accurate to a small fraction of a second.

(Wikipedia, August 29, 2016).

In ABAP you get a timestamp accurate to a second with the statement

GET TIME STAMP FIELD DATA(ts).


cl_demo_output=>display( ts ).


Here ts has the dictionary type TIMESTAMP and the result might look like 20160829131515.


And a timestamp accurate to a small fraction of a second with:


DATA ts TYPE timestampl.

GET TIME STAMP FIELD ts.


cl_demo_output=>display( ts ).

The result might look like 20160829131612.294638.

Those are are POSIX timestamps that are independent of a time zone.

The format of such  ABAP timestamps is YYYYMMDDHHMMSS.fffffff with 7 fractions of a second in case of type TIMESTAMPL.

As a rule, you use such timestamps to mark data with – well – timestamps (time of creation, time of update, …).

In order to handle timestamps, you can do the following:

  • You can directly compare different timestamps of the same type:

    GET TIME STAMP FIELD DATA(ts2).
    WAIT UP TO 1 SECONDS.
    GET TIME STAMP FIELD DATA(ts1).
    ASSERT ts2 < ts1.

  • You can convert timestamps into date and time fields of a time zone:

    GET TIME STAMP FIELD DATA(ts).

    CONVERT TIME STAMP ts TIME ZONE sy-zonlo

            INTO DATE DATA(date) TIME DATA(time)

            DAYLIGHT SAVING TIME DATA(dst).

     cl_demo_output=>display( |{ date }\n{

                                 time }\n{

                                 dst } | ).

         Giving something like 20160829, 172223, X


  • You can format timestamps in string processing:

    GET TIME STAMP FIELD DATA(ts).

    cl_demo_output=>display( |{ ts TIMESTAMP = ISO } | ).

    Giving something like 2016‑08‑29T15:27:29

  • You can serialize/deserialize timestamps, if their datatype refers to a special domain:

    DATA ts TYPE xsddatetime_z.
    GET TIME STAMP FIELD ts.

    CALL TRANSFORMATION id SOURCE ts = ts
                   RESULT XML DATA(xml).
    cl_demo_output=>display_xml( xml ).

    Giving something like: <TS>2016‑08‑29T15:33:50Z</TS>

  • You can do some simple calculations with the methods of class CL_ABAP_TSTMP:

    DATA: ts1 TYPE timestampl,

          ts2 TYPE timestampl.

    GET TIME STAMP FIELD ts2.

    WAIT UP TO 1 SECONDS.

    GET TIME STAMP FIELD ts1.

    DATA(seconds) = cl_abap_tstmp=>subtract(

        EXPORTING

          tstmp1 = ts1

          tstmp2 = ts2 ).

    cl_demo_output=>display( seconds ).

     Giving something like 1.001369.

And that is it. Timestamps are not foreseen for more and you cannot do more! Especially, you should never do direct calculations with timestamps itself:

GET TIME STAMP FIELD DATA(ts1).

DATA(ts2) = cl_abap_tstmp=>add(

                tstmp = ts1

                secs  = 3600 ).

cl_demo_output=>display( ts2 ts1 ).


The result is 10000. How that?

Well, you should know it. Timestamps don’t have an own built-in ABAP type. In another ABAP world, in NGAP, that is Release 8.x, in fact they have and we wouldn’t have to bother. But in the 7.02/7.30-7.40-7.50 Release line, timestamps are stored in type p numbers:

  • p length 8 without decimal places for dictionary type TIMESTAMP
  • p length 11 with seven decimal places for dictionary type TIMESTAMPL.

This is different to data fields of type d and time fields of type t. Those have a special meaning and are treated specially at different operand positions (either as character strings or as numbers that denote days or seconds).

With exception of the statements and methods listed above, ABAP does not recognize the semantical meaning of a timestamp. It simply treats it as a packed number of the given value. With other words, if ts1 in the last example is 20160829160257, adding 3600 seconds using the method ADD gives 20160829170257 in ts2. You see the difference? One hour later (17 compared to 16) when interpreted in the timestamp format, but a difference of 100000 when interpreted as the normal value format for type p.

Recently, I’ve also seen something as follows (and that’s the reason for this blog):

GET TIME STAMP FIELD DATA(ts).

ts = ts + 86400 * 2 + 3600 * 3.

Someone believed that ABAP timestamps are handled like a number of seconds and wanted to add 2 days and 3 hours. No, no, no. If ts again is 20160829160257, you simply add 86400 * 2 + 3600 * 3 to that number and you get 20160832943857. That is even not a valid timestamp!

Using type p for timestamps instead of a character type is simply a convenient and efficient way of storing timestamps with decimal places.But never, never, never tend to believe that you can do something meaningful with the type p number directly!


Assigned Tags

      16 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Isuru Samarasinha
      Isuru Samarasinha

      Hi Horst,

      What are your thoughts on getting the mean of a set of time stamps? Any method that you've come across that would help in finding the mean time from a set of time's.

      eg;

      • 08:00
      • 12:00
      • 20:00

      The above's mean would be 15:00 if I'm not mistaken.

      Author's profile photo Sandra Rossi
      Sandra Rossi

      8+12+20 / 3 = 13.3333 hours i.e. 13 hours + 60 minutes * 0.3333 = 20 minutes

      So, the result is 13:20

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      Hi,

      Seems that you are mixing up timestamps and time fields.

      • 08:00
      • 12:00
      • 20:00

      are times. In ABAP, times are contents of time fields of type t. In contrast to timestamps of type p those are recognized by ABAP and treated as a numbers of seconds in calculations.

      DATA(avg) =

        CONV t( ( CONV t( '080000' ) +  CONV t( '120000' ) + CONV t( '200000' ) ) / 3 ).


      or


      DATA t1 TYPE t VALUE '080000'.

      DATA t2 TYPE t VALUE '120000'.

      DATA t3 TYPE t VALUE '200000'.

      DATA avg TYPE t.

      avg = ( t1 + t2 + t3 ) / 3 .



      Giving, tataah, 132000 (same as Sandra's calculation)...


      Horst

      Author's profile photo Peter Inotai
      Peter Inotai

      Hi Horst,

      You mentioned:

      "In another ABAP world, in NGAP, that is Release 8.x, in fact they have and we wouldn't have to bother."

      Is there any plan to make release (or provide any kind of access) 8.x/ABAP NGAP to non-SAP-internal people?

      Thanks,

      Peter

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      Unfortunately, the few things (new date/time data types and with them typed literals) that were not backported from 8.0 to 7.x would require some incompatible changes/restrictions that we could carry out for NGAP but that will never be possible in 7.x.  The majority of ABAP programmers does not like touch existing code in order to adjust. We did this once with the Unicode enabling and that was a major effort and customer driven. But where are the major customers that would drive an ABAP cleanup that involves incompatible changes (with all its consequences) for the sake of happy developers?

      Author's profile photo Peter Inotai
      Peter Inotai

      Thanks for the info. Unfortunately such change could have pretty high effort 🙁

      However a cloud version of pure-ABAP NGAP would be also nice, just to be able to play around  😉

      Peter

      Author's profile photo Former Member
      Former Member

      There is one kind of operation I usually do with direct timestamp arithmetics: stripping seconds/minutes/hours.

      ts1 = ts1 - ts1 mod 100. " removes seconds -> whole minutes

      ts1 = ts1 - ts1 mod 10000. " removes seconds and minutes -> whole hours

      ts1 = ts1 - ts1 mod 1000000. " removes seconds, minutes and hours -> whole days

      The latter could be done by converting to a date and converting back to timestamp, the first two would require converting, manupulation of time and converting back.

      The above is valid for TIMESTAMP. Long timestamps (TIMESTAMPL) have to be truncated first.

      Any better way to do this in NW7x w/o using MOD operation?

      Author's profile photo Jacques Nomssi Nzali
      Jacques Nomssi Nzali

      Hello Horst,

      so what if I were to add a TIMESTAMP column to a database table and expected to get the oldest row using a ORDER BY timestamp column and UP to 1 ROWS?

      I tried, they are no errors, but the result seems wrong.

      SELECT key_field FROM my_table INTO @DATA(key) UP TO 1 ROWS
      ORDER BY timespamp.  " oldest entry first
      ENDSELECT.

      Do I understand correctly that it cannot work?

      JNN

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      For the database it is simply a DEC field. It should work.

      Horst

      Author's profile photo REDDIMADHUKARA BATHULA
      REDDIMADHUKARA BATHULA

      Hi Horst,

      how to get user local Timezone in sap ABAP(Laptop logged in Time Zone: means user can login different places like EX:EST, PST, IST..).

       

      Thanks,

      Madhu Reddy

       

       

      Author's profile photo Daniel Divjakovic
      Daniel Divjakovic

      Guten Tag Horst 🙂

      these days I am occupied with importing different .csv files in SAP HANA, because I want to try different Machine Learning (EML Library) algorithms on this data. Doing that, namely, I have found that one of my .csv files contains date time format (YYYYMMDD HHMMSS), which (in terms of format) coincide with corresponding element in SAP HANA table (which I have defined like TIMESTAMP format).

      Importing that .csv file, at first moment, was unsuccessful. After a small research, I had success and imported that .csv file (at least partially, or a little bit changed). Main reasons of my difficulties lays in next two ideas/questions/topics:

      1. TIMESTAMP format doesn't contain any information about Time Zone...it is impossible that, if I change some data with a colleague from USA (and I work in Europe) we send just YYYYMMDD HHMMSS info, without Time Zone...how is that resolved with TIMESTAMP? Something similar has asked colleague Bathula in previous post, just in ABAP context, and not SAP HANA.
      2. I had a lot of problems trying to import a row with TIMESTAMP 2000-03-26 02:46. I lose whole day until I realized that, this was a time when the clock moves (summer-winter time), so this moment of time doesn't exist at all! A little bit strange, if you agree. What also happens when, in winter period, clock moves from 02:59:59 to 02:00:00 - that seems even more "dangerous" to me - theoretically, you could have two entries in your database (lets say, e.g. some financial transactions), where one could be happen after another and actually in database would be registered like it is happened before...seems pretty confusing, I would say. Someone has idea how is that resolved?

      Many thanks in advance.

      With kind regards

      Daniel Divjakovic

       

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      All that is described in the documentation

      https://help.sap.com/http.svc/rc/abapdocu_752_index_htm/7.52/en-US/index.htm?file=abendate_time_processing.htm

      Author's profile photo Jens Schwendemann
      Jens Schwendemann

      Hi Horst,

      is there a way to convert time stamps in a expression style e.g. to use it in an expression enabled for iteration (table comprehension)

      Example:

      DATA(result) = VALUE result_type(
        FOR purchase_order IN purchase_orders
        ( result_order = order_number
          date         = '' ) ). "CONVERT TIME STAMP here for field order_timestamp
      
      

      Cheers

      Jens

      Author's profile photo Sandra Rossi
      Sandra Rossi

      But what type order_timestamp is, what does it contain, what is the target type? Maybe use a format option.

          date        = |{ order_timestamp TIMESTAMP = ISO }|
      Author's profile photo Jens Schwendemann
      Jens Schwendemann

      Hi Sandra,

      thanks for your comments / suggestions. Sorry to be not precise in the first place.

      • order_timestamp is of type TZNTSTMPS (UTC Time Stamp Short form)
      • date is of type d (built in type)
      • format does not help, unfortunatelly. It would only output the timestamp in a different format, not converting it to date / time.

      Format options, however was of course something I looked into, but have found it limited, see above. You could maybee see it as “room for improvement” for future releases. Might however also fit into a domain of a new built-in date function “convert_time_stamp()” ?

      Anyways, I currently worked around this by encapsulating “cl_abap_tstmp=>systemtstmp_utc2syst” into a helper method

      date = convert_timestamp_into_date( timestamp )

      because systemtstmp_utc2syst would have exporting pararameters making it impossible to use it in the target expression.

      Maybe there are better ways, though, the requirement should be somewhat common nowadays with all that expression enabled syntax ?

      Thanks and Cheers

      Jens

      Author's profile photo Rajesh Chinnakonda
      Rajesh Chinnakonda

      Hi,

      my requirement is as follows:

       

      Case 1 

      If I am provided with

      1. a Date,
      2. a Time and
      3. a Timezone (as in TTZZ-TZONE)

      I need to convert this to a Timestamp of 2 kinds of format

      1. Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z").
      2. Times are expressed in local time, together with a time zone offset in hours and minutes. A time zone offset of "+hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes ahead of UTC. A time zone offset of "-hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes behind UTC.

      courtesy : https://www.w3.org/TR/NOTE-datetime

      Case 2

      It is the symmetrical reverse conversion.

      If I am provided with a Timestamp of either

      1. Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z").
      2. Times are expressed in local time, together with a time zone offset in hours and minutes.

       

      I need to retrieve the parts

      1. a Date,
      2. a Time and
      3. a Timezone (as in TTZZ-TZONE)

      Kindly assist.

      Thanks in advance.