Why does datetime.timedelta only have the attributes 'days' and 'seconds'?

Peter J. Holzer hjp-python at hjp.at
Sat Apr 16 13:50:52 EDT 2022


On 2022-04-17 02:46:38 +1000, Chris Angelico wrote:
> On Sun, 17 Apr 2022 at 02:45, Peter J. Holzer <hjp-python at hjp.at> wrote:
> > On 2022-04-17 02:14:44 +1000, Chris Angelico wrote:
> > > So which one is it? Which one do you get when you add days=7 to a datetime?
> >
> > For adding a datetime and timedelta I think the answer is clear.
> > But subtracting two datetimes is ambiguous.
> >
> 
> But if the difference between two datetimes is a timedelta, then
> surely adding a timedelta to a datetime should give the other
> datetime?

Not necessarily. You might compute the difference for another purpose.
If you compute a change rate from two gauge readings you would compute
something like (r1 - r0) / (t1 - t0). You don't intend to add (t1 - t0)
to any timestamp, so that property would be irrelevant. However, you do
want something which can be used in a division and which has a
consistent unit (so one could argue that you don't want a timedelta
object at all, but a floating point number).

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/python-list/attachments/20220416/656f1132/attachment.sig>


More information about the Python-list mailing list