[issue38812] Comparing datetime.time objects incorrect for TZ aware and unaware

Jason Killen report at bugs.python.org
Fri Nov 15 13:49:27 EST 2019


Jason Killen <jsnklln at gmail.com> added the comment:

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:
from datetime import timedelta, datetime, tzinfo, timezone, time

# 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")

        return dt + timedelta(hours=4)

    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.  `dt` appears to be a datetime when utcoffset is called from a datetime.

----------
nosy: +Jason.Killen

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38812>
_______________________________________


More information about the Python-bugs-list mailing list