[Datetime-SIG] Timeline arithmetic?

Stuart Bishop stuart at stuartbishop.net
Tue Sep 8 06:48:53 CEST 2015


On 4 September 2015 at 23:01, Chris Barker <chris.barker at noaa.gov> wrote:

> I would like a flag on datetime, but it seems it might be better to put that
> flag on a tzinfo object. But the implementation is the something to argue
> about only if there is any chance of doing it at all.

I would still lean towards a separate datetimetz class, but that is
just semantics.


> Also, particularly as PEP 495 will introduce changes to tzinfo, that will
> presumable lead to changes in tzinfo implementations (like pytz, etc), it
> seems that if other changes are afoot, now is a good time to map out how
> they should be done.
>
> Stuart, if you are listening:
>
> IIUC, you want "timeline" arithmetic to work with pytz tzinfo-aware
> datetimes. To the extent that the current implementation functions in a
> maybe "hacky", and at least inconvenient, way to achieve this.
>
> So you are an obvious person to say what we might put in the stdlib that
> would facilitate cleaning all that up. If anything.
>
> BTW: I'll at least take it as a given that we're not breaking backward
> compatibility, and that arithmetic needs to stay as fast as it currently is
> -- at least in the cases where it currently works.

To clean up pytz's interface and allow it to easily bolt on timeline
arithmetic to the existing datetime library, I need two hooks to
replace calls to tzinfo.localize() and tzinfo.normalize().

When a user does datetime.datetime(2000, 10, 9, 8, 7, 6,
tzinfo=pytz.timezone('US/Eastern'), a method on the tzinfo needs to be
invoked that returns the real tzinfo to be used for that datetime (ie.
the tzinfo instance for Oct 2000, not the default one for January
1878).

When arithmetic has been performed, a method on the resulting tzinfo
needs to be invoked that returns a datetime containing the real,
adjusted result.

These hooks are entirely separate to PEP-495 AFAICT. PEP-495 doesn't
help pytz the library much. It should help pytz users though, as most
use cases can stop using pytz and switch to using stdlib.

-- 
Stuart Bishop <stuart at stuartbishop.net>
http://www.stuartbishop.net/


More information about the Datetime-SIG mailing list