Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert datetime to protobuf Timestamp in Python

So I'm trying to prepare a message with Python that takes a Timestamp, but I'm having trouble converting a datetime to a protobuf Timestamp.

Here's what I've tried so far:

from google.protobuf.timestamp_pb2 import Timestamp
import datetime
now = datetime.datetime.now()
timestamp = Timestamp()
timestamp.FromDatetime(now)

However, I'm getting an error AttributeError: 'Timestamp' object attribute 'seconds' is read-only

How can I create a Timestamp from a datetime?

like image 228
Andrew Avatar asked Jun 12 '26 00:06

Andrew


1 Answers

This code is working fine on my machine

from google.protobuf.timestamp_pb2 import Timestamp
import datetime
now = datetime.datetime.now()
timestamp = Timestamp()
timestamp.FromDatetime(now)

Output:

seconds: 1591859232
nanos: 803377000
like image 141
Damnik Jain Avatar answered Jun 14 '26 13:06

Damnik Jain