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

Loris Bennett loris.bennett at fu-berlin.de
Tue Apr 19 08:54:57 EDT 2022


Jon Ribbens <jon+usenet at unequivocal.eu> writes:

> On 2022-04-19, Loris Bennett <loris.bennett at fu-berlin.de> wrote:
>> I now realise that timedelta is not really what I need.  I am interested
>> solely in pure periods, i.e. numbers of seconds,
>
> That's exactly what timedelta is.
>
>> that I can convert back and forth from a format such as
>>
>>   11-22::44:55
>
> I don't recognise that format and can't work out what it means.
> It should be trivial to write functions to parse whatever format
> you wanted and convert between it and timedelta objects though.

days-hours:minutes:seconds

>> It is obviously fairly easy to rustle up something to do this, but I am
>> surprised that this is not baked into Python (such a class also seems to
>> be missing from R).
>
> I would be very surprised if any language supported the arbitrary format
> above you happen to be interested in!

But most languages support fairly arbitrary formatting of timedate-style
objects.  It doesn't seem unreasonable to me that such formatting might
be available for simple periods.

>> I would have thought that periods crop up all over
>> the place and therefore formatting as strings and parsing of string
>> would be supported natively by most modern languages.  Apparently not.
>
> I think most languages think that a simple number suffices to represent
> a fixed time period (commonly seconds or milliseconds). And if you want
> more dynamic intervals (e.g. x months y days) then there is insufficient
> consensus as to what that actually means.

Maybe.  It just seems to me that once you get up to more than a few
hundred seconds, the ability to convert and from a more readable format
becomes very useful.  The length of a month may be unclear, but the
definitions for year, week, day, hours, and minute are all trivial.

Cheers,

Loris

-- 
This signature is currently under construction.


More information about the Python-list mailing list