tstime/mono: fix Time.Unmarshal (#8480)

Calling both mono.Now() and time.Now() is slow and
leads to unnecessary precision errors.
Instead, directly compute mono.Time relative to baseMono and baseWall.
This is the opposite calculation as mono.Time.WallTime.

Updates tailscale/corp#8427

Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
Joe Tsai
2023-06-28 15:16:52 -07:00
committed by GitHub
parent 075abd8ec1
commit e42be5a060
2 changed files with 20 additions and 3 deletions

View File

@@ -33,6 +33,21 @@ func TestUnmarshalZero(t *testing.T) {
}
}
func TestJSONRoundtrip(t *testing.T) {
want := Now()
b, err := want.MarshalJSON()
if err != nil {
t.Errorf("MarshalJSON error: %v", err)
}
var got Time
if err := got.UnmarshalJSON(b); err != nil {
t.Errorf("UnmarshalJSON error: %v", err)
}
if got != want {
t.Errorf("got %v, want %v", got, want)
}
}
func BenchmarkMonoNow(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {