From e56302e96b19659312b85f6bb3b417c7efc344cf Mon Sep 17 00:00:00 2001 From: Jose Luis Franco Arza Date: Fri, 15 Mar 2024 11:45:52 +0100 Subject: [PATCH] Support last_update_time in nanoseconds It was observed that in old versions of weaviate, the last_update_time field is being retrieved in nanoseconds which causes a ValueError exception. fixes: gh-958 --- weaviate/collections/queries/base.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/weaviate/collections/queries/base.py b/weaviate/collections/queries/base.py index 7ebf6a5cc..26f165d10 100644 --- a/weaviate/collections/queries/base.py +++ b/weaviate/collections/queries/base.py @@ -94,6 +94,16 @@ def __init__( validate_arguments=self._validate_arguments, ) + def __retrieve_timestamp( + self, + timestamp: int, + ) -> datetime.datetime: + # Handle the case in which last_update_time_unix is in nanoseconds or milliseconds, issue #958 + if len(str(timestamp)) <= 13: + return datetime.datetime.fromtimestamp(timestamp / 1000, tz=datetime.timezone.utc) + else: + return datetime.datetime.fromtimestamp(timestamp / 1e9, tz=datetime.timezone.utc) + def __extract_metadata_for_object( self, add_props: "search_get_pb2.MetadataResult", @@ -102,16 +112,12 @@ def __extract_metadata_for_object( distance=add_props.distance if add_props.distance_present else None, certainty=add_props.certainty if add_props.certainty_present else None, creation_time=( - datetime.datetime.fromtimestamp( - add_props.creation_time_unix / 1000, tz=datetime.timezone.utc - ) + self.__retrieve_timestamp(add_props.creation_time_unix) if add_props.creation_time_unix_present else None ), last_update_time=( - datetime.datetime.fromtimestamp( - add_props.last_update_time_unix / 1000, tz=datetime.timezone.utc - ) + self.__retrieve_timestamp(add_props.last_update_time_unix) if add_props.last_update_time_unix_present else None ),