[issue14766] Non-naive time comparison throws naive time error

Chris Bergstresser report at bugs.python.org
Thu May 10 00:11:10 CEST 2012

New submission from Chris Bergstresser <chris at subtlety.com>:

The datetime module says:

An object d of type time or datetime may be naive or aware. d is aware if d.tzinfo is not None and d.tzinfo.utcoffset(d) does not return None. If d.tzinfo is None, or if d.tzinfo is not None but d.tzinfo.utcoffset(d) returns None, d is naive.

However, I can create two non-naive timezones (under this definition) which throw an exception when they're compared, because one is being considered offset-naive:

>>> import pytz, datetime
>>> UTC_TZ = pytz.utc
>>> EASTERN_TZ = pytz.timezone('America/New_York')
>>> d1 = datetime.time(10, tzinfo = UTC_TZ)
>>> d1
datetime.time(10, 0, tzinfo=<UTC>)
>>> d1.tzinfo
>>> d1.utcoffset(d1)
>>> d2 = datetime.time(10, tzinfo = EASTERN_TZ)
>>> d2
datetime.time(10, 0, tzinfo=<DstTzInfo 'America/New_York' EST-1 day, 19:00:00 STD>)
>>> d2.tzinfo
<DstTzInfo 'America/New_York' EST-1 day, 19:00:00 STD>
>>> d2.tzinfo.utcoffset(d2)
datetime.timedelta(-1, 68400)
>>> d1 < d2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't compare offset-naive and offset-aware times

messages: 160314
nosy: Chris.Bergstresser
priority: normal
severity: normal
status: open
title: Non-naive time comparison throws naive time error
type: behavior
versions: Python 2.6

Python tracker <report at bugs.python.org>

More information about the Python-bugs-list mailing list