diff --git a/internal/msgfmt/format.go b/internal/msgfmt/format.go index a8a80315..6561b8b7 100644 --- a/internal/msgfmt/format.go +++ b/internal/msgfmt/format.go @@ -10,7 +10,6 @@ package msgfmt import ( "bytes" "fmt" - "math" "reflect" "sort" "strconv" @@ -130,12 +129,16 @@ func appendKnownMessage(b []byte, m protoreflect.Message) []byte { return append(b, x+"Z"...) case genid.Duration_message_fullname: + sign := "" secs := m.Get(fds.ByNumber(genid.Duration_Seconds_field_number)).Int() nanos := m.Get(fds.ByNumber(genid.Duration_Nanos_field_number)).Int() if nanos <= -1e9 || nanos >= 1e9 || (secs > 0 && nanos < 0) || (secs < 0 && nanos > 0) { return nil } - x := fmt.Sprintf("%d.%09d", secs, int64(math.Abs(float64(nanos)))) + if secs < 0 || nanos < 0 { + sign, secs, nanos = "-", -1*secs, -1*nanos + } + x := fmt.Sprintf("%s%d.%09d", sign, secs, nanos) x = strings.TrimSuffix(x, "000") x = strings.TrimSuffix(x, "000") x = strings.TrimSuffix(x, ".000") diff --git a/internal/msgfmt/format_test.go b/internal/msgfmt/format_test.go index 6136d0ed..6ba07681 100644 --- a/internal/msgfmt/format_test.go +++ b/internal/msgfmt/format_test.go @@ -199,6 +199,11 @@ func TestFormat(t *testing.T) { OptDuration: &durpb.Duration{Seconds: -1257894123, Nanos: -456789}, }, want: `{opt_duration:-1257894123.000456789s}`, + }, { + in: &textpb.KnownTypes{ + OptDuration: &durpb.Duration{Seconds: 0, Nanos: -1}, + }, + want: `{opt_duration:-0.000000001s}`, }, { in: &textpb.KnownTypes{ OptBool: &wpb.BoolValue{},