File tree 2 files changed +42
-1
lines changed
2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -150,7 +150,13 @@ func (t *Time) UnmarshalJSON(b []byte) error {
150
150
return err
151
151
}
152
152
153
- * t = Time (v + va )
153
+ // If the value was something like -0.1 the negative is lost in the
154
+ // parsing because of the leading zero, this ensures that we capture it.
155
+ if len (p [0 ]) > 0 && p [0 ][0 ] == '-' && v + va > 0 {
156
+ * t = Time (v + va ) * - 1
157
+ } else {
158
+ * t = Time (v + va )
159
+ }
154
160
155
161
default :
156
162
return fmt .Errorf ("invalid time %q" , string (b ))
Original file line number Diff line number Diff line change 14
14
package model
15
15
16
16
import (
17
+ "strconv"
17
18
"testing"
18
19
"time"
19
20
)
@@ -130,3 +131,37 @@ func TestParseDuration(t *testing.T) {
130
131
}
131
132
}
132
133
}
134
+
135
+ func TestTimeJSON (t * testing.T ) {
136
+ tests := []struct {
137
+ in Time
138
+ out string
139
+ }{
140
+ {Time (1 ), `0.001` },
141
+ {Time (- 1 ), `-0.001` },
142
+ }
143
+
144
+ for i , test := range tests {
145
+ t .Run (strconv .Itoa (i ), func (t * testing.T ) {
146
+ b , err := test .in .MarshalJSON ()
147
+ if err != nil {
148
+ t .Fatalf ("Error marshaling time: %v" , err )
149
+ }
150
+
151
+ if string (b ) != test .out {
152
+ t .Errorf ("Mismatch in marshal expected=%s actual=%s" , test .out , b )
153
+ }
154
+
155
+ var tm Time
156
+ if err := tm .UnmarshalJSON (b ); err != nil {
157
+ t .Fatalf ("Error Unmarshaling time: %v" , err )
158
+ }
159
+
160
+ if ! test .in .Equal (tm ) {
161
+ t .Fatalf ("Mismatch after Unmarshal expected=%v actual=%v" , test .in , tm )
162
+ }
163
+
164
+ })
165
+ }
166
+
167
+ }
You can’t perform that action at this time.
0 commit comments