Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix conversion of backend-generated timestamps #1128

Merged
merged 4 commits into from
Sep 11, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions gcloud/bigquery/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import six

from gcloud._helpers import UTC
from gcloud._helpers import _datetime_from_microseconds
from gcloud._helpers import _millis_from_datetime
from gcloud.exceptions import NotFound
Expand Down Expand Up @@ -411,7 +412,7 @@ def create(self, client=None):
"""API call: create the dataset via a PUT request

See:
https://cloud.google.com/bigquery/reference/rest/v2/tables/insert
https://cloud.google.com/bigquery/docs/reference/v2/tables/insert

:type client: :class:`gcloud.bigquery.client.Client` or ``NoneType``
:param client: the client to use. If not passed, falls back to the
Expand Down Expand Up @@ -552,7 +553,7 @@ def delete(self, client=None):
"""API call: delete the table via a DELETE request

See:
https://cloud.google.com/bigquery/reference/rest/v2/tables/delete
https://cloud.google.com/bigquery/docs/reference/v2/tables/delete

:type client: :class:`gcloud.bigquery.client.Client` or ``NoneType``
:param client: the client to use. If not passed, falls back to the
Expand All @@ -565,7 +566,7 @@ def fetch_data(self, max_results=None, page_token=None, client=None):
"""API call: fetch the table data via a GET request

See:
https://cloud.google.com/bigquery/reference/rest/v2/tabledata/list
https://cloud.google.com/bigquery/docs/reference/v2/tabledata/list

.. note::

Expand Down Expand Up @@ -631,7 +632,7 @@ def insert_data(self,
"""API call: insert table data via a POST request

See:
https://cloud.google.com/bigquery/reference/rest/v2/tabledata/insertAll
https://cloud.google.com/bigquery/docs/reference/v2/tabledata/insertAll

:type rows: list of tuples
:param rows: row data to be inserted
Expand Down Expand Up @@ -761,8 +762,9 @@ def _bool_from_json(value, field):

def _datetime_from_json(value, field):
if _not_null(value, field):
# Field value will be in milliseconds.
return _datetime_from_microseconds(1000.0 * float(value))
# value will be a float in seconds, to microsecond precision, in UTC.
stamp = datetime.datetime.utcfromtimestamp(float(value))

This comment was marked as spam.

This comment was marked as spam.

return stamp.replace(tzinfo=UTC)

This comment was marked as spam.



def _record_from_json(value, field):
Expand Down
13 changes: 9 additions & 4 deletions gcloud/bigquery/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,6 @@ def test_fetch_data_w_bound_client(self):
import datetime
from gcloud._helpers import UTC
from gcloud.bigquery.table import SchemaField
from gcloud._helpers import _millis_from_datetime

PATH = 'projects/%s/datasets/%s/tables/%s/data' % (
self.PROJECT, self.DS_NAME, self.TABLE_NAME)
Expand All @@ -835,24 +834,29 @@ def test_fetch_data_w_bound_client(self):
WHEN_2 = WHEN + datetime.timedelta(seconds=2)
ROWS = 1234
TOKEN = 'TOKEN'

def _bigquery_timestamp_float_repr(ts_float):
# Preserve microsecond precision for E+09 timestamps
return '%0.15E' % (ts_float,)

DATA = {
'totalRows': ROWS,
'pageToken': TOKEN,
'rows': [
{'f': [
{'v': 'Phred Phlyntstone'},
{'v': '32'},
{'v': _millis_from_datetime(WHEN)},
{'v': _bigquery_timestamp_float_repr(WHEN_TS)},
]},
{'f': [
{'v': 'Bharney Rhubble'},
{'v': '33'},
{'v': _millis_from_datetime(WHEN_1)},
{'v': _bigquery_timestamp_float_repr(WHEN_TS + 1)},
]},
{'f': [
{'v': 'Wylma Phlyntstone'},
{'v': '29'},