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

Date/time-operations - need help

Former Member
0 Likes
608

Hi there,

I have a simple question but i don't have really good ideas to solve the problem with ABAP.

This is the situation in which I need help:

I have many date/time-pairs (every in a single variable of type d and t). Now I want to compare one date/time-pair with a given date (also as pair). This will give a time difference between the two dates.

After that I want that all other date/time-pairs are shifted by this time difference (can be + and -).

So now my solution yet:


* FORM for calculation the time difference between 2 dates
FORM idoc_compare_dates  USING    iv_date_orig
                                  iv_time_orig
                                  iv_date_new
                                  iv_time_new
                         CHANGING cv_diff       TYPE p.

  DATA: lv_timestamp_orig    TYPE timestamp,
        lv_timestamp_new     TYPE timestamp.

* Creating timestamps for comparison
  CONVERT DATE iv_date_orig TIME iv_time_orig
    INTO TIME STAMP lv_timestamp_orig TIME ZONE sy-zonlo.
  CONVERT DATE iv_date_new TIME iv_time_new
    INTO TIME STAMP lv_timestamp_new TIME ZONE sy-zonlo.

* Calculation the time difference 
  cv_diff = lv_timestamp_new - lv_timestamp_orig.

ENDFORM.                    " IDOC_COMPARE_DATES

* *********************************************************************

* FORM for shifting a date by the time period calculated in the first FORM
FORM idoc_calc_single_date  USING    iv_diff      TYPE p
                                     iv_date_orig
                                     iv_time_orig
                            CHANGING cv_date_new
                                     cv_time_new.

  DATA: lv_timestamp_orig   TYPE timestamp,
        lv_timestamp_new    TYPE timestamp.

* Creating a timestamp for the original date
  CONVERT DATE iv_date_orig TIME iv_time_orig
    INTO TIME STAMP lv_timestamp_orig TIME ZONE sy-zonlo.

* Shifting the date
  lv_timestamp_new = lv_timestamp_orig + iv_diff.

* Creating the new date into the output-variables
  CONVERT TIME STAMP lv_timestamp_new TIME ZONE sy-zonlo
    INTO DATE cv_date_new TIME cv_time_new.

ENDFORM.                    " IDOC_CALC_SINGLE_DATE


I am not sure if this coding above is really correct. Can you please check it? Perhaps you have also other ideas to solve this problem?

Thank you!!

Kind regards

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
467

Hi,

As far as I think its absolutely correct....

but also try with one small change if you get the required output...

* Shifting the date
  lv_timestamp_new = lv_timestamp_orig + iv_diff.   "  if iv_diff is negative it will be subtract automatically.

Regards,

Siddarth

3 REPLIES 3
Read only

Former Member
0 Likes
467

u can check this class

CL_ABAP_TSTMP

it has methods to compare timestamps, add, subtract etc

кu03B1ятu03B9к

Read only

Former Member
0 Likes
468

Hi,

As far as I think its absolutely correct....

but also try with one small change if you get the required output...

* Shifting the date
  lv_timestamp_new = lv_timestamp_orig + iv_diff.   "  if iv_diff is negative it will be subtract automatically.

Regards,

Siddarth

Read only

Former Member
0 Likes
467

Thank you for your answers. Now I've created a small dummy-report, that tests the logic. And I think it seems to be okay.

You can also check it if you want to with this small report:



REPORT  zjz_test_timediff.

PARAMETERS: p_dat1  TYPE sydatum DEFAULT sy-datum,
            p_zei1  TYPE syuzeit DEFAULT sy-uzeit,
            p_dat2  TYPE sydatum DEFAULT sy-datum,
            p_zei2  TYPE syuzeit DEFAULT sy-uzeit.

SELECTION-SCREEN: SKIP 2.

PARAMETERS: p_diff type p.

SELECTION-SCREEN: SKIP 2.

PARAMETERS: p_dat3  TYPE sydatum,
            p_zei3  TYPE syuzeit.


DATA: lv_timestamp_orig   TYPE timestamp,
      lv_timestamp_new    TYPE timestamp,
      lv_diff             TYPE p.

AT SELECTION-SCREEN.

  PERFORM idoc_compare_dates USING p_dat1
                                   p_zei1
                                   p_dat2
                                   p_zei2
                          CHANGING p_diff.

  PERFORM idoc_calc_single_date USING p_diff
                                      p_dat1
                                      p_zei1
                             CHANGING p_dat3
                                      p_zei3.


*&---------------------------------------------------------------------*
*&      Form  IDOC_COMPARE_DATES
*&---------------------------------------------------------------------*
FORM idoc_compare_dates  USING    iv_date_orig
                                  iv_time_orig
                                  iv_date_new
                                  iv_time_new
                         CHANGING cv_diff       TYPE p.

  DATA: lv_timestamp_orig    TYPE timestamp,
        lv_timestamp_new     TYPE timestamp.


  CONVERT DATE iv_date_orig TIME iv_time_orig
    INTO TIME STAMP lv_timestamp_orig TIME ZONE sy-zonlo.
  CONVERT DATE iv_date_new TIME iv_time_new
    INTO TIME STAMP lv_timestamp_new TIME ZONE sy-zonlo.

  cv_diff = lv_timestamp_new - lv_timestamp_orig.

ENDFORM.                    " IDOC_COMPARE_DATES

*&---------------------------------------------------------------------*
*&      Form  IDOC_CALC_SINGLE_DATE
*&---------------------------------------------------------------------*
FORM idoc_calc_single_date  USING    iv_diff      TYPE p
                                     iv_date_orig
                                     iv_time_orig
                            CHANGING cv_date_new
                                     cv_time_new.

  DATA: lv_timestamp_orig   TYPE timestamp,
        lv_timestamp_new    TYPE timestamp.

  CONVERT DATE iv_date_orig TIME iv_time_orig
    INTO TIME STAMP lv_timestamp_orig TIME ZONE sy-zonlo.

  lv_timestamp_new = lv_timestamp_orig + iv_diff.

  CONVERT TIME STAMP lv_timestamp_new TIME ZONE sy-zonlo
    INTO DATE cv_date_new TIME cv_time_new.

ENDFORM.                    " IDOC_CALC_SINGLE_DATE