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

Chris Angelico rosuav at gmail.com
Sat Apr 16 12:46:38 EDT 2022


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:
> > 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.
>

But if the difference between two datetimes is a timedelta, then
surely adding a timedelta to a datetime should give the other
datetime? It's just as ambiguous.

ChrisA


More information about the Python-list mailing list