Skip to content

Commit

Permalink
Fixes a non-deterministic failing test in bigquery.
Browse files Browse the repository at this point in the history
This has something to do with rounding of floats (and different
behavior between Python 2 and Python 3). For example, `.123` gets
"rounded down" when expressed as floating point:

>>> (0.123).hex()
'0x1.f7ced916872b0p-4'
>>> # Add 4 for exponent and multiply .123 by 1000
>>> 2**(52 + 4) * 123
8863084066665136128
>>> # Account for the factor of 1000 we've multiplied .123 by
>>> (0x1f7ced916872b0) * 1000
8863084066665136000
  • Loading branch information
dhermes committed Jul 31, 2015
1 parent 2928bfc commit 742bd08
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions gcloud/bigquery/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ def _datetime_from_prop(value):
"""
if value is not None:
# back-end returns timestamps as milliseconds since the epoch
value = datetime.datetime.utcfromtimestamp(value / 1000.0)
return value.replace(tzinfo=pytz.utc)
seconds = int(value / 1000.0)
microseconds = 1000.0 * (value - 1000 * seconds)
return (
_EPOCH +
datetime.timedelta(seconds=seconds, microseconds=microseconds)
)


def _prop_from_datetime(value):
Expand Down

0 comments on commit 742bd08

Please sign in to comment.