-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat_time_test.go
87 lines (75 loc) · 2.44 KB
/
float_time_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package durations
import (
"encoding/json"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
//2021-12-23T09:47:12-06:00
// fmt.Println(time.Unix(1640274432, 937853*1000).UnixMicro())
func TestFloatTimeUnmarshal(t *testing.T) {
given := []byte(`1640274432.937853`)
tm := &FloatTime{}
tm.UnmarshalJSON(given)
if diff := cmp.Diff(tm.Format(time.RFC3339), `2021-12-23T09:47:12-06:00`); diff != "" {
t.Errorf("Nanosecond: (-got +want)\n%s", diff)
}
}
func TestFloatTimeMarshal(t *testing.T) {
f := &FloatTime{
Time: time.Unix(1640274432, 937853000),
}
f.Precision = time.Nanosecond
s, e := json.Marshal(f)
if diff := cmp.Diff(string(s), `1640274432937853000`); diff != "" || e != nil {
t.Errorf("Nanosecond: (-got +want)\n%s; %s", diff, e)
}
f.Precision = time.Microsecond
s, e = json.Marshal(f)
if diff := cmp.Diff(string(s), `1640274432937853`); diff != "" || e != nil {
t.Errorf("Microsecond: (-got +want)\n%s; %s", diff, e)
}
f.Precision = time.Millisecond
s, e = json.Marshal(f)
if diff := cmp.Diff(string(s), `1640274432937`); diff != "" || e != nil {
t.Errorf("Millisecond: (-got +want)\n%s; %s", diff, e)
}
}
func TestNormalFloatTimeUnmarshal(t *testing.T) {
given := []byte(`1640274432`)
tm := &FloatTime{}
tm.UnmarshalJSON(given)
if diff := cmp.Diff(tm.Format(time.RFC3339), `2021-12-23T09:47:12-06:00`); diff != "" {
t.Errorf("Nanosecond: (-got +want)\n%s", diff)
}
}
func TestNormalFloatTimeMarshal(t *testing.T) {
f := &FloatTime{
Time: time.Unix(1640274432, 0),
}
f.Precision = time.Nanosecond
s, e := json.Marshal(f)
if diff := cmp.Diff(string(s), `1640274432000000000`); diff != "" || e != nil {
t.Errorf("Nanosecond: (-got +want)\n%s; %s", diff, e)
}
f.Precision = time.Microsecond
s, e = json.Marshal(f)
if diff := cmp.Diff(string(s), `1640274432000000`); diff != "" || e != nil {
t.Errorf("Microsecond: (-got +want)\n%s; %s", diff, e)
}
f.Precision = time.Millisecond
s, e = json.Marshal(f)
if diff := cmp.Diff(string(s), `1640274432000`); diff != "" || e != nil {
t.Errorf("Millisecond: (-got +want)\n%s; %s", diff, e)
}
}
// func TestFloatTimeMarshalNew(t *testing.T) {
// f := NewFloatTime()
// s, e := json.Marshal(f)
// if diff := cmp.Diff(string(s), `1640709402`); diff != "" || e != nil {
// t.Errorf("Millisecond: (-got +want)\n%s; %s", diff, e)
// }
// if diff := cmp.Diff(f.Format(time.RFC3339), `2021-12-28T16:36:42Z`); diff != "" {
// t.Errorf("Nanosecond: (-got +want)\n%s", diff)
// }
// }