Skip to content

Commit 742bd08

Browse files
committed
Fixes a non-deterministic failing test in bigquery.
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
1 parent 2928bfc commit 742bd08

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

gcloud/bigquery/_helpers.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ def _datetime_from_prop(value):
4242
"""
4343
if value is not None:
4444
# back-end returns timestamps as milliseconds since the epoch
45-
value = datetime.datetime.utcfromtimestamp(value / 1000.0)
46-
return value.replace(tzinfo=pytz.utc)
45+
seconds = int(value / 1000.0)
46+
microseconds = 1000.0 * (value - 1000 * seconds)
47+
return (
48+
_EPOCH +
49+
datetime.timedelta(seconds=seconds, microseconds=microseconds)
50+
)
4751

4852

4953
def _prop_from_datetime(value):

0 commit comments

Comments
 (0)