Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i create a protobuf3 Timestamp in python?

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?

like image 696
P Moran Avatar asked Mar 07 '18 21:03

P Moran


People also ask

What is Protobuf timestamp?

Reference documentation and code samples for the Kubernetes Engine V1 API class Google::Protobuf::Timestamp. A Timestamp represents a point in time independent of any time zone or local calendar, encoded as a count of seconds and fractions of seconds at nanosecond resolution.

What is Protobuf in Python?

Protocol buffers (Protobuf) are a language-agnostic data serialization format developed by Google. Protobuf is great for the following reasons: Low data volume: Protobuf makes use of a binary format, which is more compact than other formats such as JSON. Persistence: Protobuf serialization is backward-compatible.


2 Answers

Simple example:

message Data {
    google.protobuf.Timestamp tstamp = 1;
}

setting value:

message.tstamp.FromDatetime(datetime.datetime.now())

printing received message:

tstamp {
    seconds: 1648070801
    nanos: 661005000
}
like image 168
Marcin Avatar answered Oct 02 '22 17:10

Marcin


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
like image 28
P Moran Avatar answered Oct 02 '22 16:10

P Moran