How to use Google.Protobuf.Timestamp with elixir-grpc

I’m using elixir-grpc to write a client for my Bloomberg API gRPC implementation. This is working quite well in Python, and I now want to bring the Bloomberg API to Elixir. Naturally timestamps are going to be necessary, but I don’t know how to use them in Elixir.

Elixir-grpc says it uses the protobuf library which in turn says that “since :protobuf version 0.14.0 we include all of the well known Google Protobuf modules” (which is strange since their latest release is 0.13.0).

Anyway I’m getting errors with Google.Protobuf.Timestamp. Here is my proto file that uses timestamps, and this successfully compiles to elixir code using protoc --elixir_out=plugins=grpc:../lib *.proto. You can see the output here, but when I try to do a simple ping/pong to the server, there’s a massive error which mentions that Google.Protobuf.Timestamp is not available:

So in Python I would bring Google.Protobuf.Timestamp (and Struct and Empty) in like this:

from google.protobuf.struct_pb2 import Struct
from google.protobuf.timestamp_pb2 import Timestamp as protoTimestamp
from google.protobuf import empty_pb2

and then I could use them, but I don’t know how to do this in Elixir. Should I be generating my own code from the canonical Google.Protobuf.Timestamp proto files? This seems strange as I would think either elixir-grpc or protobuf would have done that already.

Adding this to your dependency should fix your issue GitHub - elixir-protobuf/google-protos: Elixir files generated from Google's protobuf files using protobuf-elixir

3 Likes

fabulous!

I’ll let grpc-elixir maintainers that this should be in their docs.

Goodbye python, java, c++, behold Bloomberg API data, now in Elixir:

4 Likes

So this seems to have changed: GitHub - elixir-protobuf/google-protos: Elixir files generated from Google's protobuf files using protobuf-elixir and the author of google-protos is recommending just using the main protobuf library instead, which I am doing.

But this doesn’t seem to be recognising my timestamps. Here is my IntradayBarRequest protobuf definition:

message IntradayBarRequest {
string topic = 2; // securities
google.protobuf.Timestamp start = 3; // startDate
google.protobuf.Timestamp end = 4; // endDate
int32 interval = 5; // interval in minutes
map <string, string> options = 6;
}

But when I try to create timestamps, they’re not accepted:

iex(25)> tpc
%Bloomberg.IntradayBarRequest{
topic: “USDZAR Curncy”,
start: ~U[2025-09-01 13:00:00Z],
end: ~U[2025-10-30 18:12:22.878335Z],
interval: 1,
options: %{},
unknown_fields:
}
iex(26)> Bloomberg.Bbg.Stub.intraday_bar_request(chan, tpc)
** (Protobuf.EncodeError) Got error when encoding Bloomberg.IntradayBarRequest#start: ** (Protobuf.EncodeError) struct DateTime can’t be encoded as Google.Protobuf.Timestamp: ~U[2025-09-01 13:00:00Z]
(grpc 0.11.3) lib/grpc/telemetry.ex:125: anonymous fn/2 in GRPC.Telemetry.client_span/3
(telemetry 1.3.0) /home/tbrowne/code/suprabonds/deps/telemetry/src/telemetry.erl:324: :telemetry.span/3
(grpc 0.11.3) lib/grpc/telemetry.ex:118: GRPC.Telemetry.client_span/3
iex:26: (file)
[error] ** (Protobuf.EncodeError) Got error when encoding Bloomberg.IntradayBarRequest#start: ** (Protobuf.EncodeError) struct DateTime can’t be encoded as Google.Protobuf.Timestamp: ~U[2025-09-01 13:00:00Z]
(grpc 0.11.3) lib/grpc/telemetry.ex:125: anonymous fn/2 in GRPC.Telemetry.client_span/3
(telemetry 1.3.0) /home/tbrowne/code/suprabonds/deps/telemetry/src/telemetry.erl:324: :telemetry.span/3
(grpc 0.11.3) lib/grpc/telemetry.ex:118: GRPC.Telemetry.client_span/3
(elixir 1.19.1) elixir.erl:365: :elixir.eval_external_handler/3
(stdlib 7.1) erl_eval.erl:924: :erl_eval.do_apply/7
(elixir 1.19.1) elixir.erl:343: :elixir.eval_forms/4
(elixir 1.19.1) lib/module/parallel_checker.ex:152: Module.ParallelChecker.verify/1
(iex 1.19.1) lib/iex/evaluator.ex:349: IEx.Evaluator.eval_and_inspect/3
(iex 1.19.1) lib/iex/evaluator.ex:321: IEx.Evaluator.safe_eval_and_inspect/3
(iex 1.19.1) lib/iex/evaluator.ex:212: IEx.Evaluator.loop/1
(iex 1.19.1) lib/iex/evaluator.ex:38: IEx.Evaluator.init/5
(stdlib 7.1) proc_lib.erl:333: :proc_lib.init_p_do_apply/3

Different error if I try to convert those timestamps to_unix:

iex(26)> tpc = %Bloomberg.IntradayBarRequest{topic: “USDZAR Curncy”, interval: 1, start: DateTime.new!(~D[2025-09-01], ~T[13:00:00], “Etc/UTC”) |> DateTime.to_unix, end: DateTime.utc_now() |> DateTime.to_unix()}
%Bloomberg.IntradayBarRequest{
topic: “USDZAR Curncy”,
start: 1756731600,
end: 1761848742,
interval: 1,
options: %{},
unknown_fields:
}
iex(27)> Bloomberg.Bbg.Stub.intraday_bar_request(chan, tpc)
** (Protobuf.EncodeError) Got error when encoding Bloomberg.IntradayBarRequest#start: ** (Protocol.UndefinedError) protocol Enumerable not implemented for Integer. This protocol is implemented for: DBConnection.PrepareStream, DBConnection.Stream, Date.Range, Ecto.Adapters.SQL.Stream, Explorer.Series.Iterator, File.Stream, Flow, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, Jason.OrderedObject, List, Map, MapSet, Phoenix.LiveView.LiveStream, Postgrex.Stream, Range, SFTPClient.Stream, Stream, Table.Mapper, Table.Zipper

Any info on how to get Elixir dates/times into Google protobuf timestamps>

EDIT: figured it out:

iex(27)> %Google.Protobuf.Timestamp{seconds: 1, nanos: 1}
%Google.Protobuf.Timestamp{seconds: 1, nanos: 1, unknown_fields: }