Application Development and Automation 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: 
Read only

Difference Between 2 Timestampl's

Former Member
0 Likes
3,195

Hi experts,

I am trying to find the difference between 2 timestampl's in string form ('YYYYMMDDhhmmssmmmuuun'). Last time I posted people gave me ways to find the difference between 2 timestamps which is not what I want. I want the precision of timestampl. Last time I posted the post got removed by a moderator who might not have fully read/understood the question. If you can find me another question that has an answer for finding the difference between 2 timestampl's please let me know. Any help with this is much appreciated.

Thanks,

Bradley

1 ACCEPTED SOLUTION
Read only

matt
Active Contributor
0 Likes
2,569

Perhaps an example would help people understand.

What output would you expect for the difference between 201605181502031234567 and 200905171602031269504?

14 REPLIES 14
Read only

matt
Active Contributor
0 Likes
2,570

Perhaps an example would help people understand.

What output would you expect for the difference between 201605181502031234567 and 200905171602031269504?

Read only

Former Member
0 Likes
2,569

The output I would expect is the difference between the timestampl's in decimal form unit of at least milliseconds precision. So basically the number of milliseconds between the timestampl's.

Thanks,

Bradley

Read only

matt
Active Contributor
0 Likes
2,569

And what have you tried that didn't work? (e.g. simple subtraction and a bit of multiplication and then putting the result into a string variable)

I'm struggling to understand how this question is not a basic programming question, and that my original decision to reject your post was not correct?

Read only

Former Member
0 Likes
2,569

Hi Matthew,

First off thanks for your response Matthew. I guess my question is how to do it easily without writing a bunch of custom code. For one I haven't been able to find a way to convert a string of this format: 'YYYYMMDDhhmmssmmmuuun' to a timestampl. If I could do that I could just subtract the 2 since they are represented as a decimal. I have found ways to convert a date and time to a timestamp but I need the millisecond precision.

Thanks,

Bradley

Read only

matt
Active Contributor
0 Likes
2,569

So your timestampl are not timestampl - they're strings. What is wrong with:

DATA t1s TYPE string VALUE `201605181502031234567`.

DATA t2s TYPE string VALUE `200905171602031269504`.

DATA diff TYPE decfloat34.

diff = t1s - t2s.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,569

There's a system which performs the calculations for you - CL_ABAP_TSTMP (ABAP Keyword Documentation).

Read the documentation of the method SUBTRACT, you can use the method for calculating the difference between two long timestamp(data element TZNTSTMPL) .

BR,

Suhas

Read only

Former Member
0 Likes
2,569

Will it always calculate correctly? What about leap years and different things of that nature. The whole point of a timestampl/timestamp is it is a given number of seconds from an agreed upon time...? Won't subtracting 2 strings/numbers cause issues if they are not in timestamp form?

Read only

Former Member
0 Likes
2,569

Hi Suhas,

First off thanks for your reply. How can I convert my string to TZNTSTMPL?

Thanks,

Bradley

Read only

matt
Active Contributor
0 Likes
2,569

No - it will never work.They're both strings, so it will treat the numbers as integers. Timestampl is not a given number of seconds from a base time.

What's wrong with CL_ABAP_TSTMP=>SUBTRACT() as Suhas has suggested?

DATA t1 TYPE timestampl VALUE '20160518150203.1234572'.

DATA t2 TYPE timestampl VALUE '20090517160203.2695044'.

DATA nanoseconds TYPE decfloat34.

nanoseconds = cl_abap_tstmp=>subtract( tstmp1 = t1 tstmp2 = t2 ) * 1000000.

WRITE: / nanoseconds..

Read only

matt
Active Contributor
0 Likes
2,569

Have you tried simple assignment? What happened? Why would you need to convert your string to TYPE TZNTSTMPL - the parameters of the method are P.

Read only

Former Member
0 Likes
2,569

I got an st22 dump...overflow.

Read only

matt
Active Contributor
0 Likes
2,569

And what did you try? What does your code look like? btw - please read this: At the moment you're not giving enough detail. Drip feeding information does not encourage people to help.

Read only

Former Member
0 Likes
2,569

Hi Matthew,

Your updated answer helps! Thanks I was missing the decimal place in the string.

Thanks,

Bradley

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,569

I would add a pinch of NORMALIZE to the mix -


DATA:

    ts1    TYPE string VALUE `201605181502031234567`,

    ts2    TYPE string VALUE `200905171602031269504`.



  DATA(len1) = strlen( ts1 ) - 7.

  DATA(len2) = strlen( ts2 ) - 7.



  ts1 = ts1+0(len1) && '.' && ts1+len1.

  ts2 = ts2+0(len2) && '.' && ts2+len2.



  TRY.

      cl_demo_output=>display_data(

        EXPORTING

          value cl_abap_tstmp=>subtract(

                       tstmp1  = CONV tzntstmpl( cl_abap_tstmp=>normalize( CONV tzntstmpl( ts1 ) ) )

                       tstmp2  = CONV tzntstmpl( cl_abap_tstmp=>normalize( CONV tzntstmpl( ts2 ) ) )

                   )

          name  = |Long-Timestamp Difference|    " Name

      ).

    CATCH cx_parameter_invalid_range INTO DATA(range_error).    "

      MESSAGE range_error TYPE 'E'.

    CATCH cx_parameter_invalid_type INTO DATA(type_error).    "

      MESSAGE type_error TYPE 'E'.

  ENDTRY.