Skip to content

Commit

Permalink
Fix so that empty Excel date -> ABAP date 00000000 (#792)
Browse files Browse the repository at this point in the history
issue #703

Co-authored-by: sandra <sandra.rossi@gmail.com>
Co-authored-by: Abo <andrea@borgia.bo.it>
  • Loading branch information
3 people committed Sep 14, 2021
1 parent 1cb2e42 commit 3b80566
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 21 deletions.
5 changes: 5 additions & 0 deletions src/zcl_excel_common.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,13 @@ CLASS zcl_excel_common IMPLEMENTATION.
METHOD excel_string_to_date.
DATA: lv_date_int TYPE i.

CHECK ip_value IS NOT INITIAL AND ip_value CN ' 0'.

TRY.
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.
" Needed hack caused by the problem that:
" Excel 2000 incorrectly assumes that the year 1900 is a leap year
Expand Down
81 changes: 60 additions & 21 deletions src/zcl_excel_common.clas.testclasses.abap
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,57 @@ CLASS lcl_excel_common_test IMPLEMENTATION.

zcl_excel_aunit=>assert_equals(
act = ep_value
exp = '18991231'
exp = '00000000'
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.
zcl_excel_aunit=>fail(
Expand Down Expand Up @@ -451,13 +499,13 @@ CLASS lcl_excel_common_test IMPLEMENTATION.
).
ENDTRY.

* Test 3. Index 0 is out of bounds
* Test 3. Last possible date
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(
act = ep_value
exp = '99991212'
exp = '99991231'
msg = 'Wrong date conversion'
level = if_aunit_constants=>critical
).
Expand All @@ -470,28 +518,19 @@ CLASS lcl_excel_common_test IMPLEMENTATION.

* Test 4. Exception should be thrown index out of bounds
TRY.
ep_value = zcl_excel_common=>excel_string_to_date( '2958447' ).

zcl_excel_aunit=>assert_differs(
act = ep_value
exp = '99991212'
msg = 'Wrong date conversion'
level = if_aunit_constants=>fatal
).
ep_value = zcl_excel_common=>excel_string_to_date( '2958466' ).

zcl_excel_aunit=>assert_differs(
act = ep_value
exp = '00000000'
msg = 'Wrong date conversion'
level = if_aunit_constants=>fatal
zcl_excel_aunit=>fail(
msg = |Unexpected result '{ ep_value }'|
level = if_aunit_constants=>critical
).

CATCH zcx_excel INTO lx_excel.
zcl_excel_aunit=>assert_equals(
act = lx_excel->error
exp = 'Index out of bounds'
msg = 'Wrong exception is thrown'
level = if_aunit_constants=>tolerable
exp = 'Unable to interpret date'
msg = 'Time should be a valid date'
level = if_aunit_constants=>fatal
).
ENDTRY.
ENDMETHOD. "excel_String_To_Date
Expand Down

0 comments on commit 3b80566

Please sign in to comment.