Skip to content

Commit

Permalink
Gracefully handle long time intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
DPeterK committed Feb 10, 2017
1 parent 07bbaf4 commit f532956
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/iris/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,13 @@ def __str__(self):
fmt = '{cls}({points}{bounds}' \
', standard_name={self.standard_name!r}' \
', calendar={self.units.calendar!r}{other_metadata})'
points = self._str_dates(self.points)
if self.units.is_long_time_interval():
# A time unit with a long time interval ("months" or "years")
# cannot be converted to a date using `num2date` so gracefully
# fall back to printing points as numbers, not datetimes.
points = self.points
else:
points = self._str_dates(self.points)
bounds = ''
if self.bounds is not None:
bounds = ', bounds=' + self._str_dates(self.bounds)
Expand Down
24 changes: 24 additions & 0 deletions lib/iris/tests/unit/coords/test_Coord.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,5 +368,29 @@ def test_writable_points(self):
coord2.bounds[:] = 0


class Test___str__(tests.IrisTest):

def test_short_time_interval(self):
coord = DimCoord([5], standard_name='time',
units='days since 1970-01-01')
expected = ("DimCoord([1970-01-06 00:00:00], standard_name='time', "
"calendar='gregorian')")
result = coord.__str__()
self.assertEqual(expected, result)

def test_long_time_interval(self):
coord = DimCoord([5], standard_name='time',
units='years since 1970-01-01')
expected = "DimCoord([5], standard_name='time', calendar='gregorian')"
result = coord.__str__()
self.assertEqual(expected, result)

def test_non_time_unit(self):
coord = DimCoord([1.])
expected = repr(coord)
result = coord.__str__()
self.assertEqual(expected, result)


if __name__ == '__main__':
tests.main()

0 comments on commit f532956

Please sign in to comment.