willtrnr / pyxlsb

Excel 2007+ Binary Workbook (xlsb) reader for Python
GNU Lesser General Public License v3.0
90 stars 21 forks source link

In convert_date(), round the seconds values for more accuracy #38

Closed keitherskine closed 2 years ago

keitherskine commented 2 years ago

First off, many thanks for creating this package @willtrnr . It's greatly appreciated.

This PR should address an issue I found when converting datetime values from the floats in an .xlsb spreadsheet. The date in question was July 4 2020 at 10:00:00. In Excel, this is stored as the floating point number 44016.416666666664. However, when convert_date() is used to retrieve the datetime, something unexpected happened:

>>> pyxlsb.convert_date(44016.416666666664)
datetime.datetime(2020, 7, 4, 9, 59, 59)  # the time should be 10:00:00, not 09:59:59

In convert_seconds() the seconds values are cast to integers with int. This truncates the seconds, whereas round will I believe return a better, more correct value:

>>> (44016.416666666664 % 1) * 24 * 60 * 60
35999.99999979045
>>> int((44016.416666666664 % 1) * 24 * 60 * 60)  # existing code
35999
>>> round((44016.416666666664 % 1) * 24 * 60 * 60)  # new code
36000

Hence, this PR changes the int to round so the nearest seconds value is returned, not the lower one.

sourcery-ai[bot] commented 2 years ago

Sourcery Code Quality Report

❌  Merging this PR will decrease code quality in the affected files by 0.16%.

Quality metrics Before After Change
Complexity 3.18 ⭐ 3.18 ⭐ 0.00
Method Length 48.00 ⭐ 48.67 ⭐ 0.67 👎
Working memory 6.91 🙂 6.91 🙂 0.00
Quality 76.23% 76.07% -0.16% 👎
Other metrics Before After Change
Lines 23 23 0
Changed files Quality Before Quality After Quality Change
pyxlsb/init.py 76.23% ⭐ 76.07% ⭐ -0.16% 👎

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation

Legend and Explanation

The emojis denote the absolute quality of the code:

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

willtrnr commented 2 years ago

Good catch, round is indeed much better for the job. Thanks for the fix!