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: 

If condition is not working WHILE COMPARING 2 TIMESTAMPS IN ABAP

Param_Dayal1
Explorer
0 Kudos
1,072

Hi Expert ,


We have tried one logic to compare date but the If conditon is not working . In the below program if lv_partime is less than equal to that diffrence(lv_curtime - changed_on) it will write the message.

DATA :lv_curtime TYPE timestamp,
l_back_date
TYPE sydatum,
lv_partime
TYPE timestamp,
p_nodays
TYPE grfnmw_emair_freq,
changed_on
TYPE grfnmwrtappr-changed_on,
i_changed_on
TYPE p,
i_lv_curtime
TYPE p,
l_znlo
TYPE sy-zonlo,
diff
TYPE timestamp.
l_znlo
= 'UTC'.

CONVERT DATE sy-datum TIME sy-uzeit INTO TIME STAMP lv_curtime TIME ZONE l_znlo.
lv_partime
= p_nodays * 1000000.
*
IF changed_on NE 0 AND

lv_partime <= lv_curtime
- changed_on.

WRITE :/ 'testing' .

endif.

.

1 ACCEPTED SOLUTION

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
316

Hello,

You simply cannot calculate with ABAP time stamps as you do it!

The following does not work as expected:

lv_curtime - changed_on

Type p is used for storing time stamps but the format cannot be used for doing calculations with time stamps.

See time stamps.

For calculating you need the system classes described there.

Horst

10 REPLIES 10

SuhaSaha
Advisor
Advisor
0 Kudos
316

For all timestamp related operations try to use the standard ABAP class - CL_ABAP_TSTMP. (refer. ABAP Keyword Documentation)

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
317

Hello,

You simply cannot calculate with ABAP time stamps as you do it!

The following does not work as expected:

lv_curtime - changed_on

Type p is used for storing time stamps but the format cannot be used for doing calculations with time stamps.

See time stamps.

For calculating you need the system classes described there.

Horst

0 Kudos
316

Hi Suahs and Horst,

previously I have used that class cl_abap_tstmp but issue was not resolved. Previously I have used two method subtract and substractsec of cl_abap_tstmp class .Both were failed now I have used compare but still if condition failed.why if condition is not working you can see in screen shot.I have changed date in runtime in debug mode .

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
316

Um, don't you see in the comment that parameter comp (your diff) indicates the result  with values -1, 0, and 1 and cannot be used to compare with a timestamp as you do it?

0 Kudos
316

Ya I have seen the comment . But if you check the main post there all the data type same but still the if condition is not working. I have attached one screen shot here If you see the yellow mark you can understand

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
316

PS: Method COMPARE normally must not be used at all. Direct comparisons between time stamps without calculations work as expected if fixed point arithmetic is set for the program.

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
316

Please see my first answer. You cannot subtract time stamps directly. You must first subtract using a method and then compare.

Take your time and read the documentation thoroughly.

0 Kudos
316

I have compared but still the issue exists

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
316

Sorry, but you are still mixing up all the types.

First of all you must understand that you cannot use timestamps as you are continuing doing it. Above you assign the result of a calculation to a timestamp. ABAP allows it, but the result is not a timestamp. Please, please try to understand what timestamps in ABAP are. They are documented. And it is documented that they are handled as p numbers in most operand positions, which is not what you want.

There is also a blog from the old SCN that was abolished some years ago:

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
316

See ...