Skip to content

Commit 8d81b63

Browse files
committed
Fix encoder for new test.
1 parent aebf31e commit 8d81b63

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

plotly/utils.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import threading
1313
import re
1414
import datetime
15+
import pytz
1516

1617
try:
1718
import numpy
@@ -93,11 +94,18 @@ def ensure_dir_exists(directory):
9394

9495
def iso_to_plotly_time_string(iso_string):
9596
"""Remove timezone info and replace 'T' delimeter with ' ' (ws)."""
96-
no_tz_string = '-'.join(iso_string.split('-')[:3])
97-
if no_tz_string.endswith('T00:00:00'):
98-
return no_tz_string.replace('T00:00:00', '')
97+
# make sure we don't send timezone info to plotly
98+
if (iso_string.split('-')[:3] is '00:00') or\
99+
(iso_string.split('+')[0] is '00:00'):
100+
raise Exception("Plotly won't accept timestrings with timezone info.\n"
101+
"All timestrings are assumed to be in UTC.")
102+
103+
iso_string = iso_string.replace('-00:00', '').replace('+00:00', '')
104+
105+
if iso_string.endswith('T00:00:00'):
106+
return iso_string.replace('T00:00:00', '')
99107
else:
100-
return no_tz_string.replace('T', ' ')
108+
return iso_string.replace('T', ' ')
101109

102110

103111
### Custom JSON encoders ###
@@ -215,9 +223,22 @@ def encode_as_numpy(obj):
215223
@staticmethod
216224
def encode_as_datetime(obj):
217225
"""Attempt to convert to utc-iso time string using datetime methods."""
226+
227+
# first we need to get this into utc
228+
try:
229+
obj = obj.astimezone(pytz.utc)
230+
except ValueError:
231+
# we'll get a value error if trying to convert with naive datetime
232+
pass
233+
except TypeError:
234+
# pandas throws a typeerror here instead of a value error, it's OK
235+
pass
236+
except AttributeError:
237+
# we'll get an attribute error if astimezone DNE
238+
raise NotEncodable
239+
240+
# now we need to get a nicely formatted time string
218241
try:
219-
if obj.utcoffset():
220-
obj = obj - obj.utcoffset()
221242
time_string = obj.isoformat()
222243
except AttributeError:
223244
raise NotEncodable

0 commit comments

Comments
 (0)