Skip to content

Commit d4b8343

Browse files
committed
增加可选参数timezone
1 parent 111865f commit d4b8343

File tree

2 files changed

+97
-22
lines changed

2 files changed

+97
-22
lines changed

parser.go

+42-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package carbon
22

33
import (
4-
"errors"
5-
"fmt"
64
"strconv"
75
"strings"
86
"time"
97
)
108

119
// Parse 将标准格式时间字符串解析成 Carbon 实例
12-
func (c Carbon) Parse(value string) Carbon {
10+
func (c Carbon) Parse(value string, timezone ...string) Carbon {
11+
if len(timezone) == 1 {
12+
loc, err := getLocationByTimezone(timezone[0])
13+
c.Loc = loc
14+
c.Error = err
15+
}
16+
if c.Error != nil {
17+
return c
18+
}
1319
layout := DateTimeFormat
1420
if value == "" || value == "0" || value == "0000-00-00 00:00:00" || value == "0000-00-00" || value == "00:00:00" {
1521
return c
@@ -28,43 +34,67 @@ func (c Carbon) Parse(value string) Carbon {
2834
layout = ShortDateTimeFormat
2935
}
3036
}
37+
if c.ParseByLayout(value, layout).Error != nil {
38+
c.Error = invalidValueError(value)
39+
return c
40+
}
3141
return c.ParseByLayout(value, layout)
3242
}
3343

3444
// Parse 将标准格式时间字符串解析成 Carbon 实例(默认时区)
35-
func Parse(value string) Carbon {
36-
return NewCarbon().Parse(value)
45+
func Parse(value string, timezone ...string) Carbon {
46+
return NewCarbon().Parse(value, timezone...)
3747
}
3848

3949
// ParseByFormat 将特殊格式时间字符串解析成 Carbon 实例
40-
func (c Carbon) ParseByFormat(value string, format string) Carbon {
50+
func (c Carbon) ParseByFormat(value string, format string, timezone ...string) Carbon {
51+
if len(timezone) == 1 {
52+
loc, err := getLocationByTimezone(timezone[0])
53+
c.Loc = loc
54+
c.Error = err
55+
}
56+
if c.Error != nil {
57+
return c
58+
}
4159
if value == "" || value == "0" || value == "0000-00-00 00:00:00" || value == "0000-00-00" || value == "00:00:00" {
4260
return c
4361
}
4462
layout := format2layout(format)
63+
if c.ParseByLayout(value, layout).Error != nil {
64+
c.Error = invalidFormatError(value, format)
65+
return c
66+
}
4567
return c.ParseByLayout(value, layout)
4668
}
4769

4870
// ParseByFormat 将特殊格式时间字符串解析成 Carbon 实例(默认时区)
49-
func ParseByFormat(value string, format string) Carbon {
50-
return NewCarbon().ParseByFormat(value, format)
71+
func ParseByFormat(value string, format string, timezone ...string) Carbon {
72+
return NewCarbon().ParseByFormat(value, format, timezone...)
5173
}
5274

5375
// ParseByLayout 将布局时间字符串解析成 Carbon 实例
54-
func (c Carbon) ParseByLayout(value string, layout string) Carbon {
76+
func (c Carbon) ParseByLayout(value string, layout string, timezone ...string) Carbon {
77+
if len(timezone) == 1 {
78+
loc, err := getLocationByTimezone(timezone[0])
79+
c.Loc = loc
80+
c.Error = err
81+
}
82+
if c.Error != nil {
83+
return c
84+
}
5585
if value == "" || value == "0" || value == "0000-00-00 00:00:00" || value == "0000-00-00" || value == "00:00:00" {
5686
return c
5787
}
5888
tt, err := time.ParseInLocation(layout, value, c.Loc)
5989
if err != nil {
60-
c.Error = errors.New(fmt.Sprintf("the value %q and the layout %q don't match, so the value can't parse to carbon", value, layout))
90+
c.Error = invalidLayoutError(value, layout)
6191
return c
6292
}
6393
c.Time = tt
6494
return c
6595
}
6696

6797
// ParseByLayout 将布局时间字符串解析成 Carbon 实例(默认时区)
68-
func ParseByLayout(value string, layout string) Carbon {
69-
return NewCarbon().ParseByLayout(value, layout)
98+
func ParseByLayout(value string, layout string, timezone ...string) Carbon {
99+
return NewCarbon().ParseByLayout(value, layout, timezone...)
70100
}

parser_test.go

+55-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestCarbon_Parse(t *testing.T) {
1313
tests := []struct {
1414
id int // 测试id
1515
input string // 输入值
16-
output string // 期望输出值
16+
output string // 期望值
1717
}{
1818
{1, "", ""},
1919
{2, "0", ""},
@@ -29,20 +29,35 @@ func TestCarbon_Parse(t *testing.T) {
2929
}
3030

3131
for _, test := range tests {
32-
c := Parse(test.input)
32+
c := SetTimezone(PRC).Parse(test.input)
3333
assert.Nil(c.Error)
34-
assert.Equal(c.ToDateTimeStringWithTimezone(PRC), test.output, "Current test id is "+strconv.Itoa(test.id))
34+
assert.Equal(test.output, c.ToDateTimeString(), "Current test id is "+strconv.Itoa(test.id))
35+
}
36+
37+
for _, test := range tests {
38+
c := Parse(test.input, PRC)
39+
assert.Nil(c.Error)
40+
assert.Equal(test.output, c.ToDateTimeString(), "Current test id is "+strconv.Itoa(test.id))
3541
}
3642
}
3743

44+
func TestError_Parse(t *testing.T) {
45+
value, timezone := "2020-13-50", "xxx"
46+
c1 := Parse(value)
47+
assert.Equal(t, invalidValueError(value), c1.Error, "Should catch an exception in Parse()")
48+
49+
c2 := Parse(value, timezone)
50+
assert.Equal(t, invalidTimezoneError(timezone), c2.Error, "Should catch an exception in Parse()")
51+
}
52+
3853
func TestCarbon_ParseByFormat(t *testing.T) {
3954
assert := assert.New(t)
4055

4156
tests := []struct {
4257
id int // 测试id
4358
input string // 输入值
4459
format string // 输入参数
45-
output string // 期望输出值
60+
output string // 期望值
4661
}{
4762
{1, "", "Y|m|d H:i:s", ""},
4863
{2, "0", "Y|m|d H:i:s", ""},
@@ -56,20 +71,35 @@ func TestCarbon_ParseByFormat(t *testing.T) {
5671
}
5772

5873
for _, test := range tests {
59-
c := ParseByFormat(test.input, test.format)
74+
c := SetTimezone(PRC).ParseByFormat(test.input, test.format)
75+
assert.Nil(c.Error)
76+
assert.Equal(test.output, c.ToDateTimeString(), "Current test id is "+strconv.Itoa(test.id))
77+
}
78+
79+
for _, test := range tests {
80+
c := ParseByFormat(test.input, test.format, PRC)
6081
assert.Nil(c.Error)
61-
assert.Equal(c.ToDateTimeString(), test.output, "Current test id is "+strconv.Itoa(test.id))
82+
assert.Equal(test.output, c.ToDateTimeString(), "Current test id is "+strconv.Itoa(test.id))
6283
}
6384
}
6485

86+
func TestError_ParseByFormat(t *testing.T) {
87+
value, format, timezone := "2020-08-50", "Y-m-d", "xxx"
88+
c1 := ParseByFormat(value, format)
89+
assert.Equal(t, invalidFormatError(value, format), c1.Error, "Should catch an exception in ParseByFormat()")
90+
91+
c2 := ParseByFormat(value, format, timezone)
92+
assert.Equal(t, invalidTimezoneError(timezone), c2.Error, "Should catch an exception in ParseByFormat()")
93+
}
94+
6595
func TestCarbon_ParseByLayout(t *testing.T) {
6696
assert := assert.New(t)
6797

6898
tests := []struct {
6999
id int // 测试id
70100
input string // 输入值
71-
format string // 输入参数
72-
output string // 期望输出值
101+
layout string // 输入参数
102+
output string // 期望值
73103
}{
74104
{1, "", "2006-01-02", ""},
75105
{2, "0", "2006-01-02", ""},
@@ -83,8 +113,23 @@ func TestCarbon_ParseByLayout(t *testing.T) {
83113
}
84114

85115
for _, test := range tests {
86-
c := ParseByLayout(test.input, test.format)
116+
c := SetTimezone(PRC).ParseByLayout(test.input, test.layout)
87117
assert.Nil(c.Error)
88-
assert.Equal(c.ToDateTimeString(), test.output, "Current test id is "+strconv.Itoa(test.id))
118+
assert.Equal(test.output, c.ToDateTimeString(), "Current test id is "+strconv.Itoa(test.id))
89119
}
120+
121+
for _, test := range tests {
122+
c := ParseByLayout(test.input, test.layout, PRC)
123+
assert.Nil(c.Error)
124+
assert.Equal(test.output, c.ToDateTimeString(), "Current test id is "+strconv.Itoa(test.id))
125+
}
126+
}
127+
128+
func TestError_ParseByLayout(t *testing.T) {
129+
value, layout, timezone := "2020-08-50", "2006-01-02", "xxx"
130+
c1 := ParseByLayout(value, layout)
131+
assert.Equal(t, invalidLayoutError(value, layout), c1.Error, "Should catch an exception in ParseByLayout()")
132+
133+
c2 := ParseByLayout(value, layout, timezone)
134+
assert.Equal(t, invalidTimezoneError(timezone), c2.Error, "Should catch an exception in ParseByLayout()")
90135
}

0 commit comments

Comments
 (0)