hackersleepyhead

hackersleepyhead

Protobuf NaiveDateTime type

Hello,

what would be the best type for proto3 to handle NaiveDateTime from Elixir types.

message PostResponse {
    string content = 1;
    string id = 2;
    google.protobuf.Timestamp inserted_at = 3;
    google.protobuf.Timestamp updated_at = 4;
    string user_id = 5;
}

I think Timestamp is not the best one, I want to have the NativeDateTime type value as it is.
any help would be appreciated.

First Post!

dimitarvp

dimitarvp

Well, NaiveDateTime is just a struct / map after all:

iex(1)> NaiveDateTime.now() |> Map.keys()
[:__struct__, :calendar, :day, :hour, :microsecond, :minute, :month, :second,
 :year]

The calendar field is usually the module/atom Calendar.ISO so you can strip it if you don’t deal with other calendars, and the special __struct__ field, and then end up with this:

iex(2)> NaiveDateTime.utc_now() |> Map.from_struct() |> Map.drop([:__struct__, :calendar])
%{
  day: 10,
  hour: 12,
  microsecond: {981284, 6},
  minute: 32,
  month: 3,
  second: 40,
  year: 2021
}

You can then also remove the decimal precision value from microsecond by using elem(..., 0) and then you’ll just have seven unsigned 32-bit integers (which you can encode with int32 since it uses variable-length encoding over the wire). That resulting map of seven integers can then very easily be described as a nested Protobuf structure afterwards.

Of course, you can also convert the NaiveDateTime to UNIX seconds / milliseconds / microseconds / nanoseconds and just encode the whole thing as a 64-bit unsigned integer:

naive_date_time_from_another_function
|> DateTime.from_naive!("Etc/UTC") # assuming your times are in UTC
|> DateTime.to_unix(:microsecond) # or :second, or :millisecond, or :nanosecond)

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement