-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
Comparing datetime.time objects incorrect for TZ aware and unaware #82993
Comments
import pytz
import datetime
tzaware_time1 = datetime.time(7,30,tzinfo=pytz.timezone("America/Denver"))
tzaware_time2 = datetime.time(7,30,tzinfo=pytz.utc)
tzunaware_time = datetime.time(7, 30) # This fails with exception: TypeError: can't compare offset-naive and offset-aware times # This does NOT raise an exception and should since one is aware and one isn't. |
This appears to be a bug in pytz which as far as I can tell is not part of the "standard" lib, at least it's not in Lib. Running this example which does not use pytz: # stole this from the docs, and beat it up a bit
class KabulTz(tzinfo):
def utcoffset(self, dt):
return timedelta(hours=4, minutes=30)
def fromutc(self, dt):
# Follow same validations as in datetime.tzinfo
if not isinstance(dt, datetime):
raise TypeError("fromutc() requires a datetime argument")
if dt.tzinfo is not self:
raise ValueError("dt.tzinfo is not self")
def dst(self, dt):
# Kabul does not observe daylight saving time.
return timedelta(0)
def tzname(self, dt):
return "+04"
tzaware_time2 = time(7,30,tzinfo=timezone.utc)
tzaware_time1 = time(7,30,tzinfo=KabulTz())
tzunaware_time = time(7, 30) tzaware_time1 < tzaware_time2 I get no error. When I use your example I notice that utcoffset in pytz/tzinfo.py returns None for the Denver example but for the UTC object returns ZERO which happens to be timedelta(0) which seems correct. It's weird to me that pytz is returning None in the Denver case because I think what it should return is right there in self._utcoffset. More info: It looks like pytz isn't handling the fact that it's a time only type. |
Ok. I'll file a bug on pytz. Thanks! On Sat, Nov 16, 2019 at 11:18 PM Karthikeyan Singaravelan <
|
I do not think this is a bug in pytz, but if it's a bug in Python it's one in reporting what the error is. The issue is that the time zone offset for "rules-based zones" like America/Denver (i.e. most time zones) is *undefined* for bare times, because the offset that apply depends on the *date* and the *time*. The documentation for The documentation for determining whether an object is aware or naive also specifies that if utcoffset() returns So basically, everyone is doing the right thing except the person who attached this That said, we may be able to improve the error message raised here by distinguishing between the case where there's no We could possibly be even more specific. |
Yep I wasn't seeing the forest for the trees. Thanks for clearing that up. I'll swoop back in and see what I can do. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: