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

Duration calculation using FM

a_ahmad
Participant
0 Likes
1,737

Hi,

I have two source timestamp fields of DEC-15 type in PSA having data in format 20.140.305.225.959.

Using FM which delivers output in seconds, I am trying to calculate difference through below code and put result in an Integer type infoObject.

This code is in field level routine from data source to DSO.

DATA:

assg1 TYPE TIMESTAMP,

rsld1 TYPE TIMESTAMP,

diffasrs TYPE I.


assg1 = SOURCE_FIELDS-ZZ_ASSIGNED.

rsld1 = SOURCE_FIELDS-ZZ_RESOLVED.

CALL FUNCTION 'CCU_TIMESTAMP_DIFFERENCE'

      EXPORTING

        TIMESTAMP1 = assg1

        TIMESTAMP2 = rsld1

      IMPORTING

        DIFFERENCE = diffasrs.

    IF SY-SUBRC = 0.

      RESULT = diffasrs.

    ENDIF.

This doesn't work. Is there any mistake in this code?

I also tried with something like:

DATA:

assg(18) TYPE c,

assg1 TYPE TIMESTAMP,

rsld(18) TYPE c,

rsld1 TYPE TIMESTAMP,

diffasrs TYPE I.

assg = SOURCE_FIELDS-ZZ_ASSIGNED.

rsld = SOURCE_FIELDS-ZZ_RESOLVED.

REPLACE ALL OCCURRENCES OF '.' IN assg WITH ''.

CONDENSE assg NO-GAPS.

assg1 = assg.

REPLACE ALL OCCURRENCES OF '.' IN rsld WITH ''.

CONDENSE rsld NO-GAPS.

rsld1 = rsld.

CALL FUNCTION 'CCU_TIMESTAMP_DIFFERENCE'

      EXPORTING

        TIMESTAMP1 = assg1

        TIMESTAMP2 = rsld1

      IMPORTING

        DIFFERENCE = diffasrs.

    IF SY-SUBRC = 0.

      RESULT = diffasrs.

    ENDIF.

but this also doesn't work.

could somebody please help??

Thanks

Ahmad

6 REPLIES 6
Read only

Former Member
0 Likes
1,445

HI Ahmad,

Try getting date and time from system using SY-DATUM and SY-UZEIT
Also, use function module SCOV_TIME_DIFF to calculate days and time difference between two dates.

Cheers!

Read only

Private_Member_7726
Active Contributor
0 Likes
1,445

Hi,

What about cl_abap_tstmp=>subtract(). That should works, I believe.


PROGRAM zjbtst2.


DATA: gx_ex TYPE REF TO cx_parameter_invalid .

DATA: g_tstmp TYPE timestamp .

DATA: g_tstmp1 TYPE timestamp .

DATA: g_secs  TYPE tzntstmpl .

GET TIME STAMP FIELD g_tstmp .

WAIT UP TO 5 SECONDS .

GET TIME STAMP FIELD g_tstmp1 .

TRY .

    g_secs = cl_abap_tstmp=>subtract(

        tstmp1 = g_tstmp1

        tstmp2 = g_tstmp

        ).

  CATCH

    cx_parameter_invalid_range

    cx_parameter_invalid_type

      INTO gx_ex .

    MESSAGE gx_ex TYPE 'E' .

ENDTRY .

WRITE: / g_secs .


cheers

Jānis

Message was edited by: Jānis B

Read only

0 Likes
1,445

This message was moderated.

Read only

0 Likes
1,445

Hi,

I tried this use in my routine but getting an error that 'g_tstmp' is not defined.

Tried with defining variables with other names but getting same error.

any idea??

Thanks

Ahmad

Read only

0 Likes
1,445

Hi Shree,

my source fields are already in timestamp format.

The difference calculation code is not working.

Thanks

Ahmad

Read only

0 Likes
1,445

Hi,

Well, you have to put your own variables in the method call - it's just an example demonstrating how to call the method. The method subtracts two timestamps.

In your case it would be something like this:

DATA:

assg1 TYPE TIMESTAMP,

rsld1 TYPE TIMESTAMP,

diffasrs TYPE tzntstmpl.


assg1 = SOURCE_FIELDS-ZZ_ASSIGNED.

rsld1 = SOURCE_FIELDS-ZZ_RESOLVED.

TRY .

    diffasrs = cl_abap_tstmp=>subtract(

        tstmp1 = assg1

        tstmp2 = rsld1

        ).

  CATCH

    cx_parameter_invalid_range

    cx_parameter_invalid_type

      INTO gx_ex .

    MESSAGE gx_ex TYPE 'E' .

ENDTRY .


Good luck with your learning


cheers

Jānis