forked from polaris1119/times
/
times_test.go
80 lines (73 loc) · 1.88 KB
/
times_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
package times
import (
"testing"
"time"
)
type test struct {
time time.Time
format string
strTime string
}
var testCases = []test{
{
time.Date(2012, 11, 22, 21, 28, 10, 0, time.Local),
"Y-m-d H:i:s",
"2012-11-22 21:28:10",
},
{
time.Date(2012, 11, 22, 0, 0, 0, 0, time.Local),
"Y-m-d",
"2012-11-22",
},
{
time.Date(2012, 11, 22, 21, 28, 10, 0, time.Local),
"Y-m-d H:i:s",
"2012-11-22 21:28:10",
},
}
func TestFormat(t *testing.T) {
for _, testCase := range testCases {
strTime := Format(testCase.format, testCase.time)
if strTime != testCase.strTime {
t.Errorf("(expected) %v != %v (actual)", testCase.time, strTime)
}
}
}
func TestStrToLocalTime(t *testing.T) {
for _, testCase := range testCases {
time := StrToLocalTime(testCase.strTime)
if time != testCase.time {
t.Errorf("(expected) %v != %v (actual)", time, testCase.time)
}
}
}
func TestStrToTime(t *testing.T) {
// zoneName, err := time.LoadLocation("CST")
// if err != nil {
// t.Error(err)
// }
var testCases = []test{
{
time.Date(2012, 11, 22, 21, 28, 10, 0, time.Local),
"",
"2012-11-22 21:28:10 +0800 +0800",
},
{
time.Date(2012, 11, 22, 0, 0, 0, 0, time.Local),
"",
"2012-11-22 +0800 +0800",
},
{
time.Date(2012, 11, 22, 21, 28, 10, 0, time.FixedZone("CST", 28800)),
"",
"2012-11-22 21:28:10 +0800 CST",
},
}
for _, testCase := range testCases {
time := StrToTime(testCase.strTime)
// if time != testCase.time {
if !time.Equal(testCase.time) {
t.Errorf("(expected) %v != %v (actual)", time, testCase.time)
}
}
}