forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
executable file
·178 lines (149 loc) · 7.16 KB
/
parser_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package carbon
import (
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCarbon_Parse(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
output string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{Parse("now").ToDateTimeString(), Now().ToDateTimeString() + " +0800 CST"},
{Parse("yesterday").ToDateTimeString(), Yesterday().ToDateTimeString() + " +0800 CST"},
{Parse("tomorrow").ToDateTimeString(), Tomorrow().ToDateTimeString() + " +0800 CST"},
{"2020-8-5", "2020-08-05 00:00:00 +0800 CST"},
{"2020-8-05", "2020-08-05 00:00:00 +0800 CST"},
{"2020-08-05", "2020-08-05 00:00:00 +0800 CST"},
{"2020-8-5 1:2:3", "2020-08-05 01:02:03 +0800 CST"},
{"2020-08-05 1:2:03", "2020-08-05 01:02:03 +0800 CST"},
{"2020-08-05 1:02:03", "2020-08-05 01:02:03 +0800 CST"},
{"2020-08-05 01:02:03", "2020-08-05 01:02:03 +0800 CST"},
{"2020-8-5 13:14:15PM EST", "2020-08-05 21:14:15 +0800 CST"},
{"2020-8-05 13:14:15PM EST", "2020-08-05 21:14:15 +0800 CST"},
{"2020-08-05 13:14:15PM EST", "2020-08-05 21:14:15 +0800 CST"},
{"2020-08-05 13:14:15.999999999PM EST", "2020-08-05 21:14:15.999999999 +0800 CST"},
{"2020-8-5 13:14:15 PM EST", "2020-08-05 21:14:15 +0800 CST"},
{"2020-8-05 13:14:15 PM EST", "2020-08-05 21:14:15 +0800 CST"},
{"2020-08-05 13:14:15 PM EST", "2020-08-05 21:14:15 +0800 CST"},
{"2020-08-05 13:14:15.999999999 PM EST", "2020-08-05 21:14:15.999999999 +0800 CST"},
{"2020-8-5 13:14:15 PM EST", "2020-08-05 21:14:15 +0800 CST"},
{"2020-8-05 13:14:15 PM EST", "2020-08-05 21:14:15 +0800 CST"},
{"2020-08-05 13:14:15 PM EST", "2020-08-05 21:14:15 +0800 CST"},
{"2020-08-05 13:14:15.999999999 PM EST", "2020-08-05 21:14:15.999999999 +0800 CST"},
{"2020-08-05 1:2:3.999", "2020-08-05 01:02:03.999 +0800 CST"},
{"2020-08-05 1:2:3.999999", "2020-08-05 01:02:03.999999 +0800 CST"},
{"2020-08-05 1:2:3.999999999", "2020-08-05 01:02:03.999999999 +0800 CST"},
{"2020-08-05 1:2:3.999 +0800 CST", "2020-08-05 01:02:03.999 +0800 CST"},
{"2020-08-05 1:2:3.999999 +0800 CST", "2020-08-05 01:02:03.999999 +0800 CST"},
{"2020-08-05 1:2:3.999999999 +0800 CST", "2020-08-05 01:02:03.999999999 +0800 CST"},
{"2020-8-5T13:14:15Z", "2020-08-05 21:14:15 +0800 CST"},
{"2020-8-5T13:14:15.999Z", "2020-08-05 21:14:15.999 +0800 CST"},
{"2020-8-5T13:14:15.999999Z", "2020-08-05 21:14:15.999999 +0800 CST"},
{"2020-8-5T13:14:15.999999999Z", "2020-08-05 21:14:15.999999999 +0800 CST"},
{"2020.8.5", "2020-08-05 00:00:00 +0800 CST"},
{"2020.8.05", "2020-08-05 00:00:00 +0800 CST"},
{"2020.08.05", "2020-08-05 00:00:00 +0800 CST"},
{"2020.8.5 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"2020.8.05 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"2020.08.05 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"2020/8/5", "2020-08-05 00:00:00 +0800 CST"},
{"2020/8/05", "2020-08-05 00:00:00 +0800 CST"},
{"2020/08/05", "2020-08-05 00:00:00 +0800 CST"},
{"2020/8/5 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"2020/8/05 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"2020/08/05 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"2020/8/5 13:14:15.999", "2020-08-05 13:14:15.999 +0800 CST"},
{"2020/8/05 13:14:15.999999", "2020-08-05 13:14:15.999999 +0800 CST"},
{"2020/08/5 13:14:15.999999999", "2020-08-05 13:14:15.999999999 +0800 CST"},
{"2020-8-5T13:14:15+08:00", "2020-08-05 13:14:15 +0800 CST"},
{"2020-8-05T13:14:15+08:00", "2020-08-05 13:14:15 +0800 CST"},
{"2020-08-05T13:14:15+08:00", "2020-08-05 13:14:15 +0800 CST"},
{"2020-08-05T13:14:15.999+08:00", "2020-08-05 13:14:15.999 +0800 CST"},
{"2020-08-05T13:14:15.999999+08:00", "2020-08-05 13:14:15.999999 +0800 CST"},
{"2020-08-05T13:14:15.999999999+08:00", "2020-08-05 13:14:15.999999999 +0800 CST"},
{"20200805", "2020-08-05 00:00:00 +0800 CST"},
{"20200805131415", "2020-08-05 13:14:15 +0800 CST"},
{"20200805131415+08:00", "2020-08-05 13:14:15 +0800 CST"},
{"20200805131415.999", "2020-08-05 13:14:15.999 +0800 CST"},
{"20200805131415.999999", "2020-08-05 13:14:15.999999 +0800 CST"},
{"20200805131415.999999999", "2020-08-05 13:14:15.999999999 +0800 CST"},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.output, c.ToString(), "Current test index is "+strconv.Itoa(index))
}
for index, test := range tests {
c := Parse(test.input, PRC)
assert.Nil(c.Error)
assert.Equal(test.output, c.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_ParseByFormat(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
format string // 输入参数
output string // 期望值
}{
{"", "Y|m|d H:i:s", ""},
{"0", "Y|m|d H:i:s", ""},
{"0000-00-00", "Y|m|d H:i:s", ""},
{"00:00:00", "Y|m|d H:i:s", ""},
{"0000-00-00 00:00:00", "Y|m|d H:i:s", ""},
{"2020|08|05 13:14:15", "Y|m|d H:i:s", "2020-08-05 13:14:15"},
{"It is 2020-08-05 13:14:15", "\\I\\t \\i\\s Y-m-d H:i:s", "2020-08-05 13:14:15"},
{"今天是 2020年08月05日13时14分15秒", "今天是 Y年m月d日H时i分s秒", "2020-08-05 13:14:15"},
{"上次上报时间:2020-08-05 13:14:15,请每日按时打卡", "上次上报时间:Y-m-d H:i:s,请每日按时打卡", "2020-08-05 13:14:15"},
}
for index, test := range tests {
c := SetTimezone(PRC).ParseByFormat(test.input, test.format)
assert.Nil(c.Error)
assert.Equal(test.output, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
for index, test := range tests {
c := ParseByFormat(test.input, test.format, PRC)
assert.Nil(c.Error)
assert.Equal(test.output, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_ParseByLayout(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
layout string // 输入参数
output string // 期望值
}{
{"", "2006-01-02", ""},
{"0", "2006-01-02", ""},
{"0000-00-00", "2006-01-02", ""},
{"00:00:00", "15:04:05", ""},
{"0000-00-00 00:00:00", "2006-01-02 15:04:05", ""},
{"2020|08|05 13:14:15", "2006|01|02 15:04:05", "2020-08-05 13:14:15"},
{"It is 2020|08|05 13:14:15", "It is 2006|01|02 15:04:05", "2020-08-05 13:14:15"},
{"今天是 2020年08月05日13时14分15秒", "今天是 2006年01月02日15时04分05秒", "2020-08-05 13:14:15"},
}
for index, test := range tests {
c := SetTimezone(PRC).ParseByLayout(test.input, test.layout)
assert.Nil(c.Error)
assert.Equal(test.output, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
for index, test := range tests {
c := ParseByLayout(test.input, test.layout, PRC)
assert.Nil(c.Error)
assert.Equal(test.output, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestError_Parse(t *testing.T) {
date, layout, format, timezone := "2020-08-50", "2006-01-02", DateLayout, "xxx"
assert.NotNil(t, Parse(date).Error, "It should catch an exception in Parse()")
assert.NotNil(t, ParseByLayout(date, layout, timezone).Error, "It should catch an exception in ParseByLayout()")
assert.NotNil(t, ParseByFormat(date, format, timezone).Error, "It should catch an exception in ParseByFormat()")
}