Skip to content

Commit

Permalink
Allow comparison of DateTime objects against None.
Browse files Browse the repository at this point in the history
  • Loading branch information
hannosch committed Oct 19, 2011
1 parent 5bb1761 commit 7733a10
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ from the Python standard library instead.
3.0 (unreleased)
----------------

- Allow comparison of DateTime objects against None.

3.0b2 (2011-10-19)
------------------
Expand Down
25 changes: 20 additions & 5 deletions src/DateTime/DateTime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,10 @@ def greaterThan(self, t):
"""
if isinstance(t, float):
return self._micros > long(t * 1000000)
return self._micros > t._micros
try:
return self._micros > t._micros
except AttributeError:
return self._micros > t

__gt__ = greaterThan

Expand All @@ -1256,7 +1259,10 @@ def greaterThanEqualTo(self, t):
"""
if isinstance(t, float):
return self._micros >= long(t * 1000000)
return self._micros >= t._micros
try:
return self._micros >= t._micros
except AttributeError:
return self._micros >= t

__ge__ = greaterThanEqualTo

Expand All @@ -1273,7 +1279,10 @@ def equalTo(self, t):
"""
if isinstance(t, float):
return self._micros == long(t * 1000000)
return self._micros == t._micros
try:
return self._micros == t._micros
except AttributeError:
return self._micros == t

def notEqualTo(self, t):
"""Compare this DateTime object to another DateTime object
Expand Down Expand Up @@ -1315,7 +1324,10 @@ def lessThan(self, t):
"""
if isinstance(t, float):
return self._micros < long(t * 1000000)
return self._micros < t._micros
try:
return self._micros < t._micros
except AttributeError:
return self._micros < t

__lt__ = lessThan

Expand All @@ -1332,7 +1344,10 @@ def lessThanEqualTo(self, t):
"""
if isinstance(t, float):
return self._micros <= long(t * 1000000)
return self._micros <= t._micros
try:
return self._micros <= t._micros
except AttributeError:
return self._micros <= t

__le__ = lessThanEqualTo

Expand Down
24 changes: 17 additions & 7 deletions src/DateTime/tests/testDateTime.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,26 @@ def testTZ1diff(self):
dt1 = DateTime('1997/3/9 1:45pm GMT+8')
self.assertEqual(dt1 - dt, 1.0, (dt, dt1))

def testCompareMethods(self):
def test_compare_methods(self):
# Compare two dates using several methods
dt = DateTime('1997/1/1')
dt1 = DateTime('1997/2/2')
self.failUnless(dt1.greaterThan(dt))
self.failUnless(dt1.greaterThanEqualTo(dt))
self.failUnless(dt.lessThan(dt1))
self.failUnless(dt.lessThanEqualTo(dt1))
self.failUnless(dt.notEqualTo(dt1))
self.failUnless(not dt.equalTo(dt1))
self.assertTrue(dt1.greaterThan(dt))
self.assertTrue(dt1.greaterThanEqualTo(dt))
self.assertTrue(dt.lessThan(dt1))
self.assertTrue(dt.lessThanEqualTo(dt1))
self.assertTrue(dt.notEqualTo(dt1))
self.assertFalse(dt.equalTo(dt1))

def test_compare_methods_none(self):
# Compare a date to None
dt = DateTime('1997/1/1')
self.assertTrue(dt.greaterThan(None))
self.assertTrue(dt.greaterThanEqualTo(None))
self.assertFalse(dt.lessThan(None))
self.assertFalse(dt.lessThanEqualTo(None))
self.assertTrue(dt.notEqualTo(None))
self.assertFalse(dt.equalTo(None))

def test_pickle(self):
dt = DateTime()
Expand Down

0 comments on commit 7733a10

Please sign in to comment.