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: 

Calculating time difference between Dates/Times

goodvirus
Explorer
0 Kudos
416

Dear
Experts,

I have a
tricky problem an seem not to find an elegant solution.

My task
seems very simple: calculate the number of minutes between an startdate and
time and an enddate and time.

For this I used the function: L_MC_TIME_DIFFERENCE and I was happy.

Until… I discovered that this function doesn’t support daylight saving and leap years.

So I searched a bit and found a bunch of functions that do the same thing, but none
of them seem to support this feature.

    

I could program my own function that works with timestamps, daylight saving times, subtracting
hours, leap year and so on but I refuse to believe that there isn’t a standard way to do it.

    

So I am asking you: is there a function or a piece of code that supports the following
features:

  • Calculating time difference in minutes between a start/date as well as a start/end time
  • Daylight saving (especially for the time zone CET)
  • Leap year

 

Thank you in advance

Sincerely,

Paul

1 ACCEPTED SOLUTION

rosenberg_eitan
Active Contributor
0 Kudos
268

Hi,

See if this is working for you .

Regards.

*----------------------------------------------------------------------*
FORM calc_time_diff
  USING
    p_date_f TYPE sydatum
    p_time_f TYPE syuzeit
    p_date_t TYPE sydatum
    p_time_t TYPE syuzeit
  CHANGING
    p_diff        TYPE tzntotoffs .

  DATA: stamp_1 TYPE timestamp .
  DATA: stamp_2 TYPE timestamp .

  CLEAR p_diff .

  CONVERT DATE p_date_f TIME p_time_f
    INTO TIME STAMP stamp_2 TIME ZONE sy-zonlo .

  CHECK sy-subrc EQ 0 .

  CONVERT DATE p_date_t TIME p_time_t
    INTO TIME STAMP stamp_1 TIME ZONE sy-zonlo .

  CHECK sy-subrc EQ 0 .

  DATA: r_secs TYPE tzntstmpl .

  TRY .

      CALL METHOD cl_abap_tstmp=>subtract
        EXPORTING
          tstmp1 = stamp_1
          tstmp2 = stamp_2
        RECEIVING
          r_secs = r_secs.

      p_diff = r_secs .

    CATCH cx_parameter_invalid_range .
      CLEAR p_diff .
    CATCH cx_sy_arithmetic_overflow .
      CLEAR p_diff .
  ENDTRY .

ENDFORM .                    "calc_time_diff
*----------------------------------------------------------------------*

2 REPLIES 2

rosenberg_eitan
Active Contributor
0 Kudos
269

Hi,

See if this is working for you .

Regards.

*----------------------------------------------------------------------*
FORM calc_time_diff
  USING
    p_date_f TYPE sydatum
    p_time_f TYPE syuzeit
    p_date_t TYPE sydatum
    p_time_t TYPE syuzeit
  CHANGING
    p_diff        TYPE tzntotoffs .

  DATA: stamp_1 TYPE timestamp .
  DATA: stamp_2 TYPE timestamp .

  CLEAR p_diff .

  CONVERT DATE p_date_f TIME p_time_f
    INTO TIME STAMP stamp_2 TIME ZONE sy-zonlo .

  CHECK sy-subrc EQ 0 .

  CONVERT DATE p_date_t TIME p_time_t
    INTO TIME STAMP stamp_1 TIME ZONE sy-zonlo .

  CHECK sy-subrc EQ 0 .

  DATA: r_secs TYPE tzntstmpl .

  TRY .

      CALL METHOD cl_abap_tstmp=>subtract
        EXPORTING
          tstmp1 = stamp_1
          tstmp2 = stamp_2
        RECEIVING
          r_secs = r_secs.

      p_diff = r_secs .

    CATCH cx_parameter_invalid_range .
      CLEAR p_diff .
    CATCH cx_sy_arithmetic_overflow .
      CLEAR p_diff .
  ENDTRY .

ENDFORM .                    "calc_time_diff
*----------------------------------------------------------------------*

0 Kudos
268

This works fine, thank you!