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 Jan 15, 2018
1 parent a25d32e commit c8c60e0
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 @@ -732,7 +732,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.has_bounds():
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 @@ -323,5 +323,29 @@ def test_convert_unknown_units(self):
with self.assertRaisesRegexp(UnitConversionError, emsg):
coord.convert_units('degrees')

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 c8c60e0

Please sign in to comment.