From 1a05c43d0cb852d79d6e92a1fbf2944a20c15f24 Mon Sep 17 00:00:00 2001 From: Jon Mease Date: Mon, 27 May 2019 08:08:23 -0400 Subject: [PATCH 1/2] Added timezones future flag --- _plotly_future_/timezones.py | 5 +++++ _plotly_future_/v4.py | 1 + 2 files changed, 6 insertions(+) create mode 100644 _plotly_future_/timezones.py diff --git a/_plotly_future_/timezones.py b/_plotly_future_/timezones.py new file mode 100644 index 00000000000..e6437b4a232 --- /dev/null +++ b/_plotly_future_/timezones.py @@ -0,0 +1,5 @@ +from __future__ import absolute_import +from _plotly_future_ import _future_flags, _assert_plotly_not_imported + +_assert_plotly_not_imported() +_future_flags.add('timezones') diff --git a/_plotly_future_/v4.py b/_plotly_future_/v4.py index a4acb8cf132..e6bdb567bff 100644 --- a/_plotly_future_/v4.py +++ b/_plotly_future_/v4.py @@ -6,5 +6,6 @@ remove_deprecations, v4_subplots, orca_defaults, + timezones, ) From 7a4006b4cbeb9e99f24fbccb718a1641d1af6333 Mon Sep 17 00:00:00 2001 From: Jon Mease Date: Mon, 27 May 2019 08:23:29 -0400 Subject: [PATCH 2/2] Don't convert datetimes to UTC or strip timezone info during JSON encoding --- _plotly_utils/utils.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/_plotly_utils/utils.py b/_plotly_utils/utils.py index 94d37a8b81b..10ae668946e 100644 --- a/_plotly_utils/utils.py +++ b/_plotly_utils/utils.py @@ -3,8 +3,8 @@ import json as _json import sys import re - import pytz +from _plotly_future_ import _future_flags from _plotly_utils.optional_imports import get_module @@ -104,7 +104,9 @@ def default(self, obj): self.encode_as_sage, self.encode_as_numpy, self.encode_as_pandas, - self.encode_as_datetime, + (self.encode_as_datetime_v4 + if 'timezones' in _future_flags + else self.encode_as_datetime), self.encode_as_date, self.encode_as_list, # because some values have `tolist` do last. self.encode_as_decimal @@ -170,6 +172,14 @@ def encode_as_numpy(obj): else: raise NotEncodable + @staticmethod + def encode_as_datetime_v4(obj): + """Convert datetime objects to iso-format strings""" + try: + return obj.isoformat() + except AttributeError: + raise NotEncodable + @staticmethod def encode_as_datetime(obj): """Attempt to convert to utc-iso time string using datetime methods."""