-
Notifications
You must be signed in to change notification settings - Fork 523
Add time zone factories (tzutc, tzoffset) #504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
I'm not totally clear what's happening here. Is the idea that you want multiple calls to tzutc() to return the same object? |
|
@jbrockmendel Yes, but not only for As mentioned by @abalkin in #497, there are several fast paths in the code that are activated only when Generally it won't come up, but there's actually a slight difference between inter-zone and intra-zone comparison semantics that comes up when you start working with ambiguous times and >>> from dateutil import tz
>>> from datetime import datetime
>>> NYC = tz.gettz('America/New_York')
>>> dt1 = datetime(2011, 11, 6, 1, 30, fold=1, tzinfo=NYC)
>>> dt2 = datetime(2011, 11, 6, 1, 30, tzinfo=NYC)
>>> dt1 == dt2
True
>>> dt1 == dt2.replace(tzinfo=tz.gettz('America/New_York'))
FalseThis is because intra-zone comparisons are done by comparing the naive portion of the datetime (fold is not considered), but inter-zone comparisons are done by, effectively, converting both datetimes to UTC and comparing that. Since In the end, having factory metaclasses that provide constructors that return the same instance to represent the same zone will bring us more in line with the preferred |
Why is the thread safety an issue with timezone objects? Aren't they immutable? At least the simple ones such as UTC and fixed offset timezone should certainly be immutable. |
|
@abalkin Normally yes, and certainly UTC and tzoffset will remain immutable, but one potential race condition even for the immutable classes (though I'm guessing with minor effects) at the creation of the initial object between checking if you need to create a new object and actually creating one. For The bigger race condition problem I think will be if the more complex classes like I don't think it's a real problem, just something to consider in the implementation. |
|
There doesn't seem to be any controversy about the particular strategy here, so I'm merging. |
|
A little bit late to the party... the pattern here makes sense to me, but the threadsafety issue is bothersome. What is the failure mode here?
+1 |
|
@jbrockmendel I suspect it would be a very unlikely confluence of things that would lead to anything resembling a bug from the way that this is currently implemented. Assuming that If we were to drop the
If they interleave [notation: (thread, step)] like so: (1, 1), (2, 1), (1, 2), (2, 2) or (2, 1) (1, 1) (2, 2), (1, 2), then there's no problem (whichever one finished the assignment first would construct an extra If for some reason the downstream consumer is relying on the property that This can be fixed by just acquiring a lock around lines 10 and 11 in |
This is more or less the strategy I've come up with for handling singleton zones. It's fairly efficient from my profiling with
tzoffset.The next obvious candidate is
tzlocal(), but I think that one is slightly trickier to handle, I'll make an issue for discussion.