Fix so that empty Excel date -> ABAP date 00000000 (#792)

issue #703

Co-authored-by: sandra <sandra.rossi@gmail.com>
Co-authored-by: Abo <andrea@borgia.bo.it>
This commit is contained in:
sandraros 2021-09-14 22:57:45 +02:00 committed by GitHub
parent 1cb2e42328
commit 3b80566fa8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 21 deletions

View File

@ -765,8 +765,13 @@ CLASS zcl_excel_common IMPLEMENTATION.
METHOD excel_string_to_date. METHOD excel_string_to_date.
DATA: lv_date_int TYPE i. DATA: lv_date_int TYPE i.
CHECK ip_value IS NOT INITIAL AND ip_value CN ' 0'.
TRY. TRY.
lv_date_int = ip_value. lv_date_int = ip_value.
IF lv_date_int NOT BETWEEN 1 AND 2958465.
zcx_excel=>raise_text( 'Unable to interpret date' ).
ENDIF.
ep_value = lv_date_int + c_excel_baseline_date - 2. ep_value = lv_date_int + c_excel_baseline_date - 2.
" Needed hack caused by the problem that: " Needed hack caused by the problem that:
" Excel 2000 incorrectly assumes that the year 1900 is a leap year " Excel 2000 incorrectly assumes that the year 1900 is a leap year

View File

@ -392,9 +392,57 @@ CLASS lcl_excel_common_test IMPLEMENTATION.
zcl_excel_aunit=>assert_equals( zcl_excel_aunit=>assert_equals(
act = ep_value act = ep_value
exp = '18991231' exp = '00000000'
msg = 'Wrong date conversion' msg = 'Wrong date conversion'
level = if_aunit_constants=>tolerable level = if_aunit_constants=>critical
).
CATCH zcx_excel INTO lx_excel.
zcl_excel_aunit=>fail(
msg = 'unexpected exception'
level = if_aunit_constants=>critical " Error Severity
).
ENDTRY.
* Check empty content
TRY.
ep_value = zcl_excel_common=>excel_string_to_date( '' ).
zcl_excel_aunit=>assert_equals(
act = ep_value
exp = '00000000'
msg = 'Wrong date conversion'
level = if_aunit_constants=>critical
).
CATCH zcx_excel INTO lx_excel.
zcl_excel_aunit=>fail(
msg = 'unexpected exception'
level = if_aunit_constants=>critical " Error Severity
).
ENDTRY.
* Check space character
TRY.
ep_value = zcl_excel_common=>excel_string_to_date( ` ` ).
zcl_excel_aunit=>assert_equals(
act = ep_value
exp = '00000000'
msg = 'Wrong date conversion'
level = if_aunit_constants=>critical
).
CATCH zcx_excel INTO lx_excel.
zcl_excel_aunit=>fail(
msg = 'unexpected exception'
level = if_aunit_constants=>critical " Error Severity
).
ENDTRY.
* Check first Excel date 1/1/1900
TRY.
ep_value = zcl_excel_common=>excel_string_to_date( '1' ).
zcl_excel_aunit=>assert_equals(
act = ep_value
exp = '19000101'
msg = 'Wrong date conversion'
level = if_aunit_constants=>critical
). ).
CATCH zcx_excel INTO lx_excel. CATCH zcx_excel INTO lx_excel.
zcl_excel_aunit=>fail( zcl_excel_aunit=>fail(
@ -451,13 +499,13 @@ CLASS lcl_excel_common_test IMPLEMENTATION.
). ).
ENDTRY. ENDTRY.
* Test 3. Index 0 is out of bounds * Test 3. Last possible date
TRY. TRY.
ep_value = zcl_excel_common=>excel_string_to_date( '2958446' ). ep_value = zcl_excel_common=>excel_string_to_date( '2958465' ).
zcl_excel_aunit=>assert_equals( zcl_excel_aunit=>assert_equals(
act = ep_value act = ep_value
exp = '99991212' exp = '99991231'
msg = 'Wrong date conversion' msg = 'Wrong date conversion'
level = if_aunit_constants=>critical level = if_aunit_constants=>critical
). ).
@ -470,28 +518,19 @@ CLASS lcl_excel_common_test IMPLEMENTATION.
* Test 4. Exception should be thrown index out of bounds * Test 4. Exception should be thrown index out of bounds
TRY. TRY.
ep_value = zcl_excel_common=>excel_string_to_date( '2958447' ). ep_value = zcl_excel_common=>excel_string_to_date( '2958466' ).
zcl_excel_aunit=>assert_differs( zcl_excel_aunit=>fail(
act = ep_value msg = |Unexpected result '{ ep_value }'|
exp = '99991212' level = if_aunit_constants=>critical
msg = 'Wrong date conversion'
level = if_aunit_constants=>fatal
).
zcl_excel_aunit=>assert_differs(
act = ep_value
exp = '00000000'
msg = 'Wrong date conversion'
level = if_aunit_constants=>fatal
). ).
CATCH zcx_excel INTO lx_excel. CATCH zcx_excel INTO lx_excel.
zcl_excel_aunit=>assert_equals( zcl_excel_aunit=>assert_equals(
act = lx_excel->error act = lx_excel->error
exp = 'Index out of bounds' exp = 'Unable to interpret date'
msg = 'Wrong exception is thrown' msg = 'Time should be a valid date'
level = if_aunit_constants=>tolerable level = if_aunit_constants=>fatal
). ).
ENDTRY. ENDTRY.
ENDMETHOD. "excel_String_To_Date ENDMETHOD. "excel_String_To_Date