diff --git a/tstime/mono/mono.go b/tstime/mono/mono.go index 3e582f644..c639c47a7 100644 --- a/tstime/mono/mono.go +++ b/tstime/mono/mono.go @@ -121,6 +121,10 @@ func (t *Time) UnmarshalJSON(data []byte) error { if err != nil { return err } + if tt.IsZero() { + *t = 0 + return nil + } *t = Now().Add(-time.Since(tt)) return nil } diff --git a/tstime/mono/mono_test.go b/tstime/mono/mono_test.go index afa05799f..4d678c0bc 100644 --- a/tstime/mono/mono_test.go +++ b/tstime/mono/mono_test.go @@ -5,6 +5,7 @@ package mono import ( + "encoding/json" "testing" "time" ) @@ -17,6 +18,22 @@ func TestNow(t *testing.T) { } } +func TestUnmarshalZero(t *testing.T) { + var tt time.Time + buf, err := json.Marshal(tt) + if err != nil { + t.Fatal(err) + } + var m Time + err = json.Unmarshal(buf, &m) + if err != nil { + t.Fatal(err) + } + if !m.IsZero() { + t.Errorf("expected unmarshal of zero time to be 0, got %d (~=%v)", m, m) + } +} + func BenchmarkMonoNow(b *testing.B) { for i := 0; i < b.N; i++ { Now()