From f64e6769c5ae8bcc9396c9c5495b21422ae45daf Mon Sep 17 00:00:00 2001 From: David Stansby Date: Wed, 7 Jun 2017 18:02:31 +0100 Subject: [PATCH 1/2] Allow timedelta to be converted to a ordinalf --- lib/matplotlib/dates.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/dates.py b/lib/matplotlib/dates.py index bd27bef3759a..ac79c441ab9a 100644 --- a/lib/matplotlib/dates.py +++ b/lib/matplotlib/dates.py @@ -207,9 +207,9 @@ def _get_rc_timezone(): def _to_ordinalf(dt): """ - Convert :mod:`datetime` or :mod:`date` to the Gregorian date as UTC float - days, preserving hours, minutes, seconds and microseconds. Return value - is a :func:`float`. + Convert :mod:`datetime`, :mod:`date` or :mod:`timedelta` to the Gregorian + date as UTC float days, preserving hours, minutes, seconds and + microseconds. Return value is a :func:`float`. """ # Convert to UTC tzi = getattr(dt, 'tzinfo', None) @@ -217,6 +217,9 @@ def _to_ordinalf(dt): dt = dt.astimezone(UTC) tzi = UTC + if isinstance(dt, datetime.timedelta): + return dt.total_seconds() / SEC_PER_DAY + base = float(dt.toordinal()) # If it's sufficiently datetime-like, it will have a `date()` method From 2fafd126bf28ce8e6d61e71e32cf92080b460fd0 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Thu, 8 Jun 2017 10:45:07 +0100 Subject: [PATCH 2/2] Add test for timedelta --> ordinalf conversion --- lib/matplotlib/tests/test_dates.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/matplotlib/tests/test_dates.py b/lib/matplotlib/tests/test_dates.py index 5a25e6182b7e..003789951598 100644 --- a/lib/matplotlib/tests/test_dates.py +++ b/lib/matplotlib/tests/test_dates.py @@ -466,3 +466,10 @@ def test_tz_utc(): def test_num2timedelta(x, tdelta): dt = mdates.num2timedelta(x) assert dt == tdelta + + +def test_timedelta_ordinalf(): + # Check that timedeltas can be converted to ordinalfs + dt = datetime.timedelta(seconds=60) + ordinalf = mdates._to_ordinalf(dt) + assert ordinalf == 1 / (24 * 60)