Skip to content

Commit b4be0da

Browse files
committed
Merge pull request #1171 from mhvk/time/comparisons
Add comparisons of Time and TimeDelta. Closes #601.
2 parents e51f496 + 078dec4 commit b4be0da

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

‎astropy/time/core.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,34 @@ def __add__(self, other):
671671
else:
672672
raise OperandTypeError(self, other)
673673

674+
def _tai_difference(self, other):
675+
"""If other is of same class as self, return difference in TAI.
676+
Otherwise, raise OperandTypeError.
677+
"""
678+
if other.__class__ is not self.__class__:
679+
raise OperandTypeError(self, other)
680+
self_tai = self.tai
681+
other_tai = other.tai
682+
return (self_tai.jd1 - other_tai.jd1) + (self_tai.jd2 - other_tai.jd2)
683+
684+
def __lt__(self, other):
685+
return self._tai_difference(other) < 0.
686+
687+
def __le__(self, other):
688+
return self._tai_difference(other) <= 0.
689+
690+
def __eq__(self, other):
691+
return self._tai_difference(other) == 0.
692+
693+
def __ne__(self, other):
694+
return self._tai_difference(other) != 0.
695+
696+
def __gt__(self, other):
697+
return self._tai_difference(other) > 0.
698+
699+
def __ge__(self, other):
700+
return self._tai_difference(other) >= 0.
701+
674702

675703
class TimeDelta(Time):
676704
"""
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2+
import numpy as np
3+
4+
from ...tests.helper import pytest
5+
from .. import Time, TimeDelta, OperandTypeError
6+
7+
8+
class TestTimeComparisons():
9+
"""Test Comparisons of Time and TimeDelta classes"""
10+
11+
def setup(self):
12+
self.t1 = Time(np.arange(49995,50005), format='mjd', scale='utc')
13+
self.t2 = Time(np.arange(49000,51000,200), format='mjd', scale='utc')
14+
15+
def test_time(self):
16+
t1_lt_t2 = self.t1 < self.t2
17+
assert np.all(t1_lt_t2 == np.array([False, False, False, False, False,
18+
False,True, True, True, True]))
19+
t1_ge_t2 = self.t1 >= self.t2
20+
assert np.all(t1_ge_t2 != t1_lt_t2)
21+
22+
t1_le_t2 = self.t1 <= self.t2
23+
assert np.all(t1_le_t2 == np.array([False, False, False, False, False,
24+
True,True, True, True, True]))
25+
t1_gt_t2 = self.t1 > self.t2
26+
assert np.all(t1_gt_t2 != t1_le_t2)
27+
28+
t1_eq_t2 = self.t1 == self.t2
29+
assert np.all(t1_eq_t2 == np.array([False, False, False, False, False,
30+
True, False, False, False, False]))
31+
t1_ne_t2 = self.t1 != self.t2
32+
assert np.all(t1_ne_t2 != t1_eq_t2)
33+
34+
t1_0_gt_t2_0 = self.t1[0] > self.t2[0]
35+
assert t1_0_gt_t2_0 is True
36+
t1_0_gt_t2 = self.t1[0] > self.t2
37+
assert np.all(t1_0_gt_t2 == np.array([True, True, True, True, True,
38+
False, False, False, False,
39+
False]))
40+
t1_gt_t2_0 = self.t1 > self.t2[0]
41+
assert np.all(t1_gt_t2_0 == np.array([True, True, True, True, True,
42+
True, True, True, True, True]))
43+
44+
def test_timedelta(self):
45+
dt = self.t2 - self.t1
46+
with pytest.raises(OperandTypeError):
47+
self.t1 > dt
48+
dt_gt_td0 = dt > TimeDelta(0., format='sec')
49+
assert np.all(dt_gt_td0 == np.array([False, False, False, False, False,
50+
False, True, True, True, True]))

0 commit comments

Comments
 (0)