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

Peter J. Holzer hjp-python at hjp.at
Sat Apr 16 12:44:08 EDT 2022


On 2022-04-17 02:14:44 +1000, Chris Angelico wrote:
> On Sun, 17 Apr 2022 at 02:03, Peter J. Holzer <hjp-python at hjp.at> wrote:
> > On the contrary. When a datetime is timezone aware, it must use that
> > timezone's rules. Adding one day to a datetime just before a DST switch
> > must add 23 or 25 hours, not 24. This is NOT naive.
> >
> > (There is an ambiguity, though: Should 2021-03-27T12:00 CEST -
> > 2021-03-26T12:00 CET return 1 day or 25 hours? Both results are correct,
> > and depending on context you might prefer one or the other).
> >
> 
> That's exactly the problem. A day IS 24 hours *and* it is the time
> period required for you to get to the same clock on the following
> date. It's fundamentally ambiguous.
> 
> Let's take this out of Python altogether for a moment. Imagine that
> it's 9AM Thursday. You say to your friend, hey, let's meet up again,
> same time next week. Are you planning to meet 168 hours later, or at
> 9AM the following Thursday?
> 
> OF COURSE you mean 168 hours later. That's what "next week" means.

Of course NOT. It means that only in 50 out of 52 weeks. A 4% error rate
is more than enough to make me acutely aware that it isn't true in
general. Also, won't count off 168 hours but look at my calendar/clock.


> OF COURSE you're meeting at 9AM the following Thursday. That's what
> "next week" means.

This. Certainly when meeting a friend. Almost certainly when dealing
with humans (if they are in a different time zone we may have to agree
on a time zone for "same time"). For a technical process I *might*
simplify "1 week" to "168 hours", but only if the spec gives me
sufficient leeway.


> And they can't both be true if DST is changing.
> 
> 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.

        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/e2c1d389/attachment.sig>


More information about the Python-list mailing list