#1433·protobuf

Unable to marshal Timestamp to RFC3339 date

Author: kmpmCreated Apr 13, 2022Updated Oct 7, 2025

What version of protobuf and what language are you using?

protoc
libprotoc 3.20.0

google.golang.org/protobuf/cmd/protoc-gen-go@latest

go version
go version go1.18 windows/amd64

What did you do? If possible, provide a recipe for reproducing the error. A complete runnable program is good with .proto and .go source code.

proto

syntax = "proto3";
package pb;
option go_package = "github.com/fake/fake/pb";

message Test {
    google.protobuf.Timestamp ts = 1;
}
go
package main

import (
	"encoding/json"
	"fmt"
	"time"

	"github.com/fake/fake/pb"
	"google.golang.org/protobuf/types/known/timestamppb"
)

func main() {
	t := time.Date(2022, 4, 13, 14, 54, 5, 323, time.UTC)
	want, _ := t.MarshalJSON()
	fmt.Printf("want: %s\n", want)
	test := pb.Test{Ts: timestamppb.New(t)}
	got, _ := json.Marshal(test)
	fmt.Printf("got: %s\n", got)
}

What did you expect to see? want: "2022-04-13T14:54:05.000000323Z" got: {"ts":"2022-04-13T14:54:05.000000323Z"}

What did you see instead? want: "2022-04-13T14:54:05.000000323Z" got: {"ts":{"seconds":1649861645,"nanos":323}}

Make sure you include information that can help us debug (full error message, exception listing, stack trace, logs).

Anything else we should know about your project / environment? https://developers.google.com/protocol-buffers/docs/proto3#json as well as the code in google.golang.org/[email protected]/types/known/timestamppb/tamestamp.pb.go says that ... In JSON format, the Timestamp type is encoded as a string in the RFC3339 That is, the format is "{year}-{month}-{day}T{hour}:{min}:{sec}[.{frac_sec}]Z

This is clearly not happening and it seems to be out of spec.

In issue #466 there is a hit to use https://godoc.org/github.com/golang/protobuf/jsonpb but that project is deprecated and points back here.