Skip to content

Commit

Permalink
Updating Bigquery inserted timestamps to use seconds.
Browse files Browse the repository at this point in the history
As stated in the docs
> BigQuery stores TIMESTAMP data internally as a
> UNIX timestamp with microsecond precision
> ...Specifies the number of seconds since the epoch.
  • Loading branch information
dhermes committed Nov 30, 2015
1 parent 4042086 commit e04ff5e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 6 additions & 2 deletions gcloud/bigquery/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import six

from gcloud._helpers import _datetime_from_microseconds
from gcloud._helpers import _microseconds_from_datetime
from gcloud._helpers import _millis_from_datetime
from gcloud.exceptions import NotFound
from gcloud.bigquery._helpers import _rows_from_json
Expand Down Expand Up @@ -654,8 +655,11 @@ def insert_data(self,
row_info = {}

for field, value in zip(self._schema, row):
if field.field_type == 'TIMESTAMP':
value = _millis_from_datetime(value)
if field.field_type == 'TIMESTAMP' and value is not None:
# BigQuery stores TIMESTAMP data internally as a
# UNIX timestamp with microsecond precision.
# Specifies the number of seconds since the epoch.
value = _microseconds_from_datetime(value) * 1e-6
row_info[field.name] = value

info = {'json': row_info}
Expand Down
7 changes: 5 additions & 2 deletions gcloud/bigquery/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ def test_fetch_data_w_record_schema(self):
def test_insert_data_w_bound_client(self):
import datetime
from gcloud._helpers import UTC
from gcloud._helpers import _millis_from_datetime
from gcloud._helpers import _microseconds_from_datetime
from gcloud.bigquery.table import SchemaField

WHEN_TS = 1437767599.006
Expand All @@ -1084,9 +1084,12 @@ def test_insert_data_w_bound_client(self):
]

def _row_data(row):
joined = None
if row[2] is not None:
joined = _microseconds_from_datetime(row[2]) * 1e-6
return {'full_name': row[0],
'age': row[1],
'joined': _millis_from_datetime(row[2])}
'joined': joined}

SENT = {
'rows': [{'json': _row_data(row)} for row in ROWS],
Expand Down

0 comments on commit e04ff5e

Please sign in to comment.