utils: fix Epoch timestamp parsing
Timestamp in Epoch format (floats) parsing is broken when the values passed are
strings that could be converted to floats and not floats directly.
Change-Id: I5876f4aff065dee9894b6750e9cdbc467248e3b6
(cherry-picked from 6769d3403b
)
This commit is contained in:
parent
0acbae3078
commit
0f73c75a94
|
@ -211,6 +211,15 @@ tests:
|
|||
- name: get valid metric id again 4
|
||||
GET: /v1/metric
|
||||
|
||||
- name: get measurements by start with epoch
|
||||
GET: /v1/metric/$RESPONSE['$[0].id']/measures?refresh=true&start=1425652440
|
||||
response_json_paths:
|
||||
$:
|
||||
- ["2015-03-06T14:34:12+00:00", 1.0, 12.0]
|
||||
|
||||
- name: get valid metric id again 4.5
|
||||
GET: /v1/metric
|
||||
|
||||
- name: get measurements from metric
|
||||
GET: /v1/metric/$RESPONSE['$[0].id']/measures?refresh=true
|
||||
response_json_paths:
|
||||
|
|
|
@ -39,3 +39,21 @@ class TestUtils(tests_base.TestCase):
|
|||
dt = datetime.datetime(2015, 1, 1, 15, 0,
|
||||
tzinfo=iso8601.iso8601.FixedOffset(5, 0, '+5h'))
|
||||
self._do_test_datetime_to_unix_timezone_change(1420106400.0, dt)
|
||||
|
||||
def test_to_timestamps_epoch(self):
|
||||
self.assertEqual(
|
||||
utils.to_datetime("1425652440"),
|
||||
datetime.datetime(2015, 3, 6, 14, 34,
|
||||
tzinfo=iso8601.iso8601.UTC))
|
||||
self.assertEqual(
|
||||
utils.to_datetime("1425652440.4"),
|
||||
datetime.datetime(2015, 3, 6, 14, 34, 0, 400000,
|
||||
tzinfo=iso8601.iso8601.UTC))
|
||||
self.assertEqual(
|
||||
utils.to_datetime(1425652440),
|
||||
datetime.datetime(2015, 3, 6, 14, 34,
|
||||
tzinfo=iso8601.iso8601.UTC))
|
||||
self.assertEqual(
|
||||
utils.to_datetime(utils.to_timestamp(1425652440.4)),
|
||||
datetime.datetime(2015, 3, 6, 14, 34, 0, 400000,
|
||||
tzinfo=iso8601.iso8601.UTC))
|
||||
|
|
|
@ -103,7 +103,13 @@ def to_timestamps(values):
|
|||
is_valid_timestamp(values[0])):
|
||||
times = pd.to_datetime(values, utc=True, box=False)
|
||||
else:
|
||||
times = (utcnow() + pd.to_timedelta(values)).values
|
||||
try:
|
||||
float(values[0])
|
||||
except ValueError:
|
||||
times = (utcnow() + pd.to_timedelta(values)).values
|
||||
else:
|
||||
times = pd.to_datetime(list(map(float, values)),
|
||||
utc=True, box=False, unit='s')
|
||||
except ValueError:
|
||||
raise ValueError("Unable to convert timestamps")
|
||||
|
||||
|
|
Loading…
Reference in New Issue