2014 Nov 04 8:31 AM
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:
Thank you in advance
Sincerely,
Paul
2014 Nov 04 8:46 AM
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
*----------------------------------------------------------------------*
2014 Nov 04 8:46 AM
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
*----------------------------------------------------------------------*
2014 Nov 04 9:13 AM