googleapis / python-bigquery

Apache License 2.0
746 stars 306 forks source link

Support julian day timestamp value conversion to datetime.datetime #1948

Closed erenkeskus closed 4 months ago

erenkeskus commented 5 months ago

I'm using an image called ghcr.io/goccy/bigquery-emulator:0.6.1 as a test container in my unit tests. However this image is using sqlite for database. I also use bigquery python client library 3.24.0. The problem is the following: sqlite is using floats to store timestamps (julian day form) whereas bigquery stores them as integers. When converting the int into datetime in _timstamp_from_json function there is a usage of int() which throws a ValueError when a string of a float is provided. The error: ValueError: invalid literal for int() with base 10

My suggestion is to change the line in _helpers.py to int(float(value)).

Linchin commented 5 months ago

Indeed, if you are interested in adding this, feel free to open a PR! Otherwise I'm happy to take care of it.

chalmerlowe commented 5 months ago

@Linchin @erenkeskus

Before we continue to review PR #1953 associated with this, I would like to better understand this issue. Can you help me?

I looked at Julian Dates and there are many formats. Depending on the expected format, the date that a float produces can vary widely (see this article: Julian Day).

_EPOCH = datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)

...

def _datetime_from_microseconds(value):
    """Convert timestamp to datetime, assuming UTC.

    :type value: float
    :param value: The timestamp to convert

    :rtype: :class:`datetime.datetime`
    :returns: The datetime object created from the value.
    """
    return _EPOCH + datetime.timedelta(microseconds=value)
chalmerlowe commented 4 months ago

FYI: I will close this issue and associated PR tomorrow (Jun 28th) unless we have further dialogue.

chalmerlowe commented 4 months ago

Closing as will not fix.