From 4b8420100527b75d483a17e78a4a8cf157b6b50b Mon Sep 17 00:00:00 2001 From: v0idpwn Date: Fri, 25 Apr 2025 19:21:14 -0300 Subject: [PATCH] Fix formula for converting Duration into google.protobuf.Duration --- lib/google/protobuf.ex | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/google/protobuf.ex b/lib/google/protobuf.ex index 7e18acdb..bb9bbbba 100644 --- a/lib/google/protobuf.ex +++ b/lib/google/protobuf.ex @@ -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) struct(Google.Protobuf.Duration, %{ - seconds: duration.second, - nanos: microsecond * 1_000 + seconds: seconds, + nanos: rem(microsecond, 1_000_000) * 1_000 }) end end