I see that there is a Timestamp object but it doesn't seem to work. Using Python 3.6
$ pip install protobuf3
In python:
from google.protobuf.timestamp_pb2 import Timestamp
timestamp = Timestamp()
timestamp.GetCurrentTime()
Nothing is returned. What am I doing wrong?
ARRGGG I think there was a virtual env problem. It works now!
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
>>> from google.protobuf.timestamp_pb2 import Timestamp
>>> timestamp = Timestamp()
>>> timestamp.GetCurrentTime()
>>> print(timestamp)
seconds: 1521497361
nanos: 600455000
>>>timestamp
seconds: 1521497361
nanos: 600455000
The only two args of constructing a protobuf Timestamp are seconds and nanos. They are defined as the seconds from the Epoch (1970-01-01 00:00:00). So we can do:
import datetime
from google.protobuf.timestamp_pb2 import Timestamp
t = datetime.datetime.now().timestamp()
seconds = int(t)
nanos = int(t % 1 * 1e9)
proto_timestamp = Timestamp(seconds=seconds, nanos=nanos)