Skip to content

Make datetime objects comparable #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions netcdftime.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ def __repr__(self):
return self.strftime(self.format)
def __eq__(self, date):
return self.strftime('%Y-%m-%d %H:%M:%S') == date.strftime('%Y-%m-%d %H:%M:%S')

def __lt__(self, other):
if isinstance(other, (datetime, real_datetime)):
return self.strftime('%Y-%m-%d %H:%M:%S') < other.strftime('%Y-%m-%d %H:%M:%S')
return NotImplemented

def __gt__(self, other):
if isinstance(other, (datetime, real_datetime)):
return self.strftime('%Y-%m-%d %H:%M:%S') > other.strftime('%Y-%m-%d %H:%M:%S')
return NotImplemented


def JulianDayFromDate(date,calendar='standard'):
Expand Down
7 changes: 7 additions & 0 deletions test/tst_netcdftime.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ def runTest(self):
# check timezone offset
d = datetime(2012,2,29,15)
assert(self.cdftime_mixed.date2num(d)-self.cdftime_mixed_tz.date2num(d) == 6)

# Check comparisons with Python datetime types
d1 = num2date(0, 'days since 1000-01-01', 'standard')
d2 = datetime(2000, 1, 1)
d3 = num2date(0, 'days since 3000-01-01', 'standard')
assert d1 < d2
assert d2 < d3


class TestDate2index(unittest.TestCase):
Expand Down