Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dateutils.hours() gives wrong answer #57

Open
LadderOperator opened this issue Feb 14, 2022 · 2 comments
Open

dateutils.hours() gives wrong answer #57

LadderOperator opened this issue Feb 14, 2022 · 2 comments

Comments

@LadderOperator
Copy link

LadderOperator commented Feb 14, 2022

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
Copy link
Author

LadderOperator commented Feb 14, 2022

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant