Problem statement
It seems like there is a bug in python datetime
module, following code snippet should be self-explanatory:
import datetime
dep = datetime.datetime(2021, 9, 11, 7, 25)
arr = datetime.datetime(2021, 9, 11, 12, 35)
print(f"expected: -5h10m, real: {dep - arr}")
print(f"expected: 5h10m, real: {arr - dep}")
What I get with python 3.10:
expected: -5h10m, real: -1 day, 18:50:00
expected: 5h10m, real: 5:10:00
Question
Is it a bug or a feature? If it is a bug, what can I do to fix it?
It is a feature - subtraction of datetime
from datetime
results in a timedelta
object, which is normalised so that only negative value can be days
.
That behaviour is documented, eg. here: https://pl.python.org/docs/lib/datetime-timedelta.html