Skip to content

Conversation

@pganssle
Copy link
Member

@pganssle pganssle commented Nov 5, 2017

Python assumes that tzinfo objects representing the same zone are effectively singletons, which is why intra- vs inter-zone semantics apply based on dt1.tzinfo is dt2.tzinfo, not dt1.tzinfo == dt2.tzinfo.

tzutc() is the simplest case, since it only ever represents one zone. I've also added a convenience constant to the top level tz/__init__.py, so people can just use it as a constant.

Eventually the other classes will store their representations in caches (though this is more complicated and has more implications for the API long term).

UTC0 = tz.tzutc()
UTC1 = tz.tzutc()

self.assertIsNot(UTC0, UTC1)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why I was testing this.

@jbrockmendel
Copy link
Contributor

To what extent does this complement vs duplicate pytz.utc?

@pganssle
Copy link
Member Author

pganssle commented Nov 5, 2017

@jbrockmendel pytz has a whole different thing going on, but UTC is so simple that basically all implementations will have the same properties. This version has some fast paths for dateutil-specific stuff (like checking ambiguities), but will work the same as pytz.utc. Of course, there's no way to make pytz.utc is dateutil.tz.UTC or dateutil.tz.UTC is datetime.timezone.UTC, but that's fine. This will mostly make it slightly cheaper to use UTC from dateutil (no need to construct a new instance or even call the constructor now), and move us down the road to a one-object-per-zone model.

@pganssle
Copy link
Member Author

pganssle commented Nov 6, 2017

@abalkin Any thoughts on this? Not sure who else would understand the implications of this well enough to review.

@abalkin
Copy link

abalkin commented Nov 7, 2017

This looks like a no-brainer optimization. There are several code paths in the datetime module where having the same tzinfo rather then just equal TZ objects saves several calls to utcoffset() methods.

@pganssle
Copy link
Member Author

pganssle commented Nov 7, 2017

@abalkin Thanks. That's pretty much what I thought. This is the least controversial of the singletons anyway. I'm going to try and come up with an interface for the others that's not totally nuts but ports these "fast path" qualities to the other types (particularly tzfile), I imagine the details of that one will be more important.

@pganssle pganssle merged commit 671c76a into dateutil:master Nov 7, 2017
@pganssle pganssle mentioned this pull request Mar 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants