Parsing datetime to protobuf Timestamp

107 views Asked by At

I'm attempting to parse a datetime element into a protobuf Timestamp in Python since Google Cloud Optimization V1 mandates this format for all datetime fields. However, I've encountered difficulties with this and, for instance, I'm trying:

startTime=Timestamp().FromDatetime(timeWindow.startTime)

But I'm getting:

Traceback (most recent call last):
File "C:\Users\allombart\AppData\Local\pypoetry\Cache\virtualenvs\fresctrans-yzUGheRy-py3.12\Lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 408, in run_asgi
result = await app(  # type: ignore[func-returns-value]
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\allombart\AppData\Local\pypoetry\Cache\virtualenvs\fresctrans-yzUGheRy-py3.12\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 84, in __call__
return await self.app(scope, receive, send)
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\allombart\AppData\Local\pypoetry\Cache\virtualenvs\fresctrans-yzUGheRy-py3.12\Lib\site-packages\fastapi\applications.py", line 1106, in __call__
await super().__call__(scope, receive, send)
  File "C:\Users\allombart\AppData\Local\pypoetry\Cache\virtualenvs\fresctrans-yzUGheRy-py3.12\Lib\site-packages\starlette\applications.py", line 122, in __call__
await self.middleware_stack(scope, receive, send)
  File "C:\Users\allombart\AppData\Local\pypoetry\Cache\virtualenvs\fresctrans-yzUGheRy-py3.12\Lib\site-packages\starlette\middleware\errors.py", line 184, in __call__
raise exc
  File "C:\Users\allombart\AppData\Local\pypoetry\Cache\virtualenvs\fresctrans-yzUGheRy-py3.12\Lib\site-packages\starlette\middleware\errors.py", line 162, in __call__
await self.app(scope, receive, _send)
  File "C:\Users\allombart\Projectes\fresctrans\src\backframe\__init__.py", line 11, in __call__
return await self.app(scope, receive, send)
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\allombart\AppData\Local\pypoetry\Cache\virtualenvs\fresctrans-yzUGheRy-py3.12\Lib\site-packages\starlette\middleware\exceptions.py", line 79, in __call__
raise exc
  File "C:\Users\allombart\AppData\Local\pypoetry\Cache\virtualenvs\fresctrans-yzUGheRy-py3.12\Lib\site-packages\starlette\middleware\exceptions.py", line 68, in __call__
await self.app(scope, receive, sender)
  File "C:\Users\allombart\AppData\Local\pypoetry\Cache\virtualenvs\fresctrans-yzUGheRy-py3.12\Lib\site-packages\fastapi\middleware\asyncexitstack.py", line 20, in __call__
raise e
  File "C:\Users\allombart\AppData\Local\pypoetry\Cache\virtualenvs\fresctrans-yzUGheRy-py3.12\Lib\site-packages\fastapi\middleware\asyncexitstack.py", line 17, in __call__
await self.app(scope, receive, send)
  File "C:\Users\allombart\AppData\Local\pypoetry\Cache\virtualenvs\fresctrans-yzUGheRy-py3.12\Lib\site-packages\starlette\routing.py", line 718, in __call__
await route.handle(scope, receive, send)
  File "C:\Users\allombart\AppData\Local\pypoetry\Cache\virtualenvs\fresctrans-yzUGheRy-py3.12\Lib\site-packages\starlette\routing.py", line 276, in handle
await self.app(scope, receive, send)
  File "C:\Users\allombart\AppData\Local\pypoetry\Cache\virtualenvs\fresctrans-yzUGheRy-py3.12\Lib\site-packages\starlette\routing.py", line 66, in app
response = await func(request)
           ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\allombart\AppData\Local\pypoetry\Cache\virtualenvs\fresctrans-yzUGheRy-py3.12\Lib\site-packages\fastapi\routing.py", line 274, in app
raw_response = await run_endpoint_function(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\allombart\AppData\Local\pypoetry\Cache\virtualenvs\fresctrans-yzUGheRy-py3.12\Lib\site-packages\fastapi\routing.py", line 191, in run_endpoint_function
return await dependant.call(**values)
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\allombart\Projectes\fresctrans\src\fresctrans\utils.py", line 21, in wrapper
result = await fn(*args, **kwargs)
         ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\allombart\Projectes\fresctrans\src\fresctrans\ctrl\fleet.py", line 139, in calculate_routes_automatically
return await fleet_optimizer_service.daily_route_calculation()
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\allombart\Projectes\fresctrans\src\fresctrans\service\fleet\optimizer\fleet_optimizer.py", line 128, in daily_route_calculation
fromShipmentToShipmentDTO(shipment) for shipment in shipments
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\allombart\Projectes\fresctrans\src\fresctrans\service\fleet\optimizer\model.py", line 506, in fromShipmentToShipmentDTO
fromDeliveryToDeliveryDTO(delivery) for delivery in shipment.deliveries
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\allombart\Projectes\fresctrans\src\fresctrans\service\fleet\optimizer\model.py", line 453, in fromDeliveryToDeliveryDTO
startTime=Timestamp().FromDatetime(timeWindow.startTime),
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\allombart\AppData\Local\pypoetry\Cache\virtualenvs\fresctrans-yzUGheRy-py3.12\Lib\site-packages\google\protobuf\internal\well_known_types.py", line 255, in FromDatetime
self.seconds = calendar.timegm(dt.utctimetuple())
^^^^^^^^^^^^
AttributeError: 'Timestamp' object has no attribute 'seconds'

I'm trying to import Timestamp by doing:

from google.protobuf.timestamp_pb2 import Timestamp

And, even though protobuf package is properly installed in poetry, I'm getting:

    Cannot find reference 'Timestamp' in 'timestamp_pb2.py' 

So, the only way I'm able to import Timestamp is

from google.protobuf.internal.well_known_types import Timestamp

So long story short, what's the proper way to parse datetime elements into Timestamp?

1

There are 1 answers

0
ABD On

Try using pip to install protobuf again If you still cannot import try this:

file: timestamp.proto

syntax = "proto3";
package google.protobuf;

message Timestamp {
    int64 seconds = 1;
    int32 nanos = 2;
}

compile it using protoc -I=. --python_out=. timestamp.proto

then in you code

from datetime import datetime
from timestamp_pb2 import Timestamp

startTime = datetime.strptime('Tue Feb 27 2024 13:22:34.271005', '%a %b %d %Y %H:%M:%S.%f') 
# setting arbitrary start time for demo 

startTimeProto = Timestamp()
startTimeProto.FromDatetime(startTime) 
print(startTimeProto)

# seconds: 1709040154
# nanos: 271005000