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

Chris Angelico rosuav at gmail.com
Thu Apr 14 13:58:17 EDT 2022


On Fri, 15 Apr 2022 at 03:45, Marco Sulla <Marco.Sulla.Python at gmail.com> wrote:
>
> On Thu, 14 Apr 2022 at 19:16, MRAB <python at mrabarnett.plus.com> wrote:
> >
> > When you're working only with dates, timedelta not having a 'days'
> > attribute would be annoying, especially when you consider that a day is
> > usually 24 hours, but sometimes 23 or 25 hours (DST).
>
> I agree. Furthermore, timedelta is, well, a time delta, not a date
> with a timezone. How could a timedelta take into account DST, leap
> seconds etc?

It can't. It's a simple representation of a time period. It is useful
for situations where you want to express questions like "from this
date/time, wait this long, what will the date/time be?".

In the absence of a corresponding timezone-aware datetime object, it
cannot possibly acknowledge DST.

ChrisA


More information about the Python-list mailing list