vegabook

vegabook

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.

Marked As Solved Switch mode

Also Liked

vegabook

vegabook

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:

Last Post!

vegabook

vegabook

So this seems to have changed: GitHub - elixir-protobuf/google-protos: Elixir files generated from Google's protobuf files using protobuf-elixir · GitHub 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: }

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement