2016 May 18 1:25 PM
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
2016 May 18 2:03 PM
Perhaps an example would help people understand.
What output would you expect for the difference between 201605181502031234567 and 200905171602031269504?
2016 May 18 2:03 PM
Perhaps an example would help people understand.
What output would you expect for the difference between 201605181502031234567 and 200905171602031269504?
2016 May 18 2:06 PM
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
2016 May 18 2:20 PM
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?
2016 May 18 2:35 PM
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
2016 May 18 2:47 PM
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.
2016 May 18 2:49 PM
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
2016 May 18 2:50 PM
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?
2016 May 18 2:55 PM
Hi Suhas,
First off thanks for your reply. How can I convert my string to TZNTSTMPL?
Thanks,
Bradley
2016 May 18 3:01 PM
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..
2016 May 18 3:03 PM
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.
2016 May 18 3:04 PM
2016 May 18 3:05 PM
2016 May 18 3:14 PM
Hi Matthew,
Your updated answer helps! Thanks I was missing the decimal place in the string.
Thanks,
Bradley
2016 May 18 3:45 PM
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.
| User | Count |
|---|---|
| 6 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |