With reference to The Protocol Buffer API section:
Unlike when you generate Java and C++ protocol buffer code, the Python protocol buffer compiler doesn't generate your data access code for you directly. Instead, it generates special descriptors for all your messages, enums, and fields, and some mysteriously empty classes, one for each message type...
and,
At load time, the GeneratedProtocolMessageType
metaclass uses the specified descriptors to create all the Python methods you need to work with each message type and adds them to the relevant classes. You can then use the fully-populated classes in your code.
So, you can use the generated class(s) to create the object(s) and their fields like this:
p1 = primitive_types_pb2.PrimitiveType()
p1.int_value = 1234
For your use-case, you can use timestamp_pb2.Timestamp.GetCurrentTime().
Alternatively, you can refer to Timestamp along with timestamp_pb2.Timestamp.CopyFrom():
now = time.time()
seconds = int(now)
nanos = int((now - seconds) * 10**9)
timestamp = Timestamp(seconds=seconds, nanos=nanos)
p1 = primitive_types_pb2.PrimitiveType()
p1.timestamp_value.CopyFrom( timestamp )
There are other google.protobuf.timestamp_pb2 APIs that you might be interested in for your other use-cases.
Here's a complete working example (primitive_types.proto
):
import time # For Timestamp.CopyFrom(). See commented code below
import primitive_types_pb2
from google.protobuf import timestamp_pb2
# serialization
p1 = primitive_types_pb2.PrimitiveType()
# Alternative to GetCurrentTime()
# now = time.time()
# seconds = int( now )
# nanos = int( (now - seconds) * 10**9 )
# timestamp = timestamp_pb2.Timestamp( seconds=seconds, nanos=nanos )
# p1.timestamp_value.CopyFrom( timestamp )
p1.timestamp_value.GetCurrentTime()
serialized = p1.SerializeToString()
# deserialization
p2 = primitive_types_pb2.PrimitiveType()
p2.ParseFromString( serialized )
print( p2.timestamp_value )
Output:
seconds: 1590581054
nanos: 648958000
References: