Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def timestamp_to_py(self, value, field) -> Union[datetime.datetime, str, None]:
return value
if _not_null(value, field):
# value will be a integer in seconds, to microsecond precision, in UTC.
return _datetime_from_microseconds(int(value))
return _datetime_from_microseconds(int(float(value)))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Directly casting all values to float before converting to int can lead to precision loss for very large integer values (specifically, integers greater than 2^53 - 1, which corresponds to timestamps beyond the year 2285). Since BigQuery supports timestamps up to the year 9999, we should attempt to parse the value as an integer first to preserve full precision, and only fall back to float parsing if a ValueError occurs (e.g., when scientific notation or a decimal point is present).

            try:
                microseconds = int(value)
            except ValueError:
                microseconds = int(float(value))
            return _datetime_from_microseconds(microseconds)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

return None

def datetime_to_py(self, value, field):
Expand Down
Loading