Skip to content

Subtraction of two datetime objects returns wrong timedelta #122880

@Iftakharpy

Description

@Iftakharpy

Bug report

Bug description:

The difference of the date time objects returns timedelta in days, which should instead return the smallest accurate unit like milli/micro/seconds in time.

image

Example code:

from time import sleep
from datetime import datetime as dt, timedelta as td

end_time = dt.now() + td(seconds=2)

while (end_time - dt.now()) >= td(seconds=-1):
    sleep(1/2)
    print(f"end({end_time}) - now({dt.now()}) => timedelta({(end_time - dt.now())})")

Output:

end(2024-08-10 13:18:46.660845) - now(2024-08-10 13:18:45.161281) => timedelta(0:00:01.499564)
end(2024-08-10 13:18:46.660845) - now(2024-08-10 13:18:45.661931) => timedelta(0:00:00.998914)
end(2024-08-10 13:18:46.660845) - now(2024-08-10 13:18:46.162610) => timedelta(0:00:00.498235)
end(2024-08-10 13:18:46.660845) - now(2024-08-10 13:18:46.663169) => timedelta(-1 day, 23:59:59.997676)
end(2024-08-10 13:18:46.660845) - now(2024-08-10 13:18:47.164116) => timedelta(-1 day, 23:59:59.496729)
end(2024-08-10 13:18:46.660845) - now(2024-08-10 13:18:47.664745) => timedelta(-1 day, 23:59:58.996100)

The output I am expecting should be something like below:

end(2024-08-10 13:18:46.660845) - now(2024-08-10 13:18:45.161281) => timedelta(0:00:01.499564)
end(2024-08-10 13:18:46.660845) - now(2024-08-10 13:18:45.661931) => timedelta(0:00:00.998914)
end(2024-08-10 13:18:46.660845) - now(2024-08-10 13:18:46.162610) => timedelta(0:00:00.498235)
end(2024-08-10 13:18:46.660845) - now(2024-08-10 13:18:46.663169) => timedelta(-0:00:00.997676)
end(2024-08-10 13:18:46.660845) - now(2024-08-10 13:18:47.164116) => timedelta(-0:00:01.496729)
end(2024-08-10 13:18:46.660845) - now(2024-08-10 13:18:47.664745) => timedelta(-0:00:02.996100)

CPython versions tested on:

3.12

Operating systems tested on:

Linux, Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedtype-featureA feature request or enhancement

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions