Standard class for time *period*?

Thomas Passin list1 at tompassin.net
Tue Mar 28 12:29:26 EDT 2023


On 3/28/2023 12:13 PM, Grant Edwards wrote:
> On 2023-03-28, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
> 
>> So far, you seem to be the only person who has ever asked for a
>> single entity incorporating an EPOCH (datetime.datetime) + a
>> DURATION (datetime.timedelta).
> 
> It seems to me that tuple of two timdate objects (start,end) is the
> more obvious representation, but it's six of one and a horse of the
> same color.
> 
> If one really needs it to be a class, then it woulnd't be much more
> than a dozen or two lines of code...

I think it would be more than that.  The OP said

"But even if I have a single epoch, January 2022 is obviously different
to January 2023, even thought the duration might be the same.  I am just
surprised that there is no standard Period class, with which I could
create objects and then be able to sort, check for identity, equality of
length, amount of overlap, etc.  I suppose people just use a
datetime.datetime pair, as I have been doing so far, and tack on just
the specific bit of functionality they need."

A class could be devised to have methods to do those things, but it 
would take some thought to be sure if they all made sense.  Take 
sorting, for example.  Are two "Periods" really comparable?  Can we have 
complete ordering  here?  "Identity" too would have to be carefully 
defined, and then a __eq__ method could be implemented.  And so on.

It would take more than 10 or 20 lines, I would think.  But the hard 
work of figuring out exactly what is wanted and what even makes sense to 
implement needs to be done first.



More information about the Python-list mailing list