Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/google/protobuf.ex
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,22 @@ defmodule Google.Protobuf do

iex> from_duration(Duration.new!(second: 1, microsecond: {500_000, 6}))
%Google.Protobuf.Duration{seconds: 1, nanos: 500_000_000}

iex> from_duration(Duration.new!(hour: 1, minute: 2, microsecond: {500_000, 6}))
%Google.Protobuf.Duration{seconds: 3720, nanos: 500_000_000}

iex> from_duration(Duration.new!(minute: 2, microsecond: {6_500_000, 6}))
%Google.Protobuf.Duration{seconds: 126, nanos: 500_000_000}
"""
@doc since: "0.15.0"
@spec from_duration(Duration.t()) :: Google.Protobuf.Duration.t()
def from_duration(%Duration{} = duration) do
{microsecond, _precision} = duration.microsecond
seconds = div(to_timeout(duration), 1000)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expected elixir to have a better API for that, but I didn't find one 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the correct implementation. duration.microsecond is of type Calendar.microsecond/0, and the second element of the tuple should be used in the calculation?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a couple weeks ago: elixir-lang/elixir@dd608c9

Copy link
Collaborator Author

@v0idpwn v0idpwn Apr 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL :)
I thought it was only info about how many digits of precision we could expect.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL ... I needed that extra context around precision as well, I was YOLO it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, @whatyouhide, per our discussion, this is actually correct, right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expected elixir to have a better API for that, but I didn't find one 🤔

@v0idpwn you can use System.convert_time_unit/3.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't mean the div/2 but the to_timeout. I would like to have Duration.to_unit(duration, :second)


struct(Google.Protobuf.Duration, %{
seconds: duration.second,
nanos: microsecond * 1_000
seconds: seconds,
nanos: rem(microsecond, 1_000_000) * 1_000
})
end
end
Expand Down
Loading