File tree Expand file tree Collapse file tree 2 files changed +20
-3
lines changed Expand file tree Collapse file tree 2 files changed +20
-3
lines changed Original file line number Diff line number Diff line change @@ -104,7 +104,8 @@ func (t Time) WallTime() time.Time {
104
104
105
105
// MarshalJSON formats t for JSON as if it were a time.Time.
106
106
// We format Time this way for backwards-compatibility.
107
- // This is best-effort only. Time does not survive a MarshalJSON/UnmarshalJSON round trip unchanged.
107
+ // Time does not survive a MarshalJSON/UnmarshalJSON round trip unchanged
108
+ // across different invocations of the Go process. This is best-effort only.
108
109
// Since t is a monotonic time, it can vary from the actual wall clock by arbitrary amounts.
109
110
// Even in the best of circumstances, it may vary by a few milliseconds.
110
111
func (t Time ) MarshalJSON () ([]byte , error ) {
@@ -113,7 +114,8 @@ func (t Time) MarshalJSON() ([]byte, error) {
113
114
}
114
115
115
116
// UnmarshalJSON sets t according to data.
116
- // This is best-effort only. Time does not survive a MarshalJSON/UnmarshalJSON round trip unchanged.
117
+ // Time does not survive a MarshalJSON/UnmarshalJSON round trip unchanged
118
+ // across different invocations of the Go process. This is best-effort only.
117
119
func (t * Time ) UnmarshalJSON (data []byte ) error {
118
120
var tt time.Time
119
121
err := tt .UnmarshalJSON (data )
@@ -124,6 +126,6 @@ func (t *Time) UnmarshalJSON(data []byte) error {
124
126
* t = 0
125
127
return nil
126
128
}
127
- * t = Now () .Add (- time . Since ( tt ))
129
+ * t = baseMono .Add (tt . Sub ( baseWall ))
128
130
return nil
129
131
}
Original file line number Diff line number Diff line change @@ -33,6 +33,21 @@ func TestUnmarshalZero(t *testing.T) {
33
33
}
34
34
}
35
35
36
+ func TestJSONRoundtrip (t * testing.T ) {
37
+ want := Now ()
38
+ b , err := want .MarshalJSON ()
39
+ if err != nil {
40
+ t .Errorf ("MarshalJSON error: %v" , err )
41
+ }
42
+ var got Time
43
+ if err := got .UnmarshalJSON (b ); err != nil {
44
+ t .Errorf ("UnmarshalJSON error: %v" , err )
45
+ }
46
+ if got != want {
47
+ t .Errorf ("got %v, want %v" , got , want )
48
+ }
49
+ }
50
+
36
51
func BenchmarkMonoNow (b * testing.B ) {
37
52
b .ReportAllocs ()
38
53
for i := 0 ; i < b .N ; i ++ {
You can’t perform that action at this time.
0 commit comments