seatable-api-python icon indicating copy to clipboard operation
seatable-api-python copied to clipboard

dateutils.hours() gives wrong answer

Open LadderOperator opened this issue 3 years ago • 2 comments

Following the official tutorial Python API and running dateutils.hours("2019-6-3 20:1:12", "2020-5-3 13:13:13") will give an answer of 8009, different from 8034 given by the tutorial.

LadderOperator avatar Feb 14 '22 02:02 LadderOperator

I think this part leads to the problem:

 def datediff(self, start, end, unit='S'):
        ...
        elif unit == 'H':
            delta_days = (dt_end - dt_start).days
            if delta_days == 0:
                return dt_end.hour - dt_start.hour
            return delta_days * 24 + (dt_end.hour - dt_start.hour)
       ...

delta_days == 0 means less than 24 hours. To check if they are the same day, something like start.day == end.day should be used instead. Similar mistake occurs when delta_days not equals to 0.

Since dateutils is implimented using datetime, this part can be improved:

td = dt_end - dt_start
delta = td.days * 24 + td.seconds / 3600

LadderOperator avatar Feb 14 '22 02:02 LadderOperator

OK, I think the datediff() (not only the hours()) function needs more check. Using more timedelta.days and timedelta.seconds could help making the code more clear to avoid bugs.

LadderOperator avatar Feb 14 '22 03:02 LadderOperator