-
Notifications
You must be signed in to change notification settings - Fork 33
/
change_test.go
101 lines (98 loc) · 2.19 KB
/
change_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package common
import (
"reflect"
"testing"
"time"
)
// @author: xftan
// @date: 2022/1/25 16:55
// @description: test timestamp with precision convert to time.Time
func TestTimestampConvertToTime(t *testing.T) {
type args struct {
timestamp int64
precision int
}
tests := []struct {
name string
args args
want time.Time
}{
{
name: "ms",
args: args{
timestamp: 1643068800000,
precision: PrecisionMilliSecond,
},
want: time.Date(2022, 01, 25, 0, 0, 0, 0, time.UTC),
},
{
name: "us",
args: args{
timestamp: 1643068800000000,
precision: PrecisionMicroSecond,
},
want: time.Date(2022, 01, 25, 0, 0, 0, 0, time.UTC),
},
{
name: "ns",
args: args{
timestamp: 1643068800000000000,
precision: PrecisionNanoSecond,
},
want: time.Date(2022, 01, 25, 0, 0, 0, 0, time.UTC),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := TimestampConvertToTime(tt.args.timestamp, tt.args.precision); !reflect.DeepEqual(got.UTC(), tt.want.UTC()) {
t.Errorf("TimestampConvertToTime() = %v, want %v", got, tt.want)
}
})
}
}
// @author: xftan
// @date: 2022/1/25 16:56
// @description: test time.Time with precision convert to timestamp
func TestTimeToTimestamp(t *testing.T) {
type args struct {
t time.Time
precision int
}
tests := []struct {
name string
args args
wantTimestamp int64
}{
{
name: "ms",
args: args{
t: time.Date(2022, 01, 25, 0, 0, 0, 0, time.UTC),
precision: PrecisionMilliSecond,
},
wantTimestamp: 1643068800000,
},
{
name: "us",
args: args{
t: time.Date(2022, 01, 25, 0, 0, 0, 0, time.UTC),
precision: PrecisionMicroSecond,
},
wantTimestamp: 1643068800000000,
},
{
name: "ns",
args: args{
t: time.Date(2022, 01, 25, 0, 0, 0, 0, time.UTC),
precision: PrecisionNanoSecond,
},
wantTimestamp: 1643068800000000000,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotTimestamp := TimeToTimestamp(tt.args.t, tt.args.precision); gotTimestamp != tt.wantTimestamp {
t.Errorf("TimeToTimestamp() = %v, want %v", gotTimestamp, tt.wantTimestamp)
}
})
}
}