From 0f0a3342a4543e803ebb0a6f313cb0ffbe4c80d2 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Fri, 31 Jul 2015 13:29:45 -0700 Subject: [PATCH] Fixes a non-deterministic failing test in bigquery. This has something to do with rounding of floats in Python 3. For example, .123 gets rounded down when converted to floating point: >>> (0.123).hex() '0x1.f7ced916872b0p-4' >>> # Add 4 for exponent and multiply .123 by 1000 >>> 2**(52 + 4) * 123 # Add 4 for exponent and multiply .123 by 1000 8863084066665136128 >>> # Account for the factor of 1000 we've multiplied .123 by >>> (0x1f7ced916872b0) * 1000 8863084066665136000 --- gcloud/bigquery/_helpers.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/gcloud/bigquery/_helpers.py b/gcloud/bigquery/_helpers.py index b19ffd3a27420..66ace56bf9884 100644 --- a/gcloud/bigquery/_helpers.py +++ b/gcloud/bigquery/_helpers.py @@ -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):