zopefoundation / DateTime

This package provides a DateTime data type, as known from Zope. Unless you need to communicate with Zope APIs, you're probably better off using Python's built-in datetime module.
Other
19 stars 25 forks source link

why DateTime(0).equalTo(None) is True ? #52

Closed fdiary closed 1 year ago

fdiary commented 1 year ago

BUG/PROBLEM REPORT / FEATURE REQUEST

DateTime(0).equalTo(None) behaviour is different between DateTime 2 and DateTime 4+.

With DateTime 2

>>> from DateTime import DateTime
>>> DateTime(0).equalTo(None)
False

With DateTime 4+

>>> from DateTime import DateTime
>>> DateTime(0).equalTo(None)
True

It is caused by commit b9ddd8b9f9c8ba89fcab749a1871871706e7c2c0 where equalTo(None) is treated like equalTo(0) as 'further py3k work' (and same for greaterThanEqualTo and lessThanEqualTo). But if the intention is to accept comparing with None like Python 2, None is always the first in Python 2 sort, thus the implementation should be rather the following.

def equalTo(self, t):
  if t is None:
    return False # instead of 't = 0'
  ...

def greaterThanEqualTo(self, t):
  if t is None:
    return True # instead of 't = 0'
  ...

def lessThanEqualTo(self, t):
  if t is None:
    return False # instead of 't = 0'
  ...

For your reference, the interface docstring is the following, where I see no reason to treat None as DateTime(0)

Compare this DateTime object to another DateTime object OR a floating point number such as that which is returned by the python time module. Returns true if the object represents a date/time equal to the specified DateTime or time module style time. Revised to give more correct results through comparison of long integer milliseconds.

What do you think ?

icemac commented 1 year ago

Thank you for reporting this issue. It seams reasonable to fix the behaviour of DateTime 4+ to make it like DateTime 2.

A PR is welcome.

fdiary commented 1 year ago

@icemac Thanks for your comment. I created a PR #53.