Skip to content

Inconsistent equality test around DST change with shared tzfile #338

@jamesblackburn

Description

@jamesblackburn

Reproduced in dateutil 2.6.0 and 2.4.2 : Python 2.7.11 |Anaconda custom (x86_64)

from dateutil.tz import gettz
from datetime import datetime as dt

tz = gettz('Europe/London')
x = dt(2007, 3, 25, 1, tzinfo=tz)
# x: datetime.datetime(2007, 3, 25, 1, 0, tzinfo=tzfile('/usr/share/zoneinfo/Europe/London'))

y = dt.fromtimestamp(int(x.strftime('%s')), tz)
# y:  datetime.datetime(2007, 3, 25, 0, 0, tzinfo=tzfile('/usr/share/zoneinfo/Europe/London'))
assert x == y # This fails

However if you use a new tz: gettz('Europe/London') or copy the existing one above, the equality comparison works:

z = dt.fromtimestamp(int(x.strftime('%s')), copy.copy(tz))
# z: datetime.datetime(2007, 3, 25, 0, 0, tzinfo=tzfile('/usr/share/zoneinfo/Europe/London'))
assert x == z  # This passes

This is surprising, as the only difference is the re-use of the tzfile in the first instance. I would have though tzfile objects are immutable?

x == y
>>> False
x == z
>>> True
y == z
>>> True

Given both datetimes represent the same ms-since-epoch instant I would expect both assertions to pass.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions