make proto time precision match json

This commit is contained in:
deads2k 2017-06-23 13:51:22 -04:00
parent afc4506c33
commit 5b8a483a96
2 changed files with 10 additions and 3 deletions

View File

@ -42,7 +42,10 @@ func (m *Time) ProtoTime() *Timestamp {
} }
return &Timestamp{ return &Timestamp{
Seconds: m.Time.Unix(), Seconds: m.Time.Unix(),
Nanos: int32(m.Time.Nanosecond()), // leaving this here for the record. our JSON only handled seconds, so this results in writes by
// protobuf clients storing values that aren't read by json clients, which results in unexpected
// field mutation, which fails various validation and equality code.
// Nanos: int32(m.Time.Nanosecond()),
} }
} }
@ -64,7 +67,11 @@ func (m *Time) Unmarshal(data []byte) error {
if err := p.Unmarshal(data); err != nil { if err := p.Unmarshal(data); err != nil {
return err return err
} }
m.Time = time.Unix(p.Seconds, int64(p.Nanos)).Local() // leaving this here for the record. our JSON only handled seconds, so this results in writes by
// protobuf clients storing values that aren't read by json clients, which results in unexpected
// field mutation, which fails various validation and equality code.
// m.Time = time.Unix(p.Seconds, int64(p.Nanos)).Local()
m.Time = time.Unix(p.Seconds, int64(0)).Local()
return nil return nil
} }

View File

@ -152,7 +152,7 @@ func TestTimeProto(t *testing.T) {
input Time input Time
}{ }{
{Time{}}, {Time{}},
{Date(1998, time.May, 5, 1, 5, 5, 50, time.Local)}, {Date(1998, time.May, 5, 1, 5, 5, 0, time.Local)},
{Date(1998, time.May, 5, 5, 5, 5, 0, time.Local)}, {Date(1998, time.May, 5, 5, 5, 5, 0, time.Local)},
} }