Skip to content

Commit f8d3bab

Browse files
committed
fix ParseByFormat bug
1 parent 9187ff0 commit f8d3bab

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

parser.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import (
1010
// 将标准格式时间字符串解析成 Carbon 实例
1111
func (c Carbon) Parse(value string, timezone ...string) Carbon {
1212
if len(timezone) > 0 {
13-
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
14-
c.Loc = loc
15-
c.Error = err
13+
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
1614
}
1715
if c.Error != nil {
1816
return c
@@ -52,9 +50,7 @@ func Parse(value string, timezone ...string) Carbon {
5250
// 通过格式化字符将字符串解析成 carbon 实例
5351
func (c Carbon) ParseByFormat(value string, format string, timezone ...string) Carbon {
5452
if len(timezone) > 0 {
55-
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
56-
c.Loc = loc
57-
c.Error = err
53+
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
5854
}
5955
if c.Error != nil {
6056
return c
@@ -80,17 +76,15 @@ func ParseByFormat(value string, format string, timezone ...string) Carbon {
8076
// 通过布局字符将字符串解析成 carbon 实例
8177
func (c Carbon) ParseByLayout(value string, layout string, timezone ...string) Carbon {
8278
if len(timezone) > 0 {
83-
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
84-
c.Loc = loc
85-
c.Error = err
79+
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
8680
}
8781
if c.Error != nil {
8882
return c
8983
}
9084
if value == "" || value == "0" || value == "0000-00-00 00:00:00" || value == "0000-00-00" || value == "00:00:00" {
9185
return c
9286
}
93-
tt, err := time.ParseInLocation(layout, value, c.Loc)
87+
tt, err := time.ParseInLocation(layout, value, c.loc)
9488
if err != nil {
9589
c.Error = invalidLayoutError(value, layout)
9690
return c

parser_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func TestCarbon_ParseByFormat(t *testing.T) {
6666
{"2020|08|05 13:14:15", "Y|m|d H:i:s", "2020-08-05 13:14:15"},
6767
{"It is 2020-08-05 13:14:15", "\\I\\t \\i\\s Y-m-d H:i:s", "2020-08-05 13:14:15"},
6868
{"今天是 2020年08月05日13时14分15秒", "今天是 Y年m月d日H时i分s秒", "2020-08-05 13:14:15"},
69+
{"上次上报时间:2020-08-05 13:14:15,请每日按时打卡", "上次上报时间:Y-m-d H:i:s,请每日按时打卡", "2020-08-05 13:14:15"},
6970
}
7071

7172
for index, test := range tests {

0 commit comments

Comments
 (0)