-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfcpkg_test.go
125 lines (116 loc) · 3.53 KB
/
dfcpkg_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package dfcpkg
import (
"fmt"
"testing"
"time"
)
func TestResultOfLayout(t *testing.T) {
var testDatetimeString = "2020-08-06 17:29:29.292929"
x, _ := time.Parse("2006-01-02 15:04:05.999999", testDatetimeString)
result := ResultOfLayout(x)
for i, s := range result {
if s != x.Format(FormatLayouts[i]) {
t.Error(s, " != ", x.Format(FormatLayouts[i]))
}
}
}
func TestResultOfTimestamp(t *testing.T) {
var testDatetimeString = "2020-08-06 17:29:29.292929"
x, _ := time.Parse("2006-01-02 15:04:05.999999", testDatetimeString)
nano := x.UnixNano()
result := ResultOfTimestamp(x)
exp := []string{
fmt.Sprint(x.Unix()), // 秒时间戳
fmt.Sprint(nano / int64(time.Millisecond)), // 毫秒时间戳
fmt.Sprint(nano / int64(time.Microsecond)), // 微秒时间戳
fmt.Sprint(nano), // 纳秒时间戳
}
for i, s := range result {
if s != exp[i] {
t.Error(s, " != ", exp[i])
}
}
}
func TestConvertDatestring(t *testing.T) {
var testDatetimeString = "2020-08-06 17:29:29.292929"
x, _ := time.ParseInLocation("2006-01-02 15:04:05.999999", testDatetimeString, time.Local)
result, err := Convert(testDatetimeString)
if err != nil {
t.Fatal(err)
}
nano := x.UnixNano()
exp := []string{
fmt.Sprint(x.Unix()), // 秒时间戳
fmt.Sprint(nano / int64(time.Millisecond)), // 毫秒时间戳
fmt.Sprint(nano / int64(time.Microsecond)), // 微秒时间戳
fmt.Sprint(nano), // 纳秒时间戳
}
for i := 0; i < len(exp); i++ {
if result[i] != exp[i] {
t.Fatal(result[i], " != ", exp[i])
}
}
}
func TestConvertTimestamp(t *testing.T) {
var testTimestamp int64 = 1596706169
x := time.Unix(testTimestamp, 0)
result, err := Convert(fmt.Sprint(testTimestamp))
if err != nil {
t.Fatal(err)
}
for i, s := range result {
if s != x.Format(FormatLayouts[i]) {
t.Fatal(s, " != ", x.Format(FormatLayouts[i]))
}
}
}
func TestConvertNow(t *testing.T) {
now := time.Now()
testDatetimeString := "now"
result, err := Convert(testDatetimeString)
if err != nil {
t.Fatal(err)
}
for i := 0; i < len(FormatLayouts); i++ {
if result[i] != now.Format(FormatLayouts[i]) {
t.Fatal(result[i], " != ", now.Format(FormatLayouts[i]))
}
}
nano := now.UnixNano()
exp := []string{
fmt.Sprint(now.Unix()), // 秒时间戳
fmt.Sprint(nano / int64(time.Millisecond)), // 毫秒时间戳
fmt.Sprint(nano / int64(time.Microsecond)), // 微秒时间戳
fmt.Sprint(nano), // 纳秒时间戳
}
for i := len(FormatLayouts); i < len(FormatLayouts)+len(exp); i++ {
if result[i] != exp[i-len(FormatLayouts)] {
t.Fatal(result[i], " != ", now.Format(FormatLayouts[i]))
}
}
}
func TestConvertAgo(t *testing.T) {
testDatetimeString := "1 hours ago"
result, err := Convert(testDatetimeString)
if err != nil {
t.Fatal(err)
}
now := time.Now().Add(-time.Hour * 1)
for i := 0; i < len(FormatLayouts); i++ {
if result[i] != now.Format(FormatLayouts[i]) {
t.Fatal(result[i], " != ", now.Format(FormatLayouts[i]))
}
}
nano := now.UnixNano()
exp := []string{
fmt.Sprint(now.Unix()), // 秒时间戳
fmt.Sprint(nano / int64(time.Millisecond)), // 毫秒时间戳
fmt.Sprint(nano / int64(time.Microsecond)), // 微秒时间戳
fmt.Sprint(nano), // 纳秒时间戳
}
for i := len(FormatLayouts); i < len(FormatLayouts)+len(exp); i++ {
if result[i] != exp[i-len(FormatLayouts)] {
t.Fatal(result[i], " != ", now.Format(FormatLayouts[i]))
}
}
}